Elasticsearch 多字段查询 best_fields、most_fields、cross_fields,傻傻分不清楚?

本文涉及的产品
检索分析服务 Elasticsearch 版,2核4GB开发者规格 1个月
简介: 题记Multi-match query 的目的多字段匹配,但 Multi-match query 中的 best_fields, most_fields, cross_fields 分不清楚,都什么含义?下面我们一一举例解读。

image.png

链接

best_fields

为默认值,如果不指定,默认best_fields 匹配。


含义:多个字段中,返回评分最高的。


类似:dis_max query。


等价举例:(两个一起看,加深理解)


默认 best_fields 与 dis_max等价


POST blogs/_search

{

 "query": {

   "multi_match": {

     "type": "best_fields",

     "query": "Quick pets",

     "fields": [

       "title",

       "body"

     ],

     "tie_breaker": 0.2

   }

 }

}

与上述best_fields等价


POST blogs/_search

{

 "query": {

   "dis_max": {

     "queries": [

       {

         "match": {

           "title": "Quick pets"

         }

       },

       {

         "match": {

           "body": "Quick pets"

         }

       }

     ],

     "tie_breaker": 0.2

   }

 }

}

most_fields

含义:匹配多个字段,返回的综合评分(非最高分)


类似:bool + 多字段匹配。


等价举例:(两个一起看,加深理解)


most_fields 与下面的 bool 查询等价。


GET /titles/_search

{

 "query": {

   "multi_match": {

     "query": "barking dogs",

     "type": "most_fields",

     "fields": [

       "title^10",

       "title.std"

     ]

   }

 }

}

与上面的most_fields等价


GET titles/_search

{

 "query": {

   "bool": {

     "should": [

       {

         "match": {

           "title": {

             "query": "barking dogs",

             "boost": 10

           }

         }

       },

       {

         "match": {

           "title.std": "barking dogs"

         }

       }

     ]

   }

 }

}

cross_fields

含义:跨字段匹配——待查询内容在多个字段中都显示。


类似:bool + dis_max 组合。


等价举例:(两个一起看,加深理解)


与下面的bool查询逻辑一致


GET test003/_validate/query?explain=true

{

 "query": {

   "multi_match": {

     "query": "Will Smith",

     "type": "cross_fields",

     "fields": [

       "first_name",

       "last_name"

     ],

     "operator": "and"

   }

 }

}

GET test003/_validate/query?explain=true

返回:


"explanation" : "+blended(terms:[first_name:will, last_name:will]) +blended(terms:[first_name:smith, last_name:smith])"

与上面的cross_fields 基本等价,评分不一致,待深究


POST test003/_validate/query?explain=true

{

 "query": {

   "bool": {

     "must": [

       {

         "dis_max": {

           "queries": [

             {

               "match": {

                 "first_name": "Will"

               }

             },

             {

               "match": {

                 "last_name": "Will"

               }

             }

           ]

         }

       },

       {

         "dis_max": {

           "queries": [

             {

               "match": {

                 "first_name": "Smith"

               }

             },

             {

               "match": {

                 "last_name": "Smith"

               }

             }

           ]

         }

       }

     ]

   }

 }

}

小结

类似辨识度不好区分的 Elastic 知识点,考虑通过实战例子加以区分,实战一把,有助于提升选型效率。


参考:


1、https://zhuanlan.zhihu.com/p/24832190


2、https://github.com/mingyitianxia/deep_elasticsearch


加微信:elastic6(仅有少量坑位了),和 BAT 大佬一起精进 Elastic 技术!

相关实践学习
使用阿里云Elasticsearch体验信息检索加速
通过创建登录阿里云Elasticsearch集群,使用DataWorks将MySQL数据同步至Elasticsearch,体验多条件检索效果,简单展示数据同步和信息检索加速的过程和操作。
ElasticSearch 入门精讲
ElasticSearch是一个开源的、基于Lucene的、分布式、高扩展、高实时的搜索与数据分析引擎。根据DB-Engines的排名显示,Elasticsearch是最受欢迎的企业搜索引擎,其次是Apache Solr(也是基于Lucene)。 ElasticSearch的实现原理主要分为以下几个步骤: 用户将数据提交到Elastic Search 数据库中 通过分词控制器去将对应的语句分词,将其权重和分词结果一并存入数据 当用户搜索数据时候,再根据权重将结果排名、打分 将返回结果呈现给用户 Elasticsearch可以用于搜索各种文档。它提供可扩展的搜索,具有接近实时的搜索,并支持多租户。
相关文章
|
2月前
|
存储 固态存储 Java
Elasticsearch中查询性能优化
Elasticsearch中查询性能优化
194 0
|
2月前
|
Java API iOS开发
Elasticsearch 字段别名 field-alias
Elasticsearch 字段别名 field-alias
28 0
|
3月前
Elasticsearch之RestClient查询文档
Elasticsearch之RestClient查询文档
139 1
|
2月前
|
缓存 算法 索引
【Elasticsearch专栏 07】深入探索:Elasticsearch的倒排索引如何进行模糊查询和通配符查询
Elasticsearch的倒排索引支持模糊查询和通配符查询,通过特定的算法和数据结构,能够实现对关键词的模糊匹配和通配符匹配。这两种查询类型提供了更灵活的搜索功能,但可能影响查询性能,需结合优化策略使用。
|
2月前
|
缓存 自然语言处理 数据挖掘
一篇文章让你学会Elasticsearch中的查询
一篇文章让你学会Elasticsearch中的查询
137312 118
|
2月前
|
测试技术 定位技术 API
万字长文:一文彻底搞懂Elasticsearch中Geo数据类型查询、聚合、排序
万字长文:一文彻底搞懂Elasticsearch中Geo数据类型查询、聚合、排序
94614 140
|
2月前
|
iOS开发 索引 MacOS
Elasticsearch 聚合字段aggregate-metric-double
Elasticsearch 聚合字段aggregate-metric-double
22 0
|
2月前
|
JSON 前端开发 API
【Elasticsearch】搜索结果处理和RestClient查询文档
【Elasticsearch】搜索结果处理和RestClient查询文档
338 0
|
2月前
|
JSON 自然语言处理 算法
【Elasticsearch】DSL查询文档
【Elasticsearch】DSL查询文档
315 0
|
3月前
|
Java
Springboot整合Elasticsearch 7.X 复杂查询
这里使用Springboot 2.7.12版本,Elasticsearch为7.15.0。
Springboot整合Elasticsearch 7.X 复杂查询

热门文章

最新文章