【发布时间】:2017-06-04 07:47:19
【问题描述】:
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) 个 文档 - John 的文档和两者都具有相同的 text 属性值。
但是,我希望收到一 (1) 个单词。像这样简单的事情:
{
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"my-suggest": [
{
"text": "joh",
"offset": 0,
"length": 3,
"options": [
"John"
]
}
]
}
问题:如何实现基于单词的完成提示。无需返回任何与文档相关的数据,因为我现在不需要它。
“完成建议”是否适合我的场景?还是应该使用完全不同的方法?
编辑: 正如你们中的许多人所指出的,额外的仅完成索引将是一个可行的解决方案。但是,我可以看到这种方法存在多个问题:
- 保持新索引同步。
- 自动完成后续单词可能是全局的,而不是缩小范围。例如,假设您在附加索引中有以下单词:
"John", "Doe", "David", "Smith"。查询"John D"时,不完整单词的结果应该是"Doe"而不是"Doe", "David"。
要克服第二点,仅索引单个单词是不够的,因为您还需要将所有单词映射到文档,以便正确缩小自动完成后续单词的范围。有了这个,你实际上和查询原始索引有同样的问题。因此,附加索引不再有意义。
【问题讨论】:
-
正如in this issue 所暗示的那样,这种新行为是“设计使然”,并且没有改变它的计划。他们的建议是为完成建议创建另一个索引。与下面@EdgarVonk 的建议差不多。
-
对当前索引的自定义查询呢?也许为具有不同查询(具有术语聚合)的所有建议创建一个额外的 NGram 字段?至于额外的仅建议索引,我可以确定一些问题,这些问题实际上与您提出的解决方案相矛盾(请参阅我更新的问题)。
-
当然,术语聚合也可以实现类似的目标,但这取决于您拥有的文档负载。我不是提出那个解决方案,Edgar 和 ES 人(见问题)是 ;-)
标签: elasticsearch autocomplete duplicates elasticsearch-5