作为我的主题,我想按日期统计当天和之前的文档,这是理解图表的样本。
{"index":{"_index":"login-2015.12.23","_type":"logs"}}
{"uid":"1","register_time":"2015-12-23T12:00:00Z","login_time":"2015-12-23T12:00:00Z"}
{"index":{"_index":"login-2015.12.23","_type":"logs"}}
{"uid":"2","register_time":"2015-12-23T12:00:00Z","login_time":"2015-12-23T12:00:00Z"}
{"index":{"_index":"login-2015.12.24","_type":"logs"}}
{"uid":"1","register_time":"2015-12-23T12:00:00Z","login_time":"2015-12-24T12:00:00Z"}
{"index":{"_index":"login-2015.12.25","_type":"logs"}}
{"uid":"1","register_time":"2015-12-23T12:00:00Z","login_time":"2015-12-25T12:00:00Z"}
如您所见,索引login-2015.12.23
有两个文档,索引login-2015.12.24
有一个文档,索引login-2015.12.23
有一个文档。
现在我想知道结果
{
"hits" : {
"total" : 6282,
"max_score" : 1.0,
"hits" : []
},
"aggregations" : {
"group_by_date" : {
"buckets" : [
{
"key_as_string" : "2015-12-23T12:00:00Z",
"key" : 1662163200000,
"doc_count" : 2,
},
{
"key_as_string" : "2015-12-24T12:00:00Z",
"key" : 1662163200000,
"doc_count" : 3,
},
{
"key_as_string" : "2015-12-25T12:00:00Z",
"key" : 1662163200000,
"doc_count" : 4,
}
]
}
如果我计算日期2015-12-24T12:00:00Z
,这意味着我必须同时计算日期2015-12-23T12:00:00Z
和2015-12-24T12:00:00Z
。在我的项目中,我有很多这样的索引,我寻找了很多方法来实现这个目标,但没有,这是我的演示:
{
"query": {"match_all": {}},
"size": 0,
"aggs": {
"group_by_date": {
"date_histogram": {
"field": "timestamp",
"interval": "day"
},
"aggs": {
"intersect": {
"scripted_metric": {
"init_script": "state.inner=[]",
"map_script": "state.inner.add(params.param1 == 3 ? params.param2 * params.param1 : params.param1 * params.param2)",
"combine_script": "return state.inner",
"reduce_script": "return states",
"params": {
"param1": 3,
"param2": 5
}
}
}
}
}
}
}
我想按日期分组,并使用scripted_metric
来迭代日期列表,而不是第二次迭代只能在它的桶中,而不是所有的文档,所以有人有更好的想法来解决这个问题吗?
1条答案
按热度按时间dgjrabp21#
您只需使用cumulative sum pipeline aggregation
结果如下所示: