PHP code example of joacub / elastic-search
1. Go to this page and download the library: Download joacub/elastic-search 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/ */
joacub / elastic-search example snippets
return [
'MCNElasticSearch' => [
'metadata' => [
/**
* List of object mappings
*/
'objects' => [
'Company\Entity\CompanyEntity' => [
'hydrator' => 'company',
'type' => 'companies',
'index' => 'example',
]
],
/**
* List of types E.g "SQL Tables"
*/
'types' => [
'companies' => [
'index' => 'example',
'source' => ['enabled' => false],
'properties' => [
'id' => ['type' => 'integer'],
'name' => ['type' => 'string'],
'address' => [
'type' => 'object',
'properties' => [
'id' => ['type' => 'integer'],
'type' => ['type' => 'string', 'not_analyzed' => true],
'street' => ['type' => 'string'],
'zipcode' => ['type' => 'integer'],
'country' => ['type' => 'string'],
'coordinates' => ['type' => 'geo_point'],
]
]
]
]
]
]
]
];
public/index.php es mapping create
public/index.php es mapping delete
class ElasticSearchSynchronizer extends \MCNElasticSearch\Listener\AbstractDoctrineORMSynchronizer
{
/**
* Check that an object is of the proper instance
*
* @param mixed $object
*
* @return bool
*/
public function isValid($object)
{
return $object instanceof CompanyEntity;
}
}
'eventmanager' => [
'orm_default' => [
'subscribers' => [
ElasticSearchSynchronizer::class
]
]
],
class CompanyResource implements ListenerAggregateInterface
{
use ListenerAggregateTrait;
/**
* @var \Company\Service\CompanyServiceInterface
*/
protected $companyService;
/**
* @var \MCNElasticSearch\Service\SearchServiceInterface
*/
protected $searchService;
/**
* @param CompanyServiceInterface $companyService
* @param SearchServiceInterface $searchService
*/
public function __construct(CompanyServiceInterface $companyService, SearchServiceInterface $searchService)
{
$this->searchService = $searchService;
$this->companyService = $companyService;
}
/**
* Attach one or more listeners
*
* Implementors may add an optional $priority argument; the EventManager
* implementation will pass this to the aggregate.
*
* @param EventManagerInterface $events
*
* @return void
*/
public function attach(EventManagerInterface $events)
{
$this->listeners[] = $events->attach('fetchAll', [$this, 'fetchAll']);
}
/**
* @param ResourceEvent $event
* @return \PhlyRestfully\ApiProblem|\Zend\Paginator\Paginator
*/
public function fetchAll(ResourceEvent $event)
{
$coordinates = $event->getQueryParam('coordinates');
$maxDistance = (int) $event->getQueryParam('distance', 1000);
$sort = [
'_geo_distance' => [
'companies.address.coordinates' => $coordinates,
'unit' => 'km',
'order' => 'asc'
]
];
$geoDistanceFilter = new GeoDistance('companies.address.coordinates', $coordinates, $maxDistance . 'km');
$query = new Query();
$query->addSort($sort);
$query->setFilter($geoDistanceFilter);
return $this->searchService->search(CompanyEntity::class, $query, SearchServiceInterface::HYDRATE_DOCTRINE_OBJECT);
}
}
config/MCNElasticSearch.global.php