【发布时间】:2015-12-23 23:40:24
【问题描述】:
我目前正在尝试根据 Elastic Search 中收集的数据生成图表。每次生成用户时,我都会在 ES 中插入一条记录,其中包含以下(示例)数据:
{
"country": "US",
"id": "79ca9523dcd62420030de12b75e08bb7",
"createdAt": "1450912898"
}
ID 是用户 ID 的哈希,因此出于隐私原因,无法从存储在 ES 中的 ID 确定用户 ID。
ES索引中的类型映射如下:
{
"user": {
"_timestamp": {
"enabled": true
},
"properties": {
"country": {
"type": "string"
},
"createdAt": {
"type": "date",
"format": "epoch_second"
},
"id": {
"type": "string",
"index": "not_analyzed"
}
}
}
}
现在,为了获得每天用户的图表,我有以下查询:
{
"size": 0,
"query": {
"type": {
"value": "user"
}
},
"aggs": {
"users_per_day": {
"date_histogram": {
"field": "createdAt",
"interval": "day"
}
}
}
}
这给了我一个很好的结果,例如(对于结果,我将时间间隔设置为分钟,以便让您稍微了解问题所在):
[{
"key_as_string": "1450909920",
"key": 1450909920000,
"doc_count": 8
},
{
"key_as_string": "1450909980",
"key": 1450909980000,
"doc_count": 2
},
{
"key_as_string": "1450910040",
"key": 1450910040000,
"doc_count": 5
},
{
"key_as_string": "1450910100",
"key": 1450910100000,
"doc_count": 8
},
{
"key_as_string": "1450910160",
"key": 1450910160000,
"doc_count": 4
},
{
"key_as_string": "1450910220",
"key": 1450910220000,
"doc_count": 3
},
{
"key_as_string": "1450910280",
"key": 1450910280000,
"doc_count": 6
}]
我想使用doc_count 生成一个累积图,这样我就可以看到我的用户群的增长,而不是每天创建的帐户数量。尽管在互联网上搜索,我找不到一个似乎与我的问题相关的答案。我找到的大多数答案都将我引导至Cumulative Sum Aggregation 页面,但那里给出的示例将为您提供单个存储桶中捕获的所有结果的累积总和。我想要所有存储桶总数的累积总和。
【问题讨论】:
-
那么,按照你的例子,
"key_as_string": "1450909980"的doc_count应该是 8 (previous doc_count) + 2? -
不一定要存储在doc_count中,但是可以。
-
这听起来更像是可以在客户端完成的事情。我的意思是,重要的信息就在那里,只需要调整结果数据集。此外,根据您计划如何使用这些结果(例如 UI 图表),“累积”的事情可能会在图表本身中处理。
-
我很确定 ES 能够支持这些方面的东西,因为它也能够对文档中的一个字段进行累积和,那么为什么不是桶的累积和内容计数?