我应该使用哪种Elasticsearch聚合? [英] Which elasticsearch aggregations should I use?
问题描述
我需要创建一个按日期显示活动用户数"的条形图.活动用户是指最近7天登录的用户. 因此,我需要计算last_activity日期在过去7天之内的用户总数.我需要对图表中的每个柱形(天)进行操作.
I need to create a bar chart of "number of active users by date". An active user means the user who has logged in last 7 days. so I need to count total number of users, whose last_activity date is within 7 last days. and I need to do it for each bar(day) in my chart.
我知道这需要使用聚合弹性搜索来完成,但不确定
I understand it needs to be done using aggregations elastic search, but unsure
我应该使用哪些聚合?存储桶聚合,管道聚合?
which aggregations should I use? bucket aggregations, pipeline aggregations?
如果您知道类似的例子,请告诉我.
Please let me know if you know a similar example of it.
在这里您可以找到用户"john"的两个示例文档示例
Here you can find two examples of sample documents for user "john"
{
"userid": "john",
"last_activity": "2017-08-09T16:10:10.396+01:00",
"date_of_this_report": "2017-09-24T00:00:00+01:00"
}
{
"userid": "john",
"last_activity": "2017-08-09T16:10:10.396+01:00",
"date_of_this_report": "2017-09-25T00:00:00+01:00"
}
推荐答案
您可以使用日期直方图聚合.
You can filter the users with last activity for last 7 days using date math operation of elasticsearch. You can push the filter before the date histogram aggregation.
POST active_users/document_type1/_search
{
"size": 0,
"aggs": {
"filtered_active_users_7_days": {
"filter": {
"range": {
"last_activity": {
"gte": "now-7d/d"
}
}
},
"aggs": {
"date_histogram_last_7_days": {
"date_histogram": {
"field": "last_activity",
"interval": "day"
}
}
}
}
}
}
希望这对您有用.
这篇关于我应该使用哪种Elasticsearch聚合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!