ElasticSearch:仅在输入开头查询Search_as_you_type字段

u91tlkcl  于 2022-10-06  发布在  ElasticSearch
关注(0)|答案(1)|浏览(220)

我在Elastic Search上有一个这样的Map

{
      mappings: {
        properties: {
          score: { type: 'double' },
          name: { type: 'search_as_you_type' }
        }
      }
    }

和如下所示的查询

{
        sort: [{ score: 'asc' }, '_score'],
        query: {
            multi_match: {
              query: text,
              type: 'bool_prefix',
              fields: [
                'name',
                'name._2gram',
                'name._3gram',
              ]
            }
          }
        }

我们的目标是有“名称”的自动补全功能按“分数”从名称的开头开始排序。

因此,如果搜索文本是“goo”,则应该匹配“google.com”,而不是“mail.google.com”。

现在它似乎同时完成前缀和中缀补全,我如何才能将其限制为前缀补全呢?

bxjv4tth

bxjv4tth1#

现在官方文件中有一节描述了这一点。您可以使用search_as_you_typeMap上的根字段来实现这一点,我认为您的用例应该是这样的:

{
  "sort": [{ "score": "asc" }, "_score"],
  "query": {
    "match_phrase_prefix": {
      "name": "insert query here"
    }
  }
}

相关问题