gpt4 book ai didi

elasticsearch - 如何通过获取最早的条目

转载 作者:行者123 更新时间:2023-12-02 22:16:53 25 4
gpt4 key购买 nike

对于以下事件:

{
"time": 10,
"name": "John",
"status": true
},
{
"time": 20,
"name": "John",
"status": false
},
{
"time": 20,
"name": "Mary",
"status": false
},
{
"time": 10,
"name": "Mary",
"status": true
}

在给定的 time中搜索最古老的那些(字段 name(1))的正确方法是什么?

对于上面的示例,这意味着
{
"time": 20,
"name": "John",
"status": false
},
{
"time": 20,
"name": "Mary",
"status": false
},

我尝试使用 order in aggregations来实现,
{
"query": {
"match_all": {

}
},
"aggs": {
"shortlist": {
"terms": {
"field": "name",
"size": 1,
"order" : { "time" : "asc" }
}
}
}
}

但是我得到了
"error": {
"root_cause": [
{
"type": "aggregation_execution_exception",
"reason": "Invalid term-aggregator order path [time]. Unknown aggregation [time]"
}

坦率地说,我不明白。

(1)这实际上是一个纪元时间戳,但是为了简单起见,我使用了一个简短的int

最佳答案

这样的事情应该做到这一点:

{
"size": 0,
"aggs": {
"by_name": {
"terms": {
"field": "name",
"size": 10
},
"aggs": {
"top1": {
"top_hits": {
"size": 1,
"sort": [{"time":{"order": "desc"}}]
}
}
}
}
}
}

关于elasticsearch - 如何通过<field>获取最早的条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38186728/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com