[英]How to get the last Elasticsearch document for each unique value of a field?
I have a data structure in Elasticsearch that looks like:我在 Elasticsearch 中有一个数据结构,如下所示:
{
"name": "abc",
"date": "2022-10-08T21:30:40.000Z",
"rank": 3
}
I want to get, for each unique name, the rank of the document (or the whole document) with the most recent date.对于每个唯一名称,我想获得最近日期的文档(或整个文档)的排名。
I currently have this:我目前有这个:
"aggs": {
"group-by-name": {
"terms": {
"field": "name"
},
"aggs": {
"max-date": {
"max": {
"field": "date"
}
}
}
}
}
How can I get the rank (or the whole document) for each result, and if possible, in 1 request?如何获得每个结果的排名(或整个文档),如果可能,在 1 个请求中?
You can use below options您可以使用以下选项
"collapse": {
"field": "name"
},
"sort": [
{
"date": {
"order": "desc"
}
}
]
{
"aggs": {
"group-by-name": {
"terms": {
"field": "name",
"size": 100
},
"aggs": {
"top_doc": {
"top_hits": {
"sort": [
{
"date": {
"order": "desc"
}
}
],
"size": 1
}
}
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.