目 录CONTENT

文章目录

【Elastic Search】 multi_match查询详解

EulerBlind
2025-07-01 / 0 评论 / 0 点赞 / 0 阅读 / 0 字
  1. 官方文档

    概述

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

    格式样例

    {
        "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

评论区