天天看点

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

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

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等价

   "dis_max": {

     "queries": [

       {

         "match": {

           "title": "Quick pets"

         }

       },

           "body": "Quick pets"

       }

most_fields

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

类似:bool + 多字段匹配。

most_fields 与下面的 bool 查询等价。

GET /titles/_search

     "query": "barking dogs",

     "type": "most_fields",

       "title^10",

       "title.std"

     ]

与上面的most_fields等价

GET titles/_search

   "bool": {

     "should": [

           "title": {

             "query": "barking dogs",

             "boost": 10

           }

           "title.std": "barking dogs"

cross_fields

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

类似:bool + dis_max 组合。

与下面的bool查询逻辑一致

GET test003/_validate/query?explain=true

     "query": "Will Smith",

     "type": "cross_fields",

       "first_name",

       "last_name"

     "operator": "and"

返回:

"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

     "must": [

         "dis_max": {

           "queries": [

             {

               "match": {

                 "first_name": "Will"

               }

             },

                 "last_name": "Will"

             }

           ]

                 "first_name": "Smith"

                 "last_name": "Smith"

小结

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

参考:

1、

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

2、

https://github.com/mingyitianxia/deep_elasticsearch

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

继续阅读