【问题标题】:Favor exact matches over nGram in elasticsearch在弹性搜索中倾向于精确匹配而不是 nGram
【发布时间】:2014-09-30 21:48:47
【问题描述】:

我正在尝试将字段映射为 nGram 和“精确”匹配,并使精确匹配首先出现在搜索结果中。这是answer to a similar question,但我正在努力让它发挥作用。

无论我为“精确”字段指定什么提升值,我每次都会得到相同的结果顺序。这就是我的字段映射的样子:

"name" : {
    "type" : "multi_field",
    "fields" : {
      "name" : {
        "type" : "string",
        "boost" : 2.0,
        "analyzer" : "ngram"
      },
      "exact" : {
        "type" : "string",
        "boost" : 4.0,
        "analyzer" : "simple",
        "include_in_all" : false
      }
    }
  }

这就是查询的样子:

{
    "query": {
        "filtered": {
            "query": {
                "query_string": {
                    "fields":["name","name.exact"],
                    "query":"Woods"
                }
            }
        }
    }
}

【问题讨论】:

    标签: elasticsearch n-gram


    【解决方案1】:

    了解分数的计算方式

    Elasticsearch 可以选择为每个搜索结果生成解释。通过将 explain 参数设置为 true

    POST  <Index>/<Type>/_search?explain&format=yaml
    {
    "query" : " ....."
    }
    

    每次点击都会产生大量输出,这可能会让人不知所措,但值得花一些时间来了解这一切的含义

    eplian 的输出在 json 中可能更难阅读,所以添加 format=yaml 会更容易阅读

    了解文档匹配与否的原因

    您可以将查询传递到如下所示的特定文档,以查看如何进行匹配的说明。

    GET <Index>/<type>/<id>/_explain
    {
    "query": "....."
    }
    

    【讨论】:

    【解决方案2】:

    multi_field 映射是正确的,但是搜索查询需要改成这样:

    {
        "query": {
            "filtered": {
                "query": {
                    "multi_match": { # changed from "query_string"
                        "fields": ["name","name.exact"],
                        "query": "Woods",
                        # added this so the engine does a "sum of" instead of a "max of"
                        # this is deprecated in the latest versions but works with 0.x
                        "use_dis_max": false
                    }
                }
            }
        }
    }
    

    现在结果会考虑“精确”匹配并累加得分。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-10-27
      • 2017-01-09
      • 2020-03-06
      • 2015-08-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-04
      相关资源
      最近更新 更多