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

这篇具有很好参考价值的文章主要介绍了ElasticSearch系列 - 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。文章来源地址https://www.toymoban.com/news/detail-420555.html

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系列 - SpringBoot整合ES:多字段查询 multi_match的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • ElasticSearch系列 - SpringBoot整合ES:多个精确值查询 terms

    ElasticSearch - SpringBoot整合ES:多个精确值查询 terms 01. ElasticSearch terms 查询支持的数据类型 在Elasticsearch中,terms查询支持多种数据类型,包括: 字符串类型:可以将多个字符串值作为数组传递给terms查询,以匹配包含任何一个指定字符串值的文档。 数值类型:可以将多个数值作

    2024年02月16日
    浏览(58)
  • ElasticSearch系列 - SpringBoot整合ES之全文搜索匹配查询 match

    官方文档地址:https://www.elastic.co/guide/en/elasticsearch/reference/index.html 权威指南:https://www.elastic.co/guide/cn/elasticsearch/guide/current/structured-search.html 1. 数据准备 官方测试数据下载地址:https://download.elastic.co/demos/kibana/gettingstarted/accounts.zip ,数据量很大,我们自己构造数据吧。 2. m

    2023年04月08日
    浏览(40)
  • ElasticSearch系列 - SpringBoot整合ES:短语匹配查询 match_phrase

    1. ElasticSearch match_phrase查询是什么?它与match查询有什么区别? match_phrase查询是一种用于匹配短语的查询方式,可以用于精确匹配多个单词组成的短语。它会将查询字符串分解成单词,然后按照顺序匹配文档中的单词,只有当文档中的单词顺序与查询字符串中的单词顺序完全

    2024年02月12日
    浏览(39)
  • ElasticSearch系列 - SpringBoot整合ES:查询条件 query 和过滤条件 filter 的区别

    01. Elasticsearch 查询条件和过滤条件的区别? Elasticsearch中的查询条件和过滤条件都是用于搜索和过滤文档的条件,但它们之间有一些区别。 查询条件是用于计算文档相关度得分的条件,它会将所有符合条件的文档按照相关度得分从高到低排序,并返回前N个文档。查询条件可以

    2024年02月14日
    浏览(45)
  • ElasticSearch序列 - SpringBoot整合ES:范围查询 range

    01. ElasticSearch range查询是什么? Elasticsearch 中的 range 查询可以用于查询某个字段在一定范围内的文档。 range 查询可同时提供包含和不包含这两种范围表达式,可供组合的选项如下: gt : 大于(greater than) lt : 小于(less than) gte : = 大于或等于(greater than or equal to) lte : = 小于

    2024年02月09日
    浏览(31)
  • ElasticSearch序列 - SpringBoot整合ES:根据指定的 ids 查询

    1. ElasticSearch 根据 ids 查询文档 ① 索引文档,构造数据 ② 查询文档 id 为 1 或者 2 的文档: 我们索引文档时,文档的id为整型,为什么查询出来的文档 id为字符串类型呢?如果我们使用字符串类型的文档id查询呢? 可以看到仍然可以查询到匹配的文档。 在Elasticsearch中,文档

    2024年02月11日
    浏览(45)
  • ElasticSearch系列 - SpringBoot整合ES:ElasticSearch分析器

    1. ElasticSearch match 文本搜索的过程? Elasticsearch 的 match 查询是一种基于文本匹配的查询方式,它的搜索过程如下: ① 将查询字符串分词:Elasticsearch 会将查询字符串分成一个个词项(term),并去除停用词(如“的”、“是”等常用词汇)和标点符号等无意义的字符。 ② 构建

    2023年04月18日
    浏览(112)
  • ElasticSearch系列 - SpringBoot整合ES:分析器

    1. ElasticSearch match 文本搜索的过程? Elasticsearch 的 match 查询是一种基于文本匹配的查询方式,它的搜索过程如下: ① 将查询字符串分词:Elasticsearch 会将查询字符串分成一个个词项(term),并去除停用词(如“的”、“是”等常用词汇)和标点符号等无意义的字符。 ② 构建

    2024年02月06日
    浏览(48)
  • ElasticSearch系列 - SpringBoot整合ES:实现搜索结果排序 sort

    00. 数据准备 01. Elasticsearch 默认的排序方式是什么? ElasticSearch 默认的排序方式是相关性排序。相关性排序是根据查询条件与文档的匹配程度来计算每个文档的相关性得分,然后按照得分从高到低进行排序。相关性排序是 ElasticSearch 中最常用的排序方式,因为它可以根据查询

    2024年02月02日
    浏览(39)
  • ElasticSearch系列 - SpringBoot整合ES:restHighLevelClient.count(countRequest, RequestOptions.DEFAULT)

    restHighLevelClient.count(countRequest, RequestOptions.DEFAULT) 是 Elasticsearch Java High Level REST Client 中用于执行计数请求的方法。 具体来说,它接受两个参数: countRequest:一个 CountRequest 对象,表示计数请求的参数,包括要计数的索引、查询条件等。 RequestOptions.DEFAULT:一个 RequestOptions 对象

    2024年02月08日
    浏览(47)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包