如评论中所暗示的,在不获取重复文档的情况下实现此目的的另一种方法是为
firstname包含ngram
个字段的字段创建一个子字段。首先,您要像这样定义映射:
PUT my-index{ "settings": { "analysis": { "analyzer": { "completion_analyzer": { "type": "custom", "filter": [ "lowercase", "completion_filter" ], "tokenizer": "keyword" } }, "filter": { "completion_filter": { "type": "edge_ngram", "min_gram": 1, "max_gram": 24 } } } }, "mappings": { "users": { "properties": { "autocomplete": { "type": "text", "fields": { "raw": { "type": "keyword" }, "completion": { "type": "text", "analyzer": "completion_analyzer", "search_analyzer": "standard" } } }, "firstName": { "type": "text" }, "lastName": { "type": "text" } } } }}然后您索引一些文档:
POST my-index/users/_bulk{"index":{}}{ "firstName": "John", "lastName": "Doe", "autocomplete": "John Doe"}{"index":{}}{ "firstName": "John", "lastName": "Deere", "autocomplete": "John Deere" }{"index":{}}{ "firstName": "Johnny", "lastName": "Cash", "autocomplete": "Johnny Cash" }然后您可以查询
joh并获得一个结果,
John而另一个获得
Johnny
{ "size": 0, "query": { "term": { "autocomplete.completion": "john d" } }, "aggs": { "suggestions": { "terms": { "field": "autocomplete.raw" } } }}结果:
{ "aggregations": { "suggestions": { "doc_count_error_upper_bound": 0, "sum_other_doc_count": 0, "buckets": [ { "key": "John Doe", "doc_count": 1 }, { "key": "John Deere", "doc_count": 1 } ] } }}更新(2019年6月25日):
ES
7.2引入了一种称为的新数据类型
search_as_you_type,该数据类型本身就允许这种行为。有关更多信息,请访问:https :
//www.elastic.co/guide/en/elasticsearch/reference/7.2/search-as-you-
type.html



