当前位置:   article > 正文

分布式搜索Elasticsearch——QueryBuilders.multiMatchQuery

querybuilders.multimatchquery

        注:该文项目基础为分布式搜索Elasticsearch——项目过程(一)分布式搜索Elasticsearch——项目过程(二),项目骨架可至这里下载。

        ES中对multiMatchQuery的描述如下所示:

  1. /**
  2. * Creates a match query with type "BOOLEAN" for the provided field name and text.
  3. *
  4. * @param fieldNames The field names.
  5. * @param text The query text (to be analyzed).
  6. */
  7. public static MultiMatchQueryBuilder multiMatchQuery(Object text, String... fieldNames) {
  8. return new MultiMatchQueryBuilder(text, fieldNames); // BOOLEAN is the default
  9. }

        相对于matchQuery,multiMatchQuery针对的是多个fi eld,也就是说,当multiMatchQuery中,fieldNames参数只有一个时,其作用与matchQuery相当;而当fieldNames有多个参数时,如field1和field2,那查询的结果中,要么field1中包含text,要么field2中包含text。

        示例代码如下所示:

  1. /**
  2. * @author Geloin
  3. */
  4. package com.geloin.esample.util;
  5. import java.util.UUID;
  6. import junit.framework.Assert;
  7. import org.elasticsearch.action.bulk.BulkRequestBuilder;
  8. import org.elasticsearch.action.bulk.BulkResponse;
  9. import org.elasticsearch.action.index.IndexRequest;
  10. import org.elasticsearch.action.search.SearchResponse;
  11. import org.elasticsearch.index.query.QueryBuilder;
  12. import org.elasticsearch.index.query.QueryBuilders;
  13. import org.elasticsearch.search.SearchHit;
  14. import org.elasticsearch.search.SearchHits;
  15. import org.junit.Test;
  16. import com.geloin.esample.BaseTest;
  17. import com.geloin.esample.entity.Person;
  18. /**
  19. * @author Geloin
  20. *
  21. */
  22. public class MultiMatchQueryTest extends BaseTest {
  23. @Test
  24. public void multiMatchQueryTest() {
  25. try {
  26. // 创建索引
  27. BulkRequestBuilder builder = client.prepareBulk();
  28. for (int i = 0; i < 5; i++) {
  29. Person p = new Person();
  30. p.setId(UUID.randomUUID().toString());
  31. p.setAge(20);
  32. p.setIsStudent(false);
  33. p.setSex("男");
  34. p.setName("小别克听老别克讲别克的故事");
  35. String source = ElasticSearchUtil.BeanToJson(p);
  36. IndexRequest request = client
  37. .prepareIndex(index, type, p.getId()).setSource(source)
  38. .request();
  39. builder.add(request);
  40. }
  41. for (int i = 0; i < 5; i++) {
  42. Person p = new Person();
  43. p.setId(UUID.randomUUID().toString());
  44. p.setAge(20);
  45. p.setIsStudent(false);
  46. p.setSex("男装别克");
  47. p.setName("名字里面没有查询的关键字");
  48. String source = ElasticSearchUtil.BeanToJson(p);
  49. IndexRequest request = client
  50. .prepareIndex(index, type, p.getId()).setSource(source)
  51. .request();
  52. builder.add(request);
  53. }
  54. BulkResponse response = builder.execute().actionGet();
  55. if (response.hasFailures()) {
  56. Assert.fail("创建索引失败!");
  57. }
  58. String text = "别克";
  59. // 检索
  60. QueryBuilder qb = QueryBuilders
  61. .multiMatchQuery(text, "name", "sex");
  62. SearchResponse sResponse = client.prepareSearch(index)
  63. .setTypes(type).setQuery(qb).setFrom(0).setSize(12)
  64. .execute().actionGet();
  65. SearchHits hits = sResponse.getHits();
  66. if (null != hits && hits.totalHits() > 0) {
  67. for (SearchHit hit : hits) {
  68. String json = hit.getSourceAsString();
  69. Person newPerson = mapper.readValue(json, Person.class);
  70. System.out.println("name\t\t" + newPerson.getName());
  71. System.out.println("sex\t\t" + newPerson.getSex());
  72. System.out.println("age\t\t" + newPerson.getAge());
  73. System.out.println("isStudent\t\t"
  74. + newPerson.getIsStudent());
  75. }
  76. } else {
  77. log.info("没有查询到任何结果!");
  78. }
  79. } catch (Exception e) {
  80. e.printStackTrace();
  81. }
  82. }
  83. }


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/698100
推荐阅读
相关标签
  

闽ICP备14008679号