gpt4 book ai didi

jquery - Elasticsearch 查询 - 如何聚合时间戳并按术语过滤?

转载 作者:行者123 更新时间:2023-11-29 02:56:59 25 4
gpt4 key购买 nike

我有一个有效的过滤器查询:

{
"filter": {
"and": [{
"range": {
"timestamp": {
"gte": "2015-05-07T12:04:30Z"
"lte": "2015-08-07T12:04:30Z"
}
}
}, {
"term": {
"in_context.course_id": "1234567"
}
}]
},
"fields": ["timestamp"]
}

返回最近三个月每个事件的时间戳。但是返回了大约 100 万个时间戳,这使得响应太大,所以我想汇总结果并获得每天的文档总和。

我发现了这个使用日期直方图的片段:

{
"aggs" : {
"histo1" : {
"date_histogram" : {
"field" : "timestamp",
"interval" : "day"
},
"aggs" : {
"price_stats" : {
"stats" : {
"field" : "price"
}
}
}
}
}
}

这正是我所需要的,但我不知道如何使用术语过滤器将它合并到我的原始查询中!非常乐意提供帮助!

最佳答案

您可以通过在查询的顶层添加 aggs 部分来简单地做到这一点。请注意,我将您的 filter 包装到一个 filtered 查询中。

{
"size": 0,
"query": {
"filtered": {
"filter": {
"and": [
{
"range": {
"timestamp": {
"gte": "2015-05-07T12:04:30Z",
"lte": "2015-08-07T12:04:30Z"
}
}
},
{
"term": {
"in_context.course_id": "1234567"
}
}
]
}
}
},
"aggs": {
"histo1": {
"date_histogram": {
"field": "timestamp",
"interval": "day"
}
}
}
}

关于jquery - Elasticsearch 查询 - 如何聚合时间戳并按术语过滤?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32198995/

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