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

Elasticsearch 最少必要知识实战教程直播回放

题记

Multi-match query 的目的多字段匹配,但 Multi-match query 中的 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等价

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 方法论认知清单(2020年国庆更新版)

你的 Elasticsearch 难题,官方文档早就有了答案......

能拿驾照就能通过 Elastic 认证考试!

潜心一技、做到极致!——Elastic认证工程师之路

升级这十点认知,你就是大佬!

认知升级——不做开始爱好者!


短时间快习得多干货!

中国 近 50%+ Elastic 认证工程师出自于此!

和全球 875+ Elastic 爱好者一起精进 Elasticsearch!

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

铭毅天下

和你一起,死磕Elastic!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值