# Date histogram every half a month

**URL:** <https://discuss.elastic.co/t/date-histogram-every-half-a-month/150675>\
**Category:** Elasticsearch\
**Created:** [October 2, 2018, 10:22am UTC](https://discuss.elastic.co/t/date-histogram-every-half-a-month/150675 "2018-10-02T10:22:45Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Islam\_Elshobokshy](https://avatars.discourse-cdn.com/v4/letter/i/77aa72/32.png) [@Islam\_Elshobokshy](https://discuss.elastic.co/u/Islam_Elshobokshy)\
**Post date:** [October 2, 2018, 10:22am UTC](https://discuss.elastic.co/t/date-histogram-every-half-a-month/150675/1 "2018-10-02T10:22:45Z")

</div>

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
```

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [October 2, 2018, 10:46am UTC](https://discuss.elastic.co/t/date-histogram-every-half-a-month/150675/2 "2018-10-02T10:46:17Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Islam\_Elshobokshy](https://avatars.discourse-cdn.com/v4/letter/i/77aa72/32.png) [@Islam\_Elshobokshy](https://discuss.elastic.co/u/Islam_Elshobokshy)\
**Post date:** [October 2, 2018, 10:51am UTC](https://discuss.elastic.co/t/date-histogram-every-half-a-month/150675/3 "2018-10-02T10:51:11Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [October 2, 2018, 12:50pm UTC](https://discuss.elastic.co/t/date-histogram-every-half-a-month/150675/4 "2018-10-02T12:50:23Z")

</div>

You can index this:

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

```

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

Or index:

```auto
{
  "month-period": "2018jan1"
}

```

---

<div class="post-metadata">

**Author:** ![Islam\_Elshobokshy](https://avatars.discourse-cdn.com/v4/letter/i/77aa72/32.png) [@Islam\_Elshobokshy](https://discuss.elastic.co/u/Islam_Elshobokshy)\
**Post date:** [October 2, 2018, 1:05pm UTC](https://discuss.elastic.co/t/date-histogram-every-half-a-month/150675/5 "2018-10-02T13:05:40Z")

</div>

Thank you so much!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [October 30, 2018, 1:18pm UTC](https://discuss.elastic.co/t/date-histogram-every-half-a-month/150675/6 "2018-10-30T13:18:28Z")

</div>

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