Date histogram every half a month

How to write an interval that groups by every half a month? Rather than 1M I want something like 1/2M to group by from the first to the 16th and from the 16th to the end of the month, every month. Is there a way to do so?

I don't want to end up doing an interval on each day and then calculate manually my results as it's not clean and it would be resource hungry, is there a simple way to do so using setInterval? (in Elasticsearch or Elastica I don't care, I just want the algorithm behind it, thanks!)

$date_grp_agg = new \Elastica\Aggregation\DateHistogram('date');
$date_grp_agg->setField('date')->setFormat("MM-yy")->setInterval('1M'); // This one

The only way is to use days but this will give approximative results in a sense that splitting in 15d in January will left one day that will go in 1st half of February.

The other way would be by indexing a text field like "date": "Jan1", "date": "Jan2","date": "Feb1", "date": "Feb2" and compute that value at index time based on your rules and calendar rules like some months of february have 28 days while others might have 29 days...

My 0.05 cents

1 Like

How would you want me to index a text field if I have multiple years? How would I group them to filter their results?

You can index this:

{
  "year": 2018,
  "month-period": "jan1"
}

And then run 2 terms agg, the first on year and the second on month-period.

Or index:

{
  "month-period": "2018jan1"
}
1 Like

Thank you so much!

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.