Date histogram every half a month


(Islam Elshobokshy) #1

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

(David Pilato) #2

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


(Islam Elshobokshy) #3

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


(David Pilato) #4

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"
}

(Islam Elshobokshy) #5

Thank you so much!


(system) #6

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