PHPackages                             babenkoivan/elastic-adapter - 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. babenkoivan/elastic-adapter

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

babenkoivan/elastic-adapter
===========================

Adapter for official PHP Elasticsearch client

v5.1.0(3mo ago)404.0M—2.8%358MITPHPPHP ^8.2CI passing

Since May 1Pushed 2mo ago1 watchersCompare

[ Source](https://github.com/babenkoivan/elastic-adapter)[ Packagist](https://packagist.org/packages/babenkoivan/elastic-adapter)[ Fund](https://ko-fi.com/ivanbabenko)[ Fund](https://paypal.me/babenkoi)[ RSS](/packages/babenkoivan-elastic-adapter/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (6)Versions (45)Used By (8)

Elastic Adapter
===============

[](#elastic-adapter)

[![Latest Stable Version](https://camo.githubusercontent.com/9b312211ef697d4386a516597eb7e456ff8ee4b62398a3fc19d1a7ef92334d1f/68747470733a2f2f706f7365722e707567782e6f72672f626162656e6b6f6976616e2f656c61737469632d616461707465722f762f737461626c65)](https://packagist.org/packages/babenkoivan/elastic-adapter)[![Total Downloads](https://camo.githubusercontent.com/71603452396c5437225fde1151e0cbec593906afe26434a0cbf6c0335a64b21a/68747470733a2f2f706f7365722e707567782e6f72672f626162656e6b6f6976616e2f656c61737469632d616461707465722f646f776e6c6f616473)](https://packagist.org/packages/babenkoivan/elastic-adapter)[![License](https://camo.githubusercontent.com/079303cc397e1668198e3d2b521bf6ac4274d5e887ad14b154a0f0969bd2a935/68747470733a2f2f706f7365722e707567782e6f72672f626162656e6b6f6976616e2f656c61737469632d616461707465722f6c6963656e7365)](https://packagist.org/packages/babenkoivan/elastic-adapter)[![Tests](https://github.com/babenkoivan/elastic-adapter/workflows/Tests/badge.svg)](https://github.com/babenkoivan/elastic-adapter/actions?query=workflow%3ATests)[![Code style](https://github.com/babenkoivan/elastic-adapter/workflows/Code%20style/badge.svg)](https://github.com/babenkoivan/elastic-adapter/actions?query=workflow%3A%22Code+style%22)[![Static analysis](https://github.com/babenkoivan/elastic-adapter/workflows/Static%20analysis/badge.svg)](https://github.com/babenkoivan/elastic-adapter/actions?query=workflow%3A%22Static+analysis%22)[![Donate PayPal](https://camo.githubusercontent.com/0b8a275d67dfb2aa74ac299fa07b7fce95217f6620448d88cf05ff9e3653e9bf/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f646f6e6174652d70617970616c2d626c7565)](https://paypal.me/babenkoi)

 [![Support the project!](https://camo.githubusercontent.com/201ef269611db7eb6b5d08e9f756ab8980df3014b64492770bdf13a6ed924641/68747470733a2f2f6b6f2d66692e636f6d2f696d672f676974687562627574746f6e5f736d2e737667)](https://ko-fi.com/ivanbabenko)

---

Elastic Adapter is an adapter for the official PHP Elasticsearch client. It's designed to simplify basic index and document operations.

Contents
--------

[](#contents)

- [Compatibility](#compatibility)
- [Installation](#installation)
- [Configuration](#configuration)
- [Index Management](#index-management)
- [Document Management](#document-management)
- [Point in Time Management](#point-in-time-management)

Compatibility
-------------

[](#compatibility)

The current version of Elastic Adapter has been tested with the following configuration:

- PHP 8.2
- Elasticsearch 9.x
- Laravel 12.x

If your project uses older Elasticsearch, Laravel, or PHP version check [the previous major version](https://github.com/babenkoivan/elastic-adapter/tree/v4.1.1#compatibility) of the package.

Installation
------------

[](#installation)

The library can be installed via Composer:

```
composer require babenkoivan/elastic-adapter
```

Configuration
-------------

[](#configuration)

Elastic Adapter uses [babenkoivan/elastic-client](https://github.com/babenkoivan/elastic-client) as a dependency. To change the client settings you need to publish the configuration file first:

```
php artisan vendor:publish --provider="Elastic\Client\ServiceProvider"
```

In the newly created `config/elastic.client.php` file you can define the default connection name and describe multiple connections using configuration hashes. Please, refer to the [elastic-client documentation](https://github.com/babenkoivan/elastic-client) for more details.

Index Management
----------------

[](#index-management)

`\Elastic\Adapter\Indices\IndexManager` is used to manipulate indices.

### Create

[](#create)

Create an index, either with the default settings and mapping:

```
$index = new \Elastic\Adapter\Indices\Index('my_index');

$indexManager->create($index);
```

or configured according to your needs:

```
$mapping = (new \Elastic\Adapter\Indices\Mapping())
    ->text('title', [
        'boost' => 2,
    ])
    ->keyword('tag', [
        'null_value' => 'NULL'
    ])
    ->geoPoint('location')
    ->dynamic(true)
    ->dynamicTemplate('no_doc_values', [
        'match_mapping_type' => '*',
        'mapping' => [
            'type' => '{dynamic_type}',
            'doc_values' => false,
        ],
    ]);

$settings = (new \Elastic\Adapter\Indices\Settings())
    ->index([
        'number_of_replicas' => 2,
        'refresh_interval' => -1
    ]);

$index = new \Elastic\Adapter\Indices\Index('my_index', $mapping, $settings);

$indexManager->create($index);
```

Alternatively, you can create an index using raw input:

```
$mapping = [
    'properties' => [
        'title' => [
            'type' => 'text'
        ]
    ]
];

$settings = [
    'number_of_replicas' => 2
];

$indexManager->createRaw('my_index', $mapping, $settings);
```

### Drop

[](#drop)

Delete an index:

```
$indexManager->drop('my_index');
```

### Put Mapping

[](#put-mapping)

Update an index mapping using builder:

```
$mapping = (new \Elastic\Adapter\Indices\Mapping())
    ->text('title', [
        'boost' => 2,
    ])
    ->keyword('tag', [
        'null_value' => 'NULL'
    ])
    ->geoPoint('location');

$indexManager->putMapping('my_index', $mapping);
```

or using raw input:

```
$mapping = [
    'properties' => [
        'title' => [
            'type' => 'text'
        ]
    ]
];

$indexManager->putMappingRaw('my_index', $mapping);
```

### Put Settings

[](#put-settings)

Update an index settings using builder:

```
$settings = (new \Elastic\Adapter\Indices\Settings())
    ->analysis([
        'analyzer' => [
            'content' => [
                'type' => 'custom',
                'tokenizer' => 'whitespace'
            ]
        ]
    ]);

$indexManager->putSettings('my_index', $settings);
```

or using raw input:

```
$settings = [
    'number_of_replicas' => 2
];

$indexManager->putSettingsRaw('my_index', $settings);
```

### Exists

[](#exists)

Check if an index exists:

```
$indexManager->exists('my_index');
```

### Open

[](#open)

Open an index:

```
$indexManager->open('my_index');
```

### Close

[](#close)

Close an index:

```
$indexManager->close('my_index');
```

### Put Alias

[](#put-alias)

Create an alias:

```
$alias = new \Elastic\Adapter\Indices\Alias('my_alias', true, [
    'term' => [
        'user_id' => 12,
    ],
]);

$indexManager->putAlias('my_index', $alias);
```

The same with raw input:

```
$settings = [
    'is_write_index' => true,
    'filter' => [
        'term' => [
            'user_id' => 12,
        ],
    ],
];

$indexManager->putAliasRaw('my_index', 'my_alias', $settings);
```

### Get Aliases

[](#get-aliases)

Get index aliases:

```
$indexManager->getAliases('my_index');
```

### Delete Alias

[](#delete-alias)

Delete an alias:

```
$indexManager->deleteAlias('my_index', 'my_alias');
```

### Connection

[](#connection)

Switch Elasticsearch connection:

```
$indexManager->connection('my_connection');
```

Document Management
-------------------

[](#document-management)

`\Elastic\Adapter\Documents\DocumentManager` is used to manage and search documents.

### Index

[](#index)

Add a document to the index:

```
$documents = collect([
    new \Elastic\Adapter\Documents\Document('1', ['title' => 'foo']),
    new \Elastic\Adapter\Documents\Document('2', ['title' => 'bar']),
]);

$documentManager->index('my_index', $documents);
```

There is also an option to refresh index immediately:

```
$documentManager->index('my_index', $documents, true);
```

Finally, you can set a custom routing:

```
$routing = (new \Elastic\Adapter\Documents\Routing())
    ->add('1', 'value1')
    ->add('2', 'value2');

$documentManager->index('my_index', $documents, false, $routing);
```

### Delete

[](#delete)

Remove a document from the index:

```
$documentIds = ['1', '2'];

$documentManager->delete('my_index', $documentIds);
```

If you want the index to be refreshed immediately pass `true` as the third argument:

```
$documentManager->delete('my_index', $documentIds, true);
```

You can also set a custom routing:

```
$routing = (new \Elastic\Adapter\Documents\Routing())
    ->add('1', 'value1')
    ->add('2', 'value2');

$documentManager->delete('my_index', $documentIds, false, $routing);
```

Finally, you can delete documents using query:

```
$documentManager->deleteByQuery('my_index', ['match_all' => new \stdClass()]);
```

### Search

[](#search)

Search documents in the index:

```
// configure search parameters
$searchParameters = new \Elastic\Adapter\Search\SearchParameters();

// specify indices to search in
$searchParameters->indices(['my_index1', 'my_index2']);

// define the query
$searchParameters->query([
    'match' => [
        'message' => 'test'
    ]
]);

// configure highlighting
$searchParameters->highlight([
    'fields' => [
        'message' => [
            'type' => 'plain',
            'fragment_size' => 15,
            'number_of_fragments' => 3,
            'fragmenter' => 'simple'
        ]
    ]
]);

// add suggestions
$searchParameters->suggest([
    'message_suggest' => [
        'text' => 'test',
        'term' => [
            'field' => 'message'
        ]
    ]
]);

// enable source filtering
$searchParameters->source(['message', 'post_date']);

// collapse fields
$searchParameters->collapse([
    'field' => 'user'
]);

// aggregate data
$searchParameters->aggregations([
    'max_likes' => [
        'max' => [
            'field' => 'likes'
        ]
    ]
]);

// sort documents
$searchParameters->sort([
    ['post_date' => ['order' => 'asc']],
    '_score'
]);

// rescore documents
$searchParameters->rescore([
    'window_size' => 50,
    'query' => [
        'rescore_query' => [
            'match_phrase' => [
                'message' => [
                    'query' => 'the quick brown',
                    'slop' => 2,
                ],
            ],
        ],
        'query_weight' => 0.7,
        'rescore_query_weight' => 1.2,
    ]
]);

// add a post filter
$searchParameters->postFilter([
    'term' => [
        'cover' => 'hard'
    ]
]);

// track total hits
$searchParameters->trackTotalHits(true);

// track scores
$searchParameters->trackScores(true);

// script fields
$searchParameters->scriptFields([
    'my_doubled_field' => [
        'script' => [
            'lang' => 'painless',
            'source' => 'doc[params.field] * params.multiplier',
            'params' => [
                'field' => 'my_field',
                'multiplier' => 2,
            ],
        ],
    ],
]);

// runtime mappings
$searchParameters->runtimeMappings([
    'day_of_week' => [
        'type' => 'long',
        'script' => [
            'lang' => 'painless',
            'source' => 'doc[params.field] * params.multiplier',
            'params' => [
                'field' => 'my_field',
                'multiplier' => 2,
            ],
        ],
    ],
]);

// boost indices
$searchParameters->indicesBoost([
    ['my-alias' => 1.4],
    ['my-index' => 1.3],
]);

// define the search type
$searchParameters->searchType('query_then_fetch');

// set the preference
$searchParameters->preference('_local');

// use pagination
$searchParameters->from(0)->size(20);

// search after
$searchParameters->pointInTime([
    'id' => '46ToAwMDaWR5BXV1',
    'keep_alive' => '1m',
]);

$searchParameters->searchAfter([
    '2021-05-20T05:30:04.832Z',
    4294967298,
]);

// use custom routing
$searchParameters->routing(['user1', 'user2']);

// enable explanation
$searchParameters->explain(true);

// set maximum number of documents to collect for each shard
$searchParameters->terminateAfter(10);

// enable caching
$searchParameters->requestCache(true);

// perform the search and get the result
$searchResult = $documentManager->search($searchParameters);

// get the total number of matching documents
$total = $searchResult->total();

// get the corresponding hits
$hits = $searchResult->hits();

// every hit provides access to the related index name, the score, the document, the highlight and more
// in addition, you can get a raw representation of the hit
foreach ($hits as $hit) {
    $indexName = $hit->indexName();
    $score = $hit->score();
    $document = $hit->document();
    $highlight = $hit->highlight();
    $innerHits = $hit->innerHits();
    $innerHitsTotal = $hit->innerHitsTotal();
    $raw = $hit->raw();

    // get an explanation
    $explanation = $searchResult->explanation();

    // every explanation includes a value, a description and details
    // it is also possible to get its raw representation
    $value = $explanation->value();
    $description = $explanation->description();
    $details = $explanation->details();
    $raw = $explanation->raw();
}

// get suggestions
$suggestions = $searchResult->suggestions();

// get aggregations
$aggregations = $searchResult->aggregations();
```

### Connection

[](#connection-1)

Switch Elasticsearch connection:

```
$documentManager->connection('my_connection');
```

Point in Time Management
------------------------

[](#point-in-time-management)

`\Elastic\Adapter\Search\PointInTimeManager` is used to control points in time.

### Open

[](#open-1)

Open a point in time:

```
$pointInTimeId = $pointInTimeManager->open('my_index', '1m');
```

### Close

[](#close-1)

Close a point in time:

```
$pointInTimeManager->close($pointInTimeId);
```

### Connection

[](#connection-2)

Switch Elasticsearch connection:

```
$pointInTimeManager->connection('my_connection');
```

###  Health Score

66

—

FairBetter than 99% of packages

Maintenance83

Actively maintained with recent releases

Popularity57

Moderate usage in the ecosystem

Community27

Small or concentrated contributor base

Maturity81

Battle-tested with a long release history

 Bus Factor1

Top contributor holds 87.4% 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

Every ~49 days

Recently: every ~25 days

Total

44

Last Release

81d ago

Major Versions

v1.17.0 → v2.0.02021-09-24

v2.4.0 → v3.0.02022-07-26

v3.x-dev → v4.0.02024-06-18

v4.x-dev → v5.0.02025-11-16

PHP version history (5 changes)v1.0.1PHP ^7.2

v1.13.0PHP ^7.2 || ^8.0

v2.0.0PHP ^7.3 || ^8.0

v3.0.0PHP ^7.4 || ^8.0

v4.0.0PHP ^8.2

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/25812954?v=4)[Ivan Babenko](/maintainers/babenkoivan)[@babenkoivan](https://github.com/babenkoivan)

---

Top Contributors

[![babenkoivan](https://avatars.githubusercontent.com/u/25812954?v=4)](https://github.com/babenkoivan "babenkoivan (153 commits)")[![spiritinlife](https://avatars.githubusercontent.com/u/6434983?v=4)](https://github.com/spiritinlife "spiritinlife (8 commits)")[![stevebauman](https://avatars.githubusercontent.com/u/6421846?v=4)](https://github.com/stevebauman "stevebauman (5 commits)")[![beblife](https://avatars.githubusercontent.com/u/9271492?v=4)](https://github.com/beblife "beblife (3 commits)")[![jariesdev](https://avatars.githubusercontent.com/u/17381846?v=4)](https://github.com/jariesdev "jariesdev (3 commits)")[![marcintokarskipwn](https://avatars.githubusercontent.com/u/81288963?v=4)](https://github.com/marcintokarskipwn "marcintokarskipwn (2 commits)")[![Yi-pixel](https://avatars.githubusercontent.com/u/31845646?v=4)](https://github.com/Yi-pixel "Yi-pixel (1 commits)")

---

Tags

adapterclientelasticsearchphpphpclientelasticsearchadapterelastic

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/babenkoivan-elastic-adapter/health.svg)

```
[![Health](https://phpackages.com/badges/babenkoivan-elastic-adapter/health.svg)](https://phpackages.com/packages/babenkoivan-elastic-adapter)
```

###  Alternatives

[elasticsearch/elasticsearch

PHP Client for Elasticsearch

5.3k178.3M943](/packages/elasticsearch-elasticsearch)[babenkoivan/elastic-client

The official PHP Elasticsearch client integrated with Laravel

544.0M6](/packages/babenkoivan-elastic-client)[babenkoivan/elastic-scout-driver

Elasticsearch driver for Laravel Scout

2773.8M5](/packages/babenkoivan-elastic-scout-driver)[babenkoivan/elastic-scout-driver-plus

Extension for Elastic Scout Driver

2862.8M1](/packages/babenkoivan-elastic-scout-driver-plus)[babenkoivan/elastic-migrations

Elasticsearch migrations for Laravel

1962.5M6](/packages/babenkoivan-elastic-migrations)

PHPackages © 2026

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