Skip to content

abdullahercan/elasticsearch-query-builder

 
 

Repository files navigation

ElasticSearch Query Builder

img php

This is a PHP library which helps you build query for an ElasticSearch client by using a fluent interface.

WARNING: This branch contains the next 3.x release. Check the corresponding issue for the roadmap.

Installation

composer require erichard/elasticsearch-query-builder

Usage

use Erichard\ElasticQueryBuilder\QueryBuilder;
use Erichard\ElasticQueryBuilder\Aggregation\Aggregation;
use Erichard\ElasticQueryBuilder\Filter\Filter;

$query = new QueryBuilder();

$query
    ->setType('my_type')
    ->setIndex('app')
    ->setSize(10)
;

// Add an aggregation
$query->addAggregation(Aggregation::terms('agg_name', 'my_field'));
$query->addAggregation(Aggregation::terms('agg_name_same_as_field'));

// Add a filter
$boolFilter = Filter::bool();
$boolFilter->addFilter(Filter::terms('field', 'value'));


$query->addFilter($boolFilter);

// I am using a client from elasticsearch/elasticsearch here
$results = $client->search($query->build());

with PHP 8.1 you can do this:

new BoolQuery(should: [
    new RangeQuery(
        field: PriceTermsIndex::PREFIX_OPTION . OptionIds::PERSONS_MAX,
        gte: $this->roomCount
    ),
    new RangeQuery(
        field: PriceTermsIndex::PREFIX_OPTION . OptionIds::PERSONS_MAX,
        gte: $this->roomCount
    ),
])
]

Contribution

  • Use PHPCS fixer and PHPStan
    • composer lint
  • Update tests (PHPUnit)
    • composer test

About

Build query for an ElasticSearch client using a fluent interface

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 100.0%