mcn / elastic-search
一个帮助整合Elasticsearch的模块
1.0.1
2015-03-26 15:08 UTC
Requires
- php: >=5.5
- elasticsearch/elasticsearch: ~1.0.0
- psr/log: 1.0.0
- zendframework/zendframework: ~2.2
Requires (Dev)
- phpunit/phpunit: ~3.7
- satooshi/php-coveralls: ~0.6
- squizlabs/php_codesniffer: dev-master
This package is auto-updated.
Last update: 2024-08-29 04:08:57 UTC
README
这是一个相对简单的模块,可以帮助您保持Elasticsearch索引与数据库同步。
您希望...
- 您希望有一个用于更新/删除映射的实用程序
- 您希望将Elasticsearch与ORM同步
- 您希望有一个提供简单接口以搜索和返回 doctrine 实体的服务
步骤1,设置映射
首先,将文件 config/MCNElasticSearch.global.php
复制到您的 config/autoload/
目录。类型数组是一个关联数组,名称 => 映射信息。检查映射中的所有选项 MCNElasticSearch\Options\TypeMappingOptions
,目前只有基本选项可用,但欢迎PR!
示例配置
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'], ] ] ] ] ] ] ] ];
现在您已经设置了映射,我们需要将其运行在我们的Elasticsearch上 php public/index.php es mapping create
如果您想删除它,请运行 php public/index.php es mapping delete
步骤2,设置同步器
现在我们需要实现同步器,这非常简单!
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; } }
您还需要设置一个工厂,并传递一个 MCNElasticSearch\Service\DocumentService
的实例,但希望将来可以移除这个功能!
现在我们需要告诉 doctrine 发布事件到您的同步器。所以,在您的 doctrine 配置中,您需要添加
'eventmanager' => [ 'orm_default' => [ 'subscribers' => [ ElasticSearchSynchronizer::class ] ] ],
步骤3,执行搜索
现在我将继续上一个示例,并从我的使用 PhlyRestfully
编写的API中提取一段代码,对公司的类型进行搜索,按距离排序,并过滤掉所有超过1000km的公司
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); } }
步骤4,万岁时刻
盈利!