PHP code example of nordsoftware / lumen-elasticsearch
1. Go to this page and download the library: Download nordsoftware/lumen-elasticsearch 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/ */
nordsoftware / lumen-elasticsearch example snippets
use Nord\Lumen\Elasticsearch\Console\IndexCommand;
class IndexPersonsCommand extends IndexCommand
{
protected $signature = 'app:index:persons';
protected $description = 'Indexes all persons into the search index';
public function getData()
{
return [
new Person('Joe'),
new Person('Jane'),
];
}
public function getIndex()
{
return 'persons';
}
public function getType()
{
return 'person';
}
public function getItemBody($item)
{
// Item is an instance of Person in this case
return $item->getName();
}
public function getItemId($item)
{
// Item is an instance of Person in this case
return $item->getId();
}
public function getItemParent($item)
{
// Return null if your objects don't have any parent/child relationship
return $item->getParent();
}
}
// Get an instance of ElasticSearchService
$service = app(ElasticsearchServiceContract::class);
// Create the query
$query = (new BoolQuery())
->addMust(
(new TermQuery())
->setField('user')
->setValue('kimchy'))
->addFilter(
(new TermQuery())
->setField('tag')
->setValue('tech'))
->addMustNot(
(new RangeQuery())
->setField('age')
->setGreaterThanOrEquals(18)
->setLessThanOrEquals(40))
->addShould(
(new TermQuery())
->setField('tag')
->setValue('wow'))
->addShould(
(new TermQuery())
->setField('tag')
->setValue('elasticsearch'));
// Create the search
$search = $service->createSearch()
->setIndex('index')
->setType('document')
->setQuery($query)
->setSize(50)
->setPage(1);
// Execute the search to retrieve the results
$result = $service->execute($search);