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);
}
/** @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);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.