PHPackages                             madewithlove/elasticsearcher - 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. madewithlove/elasticsearcher

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

madewithlove/elasticsearcher
============================

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

0.7.0(4y ago)264133.2k↓37.5%29[9 issues](https://github.com/madewithlove/elasticsearcher/issues)2MITPHP

Since Dec 12Pushed 4y ago24 watchersCompare

[ Source](https://github.com/madewithlove/elasticsearcher)[ Packagist](https://packagist.org/packages/madewithlove/elasticsearcher)[ RSS](/packages/madewithlove-elasticsearcher/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (4)Versions (22)Used By (2)

elasticsearcher
===============

[](#elasticsearcher)

[![checks](https://github.com/madewithlove/elasticsearcher/actions/workflows/run-checks.yml/badge.svg)](https://github.com/madewithlove/elasticsearcher/actions/workflows/run-checks.yml/badge.svg)

This agnostic package is a lightweight wrapper on top of the [Elasticsearch PHP client](http://www.elastic.co/guide/en/elasticsearch/client/php-api/current/index.html). Its main goal is to allow for easier structuring of queries and indices in your application. It does not want to hide or replace functionality of the Elasticsearch PHP client.

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

[](#installation)

Installation of the latest version is easy via [composer](https://getcomposer.org/):

```
composer require madewithlove/elasticsearcher

```

### Versions

[](#versions)

ElasticsearchElasticsearcher&gt;= 7.0&gt;= 0.7&gt;= 5.0&gt;= 0.5&gt;= 2.0&gt;= 0.4&gt;= 1.0, &lt; 2.00.3Features
--------

[](#features)

### Query class

[](#query-class)

Structure queries inside a class for clearer oversight in your application.

```
class MoviesFrom2014Query extends AbstractQuery
{
	public function setup()
	{
		$this->searchIn('movies', 'movies');

		// Full notation
		$body = [
			'query' => [
				'bool' => [
					'filter' => [
						'term' => ['year' => 2014]
					]
				]
			]
		];
		$this->setBody($body);

		// Short (dotted) notation
		$this->set('query.bool.filter.term.year', 2014);

		$this->paginate(2, 20);
		$this->sortBy('name', 'asc');
	}
}

// Usage
$query = new MoviesFrom2014Query($this->getElasticSearcher());
$query->run();
```

### Query with custom/re-usable fragments

[](#query-with-customre-usable-fragments)

Move re-occuring or complex fragments of your query or index to a separate class.

```
class MoviesFrom2014Query extends AbstractQuery
{
	public function setup()
	{
		$this->searchIn('movies', 'movies');

		$this->set('query.bool.filter', [new YearFilter(2014)]);
	}
}
```

### Query with custom result parsing

[](#query-with-custom-result-parsing)

Perform actions on the response from Elasticsearch before the Query returns the results. It can be used for converting the Elasticsearch documents into models/entities from your ORM. Re-use it in multiple queries.

```
class MoviesFrom2014Query extends AbstractQuery
{
	public function setup()
	{
		$this->searchIn('movies', 'movies');
		$this->parseResultsWith(new MoviesResultParser());

		$body = array(...);

		$this->setBody($body);
	}
}

// Usage
$query = new MoviesFrom2014Query($this->getElasticSearcher());
$result = $query->run();
foreach ($result->getResults() as $movie) {
	var_dump($movie->title, $movie->id, $movie->year);
}
```

### Indices management

[](#indices-management)

```
$searcher->indicesManager()->exists('listings');
$searcher->indicesManager()->create('suggestions');
$searcher->indicesManager()->update('suggestions');
$searcher->indicesManager()->delete('suggestions');
```

### Documents management

[](#documents-management)

```
$manager->index('suggestions', $data);
$manager->bulkIndex('suggestions', [$data, $data, $data]);
$manager->update('suggestions', 123, ['name' => 'Fight Club 2014']);
$manager->updateOrIndex('suggestions', 123, ['name' => 'Fight Club 2014']);
$manager->delete('suggestions', 123);
$manager->exists('suggestions', 123);
$manager->get('suggestions', 123);
```

### Cluster Health

[](#cluster-health)

Sometimes when you're re-indexing your ES data, you might have some issues between your index recreation and indexing your data. That's because ES can take a bit longer to recreate your indexes, causing your reindex task to fail - we are talking about *microseconds* here. You can find some references [here](https://www.elastic.co/guide/en/elasticsearch/reference/current/cluster-health.html)and [here](http://chrissimpson.co.uk/elasticsearch-yellow-cluster-status-explained.html).

In order to avoid this, we built this helper in the ElasticSearcher class to check the cluster health. You can use like this:

```
