我在索引中有如下条目:
ID BuildingName Postalcode Type
1 ABCD 1234 1
2 ABCD 7890 1
我需要删除搜索时出现在“BuildingName”字段中的重复项(而不是索引中的重复项,因为您看到它们是两个不同的条目)。最后,我只想看看(所有带有搜索名称的建筑物)
ID BuildingName Postalcode Type
1 ABCD 1234 1
为什么我不能按此处描述的那样使用字段折叠/聚合(Remove duplicate documents from a search in Elasticsearch)->因为我需要对BuildingName进行n-gram分析,并且字段折叠/聚合仅适用于未分析的字段。
有什么办法做到这一点?
所有帮助表示赞赏!谢谢!
最佳答案
在BuildingName
字段中添加一个子字段,该字段应为not_analyzed
或使用诸如keyword
之类的分析器进行分析,而该分析器不会对文本造成太大的影响。当您搜索经过nGram-ed处理的常规BuildingName
字段时,将在未更改的子字段上执行聚合:
"mappings": {
"test": {
"properties": {
"BuildingName": {
"type": "string",
"analyzer": "my_ngram_analyzer",
"fields": {
"notAnalyzed": {
"type": "string",
"index": "not_analyzed"
}
}
}
}
}
}
{
"query": {
"term": {
"BuildingName": {
"value": "ab"
}
}
},
"aggs": {
"unique": {
"terms": {
"field": "BuildingName.notAnalyzed",
"size": 10
},
"aggs": {
"sample": {
"top_hits": {
"size": 1
}
}
}
}
}
}