我正在尝试在 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}
我想找到所有文档,如果文档的 chdate 最高,则将 field1 设置为 x,对于每个标识和版本,将 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
}
任何知道在这种情况下桶路径应该是什么的人
提前致谢
您可以结合使用 multi-terms 和 top hits 聚合来达到您需要的结果。
Multi Terms 聚合,将创建基于
version
和 ident
的独特桶。然后 top-hits bucket 将检索具有最高 chdate
的文档(基于创建的 buckets)
搜索查询:
{
"size": 0,
"query": {
"term": {
"field1": 0
}
},
"aggs": {
"genres_and_products": {
"multi_terms": {
"terms": [
{
"field": "version"
},
{
"field": "ident.keyword"
}
]
},
"aggs": {
"topHits": {
"top_hits": {
"sort": [
{
"chdate": {
"order": "desc"
}
}
],
"size": 1,
"_source": {
"includes": "objid"
}
}
}
}
}
}
}
搜索结果:
{
"took": 15,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 3,
"relation": "eq"
},
"max_score": null,
"hits": []
},
"aggregations": {
"genres_and_products": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": [
2,
"group1"
],
"key_as_string": "2|group1",
"doc_count": 2,
"topHits": {
"hits": {
"total": {
"value": 2,
"relation": "eq"
},
"max_score": null,
"hits": [
{
"_index": "75889252",
"_id": "gtowPIcBFkuh8-KNVHAi",
"_score": null,
"_source": {
"objid": 4
},
"sort": [
4
]
}
]
}
}
},
{
"key": [
3,
"group1"
],
"key_as_string": "3|group1",
"doc_count": 1,
"topHits": {
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": null,
"hits": [
{
"_index": "75889252",
"_id": "g9owPIcBFkuh8-KNVHAi",
"_score": null,
"_source": {
"objid": 5
},
"sort": [
1
]
}
]
}
}
}
]
}
}
}
如上图所示,您会注意到 top hits 聚合桶,为您提供所需的结果。