Range Aggregation

More info about range aggregation is in the official elasticsearch docs

A multi-bucket value source based aggregation that enables the user to define a set of ranges - each representing a bucket.

Simple example

{
    "aggs" : {
        "price_ranges" : {
            "range" : {
                "field" : "price",
                "keyed" : false,
                "ranges" : [
                    { "to" : 50 },
                    { "from" : 50, "to" : 100 },
                    { "from" : 100 }
                ]
            }
        }
    }
}

And now the query via DSL:

$rangeAggregation = new RangeAggregation(
    'price_ranges',
    'price',
    [
        ['to' => 50],
        ['from' => 50, 'to' => 100],
        ['from' => 100],
    ]
);

$search = new Search();
$search->addAggregation($rangeAggregation);

$queryArray = $search->toArray();

Keyed example

$rangeAggregation = new RangeAggregation(
    'price_ranges',
    'price',
    [
        ['key' => 'cheap', 'to' => 50],
        ['from' => 50, 'to' => 100],
        ['key' => 'expensive', 'from' => 100],
    ],
    true
);

$search = new Search();
$search->addAggregation($rangeAggregation);

$queryArray = $search->toArray();

Adder example

$rangeAggregation = new RangeAggregation('price_ranges', 'price');
$rangeAggregation->setKeyed(true);
$rangeAggregation->addRange(null, 50, 'cheap');
$rangeAggregation->addRange(50, 100);
$rangeAggregation->addRange(100, null, 'expensive');

$search = new Search();
$search->addAggregation($rangeAggregation);

$queryArray = $search->toArray();