ElasticSearch 5.x引入了对Suggester API (Documentation)的一些(破坏性的)更改。最值得注意的变化如下:
完成建议程序是面向文档的
建议知道它们所属的文档。现在,相关文档(_source
)将作为完成建议的一部分返回。
简而言之,所有完成查询都返回所有匹配的文档,而不仅仅是匹配的单词。这就是问题所在-如果自动补全的单词出现在多个文档中,则会出现重复。
假设我们有一个简单的映射:
{
"my-index": {
"mappings": {
"users": {
"properties": {
"firstName": {
"type": "text"
},
"lastName": {
"type": "text"
},
"suggest": {
"type": "completion",
"analyzer": "simple"
}
}
}
}
}
}
使用几个测试文档:
{
"_index": "my-index",
"_type": "users",
"_id": "1",
"_source": {
"firstName": "John",
"lastName": "Doe",
"suggest": [
{
"input": [
"John",
"Doe"
]
}
]
}
},
{
"_index": "my-index",
"_type": "users",
"_id": "2",
"_source": {
"firstName": "John",
"lastName": "Smith",
"suggest": [
{
"input": [
"John",
"Smith"
]
}
]
}
}
和一个按书查询:
POST /my-index/_suggest?pretty
{
"my-suggest" : {
"text" : "joh",
"completion" : {
"field" : "suggest"
}
}
}
这将产生以下结果:
{
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"my-suggest": [
{
"text": "joh",
"offset": 0,
"length": 3,
"options": [
{
"text": "John",
"_index": "my-index",
"_type": "users",
"_id": "1",
"_score": 1,
"_source": {
"firstName": "John",
"lastName": "Doe",
"suggest": [
{
"input": [
"John",
"Doe"
]
}
]
}
},
{
"text": "John",
"_index": "my-index",
"_type": "users",
"_id": "2",
"_score": 1,
"_source": {
"firstName": "John",
"lastName": "Smith",
"suggest": [
{
"input": [
"John",
"Smith"
]
}
]
}
}
]
}
]
}
简而言之,对于文本"joh“的补全建议,返回了两(2)个文档-约翰的和都具有相同的text
属性值。
但是,我希望收到一(1)个word。像这样简单的东西:
{
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"my-suggest": [
{
"text": "joh",
"offset": 0,
"length": 3,
"options": [
"John"
]
}
]
}
Question:如何实现基于单词的补全提示程序。不需要返回任何与文档相关的数据,因为我现在不需要它。
“完成提示者”是否适合我的场景?或者我应该使用一种完全不同的方法?
:正如你们中的许多人所指出的,附加的仅限完成的索引将是一个可行的解决方案。但是,我可以看到这种方法存在多个问题:
在sync.
"John", "Doe", "David", "Smith"
。查询"Doe", "David"
.时,不完整单词的结果应为"John D"
,而不是"Doe"
要克服第二点,仅对单个单词进行索引是不够的,因为您还需要将所有单词映射到文档,以便适当地缩小自动完成后续单词的范围。这样,您实际上遇到了与查询原始索引相同的问题。因此,额外的索引不再有意义。
发布于 2017-01-23 14:01:16
正如评论中所暗示的,另一种在不获取重复文档的情况下实现此目的的方法是为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年06月25日):
ES7.2引入了一种名为search_as_you_type
的新数据类型,它允许这种行为。有关更多信息,请访问:https://www.elastic.co/guide/en/elasticsearch/reference/7.2/search-as-you-type.html
发布于 2017-09-15 14:39:27
在下一个版本6.x中将添加一个额外的字段skip_duplicates。
POST music/_search?pretty
{
"suggest": {
"song-suggest" : {
"prefix" : "nor",
"completion" : {
"field" : "suggest",
"skip_duplicates": true
}
}
}
}
发布于 2017-01-22 04:18:21
我们面临着完全相同的问题。在Elasticsearch 2.4中,像您所描述的方法对我们来说工作得很好,但现在正如您所说的,suggester已经变得基于文档,而像您一样,我们只对唯一的单词感兴趣,而对文档没有兴趣。
到目前为止,我们能想到的唯一“解决方案”是为我们想要执行建议查询的单词创建一个单独的索引,并且在这个单独的索引中,以某种方式确保相同的单词只被索引一次。然后,您可以在这个单独的索引上执行建议查询。这并不理想,因为我们需要确保此索引与其他查询所需的其他索引保持同步。
https://stackoverflow.com/questions/41744712
复制相似问题