ElasticSearch系列 - SpringBoot整合ES:多字段查询 multi_match

文章目录

      • 1. 什么是 ElasticSearch 的 multi_match 查询?
      • 2. 如何在 multi_match 查询中指定查询字段?
      • 3. 如何在 multi_match 查询中指定查询权重?
      • 4. SpringBoot整合ES实现 multi_match 查询

1. 什么是 ElasticSearch 的 multi_match 查询?

有时用户需要在多个字段中查询关键词,除了使用布尔查询封装多个match查询之外,可替代的方案是使用multi_match。可以在multi_match的query子句中组织数据匹配规则,并在fields子句中指定需要搜索的字段列表。

以下是一个示例multi-match查询的语法:

{
  "query": {
    "multi_match": {
      "query": "搜索文本",
      "fields": ["字段1", "字段2", "字段3"]
    }
  }
}

在上面的查询中,我们指定了要搜索的文本和要搜索的字段列表。ElasticSearch将搜索所有指定的字段,并将它们合并为一个结果集。

2. 如何在 multi_match 查询中指定查询字段?

在 Elasticsearch 的 multi_match 查询中,可以使用 “fields” 参数来指定要查询的字段。该参数接受一个数组,其中包含要查询的字段名称。

① 构造数据:

PUT /my_index
{
  "mappings": {
    "properties": {
      "title":{
        "type": "text"
      },
      "content":{
        "type": "text"
      }
    }
  }
}

PUT /my_index/_doc/1
{
  "title": "Jindu Fashion Couple Romantic Theme Hotel",
  "content": "Qingdao City"
}

PUT /my_index/_doc/2
{
  "title": "Jindu Jiayi Holiday Inn",
  "content": "Beijing City"
}

PUT /my_index/_doc/3
{
  "title": "Jindu Xinxin 24 hour Hotel",
  "content": "Huaibei City"
}

② 以下查询将在 “title” 和 “content” 字段中搜索包含 “Beijing” 和 “Xinxin” 的文档:

{
  "query": {
    "multi_match": {
      "query": "Beijing Xinxin",
      "fields": ["title", "content"]
    }
  }
}

如果未指定 “fields” 参数,则默认情况下将在所有字段中搜索。

{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 0.9808292,
    "hits" : [
      {
        "_index" : "my_index",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 0.9808292,
        "_source" : {
          "title" : "Jindu Jiayi Holiday Inn",
          "content" : "Beijing City"
        }
      },
      {
        "_index" : "my_index",
        "_type" : "_doc",
        "_id" : "3",
        "_score" : 0.9808292,
        "_source" : {
          "title" : "Jindu Xinxin 24 hour Hotel",
          "content" : "Huaibei City"
        }
      }
    ]
  }
}

3. 如何在 multi_match 查询中指定查询权重?

在 Elasticsearch 的 multi_match 查询中,可以使用 “fields” 参数来指定要搜索的字段,并使用 “^” 符号来指定每个字段的权重。

例如:“title” 的权重为 5,“content” 的权重为 2。

GET /my_index/_search
{
  "query": {
    "multi_match": {
      "query": "Beijing Xinxin",
      "fields": ["title^5","content^2"]
    }
  }
}
{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 4.904146,
    "hits" : [
      {
        "_index" : "my_index",
        "_type" : "_doc",
        "_id" : "3",
        "_score" : 4.904146,
        "_source" : {
          "title" : "Jindu Xinxin 24 hour Hotel",
          "content" : "Huaibei City"
        }
      },
      {
        "_index" : "my_index",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 1.9616584,
        "_source" : {
          "title" : "Jindu Jiayi Holiday Inn",
          "content" : "Beijing City"
        }
      }
    ]
  }
}

4. SpringBoot整合ES实现 multi_match 查询

@Slf4j
@Service
public class ElasticSearchImpl {

    @Autowired
    private RestHighLevelClient restHighLevelClient;

    public void searchUser() throws IOException {
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();

        MultiMatchQueryBuilder multiMatchQueryBuilder = QueryBuilders.multiMatchQuery("Beijing Xinxin", "title", "content");
        searchSourceBuilder.query(multiMatchQueryBuilder);

        SearchRequest searchRequest = new SearchRequest(new String[]{"hotel"},searchSourceBuilder);
        SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
        System.out.println(searchResponse);
    }
}

你可能感兴趣的:(elasticsearch,spring,boot,java)