headoo / elasticsearch-bundle
ElasticSearch Bundle
Requires
- php: >=7.0
- doctrine/doctrine-bundle: ^1.6
- doctrine/doctrine-fixtures-bundle: >=2.3
- doctrine/orm: ^2.5
- ruflin/elastica: ^5.3
- symfony/console: ~3.4
- symfony/finder: ~3.4
- symfony/framework-bundle: ~3.4
- symfony/process: ~3.4
- symfony/validator: ~3.4
Requires (Dev)
- phpunit/phpunit: 7.4
This package is not auto-updated.
Last update: 2024-09-16 18:28:06 UTC
README
ElasticSearchBundle 是一个为简化使用 Doctrine 2.x 和 ElasticSearch 5.x 而设计的 Symfony2/3 Bundle。
安装
通过 Composer
$ composer require headoo/elasticsearch-bundle
或在 composer.json 文件中
"headoo/elasticsearch-bundle": "dev-master"
在 app/AppKernel.php
中注册该 Bundle
public function registerBundles() { return array( // ... new Headoo\ElasticSearchBundle\HeadooElasticSearchBundle(), // ... ); }
配置
在 config.yml
中配置您的连接和映射:我们将将其链接到 PHP 配置。您可以使用任何您想要的。我更倾向于这种方式,因为它更接近 ElasticSearch 的方式。
imports: - { resource: elastic.php } headoo_elastic_search: connections: %elastica_connections% mappings: %elastica_mappings% services: name.of.your.transformer.service: class: Name\NameBundle\Entity\Transformer\EntityTransformer
以下是一个 elastic.php
的示例。
<?php /********************************************************************** ************************************CONNECTIONS*********************** ********************************************************************** */ $connections = array('localhost'=> array( 'host' => 'localhost', 'port' => '9200', ) ); $container->setParameter('elastica_connections', $connections); /********************************************************************** ************************************INDEX***************************** ********************************************************************** */ $elasticaIndex = array( 'number_of_shards' => 1, 'number_of_replicas' => 1, 'analysis' => array( 'analyzer' => array( 'default' => array( 'type' => 'custom', 'tokenizer' => 'standard', 'filter' => array('lowercase'') ), 'default_search' => array( 'type' => 'custom', 'tokenizer' => 'standard', 'filter' => array('standard', 'lowercase') ), ), ) ); $mapping['YourEntityClassName']['mapping'] = array( 'id' => array('type' => 'integer', 'include_in_all' => TRUE), 'date' => array('type' => 'date', 'include_in_all' => FALSE), ); $mapping['YourEntityClassName']['class'] = '\Name\NameBundle\Entity\YourEntityClassName'; $mapping['YourEntityClassName']['index'] = $elasticaIndex; $mapping['YourEntityClassName']['transformer'] = 'name.of.your.transformer.service'; $mapping['YourEntityClassName']['connection'] = 'localhost'; //Optional, if you need a customize Index Name. If you don't use it, the index will be strlower of your Entity Name $mapping['YourEntityClassName']['index_name'] = 'indexname'; //Optional, it's use if you want to trigger automatically creation,update and deletion of entity $mapping['YourEntityClassName']['auto_event'] = true; $container->setParameter('elastica_mappings', $mapping);
实体示例
<?php namespace Name\NameBundle\Entity; use Doctrine\ORM\Mapping as ORM; /** * @ORM\Entity() */ class YourEntityClassName { /** * @var integer * * @ORM\Column(name="id", type="integer") * @ORM\Id * @ORM\GeneratedValue(strategy="AUTO") */ protected $id; /** * Set id * * @param integer $id * @return Id */ public function setId($id) { $this->id = $id; return $this; } /** * @var string * * @ORM\Column(name="name", type="string", length=255) */ protected $name; /** * Get id * * @return integer */ public function getId() { return $this->id; } /** * Set name * * @param string $name * @return City */ public function setName($name) { $this->name = $name; return $this; } /** * Get name * * @return string */ public function getName() { return $this->name; } }
如您所见,您必须为您的实体创建一个转换器。
<?php namespace Name\NameBundle\Entity\Transformer\EntityTransformer; use Elastica\Document; use Name\NameBundle\Entity\YourEntityClassName; class YourEntityClassName { /** * @param YourEntityClassName $yourEntityClassName * @return Document */ public function transform(YourEntityClassName $yourEntityClassName) { $identifier = $yourEntityClassName->getId(); $values = array( 'name' => $yourEntityClassName->getName(), 'date' => (new \DateTime('now'))->format('c'), ); //Create a document to index $document = new Document($identifier, $values); return $document; } }
用法
如果设置了 auto_event,您在实体创建、更新和删除时无需做任何事情。
您可以使用 HeadooElasticService 和 Ruflin 来调用 Elastic。以下是在控制器中的示例。
use Elastica\Query; use Elastica\Search; $elasticClient = $this->container->get('headoo.elasticsearch.helper')->getClient('localhost'); $search = new Search($elasticClient); //We will search in our index defined in mapping $search->addIndex('indexname'); $query = new Query(); $boolQuery = new Query\BoolQuery(); $queryMatch = new Query\Match(); $queryMatch->setFieldQuery('name', 'whatever'); $boolQuery->addMust($queryMatch); $query->setQuery($boolQuery); $resultSet = $search->search($query);
有关查询 Elastic 的更多信息,请参阅Ruflin elastica.io
如果没有设置 auto_event,您可以像这样监听 headoo.elasticsearch.event
name.elasticsearch.listener: class: Name\NameBundle\EventListener\ElasticSearchListener tags: - { name: kernel.event_listener, event: headoo.elasticsearch.event, method: onElasticEntityAction }
并在您的 EventListener 类中
<?php namespace Name\NameBundle\EventListener; use Headoo\ElasticSearchBundle\Event\ElasticSearchEvent; class ElasticSearchListener { /** * @param ElasticSearchEvent $event */ public function onElasticEntityAction(ElasticSearchEvent $event) { //Action can be persist, update and delete $action = $event->getAction(); //Your Doctrine Entity $entity = $event->getEntity(); } }
填充命令
小心:您必须在第一次填充类型或所有类型时在命令上设置 --reset 标志。
在配置了实体之后,您可能希望使它们在 ElasticSearch 中可用。您必须使用 php app/console headoo:elastic:populate
(对于 Symfony 2)或 php bin/console headoo:elastic:populate
(对于 Symfony 3)来进行操作。不同的选项是可用的。
- --limit=int : 集合的限制
- --offset=int : 集合的偏移量
- --type=string : 对象的名称(在我们的示例中是 YourEntityClassName)
- --threads=int : 您想要使用的线程数。如果使用它,则限制将不可用,并且您必须设置一个批处理。
- --reset : 重置索引。小心,您的 Elastic 集群中的所有数据都将丢失
- --batch=int : 每个线程的集合长度。仅与线程一起使用此选项
流亡命令
此命令检查 ElasticSearch 中的每个文档是否仍与 Doctrine 中的实体相关联。如果没有,此命令将删除 ES 中的孤儿文档。
提醒:Doctrine 和 ES 应始终一致(使用 'auto_event' 选项,如果没有该选项,可能会有轻微的延迟)。
如果此命令发现未关联的文档,会询问您原因!
- --limit=int : 集合的限制
- --offset=int : 集合的偏移量
- --type=string : 对象的名称(在我们的示例中是 YourEntityClassName)
- --batch=int : 每个线程的集合长度。仅与线程一起使用此选项
- --dry-run : 仅测试。不要从 ES 中删除任何文档
- --verbose : 使输出更加详细
安全
如果您发现安全漏洞,请通过电子邮件联系,而不是使用问题跟踪器。所有安全漏洞都将得到迅速解决。
独立测试
如何测试
- 克隆仓库:
$ sudo git clone https://github.com/Headoo/ElasticSearchBundle.git
- 进入目录:
$ cd ElasticSearchBundle/
- 按照这里说明安装 composer:https://getcomposer.org.cn/download/
- 运行 composer update:
$ ./composer.phar update
- 运行测试:
$ ./vendor/bin/phpunit
许可证
此 Bundle 是开源软件,许可证为 MIT 许可证。