【问题标题】:Elastic ngram prioritise whole words弹性 ngram 优先考虑整个单词
【发布时间】:2020-03-27 14:27:24
【问题描述】:

我正在尝试构建具有数百万个可能值的自动完成功能。我已经设法用两种不同的方法 match 和 ngram 来做到这一点。问题是 match 要求用户输入整个单词,而 ngram 返回的结果很差。如果没有匹配结果,有没有办法只返回 ngram 结果?

方法一:匹配

返回非常相关的结果,但需要用户输入完整的单词

//mapping
analyzer: {
   std_english: {
   type: 'standard',
   stopwords: '_english_',
   },
} 

//search
query: {
   bool: {
   must: [
       { term: { semanticTag: type } }, 
       { match: { search } }
    ]}
}

方法二:ngram

返回差匹配

//mapping
analysis: {
  filter: { 
     autocomplete_filter: {
        type: 'edge_ngram',
        min_gram: 1,
        max_gram: 20,
     },
},
analyzer: {
  autocomplete: {
    type: 'custom',
    tokenizer: 'standard',
    filter: ['lowercase', 'autocomplete_filter'],
    },
},


//search
query: {
   bool: {
   must: [
       { term: { semanticTag: type } }, 
       { match: {
          term: { 
             query: search,
             operator: 'and',
             }
          }
        }
    ]}
}

【问题讨论】:

标签: elasticsearch


【解决方案1】:

尝试将查询更改为类似的内容 -

{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "semanticTag": "type"
          }
        },
        {
          "match_phrase_prefix": {
            "fieldName": {
              "query": "valueToSearch"
            }
          }
        }
      ]
    }
  }
}

您可以使用match_phrase_prefix,使用此用户无需输入整个单词,用户输入的任何以索引字段数据开头的内容都会返回。

请注意,这也将从索引文档中任何可用的中间词中提取结果。
例如如果在某个字段中索引的数据类似于 - "lorem ipsum" 和用户类型 "ips",那么您将获得整个文档以及以 "ips" 开头的其他文档

您可以使用标准或自定义分析仪,您必须检查哪种分析仪更适合您的用例。根据相关信息,上述方法适用于standard analyzer

【讨论】:

    猜你喜欢
    • 2016-06-18
    • 2016-11-25
    • 2022-01-16
    • 1970-01-01
    • 2014-07-21
    • 1970-01-01
    • 1970-01-01
    • 2010-10-27
    • 1970-01-01
    相关资源
    最近更新 更多