PHP code example of everzel / elasticsearch-query-builder

1. Go to this page and download the library: Download everzel/elasticsearch-query-builder library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

everzel / elasticsearch-query-builder example snippets


use Everzel\ElasticsearchQueryBuilder\Aggregations\MaxAggregation;
use Everzel\ElasticsearchQueryBuilder\Builder;
use Everzel\ElasticsearchQueryBuilder\Queries\MatchQuery;

$client = Elasticsearch\ClientBuilder::create()->build();

$companies = (new Builder($client))
    ->index('companies')
    ->addQuery(MatchQuery::create('name', 'spatie', fuzziness: 3))
    ->addAggregation(MaxAggregation::create('score'))
    ->search();

use Everzel\ElasticsearchQueryBuilder\Queries\RangeQuery;
use Everzel\ElasticsearchQueryBuilder\Builder;

$client = Elasticsearch\ClientBuilder::create()->build();

$builder = new Builder($client);

$builder->addQuery(RangeQuery::create('age')->gte(18));

$results = $builder->search(); // raw response from ElasticSearch

\Everzel\ElasticsearchQueryBuilder\Queries\ExistsQuery::create('terms_and_conditions');

\Everzel\ElasticsearchQueryBuilder\Queries\MatchQuery::create('name', 'john doe', fuzziness: 2);

\Everzel\ElasticsearchQueryBuilder\Queries\MultiMatchQuery::create('john', ['email', 'email'], fuzziness: 'auto');

\Everzel\ElasticsearchQueryBuilder\Queries\NestedQuery::create(
    'user', 
    new \Everzel\ElasticsearchQueryBuilder\Queries\MatchQuery('name', 'john')
);

\Everzel\ElasticsearchQueryBuilder\Queries\RangeQuery::create('age')
    ->gte(18)
    ->lte(1337);

\Everzel\ElasticsearchQueryBuilder\Queries\TermQuery::create('user.id', 'flx');

\Everzel\ElasticsearchQueryBuilder\Queries\WildcardQuery::create('user.id', '*doe');

\Everzel\ElasticsearchQueryBuilder\Queries\BoolQuery::create()
    ->add($matchQuery, 'must_not')
    ->add($existsQuery, 'must_not');

$builder
    ->addQuery(
        MatchQuery::create('name', 'billie'), 
        'must_not' // available types: must, must_not, should, filter
    )
    ->addQuery(
        MatchQuery::create('team', 'eillish')
    );

use Everzel\ElasticsearchQueryBuilder\Aggregations\TermsAggregation;
use Everzel\ElasticsearchQueryBuilder\Builder;

$results = (new Builder(Elasticsearch\ClientBuilder::create()->build()))
    ->addAggregation(TermsAggregation::create('genres', 'genre'))
    ->search();

$genres = $results['aggregations']['genres']['buckets'];

\Everzel\ElasticsearchQueryBuilder\Aggregations\CardinalityAggregation::create('team_agg', 'team_name');

\Everzel\ElasticsearchQueryBuilder\Aggregations\FilterAggregation::create(
    'tshirts',
    \Everzel\ElasticsearchQueryBuilder\Queries\TermQuery::create('type', 'tshirt'),
    \Everzel\ElasticsearchQueryBuilder\Aggregations\MaxAggregation::create('max_price', 'price')
);

\Everzel\ElasticsearchQueryBuilder\Aggregations\MaxAggregation::create('max_price', 'price');

\Everzel\ElasticsearchQueryBuilder\Aggregations\MinAggregation::create('min_price', 'price');

\Everzel\ElasticsearchQueryBuilder\Aggregations\NestedAggregation::create(
    'resellers',
    'resellers',
    \Everzel\ElasticsearchQueryBuilder\Aggregations\MinAggregation::create('min_price', 'resellers.price'),
    \Everzel\ElasticsearchQueryBuilder\Aggregations\MaxAggregation::create('max_price', 'resellers.price'),
);

\Everzel\ElasticsearchQueryBuilder\Aggregations\ReverseNestedAggregation::create(
    'name',
    ...$aggregations
);

\Everzel\ElasticsearchQueryBuilder\Aggregations\TermsAggregation::create(
    'genres',
    'genre'
)
    ->size(10)
    ->order(['_count' => 'asc'])
    ->missing('N/A')
    ->aggregation(/* $subAggregation */);

\Everzel\ElasticsearchQueryBuilder\Aggregations\TopHitsAggregation::create(
    'top_sales_hits',
    size: 10,
);

use Everzel\ElasticsearchQueryBuilder\Sorts\Sort;

$builder
    ->addSort(Sort::create('age', Sort::DESC))
    ->addSort(
        Sort::create('score', Sort::ASC)
            ->unmappedType('long')
            ->missing(0)
    );

$builder->fields('user.id', 'http.*.status');

use Everzel\ElasticsearchQueryBuilder\Builder;

$pageSize = 100;
$pageNumber = $_GET['page'] ?? 1;

$pageResults = (new Builder(\Elasticsearch\ClientBuilder::create()))
    ->size($pageSize)
    ->from(($pageNumber - 1) * $pageSize)
    ->search();