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

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

aman-rawat/elastic-adapter
==========================

Adapter for official PHP Elasticsearch client

1.0.1(3y ago)023[1 PRs](https://github.com/aman-rawat/elastic-adapter/pulls)MITPHPPHP ^7.3 || ^8.0

Since Aug 1Pushed 1y agoCompare

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

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

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 official PHP Elasticsearch client. It's designed to simplify basic index and document operations.

Contents
--------

[](#contents)

- [Compatibility](#compatibility)
- [Installation](#installation)
- [Index Management](#index-management)
- [Document Management](#document-management)

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

[](#compatibility)

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

- PHP 7.3-8.0
- Elasticsearch 7.x
- Laravel 6.x-8.x

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

[](#installation)

The library can be installed via Composer:

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

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

[](#index-management)

`IndexManager` can be used to manipulate indices. It uses Elasticsearch client as a dependency, therefore you need to initiate the client before you create an `IndexManager` instance:

```
$client = \Elasticsearch\ClientBuilder::fromConfig([
  'hosts' => [
      'localhost:9200'
  ]
]);

$indexManager = new \ElasticAdapter\Indices\IndexManager($client);
```

The manager provides a set of useful methods, which are listed below.

### Create

[](#create)

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

```
$index = new \ElasticAdapter\Indices\IndexBlueprint('my_index');

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

or configured according to your needs:

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

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

$index = new \ElasticAdapter\Indices\IndexBlueprint('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 \ElasticAdapter\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 \ElasticAdapter\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 \ElasticAdapter\Indices\Alias('my_alias', [
    'term' => [
        'user_id' => 12,
    ],
]);

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

### Get Aliases

[](#get-aliases)

Get index aliases:

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

### Delete Alias

[](#delete-alias)

Delete an alias:

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

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

[](#document-management)

Similarly to `IndexManager`, the `DocumentManager` class also depends on Elasticsearch client:

```
$client = \Elasticsearch\ClientBuilder::fromConfig([
  'hosts' => [
      'localhost:9200'
  ]
]);

$documentManager = new \ElasticAdapter\Documents\DocumentManager($client);
```

### Index

[](#index)

Add a document to the index:

```
$documents = collect([
    new ElasticAdapter\Documents\Document('1', ['title' => 'foo']),
    new ElasticAdapter\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 ElasticAdapter\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 ElasticAdapter\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:

```
// create a search request
$request = new \ElasticAdapter\Search\SearchRequest([
    'match' => [
        'message' => 'test'
    ]
]);

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

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

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

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

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

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

// rescore documents
$request->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
$request->postFilter([
    'term' => [
        'cover' => 'hard'
    ]
]);

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

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

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

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

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

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

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

// execute the search request and get the response
$response = $documentManager->search('my_index', $request);

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

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

// every hit provides access to the related index name, the score, the document, the highlight and the inner hits
// 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();
    $raw = $hit->raw();
}

// get the suggestions
$suggestions = $response->suggestions();

// get the aggregations
$aggregations = $response->aggregations();
```

###  Health Score

26

—

LowBetter than 43% of packages

Maintenance27

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity53

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 85% 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 ~0 days

Total

2

Last Release

1378d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/e1590ae0f585833562a4c7cafd40cf0739ed06c88ebcbda18a9321bca943fe96?d=identicon)[aman-rawat](/maintainers/aman-rawat)

---

Top Contributors

[![babenkoivan](https://avatars.githubusercontent.com/u/25812954?v=4)](https://github.com/babenkoivan "babenkoivan (130 commits)")[![aman-rawat](https://avatars.githubusercontent.com/u/6269995?v=4)](https://github.com/aman-rawat "aman-rawat (8 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)")[![marcintokarskipwn](https://avatars.githubusercontent.com/u/81288963?v=4)](https://github.com/marcintokarskipwn "marcintokarskipwn (2 commits)")

---

Tags

phpclientelasticsearchadapterelastic

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

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

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

###  Alternatives

[elasticsearch/elasticsearch

PHP Client for Elasticsearch

5.3k178.3M940](/packages/elasticsearch-elasticsearch)[babenkoivan/elastic-adapter

Adapter for official PHP Elasticsearch client

404.0M9](/packages/babenkoivan-elastic-adapter)[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)[jeroen-g/explorer

Next-gen Elasticsearch driver for Laravel Scout.

397612.3k](/packages/jeroen-g-explorer)

PHPackages © 2026

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