PHPackages                             crowdskout/es-search-builder - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Search &amp; Filtering](/categories/search)
4. /
5. crowdskout/es-search-builder

ActiveProject[Search &amp; Filtering](/categories/search)

crowdskout/es-search-builder
============================

Simplified Elasticsearch search api and results parsing for queries and aggregations

v5.0.2(8y ago)5201MITPHPPHP &gt;=5.6.6

Since Sep 12Pushed 8y ago2 watchersCompare

[ Source](https://github.com/crowdskout/es-search-builder)[ Packagist](https://packagist.org/packages/crowdskout/es-search-builder)[ RSS](/packages/crowdskout-es-search-builder/feed)WikiDiscussions master Synced today

READMEChangelog (2)Dependencies (2)Versions (2)Used By (0)

es-search-builder
=================

[](#es-search-builder)

This package provides simplified api and results parsing for Elasticsearch search queries and aggregations. It builds upon the official elasticsearch-php library here: .

Quick example - Query api
-------------------------

[](#quick-example---query-api)

```
    $query = Query::terms('intField', [1, 2, 3, 4, 5, 6])
    print_r($query);
```

The query api provides simpler, less verbose query generation.

```
    Array
    (
        [terms] => Array
            (
                [intField] => Array
                    (
                        [0] => 1
                        [1] => 2
                        [2] => 3
                        [3] => 4
                        [4] => 5
                        [5] => 6
                    )

            )

    )
```

Quick example - Agg builder api
-------------------------------

[](#quick-example---agg-builder-api)

```
    use Crowdskout\EsSearchBuilder\Agg as AggBuilder;

    $aggBuilder = new AggBuilder();

    $agg = $aggBuilder->nested('parentField', $aggBuilder->terms('parentField.subField'));
    $aggQuery = $agg->generateQuery();

    print_r($aggQuery);
```

Aggregations can be nested with each other

```
Array
(
    [parentField_nested_agg] => Array
        (
            [nested] => Array
                (
                    [path] => parentField
                )

            [aggs] => Array
                (
                    [parentField.subField_terms_agg] => Array
                        (
                            [terms] => Array
                                (
                                    [field] => parentField.subField
                                )

                        )

                )

        )

)

```

Use the agg object to parse the results from Elasticsearch as well

```
    // ... (code from above)

    // Aggregation results from Elasticsearch
    $elasticResult = [
         'parentField_nested_agg' => [
             'doc_count' => 5,
             'parentField.subField_terms_agg' => [
                 'buckets' => [
                     [
                         'key' => 'subFieldValue1',
                         'doc_count' => 3
                     ],
                     [
                         'key' => 'subFieldValue2',
                         'doc_count' => 2
                     ]
                 ]
             ]
         ]
     ];

    $parsedResult = $agg->generateResults($elasticResult);
    print_r($parsedResult);
```

```
    Array
    (
        [Total] => 5
        [options] => Array
            (
                [subFieldValue1] => 3
                [subFieldValue2] => 2
            )

    )
```

Installation using Composer
===========================

[](#installation-using-composer)

If you don't have composer, please install it -

Add this package to your project from the terminal.

```
composer require crowdskout/es-search-builder
```

If your project is not already setup to autoload composer libraries, you can put this at the top of your boostrap file or script

```
    use Crowdskout\EsSearchBuilder\Agg as AggBuilder;
    use Crowdskout\EsSearchBuilder\Query;

    require 'vendor/autoload.php';

    // Query
    $query = Query::terms('intField', [1, 2, 3, 4, 5, 6])

    // Agg
    $aggBuilder = new AggBuilder();

    $agg = $aggBuilder->nested('parentField', $aggBuilder->terms('parentField.subField'));
    $aggQuery = $agg->generateQuery();
```

Usage with elasticsearch-php
============================

[](#usage-with-elasticsearch-php)

This library creates simple arrays to pass into the body portion of Elasticsearch search queries. You can pass the aggregation portion of the search result into the generateQuery function of the aggregation.

```
    use Crowdskout\EsSearchBuilder\Agg as AggBuilder;
    use Crowdskout\EsSearchBuilder\Query;
    use Elasticsearch\ClientBuilder;

    // Build a query
    $query = Query::terms('intField', [1, 2, 3, 4, 5, 6])

    // Build an aggregation
    $aggBuilder = new AggBuilder();
    $agg = $aggBuilder->nested('parentField', $aggBuilder->terms('parentField.subField'));

    // Initialize the Elasticsearch client
    $client = ClientBuilder::create()->build()

    // Run the search query
    $params = [
            'index' => 'my_index',
            'type' => 'my_type',
            'body' => [
                'query' => $query,
                'aggs' => $agg->generateQuery()
            ]
        ];
    $response = $client->search($params);

    // Parse the results
    $parsedResult = $agg->generateResults($response['aggregations']);
```

This library does not currently support all queries and aggregations
====================================================================

[](#this-library-does-not-currently-support-all-queries-and-aggregations)

The current supported queries are here: .

The current supported aggregations are here: .

If there's a query or or aggregation that you would like to see supported, please open an issue. You can also take a stab at writing it and open a pull request :).

Additional examples
===================

[](#additional-examples)

You can see other, more complex examples of queries and aggregations in the tests/ directory. You can also see an example of a custom agg results generator in tests/AggGeneratorTest.php/

###  Health Score

27

—

LowBetter than 49% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity12

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity58

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

3161d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/a7c18ff167e36785d1ba23ecc15a0071b12323fc192a00fd95310eac2dbcc868?d=identicon)[feydan](/maintainers/feydan)

---

Top Contributors

[![feydan](https://avatars.githubusercontent.com/u/4978032?v=4)](https://github.com/feydan "feydan (14 commits)")

---

Tags

elasticsearch

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/crowdskout-es-search-builder/health.svg)

```
[![Health](https://phpackages.com/badges/crowdskout-es-search-builder/health.svg)](https://phpackages.com/packages/crowdskout-es-search-builder)
```

###  Alternatives

[mailerlite/laravel-elasticsearch

An easy way to use the official PHP ElasticSearch client in your Laravel applications.

934529.3k2](/packages/mailerlite-laravel-elasticsearch)[matchish/laravel-scout-elasticsearch

Search among multiple models with ElasticSearch and Laravel Scout

7431.6M2](/packages/matchish-laravel-scout-elasticsearch)[jeroen-g/explorer

Next-gen Elasticsearch driver for Laravel Scout.

397612.3k](/packages/jeroen-g-explorer)[jsq/amazon-es-php

Support for using IAM authentication with the official Elasticsearch PHP client

9310.6M13](/packages/jsq-amazon-es-php)[babenkoivan/elastic-client

The official PHP Elasticsearch client integrated with Laravel

544.0M6](/packages/babenkoivan-elastic-client)[madewithlove/elasticsearcher

Wrapper on top of the ElasticSearch PHP SDK which allows easier index/document/query management.

264133.2k2](/packages/madewithlove-elasticsearcher)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
