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

559 篇文章 547 订阅 ¥79.90 ¥99.00

在这里插入图片描述

1.概述

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

1.1 题记

Multi-match query 的目的多字段匹配,但 Multi-match query 中的 best_fields, most_fields, cross_fields 分不清楚,都什么含义?

下面我们一一举例解读。

1.1.1 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
    }
  }
}

1.1.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"
          }
        }
      ]
    }
  }
}

1.1.3 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"
                }
              }
            ]
          }
        }
      ]
    }
  }
}

扩展阅读:elasticsearch应用场景之cross_fields

2.小结

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

N.参考:

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

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

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值