Problem
I have data in the following diagram:
{
start_date: '2017-01-01',
end_date: '2017-01-05',
},
{
start_date: '2017-01-03',
end_date: '2017-01-07',
}
I am trying to create a histogram on a day that would give me if there was a start and end date on that day in a particular document.
With data on, output buckets will be:
{
"2017-01-01": { "doc_count": 1 },
"2017-01-02": { "doc_count": 1 },
"2017-01-03": { "doc_count": 2 },
"2017-01-04": { "doc_count": 2 },
"2017-01-05": { "doc_count": 2 },
"2017-01-06": { "doc_count": 1 },
"2017-01-07": { "doc_count": 1 }
}
After reading all the elasticsearch aggregation documents, I don't see how this will be possible. Any help is appreciated.
Decision
Turning below to Olivier, I did the following:
Create a helper function to create all included days between the start and end dates:
const generateDateRange = (start, end) => {
const startDate = moment(start);
const endDate = moment(end);
const range = [];
const date = startDate;
while (date.isSameOrBefore(endDate)) {
range.push(date.format('YYYY-MM-DD'));
date.add(1, 'day');
}
return range;
};
Created a helper function to generate all the filters needed for aggregation, depending on the date range:
const generateActivityFilters = (range, options = {}) => {
const filters = {};
range.map((date) => {
filters[date] = {
bool: {
filter: [
{ range: { [options.start]: { lte: date } } },
{ range: { [options.end]: { gte: date } } },
],
},
};
return true;
});
return filters;
};
Finally, I executed the request as follows:
{
"size": 0,
"aggs": {
"date_histo": {
"filters": {
"filters": filters // from generateActivityFilters
}
}
}
}
, , script , elasticsearch .