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

411 阅读4分钟

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、zhuanlan.zhihu.com/p/24832190

2、github.com/mingyitianx…

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

推荐阅读:

重磅 | 死磕 Elasticsearch 方法论认知清单(2020年国庆更新版)

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

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

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

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

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


短时间快习得多干货!

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

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