gpt4 book ai didi

elasticsearch - Elastic Luence查询平均结果

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

我是Elastic Search和Lucene语法的新手。

无论如何,我想知道的是获得特定时间段的平均值。

例如,我将以下信息存储在索引中。

Index Name Result TimeStamp
a-3 A 7 March 7th 2018, 16:17:35.000
a-3 A 13 March 7th 2018, 16:17:36.000
a-3 B 5 March 7th 2018, 16:17:37.000

我想写一个查询返回我,在16:17:35和16:17:38之间,A = 10和B = 5的平均结果。

这可能吗 ?

谢谢。

最佳答案

您可以组合range query并使用嵌套的聚合(term aggregationavg aggregation)

POST my_index/_search
{
"size": 0,
"query": {
"range": {
"TimeStamp": {
"gte": "2018-03-07 16:17:35",
"lte": "2018-03-07 16:17:38"
}
}
},

"aggs": {
"name": {
"terms": {
"field": "Name",
"size": 10
}
, "aggs": {
"stats": {
"avg": {
"field": "Result"
}
}
}
}
}
}

结果将如下所示
{
"took": 4,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 3,
"max_score": 0,
"hits": []
},
"aggregations": {
"name": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "A",
"doc_count": 2,
"stats": {
"value": 10
}
},
{
"key": "B",
"doc_count": 1,
"stats": {
"value": 5
}
}
]
}
}
}

关于elasticsearch - Elastic Luence查询平均结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49156699/

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