Elasticsearch多匹配字段不包含查询字符串

问题描述 投票:0回答:1

我正在使用以下查询对象进行多重匹配搜索。

{
    _source: [
        'baseline',
        'cdrp',
        'date',
        'description',
        'dev_status',
        'element',
        'event',
        'id'
    ],
    track_total_hits: true,
    query: {
        bool: {
            filter: [{name: "baseline", values: ["1f.0.1.0", "1f.1.8.3"]}],
            should: [
                {
                    multi_match:{
                        query: "national",
                        fields: ["cdrp","description","narrative.*","title","cop"]
                    }
                }
            ]
        } 
    },
    highlight: { fields: { '*': {} } },
    sort: [],
    from: 0,
    size: 50
}

我希望在description或narrative.*字段中找到 "national "这个词 但在返回的2条记录中只有一条符合我的期望值 我想知道为什么。

elasticsearch.config.ts

"settings": {
    "analysis": {
        "analyzer": {
            "search_synonyms": {
                "tokenizer": "whitespace",
                "filter": [
                    "graph_synonyms",
                    "lowercase",
                    "asciifolding"
                ],
            }
        }
    }
},

"mappings": {
    "properties": {
        "description": {
            "type": "text",
            "analyzer": "search_synonyms"
        },
        "narrative": {
            "type":"object",
            "properties":{
                "_all":{
                    "type": "text",
                    "analyzer": "search_synonyms"
                }
            }
        },
    }
}
elasticsearch indexing filter analyzer
1个回答
0
投票

Should子句的工作原理和OR一样,它并不过滤掉文档,而是影响评分。匹配should子句的文档得分较高。

如果你想过滤多重匹配,你可以把它移到过滤子句里面。

filter: [
           {
              name: "baseline", values: ["1f.0.1.0", "1f.1.8.3"]
           },
           {
               multi_match:
               {
                     query: "national",
                     fields: ["cdrp","description","narrative.*","title","cop"]
               }
           }
        ]

Filter与Must:-两者都返回与指定条款相匹配的文档。Filter不对文档进行打分,所以如果对文档的分数不感兴趣,或者不关心文档返回的顺序,可以使用filter。所以如果你对文档的评分不感兴趣,或者不关心返回文档的顺序,你可以使用过滤器。所以两者都是一样的,只是打分不同。

匹配度越高的文档得分越高

多重匹配 默认使用 最好的领域

查找与任何字段匹配的文档,但使用最佳字段的_score。

它使用字段的最大匹配数返回的分数来计算每个文档的分数。

例子

文件1有两个字段匹配,字段1(得分2),字段2(得分1)。

文件2有一个字段匹配,字段2(得分3)。

即使有1个字段匹配,文献网2的排名也会更高。

你可以把它改为most_fields。

查找与任何字段匹配的文档,并将每个字段的_score合并。

{
  "query": {
    "bool": {
      "must": [
        {
          "multi_match": {
            "query": "test",
            "fields": [],
            "type": "most_fields"
          }
        }
      ]
    }
  }
}

但是,如果一个文档匹配的字段数量较少,也会因为多个术语造成的字段得分较高而获得较高的排名。

如果您想给一个字段相同的分数,而不考虑匹配的标记数量。您需要使用 恒定的分数 疑问

{
  "query": {
    "bool": {
      "should": [
        {
          "constant_score": {
            "filter": {
              "term": {
                "field1": "test"
              }
            }
          }
        },
        {
          "constant_score": {
            "filter": {
              "term": {
                "field2": "test"
              }
            }
          }
        }
      ]
  }
},
  "highlight": {
    "fields": {
      "field1": {},
      "field2": {}
    }
  }
}

结果:

"hits" : [
      {
        "_index" : "index18",
        "_type" : "_doc",
        "_id" : "iSCe6nEB8J88APx3YBGn",
        "_score" : 2.0, --> one score per field matched
        "_source" : {
          "field1" : "test",
          "field2" : "test"
        },
        "highlight" : {
          "field1" : [
            "<em>test</em>"
          ],
          "field2" : [
            "<em>test</em>"
          ]
        }
      },
      {
        "_index" : "index18",
        "_type" : "_doc",
        "_id" : "iiCe6nEB8J88APx3ghF-",
        "_score" : 1.0,
        "_source" : {
          "field1" : "test",
          "field2" : "abc"
        },
        "highlight" : {
          "field1" : [
            "<em>test</em>"
          ]
        }
      },
      {
        "_index" : "index18",
        "_type" : "_doc",
        "_id" : "iyCf6nEB8J88APx3UhF8",
        "_score" : 1.0,
        "_source" : {
          "field1" : "test do",
          "field2" : "abc"
        },
        "highlight" : {
          "field1" : [
            "<em>test</em> do"
          ]
        }
      }
    ]
  }
© www.soinside.com 2019 - 2024. All rights reserved.