美文网首页already
match、match_phrase的区别

match、match_phrase的区别

作者: 随风_d6a2 | 来源:发表于2022-05-07 00:30 被阅读0次

    一、数据准备

    PUT /tehero_index
    {
      "settings": {
        "index": {
          "number_of_shards": 1,
          "number_of_replicas": 1
        }
      },
      "mappings": {
        "properties": {
            "id": {
              "type": "integer"
            },
            "content": {
              "type": "text",
              "fields": {
                "ik_max_analyzer": {
                  "type": "text",
                  "analyzer": "ik_max_word",
                  "search_analyzer": "ik_max_word"
                }
              }
            },
            "name":{
              "type":"text"
            },
            "createAt": {
              "type": "date"
            }
          }
      }
    }
    
    

    导入测试数据

    POST /tehero_index/_doc/1
    { "id" : 1, "content" : "关注我,系统学编程"}
    POST /tehero_index/_doc/2
    { "id" : 1, "content" : "系统学编程,关注我"}
    POST /tehero_index/_doc/3
    { "id" : 1, "content" : "系统编程,关注我"}
    POST /tehero_index/_doc/4
    { "id" : 1, "content" : "关注我,间隔系统学编程"}
    

    查看全部数据:

    POST /tehero_index/_search 
    { "query":{ "match_all": { } } }
    

    原始数据:
    { "id" : 1,"content":"关注我,系统学编程" }
    { "id" : 2,"content":"系统学编程,关注我" }
    { "id" : 3,"content":"系统编程,关注我" }
    { "id" : 4,"content":"关注我,间隔系统学编程" }

    三、match query 对应到mysql

    POST /tehero_index/_search 
    { "query":{ "match":{ "content":"系统编程" } } }
    
    
    

    DSL执行步骤分析:

    1)检索词“系统编程”被分词器分词为两个Token【系统】【编程】;
    2)将这两个Token在【倒排索引】中,针对Token字段进行检索,等价于sql:【where Token = 系统 or Token = 编程】;
    3)对照图【数据的倒排序索引】,可见,该DSL能检索到所有文档,文档3的评分最高(因为它包含两个Token),其他3个文档评分相同。

    三、match_phrase query

    match_phrase查询分析文本并根据分析的文本创建一个短语查询。match_phrase 会将检索关键词分词。match_phrase的分词结果必须在被检索字段的分词中都包含,而且顺序必须相同,而且默认必须都是连续的。

    使用 match_phrase 查询:

    POST /tehero_index/_doc/_search
    {
        "query": {
            "match_phrase": {
                "content":"关注我,系统学"
            }
        }
    }
    
    

    结果:只有文档1

    #! Deprecation: [types removal] Specifying types in search requests is deprecated.
    {
      "took" : 1,
      "timed_out" : false,
      "_shards" : {
        "total" : 1,
        "successful" : 1,
        "skipped" : 0,
        "failed" : 0
      },
      "hits" : {
        "total" : {
          "value" : 1,
          "relation" : "eq"
        },
        "max_score" : 0.730023,
        "hits" : [
          {
            "_index" : "tehero_index",
            "_type" : "_doc",
            "_id" : "1",
            "_score" : 0.730023,
            "_source" : {
              "id" : 1,
              "content" : "关注我,系统学编程"
            }
          }
        ]
      }
    }
    
    

    使用 match 查询:

    POST /tehero_index/_search 
    { "query":{ "match":{ "content":"关注我,系统学" } } }
    
    # 可以查询出所有结果
    

    分析:检索词“关注我,系统学”会被分词为3个Token【关注、我、系统学】;而文档1、文档2和文档4 的content被分词后都包含这3个关键词,但是只有文档1的Token的顺序和检索词一致,且连续。所以使用 match_phrase 查询只能查询到文档1(ps:文档2 Token顺序不一致;文档4 Token不连续;文档3 Token没有完全包含)。使用 match查询可以查询到所有文档,是因为所有文档都有【关注、我】这两个Token。

    match_phrase 核心参数:slop 参数-Token之间的位置距离容差值

    POST /tehero_index/_doc/_search
    {
        "query": {
            "match_phrase": {
                "content":{
                  "query": "关注我,系统学",
                    "slop":2
                }
            }
        }
    }
    
    # 结果:文档1和文档4都被检索出来
    

    相关文章

      网友评论

        本文标题:match、match_phrase的区别

        本文链接:https://www.haomeiwen.com/subject/vrcturtx.html