Alexandre Elshobokshy
Alexandre Elshobokshy

Reputation: 10922

group by custom date

I want to "group by" my results by year and month using a field that has a format of YYYY-MM-DD. I want to group them by month, so with a format of YYYY-MM . I tried with a Terms aggregation to group them but with no avail, so now I went and looked at the DateHistogram aggregation and tried the following but it still doesn't work.

$date_grp_agg = new \Elastica\Aggregation\DateHistogram('achats_date');
$date_grp_agg->setField('achats_date')->setFormat("MM-yyy")->setInterval('1');

Does any of you know how to correctly group my results using a formated date field?

Upvotes: 0

Views: 84

Answers (1)

Alexandre Elshobokshy
Alexandre Elshobokshy

Reputation: 10922

The answer was simple

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

To group them by month, you'd need to set an interval of 1 month, that way the results would be filtered for each month.

Source : https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-datehistogram-aggregation.html

Upvotes: 1

Related Questions