gpt4 book ai didi

elasticsearch - ElasticSearch中的聚合时间序列查询

转载 作者:行者123 更新时间:2023-12-03 00:18:16 25 4
gpt4 key购买 nike

我在elasticsearch中有数据,其记录如下:

data    
{
start: 20,
userid: "123",
},
{
start: 34,
userid: "234",
},
{
start: 8,
userid: "123",
},
{
start: 12,
userid: "234",
},
{
start: 18,
userid: "345",
}

“start”是一个很长的(时间的度量),“userid”是一个String。数据包含数百万个用户,并且同一用户有多个记录。
Question:

我需要所有拥有第一条记录(基于“开始”排序)的用户ID位于时间t1和t2之间,例如在10到15之间。
For userid 123, sorted times are: {8, 20}
For userid 234, sorted times are: {12, 34}
For userid 345, sorted times are: {18}

这就是为什么它应该仅返回用户标识“234”的原因,因为仅针对该用户,时间数组中的第一个条目(排序后)在10到15之间。
Answer
234

最佳答案

您可以使用ES 2.0中的新bucket selector aggregation完成此操作。

为了测试它,我使用您提供的数据建立了一个琐碎的索引(我添加了更多代码以清楚表明聚合在起作用):

DELETE /test_index

POST /test_index/doc/_bulk
{"index":{"_id":1}}
{"start":20,"userid":"123"}
{"index":{"_id":2}}
{"start":34,"userid":"234"}
{"index":{"_id":3}}
{"start":8,"userid":"123"}
{"index":{"_id":4}}
{"start":12,"userid":"234"}
{"index":{"_id":5}}
{"start":18,"userid":"345"}
{"index":{"_id":6}}
{"start":8,"userid":"555"}
{"index":{"_id":7}}
{"start":12,"userid":"555"}

然后,我可以通过以下查询获得所需的内容:
POST /test_index/_search
{
"size": 0,
"aggs": {
"userid_terms": {
"terms": {
"field": "userid"
},
"aggs": {
"min_start": {
"min": {
"field": "start"
}
},
"min_start_filter": {
"bucket_selector": {
"buckets_path": {
"min_start": "min_start"
},
"script": "min_start >= 10 && min_start <= 15"
}
}
}
}
}
}

返回:
{
"took": 7,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 7,
"max_score": 0,
"hits": []
},
"aggregations": {
"userid_terms": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "234",
"doc_count": 2,
"min_start": {
"value": 12
}
}
]
}
}
}

这是我用来测试的代码:

http://sense.qbox.io/gist/7427b87e878c23ce03bac199d6975434d66046f9

关于elasticsearch - ElasticSearch中的聚合时间序列查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34030565/

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