Elasticsearch搜索结果相关性问题

时间:2019-04-09 18:20:48

标签: elasticsearch search match

有人可以解释一下为什么匹配查询首先返回较少的相关结果吗?我有一个名为“规范化”的索引字段。其映射为:

normalized: {
    type: "text"
    analyzer: "autocomplete"
}

此字段的设置为:

analysis; {
    filter: {
        autocomplete_filter: {
            type: "edge_ngram",
            min_gram => "1",
            max_gram => "20"
        }
    analyzer: {
        autocomplete: {
            filter: [
                "lowercase",
                "asciifolding",
                "autocomplete_filter"
            ],
            type: "custom",
            tokenizer: "standard"
        }
    }

据我所知,它会生成ascii,小写的令牌,例如MOUSE = m,mo,mou,mous,mouse。 问题是这样的请求:

{
    'query': {
        'bool': {
            'must': {
                'match': {
                    'normalized': 'simag'
                }
             }
         }
     }
 }

返回类似

的结果
  1. “ siman siman服务”
  2. “ mgr simona simunkova s​​imiki”
  3. “ Siman-SIMANS”
  4. “ simunek simunek simunek”
  5. .....

但是没有 SIMAG 包含匹配词组的所有字母。 如何获得最相关的结果将是在不包含所有字母的标记之前包含所有字母的单词。 希望有人了解我的需求。 谢谢。

PS:我不确定,但是这个查询呢?

{
    'query': {
        'bool': {
            'should': [
                {'term': {'normalized': 'simag'}},
                {'match': {'normalized': 'simag'}}
             ]
         }
     }
 }

与以前的代码相比,这有意义吗?

1 个答案:

答案 0 :(得分:2)

请注意,对匹配查询进行了分析,这意味着在查询时使用的是同一分析器,在查询中提到的字段在索引时使用了同一分析器。

在您的情况下,您在autocomplete字段上应用了normalized分析器,并且如上所述,它为MOUSE生成了以下令牌:

MOUSE = m, mo, mou, mous, mouse.

以类似的方式,如果您在同一字段上使用mouse查询来搜索match,它将搜索以下查询字符串:-

m, mo, mou, mous, mouse ..因此包含mouseemouser之类单词的结果也将在索引..期间出现,它创建了与搜索词上生成的标记匹配的标记。

在弹性网站https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query.html上了解有关匹配查询的更多信息第一行本身解释了您的搜索结果

  

匹配查询接受文本/数字/日期,对其进行分析,以及   构造一个查询:

如果您想深入了解,您的搜索查询如何匹配文档及其得分,请使用explain API

https://www.elastic.co/guide/en/elasticsearch/reference/current/search-explain.html

如果您有任何疑问,请告诉我