Elasticsearch query builder. Intended to be used with elasticsearch-php official client.
composer require pskordilakis/elastin
use Elastin\Builder;
$builder = Builder::create();
$builder->index('index_1')
or
$builder->indices(['index_2', 'index_3']);
Supported bool queries: must, filter, mustNot, should
$builder->filter('term', [ 'tag' => 'search' ]);
Add simple aggregations to query
$builder->aggregation('aggregation_name', [ 'aggregation_type' => $aggregation_body ]);
Nested aggregation supported by defining a dot separated name
$builder->aggregation('parent_aggregation_path.aggregation_name', [ 'aggregation_type' => $aggregation_body ]);
Build method return an array representation of query that can be past to elasticsearch-php client.
$query = $builder->build();
$client->search($query);
Some aliases are provided for common cases.
Aggregation aliases can be used as nested aggregations by passing a dot separated path in name parameter.
Define a term filter
$builder->where($field, $value);
Define a range filter
$builder->whereBetween($field, $gte, $lte);
Create a value_count aggregation
$builder->count($name, $field);
Create a cardinality aggregation
$builder->cardinality($name, $field);
Create a term aggregation
$builder->groupBy($name, $field);
Create a date_histogram aggregation
$builder->timeSeries($name, $field, $options);