我刚刚开始进行 Elasticsearch ,需要解决一个对我来说太复杂的问题。我在索引中有数千个文档,必须从中查询预定义数量的文档(也可以是几千个),我必须根据某些字段从基于另一个字段的唯一文档中查找文档组。 (唯一文档的数量最多可以是几百个)。
我的索引中的文档如下所示:
{
"complexProperty1" : {
"A" : "example",
"B" : "1",
"D" : true,
"E" : "case",
"F" : ["guide1","guide2"]
},
"complexProperty2" : {
"X" : "10",
"Y" : ["specimen1","specimen2"],
"Z" : "blueprint"
}
}
许多文档将
complexProperty1.A
作为“示例” 。我想一次包含它们,并且将生成的文档按complexProperty1.D
和complexProperty1.E
分组,即对于每对complexProperty1.D
和complexProperty1.E
,我都有一个文档列表(我的结果中只需要这些文档)。我正在使用Nest实现此目的。
最佳答案
您可以从一堆原始 terms
聚合开始,然后回到NEST DSL:
POST complexities/_doc
{
"complexProperty1": {
"A": "example",
"B": "1",
"D": true,
"E": "case",
"F": [
"guide1",
"guide2"
]
},
"complexProperty2": {
"X": "10",
"Y": [
"specimen1",
"specimen2"
],
"Z": "blueprint"
}
}
GET complexities/_search
{
"size": 0,
"aggs": {
"by_A": {
"terms": {
"field": "complexProperty1.A.keyword"
},
"aggs": {
"by_D": {
"terms": {
"field": "complexProperty1.D"
}
},
"by_E": {
"terms": {
"field": "complexProperty1.E.keyword"
}
}
}
}
}
}
为了获得基础文档,您可以将
top_hits
agg附加到每个子agg,包括。最高一级:{
"size": 0,
"aggs": {
"by_A": {
"terms": {
"field": "complexProperty1.A.keyword"
},
"aggs": {
"top_hits_only": {
"top_hits": {
"_source": "*"
}
},
"by_D": {
"terms": {
"field": "complexProperty1.D"
},
"aggs": {
"top_hits_only": {
"top_hits": {
"_source": "*"
}
}
}
},
"by_E": {
"terms": {
"field": "complexProperty1.E.keyword"
},
"aggs": {
"top_hits_only": {
"top_hits": {
"_source": "*"
}
}
}
}
}
}
}
}
关于elasticsearch - 如何在基于字段的 Elasticsearch 中获取唯一文档,以及如何基于其他字段 'group by'结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61305890/