PHP code example of madewithlove / elasticsearcher

1. Go to this page and download the library: Download madewithlove/elasticsearcher 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/ */

    

madewithlove / elasticsearcher example snippets


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();

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

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

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);
}

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

$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);


/** @var \ElasticSearcher\ElasticSearcher $searcher */
while (!$searcher->isHealthy()) { sleep(1); }

$client = $searcher->getClient();

use ElasticSearcher\Environment;
use ElasticSearcher\ElasticSearcher;

$env = new Environment(
  ['hosts' => ['localhost:9200']]
);
$searcher = new ElasticSearcher($env);