喜欢在弹性搜索中的nGram上的精确匹配 [英] Favor exact matches over nGram in elasticsearch

查看:178
本文介绍了喜欢在弹性搜索中的nGram上的精确匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将一个字段映射为nGram和'exact'匹配,并使搜索结果中首先出现确切的匹配项。这是回答类似问题 ,但我正在努力使其工作。

I am trying to map a field as nGram and 'exact' match, and make the exact matches appear first in the search results. This is an answer to a similar question, but I am struggling to make it work.

无论我为确切字段指定的升值值,我每次都得到相同的结果顺序。这是我的字段映射的外观:

No matter what boost value I specify for the 'exact' field I get the same results order each time. This is how my field mapping looks:

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


推荐答案

多字段映射是正确的,但搜索查询需要如下更改:

The multi_field mapping is correct, but the search query needs to be changed like this:

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

考虑到确切比赛,并加上得分。

Now the results take into account the 'exact' match and adds up to the score.

这篇关于喜欢在弹性搜索中的nGram上的精确匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆