How can we calculate unique users per month for given year, but if user is already accounted for one month, then it should not consider in next months.
Tried date_histogram
with cardinality but its counting unique users per month, and it is giving repeating counts.
Able to solve this issue with scripted_metric
(map-reduce) and it is giving correct counts, but this is memory intensive and not performing for large datasets. So, want to take date_histogram
approach which is quite faster in nature compared to scripts.
Query based on date_histogram
,
{
"size": 0,
"aggs": {
"monthly_counts": {
"composite": {
"size": 1000,
"sources": [
{
"month": {
"date_histogram": {
"field": "date",
"calendar_interval": "month",
"format": "yyyy-MM",
"order": "asc"
}
}
}
]
},
"aggs": {
"distinct_users": {
"cardinality": {
"field": "user"
}
}
}
}
}
Expecting an output something like in below format, below output is from scripted_metric
,
"monthly_counts": {
"value": {
"2024-02": 4,
"2024-01": 4,
"2024-04": 2,
"2024-03": 3,
"2024-05": 1
}
}