我试图在elasticsearch中创建一个查询,它能够检索每个组的文档,这是每个组中最新的文档,并满足特定的标准。但我还没有能够解决这个问题。
假设以下文档在elasticsearch的myindex中被索引:
POST /myindex/_bulk
{ "index":{} }
{ "objid": 1, "ident":"group1","version":1, "chdate": 1, "field1" : 1}
{ "index":{} }
{ "objid": 2, "ident":"group1","version":2, "chdate": 2, "field1" : 0}
{ "index":{} }
{ "objid": 3, "ident":"group1","version":2, "chdate": 3, "field1" : 1}
{ "index":{} }
{ "objid": 4, "ident":"group1","version":2, "chdate": 4, "field1" : 0}
{ "index":{} }
{ "objid": 5, "ident":"group1","version":3, "chdate": 1, "field1" : 0}
我想找到所有的文件,其中有field1设置为x,如果文件具有最高的chdate,为每个ident和版本,其中有field1设置为x。
在x为0的情况下,应该返回具有objid 4和5的文档。在x为1的情况下,应该返回具有objid 1的文档。
ChatGpt建议了以下查询:
{
"size": 0,
"aggs": {
"ident": {
"terms": {
"field": "ident"
},
"aggs": {
"version": {
"terms": {
"field": "version"
},
"aggs": {
"top_hits_agg": {
"top_hits": {
"size": 1,
"sort": [
{
"chdate": {
"order": "desc"
}
}
]
}
},
"field1_filter": {
"bucket_selector": {
"buckets_path": {
"hits": "top_hits_agg.hits.hits",
"field1": "top_hits_agg.hits.hits._source.field1"
},
"script": {
"source": "params.field1 == 0"
}
}
}
}
}
}
}
}
}
但是elasticsearch出现了如下错误
{
"error" : {
"root_cause" : [
{
"type" : "action_request_validation_exception",
"reason" : "Validation Failed: 1: No aggregation found for path [top_hits_agg.hits.hits._source.field1];"
}
],
"type" : "action_request_validation_exception",
"reason" : "Validation Failed: 1: No aggregation found for path [top_hits_agg.hits.hits._source.field1];"
},
"status" : 400
}
任何知道在这种情况下桶路径应该是什么的人
先谢了
1条答案
按热度按时间bpzcxfmw1#
您可以使用multi-terms和top hits聚合的组合来实现所需的结果。
多术语聚合,将基于
version
和ident
创建唯一的存储桶。然后,热门存储桶将检索具有最高chdate
的文档(基于创建的存储桶搜索查询:
搜索结果:
如上面的结果所示,您会注意到顶部命中聚合桶,为您提供所需的结果。