赞
踩
注:该文项目基础为分布式搜索Elasticsearch——项目过程(一)和分布式搜索Elasticsearch——项目过程(二),项目骨架可至这里下载。
ES中对multiMatchQuery的描述如下所示:
- /**
- * Creates a match query with type "BOOLEAN" for the provided field name and text.
- *
- * @param fieldNames The field names.
- * @param text The query text (to be analyzed).
- */
- public static MultiMatchQueryBuilder multiMatchQuery(Object text, String... fieldNames) {
- return new MultiMatchQueryBuilder(text, fieldNames); // BOOLEAN is the default
- }
示例代码如下所示:
- /**
- * @author Geloin
- */
- package com.geloin.esample.util;
-
- import java.util.UUID;
-
- import junit.framework.Assert;
-
- import org.elasticsearch.action.bulk.BulkRequestBuilder;
- import org.elasticsearch.action.bulk.BulkResponse;
- import org.elasticsearch.action.index.IndexRequest;
- import org.elasticsearch.action.search.SearchResponse;
- import org.elasticsearch.index.query.QueryBuilder;
- import org.elasticsearch.index.query.QueryBuilders;
- import org.elasticsearch.search.SearchHit;
- import org.elasticsearch.search.SearchHits;
- import org.junit.Test;
-
- import com.geloin.esample.BaseTest;
- import com.geloin.esample.entity.Person;
-
- /**
- * @author Geloin
- *
- */
- public class MultiMatchQueryTest extends BaseTest {
-
- @Test
- public void multiMatchQueryTest() {
- try {
- // 创建索引
- BulkRequestBuilder builder = client.prepareBulk();
-
- for (int i = 0; i < 5; i++) {
- Person p = new Person();
- p.setId(UUID.randomUUID().toString());
- p.setAge(20);
- p.setIsStudent(false);
- p.setSex("男");
- p.setName("小别克听老别克讲别克的故事");
-
- String source = ElasticSearchUtil.BeanToJson(p);
-
- IndexRequest request = client
- .prepareIndex(index, type, p.getId()).setSource(source)
- .request();
-
- builder.add(request);
- }
-
- for (int i = 0; i < 5; i++) {
- Person p = new Person();
- p.setId(UUID.randomUUID().toString());
- p.setAge(20);
- p.setIsStudent(false);
- p.setSex("男装别克");
- p.setName("名字里面没有查询的关键字");
-
- String source = ElasticSearchUtil.BeanToJson(p);
-
- IndexRequest request = client
- .prepareIndex(index, type, p.getId()).setSource(source)
- .request();
-
- builder.add(request);
- }
-
- BulkResponse response = builder.execute().actionGet();
-
- if (response.hasFailures()) {
- Assert.fail("创建索引失败!");
- }
-
- String text = "别克";
- // 检索
- QueryBuilder qb = QueryBuilders
- .multiMatchQuery(text, "name", "sex");
- SearchResponse sResponse = client.prepareSearch(index)
- .setTypes(type).setQuery(qb).setFrom(0).setSize(12)
- .execute().actionGet();
-
- SearchHits hits = sResponse.getHits();
- if (null != hits && hits.totalHits() > 0) {
- for (SearchHit hit : hits) {
- String json = hit.getSourceAsString();
- Person newPerson = mapper.readValue(json, Person.class);
- System.out.println("name\t\t" + newPerson.getName());
- System.out.println("sex\t\t" + newPerson.getSex());
- System.out.println("age\t\t" + newPerson.getAge());
- System.out.println("isStudent\t\t"
- + newPerson.getIsStudent());
- }
- } else {
- log.info("没有查询到任何结果!");
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。