Reputation: 6585
I'm having data in my index from 2010 to 2015.
I have used following code to get the aggregated first name details in every year from 2010 to 2015, it works as expected
POST profile/_search
{
"size": "0",
"aggs": {
"count_by_year": {
"date_histogram": {
"field": "logdate",
"interval": "year",
"format": "yyyy"
},
"aggs": {
"count_by_firstname": {
"terms": {
"field": "profile.firstname"
}
}
}
}
}
}
how to aggregate my data based on particular date in every year.
For Example, I would like to retrieve data from Feb. 15 to Apr. 15 in every year.
Upvotes: 1
Views: 1686
Reputation: 217504
You may try to use a filter
aggregation with a script
filter like this. Note that February 15th is the 46th day of the year and April 15th is the 105th day of the year (with an exception on leap years of course, but this illustrates a solution).
The script
will take any document with a logdate
between Feb 15th and Apr 15th (of any year) and then the date_histogram
will dispatch them in the proper year buckets. Finally, the terms
aggregation splits the counts by first names.
{
"size": "0",
"aggs": {
"specific_period": {
"filter": {
"script": {
"script": "doc.logdate.date.dayOfYear >= 46 && doc.logdate.date.dayOfYear <= 105"
}
},
"aggs": {
"byyear": {
"date_histogram": {
"field": "logdate",
"interval": "year",
"format": "yyyy"
},
"aggs": {
"count_by_firstname": {
"terms": {
"field": "profile.firstname"
}
}
}
}
}
}
}
}
Upvotes: 4
Reputation: 7840
date range aggregation with range as below :
{
"aggs": {
"range": {
"date_range": {
"field": "logdate",
"format": "YYYYMMDD",
"ranges": [
{
"from": "20150201"
},
{
"to": "20150430"
}
]
},
"aggs": {
"count_by_firstname": {
"terms": {
"field": "profile.firstname"
}
}
}
}
}
}
Upvotes: 2