Elasticsearch:为什么精确匹配的得分低于部分匹配的得分

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

我的问题

我搜索单词form,但完全匹配的单词form不是第一个结果。有什么办法可以解决这个问题?

我的搜索查询

{
  "query": {
    "match": {
      "word": "form"
    }
  }
}

结果

word             score
--------------------------
formulation      10.864353
formaldehyde     10.864353
formless         10.864353
formal   10.84412
formerly         10.84412
forma    10.84412
formation        10.574185
formula          10.574185
formulate        10.574185
format   10.574185
formally         10.574185
form     10.254687
former   10.254687
formidable       10.254687
formality        10.254687
formative        10.254687
ill-formed       10.054999
in form          10.035862
pro forma        9.492243

POST my_index / _analyze

搜索中的单词form只有一个标记form

在索引中,form个标记为[“ f”,“ fo”,“ for”,“ form”]; formulation标记为[“ f”,“ fo”,...,“公式”,“公式”]。

我的配置

过滤器

        "edgengram_filter": {
          "type": "edge_ngram",
          "min_gram": 1,
          "max_gram": 20
        }

分析器

      "analyzer": {
        "abc_vocab_analyzer": {
          "type": "custom",
          "tokenizer": "standard",
          "filter": [
            "keyword_repeat",
            "lowercase",
            "asciifolding",
            "edgengram_filter",
            "unique"
          ]
        },
        "abc_vocab_search_analyzer": {
          "type": "custom",
          "tokenizer": "standard",
          "filter": [
            "keyword_repeat",
            "lowercase",
            "asciifolding",
            "unique"
          ]
        }
      }

映射

        "word": {
          "type": "text",
          "analyzer": "abc_vocab_analyzer",
          "search_analyzer": "abc_vocab_search_analyzer"
        }
elasticsearch elasticsearch-5 elasticsearch-dsl elasticsearch-6
2个回答
0
投票

因为您在此字段中输入的是文本,这意味着ES会对该字段进行全文搜索分析。 ES搜索过程可以找到与您所给单词最相似的结果。要准确地搜索单词“ form”,请将搜索方法更改为match_phrase。此外,您还可以阅读以下文章,以了解有关不同ES搜索方法的更多信息:https://www.cnblogs.com/yjf512/p/4897294.htmlhttps://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query-phrase.html


0
投票

[在我看来,您的用例是在存在完全匹配时提供boost

为此,我建议为word创建同级字段作为word.keyword类型的keyword,并通过以下方式使用Term Query进行增强。关键字字段不会进行分析,而是按原样存储。

映射:

   {
    "word":{
      "type": "text",
      "analyzer": "abc_vocab_analyzer",
      "search_analyzer": "abc_vocab_search_analyzer"
      "fields":{
        "keyword":{
          "type": "keyword"
        }
      }
    }
  }

查询:

POST <your_index_name>/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "word": "form"
          }
        }
      ],
      "should": [                          <---- Note this
        {
          "term": {
            "word.keyword": {
              "value": "form",
              "boost": 10                 <---- Provide boosting for exact matches. 
            }
          }
        }
      ]
    }
  }
}

让我知道是否有帮助!

© www.soinside.com 2019 - 2024. All rights reserved.