Version 2.6.0
Changelog
- Feature: Added
aggregate()
method to query builder, allowing to fluently add aggregations. - Chore: Updated dependencies
Adding aggregations
From 2.6.0 onwards, you can add aggregations to your query using the fluent query builder:
$query->aggregate('color');
Specifying the name only will create a simple term query:
{
"aggs": {
"color": {
"terms": {
"field": "color"
}
}
}
Pass a string to use a different field:
$query->aggregate('color', 'color_field');
Specifying the name only will create a simple term query:
{
"aggs": {
"color": {
"terms": {
"field": "color_field"
}
}
}
To have custom aggregations, pass an array instead of the field name:
$query->aggregate('date', [
'date_histogram' => [
'field' => 'date',
'interval' => 'month'
]
]);
Specifying the name only will create a simple term query:
{
"aggs": {
"date": {
"date_histogram": {
"field": "date",
"interval": "month"
}
}
}
}