【Elastic Search】 multi_match查询详解

官方文档

概述

用于在多个字段上执行相同查询

格式样例

{
    "multi_match": {
        "query":                "Quick brown fox",    # 查询词
        "type":                 "best_fields",        # 匹配方式
        "fields":               [ "title", "body" ],  # 查询字段
        "tie_breaker":          0.3,                  # 多个字段的相关性
        "minimum_should_match": "30%",                # 匹配度
        "analyzer":             "standard",           # 分词器
				"operator":             "and"                 # 匹配逻辑
    }
}

参数解释

  • query : 查询词、句,

  • type :匹配方式,有三种方式可选

    • best_fields 最佳匹配 (默认类型,可不指定)

    • most_fields 多数字段匹配

      {
        "query": {
          "multi_match" : {
            "query":      "quick brown fox",
            "type":       "most_fields",
            "fields":     [ "title", "title.original", "title.shingles" ]
          }
        }
      }
      相当于
      {
        "query": {
          "bool": {
            "should": [
              { "match": { "title":          "quick brown fox" }},
              { "match": { "title.original": "quick brown fox" }},
              { "match": { "title.shingles": "quick brown fox" }}
            ]
          }
        }
      }
      
    • cross_fields 跨字段匹配

      • 所有的匹配词必须至少在一个字段中出现才会匹配(通常对boost为1的短字符串有用)
      • fuzziness参数不可和cross_fields一起使用
      {
        "query": {
          "multi_match" : {
            "query":      "Will Smith",
            "type":       "cross_fields",
            "fields":     [ "first_name", "last_name" ],
            "operator":   "and"
          }
        }
      }
      相当于
      +(first_name:will last_name:will)+(first_name:smith last_name:smith)
      
      {
        "query": {
          "multi_match" : {
            "query":      "Jon",
            "type":       "cross_fields",
            "fields":     [
              "first", "first.edge",
              "last",  "last.edge"
            ]
          }
        }
      }
      相当于
      blended("jon", fields: [first, last])
      | (
          blended("j",   fields: [first.edge, last.edge])
          blended("jo",  fields: [first.edge, last.edge])
          blended("jon", fields: [first.edge, last.edge])
      )
      
    • phrase 短语匹配,该方式下不会被分词。(该方式因为不会被分词,用于精确匹配)

    • phrase_prefix 前缀短语匹配

      {
        "query": {
          "multi_match" : {
            "query":      "quick brown f",
            "type":       "phrase_prefix",
            "fields":     [ "subject", "message" ]
          }
        }
      }
      相当于
      {
        "query": {
          "dis_max": {
            "queries": [
              { "match_phrase_prefix": { "subject": "quick brown f" }},
              { "match_phrase_prefix": { "message": "quick brown f" }}
            ]
          }
        }
      }
      
    • bool_prefix 指定为该方式,表明只希望查询到一个bool值

  • fields :需要查询的字段列表,默认最多给定1024个 。

    • 给定字段本身支持模糊查询,如 "fields": "*_title"
    • 可以指定每个字段的匹配权重,如 "fields": [ "*_title", "chapter_title^2" ] ,其中chapter_title字段的boost为2,
  • tie_breaker:字段之间的相关度

    分数解释
    0.0从(例如)first_name:will 和last_name:will(默认)中取出单个最佳分数
    1.0将(例如)first_name:will 和 last_name:will 的分数加在一起
    0.0 < n < 1.0将单个最佳得分加上 tie_breaker 乘以其他匹配字段/组的每个得分
  • minimum_should_match :匹配阈值

  • operator :指定给定词之间的逻辑(默认使用的是or),具体示例如下

    对于如下匹配

    {
      "query": {
        "multi_match" : {
          "query":      "Will Smith",
          "type":       "best_fields",
          "fields":     [ "first_name", "last_name" ],
          "operator":   "and" 
        }
      }
    }
    

    实际执行逻辑如下,即所有匹配词必须同时满足到一个给定字段(区别于or,只需要满足一个匹配即匹配)

    (+first_name:will +first_name:smith) | (+last_name:will  +last_name:smith)
    

    组合字段查询(combined_fields)以及cross_fields模式可以打破单个字段限制

  • analyzer 指定分词器

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值