gbprod / elastica-provider-bundle
此包已被弃用且不再维护。未建议替代包。
使用Elastica轻松提供ElasticSearch数据的包
v1.1.1
2018-02-08 05:41 UTC
Requires
- ruflin/elastica: ^3.2|^5.0|^6.0
- symfony/console: ^2.7|^3.0|^4.0
- symfony/event-dispatcher: ^2.7|^3.0|^4.0
- symfony/expression-language: ^2.7|^3.0|^4.0
- symfony/framework-bundle: ^2.7|^3.0|^4.0
- symfony/yaml: ^2.7|^3.0|^4.0
Requires (Dev)
- phpunit/phpunit: ^5.7|^6.0
Suggests
- gbprod/elastica-bundle: Really simple bundle to use elastica within Symfony applications.
- gbprod/elastica-extra-bundle: Bundle providing extra elastica tools for managing indices and types settings.
This package is auto-updated.
Last update: 2021-08-07 10:44:34 UTC
README
使用Elastica轻松提供Elasticsearch数据的包。
安装
使用composer下载包
composer require gbprod/elastica-provider-bundle
在您的app/AppKernel.php
文件中声明
<?php // app/AppKernel.php public function registerBundles() { $bundles = array( new GBProd\ElasticaProviderBundle\ElasticaProviderBundle(), ); }
使用方法
配置Elastica客户端
gbprod_elastica_provider: default_client: 'elastica.default_client' # Elastica client service's name
您可以使用类似FOSElasticaBundle的包来创建Elastica客户端
- FOSElasticaBundle
服务名称将类似于fos_elastica.client.my_client
- 我的轻量级包ElasticaBundle
服务名称将类似于elastica.default_client
- DIY
创建Provider
<?php namespace GBProd\AcmeBundle\Provider; use GBProd\ElasticaProviderBundle\Provider\BulkProvider; class SuperHeroProvider extends BulkProvider { protected function populate() { $this->index( 'Spider-Man', // id of the document [ "name" => "Spider-Man", "description" => "Bitten by a radioactive spider, high school student Peter Parker gained the speed, strength and powers of a spider. Adopting the name Spider-Man, Peter hoped to start a career using his new abilities. Taught that with great power comes great responsibility, Spidey has vowed to use his powers to help people.", ] ); $this->update( 'Hulk', [ "name" => "Hulk", "description" => "Caught in a gamma bomb explosion while trying to save the life of a teenager, Dr. Bruce Banner was transformed into the incredibly powerful creature called the Hulk. An all too often misunderstood hero, the angrier the Hulk gets, the stronger the Hulk gets.", ] ); $this->create( 'Thor', [ "name" => "Thor", "description" => "As the Norse God of thunder and lightning, Thor wields one of the greatest weapons ever made, the enchanted hammer Mjolnir. While others have described Thor as an over-muscled, oafish imbecile, he's quite smart and compassionate. He's self-assured, and he would never, ever stop fighting for a worthwhile cause.", ] ); $this->delete('Captain America'); } public function count() { return 4; } }
注册Provider
# AcmeBundle/Resources/config/services.yml services: acme_bundle.superhero_provider: class: GBProd\AcmeBundle\Provider\SuperHeroprovider tags: - { name: elastica.provider, index: app, type: superheros }
提供数据
php app/console elasticsearch:provide app superheros
您也可以提供完整的索引
php app/console elasticsearch:provide app
或运行所有Provider
php app/console elasticsearch:provide
您可以设置特定客户端(如果不是默认客户端)来使用
php app/console elasticsearch:provide app superheros --client=client_service_name
使用doctrine的示例
<?php namespace GBProd\AcmeBundle\Provider; use GBProd\ElasticaProviderBundle\Provider\BulkProvider; use Doctrine\ORM\EntityManager; class SuperHeroprovider extends BulkProvider { private $em; public function __construct(EntityManager $em) { $this->em = $em; } protected function populate() { $query = $this->em->createQuery('SELECT s FROM AcmeBundle\Model\SuperHero s'); $results = $query->iterate(); foreach ($results as $row) { $this->index( $row[0], [ "name" => $row[0], "description" => $row[1], ] ); $this->em->detach($row[0]); } } public function count() { $query = $this->em ->createQuery('SELECT COUNT(s.id) FROM AcmeBundle\Model\SuperHero s') ; return $query->getSingleScalarResult(); } }
# AcmeBundle/Resources/config/services.yml services: acme_bundle.superhero_provider: class: GBProd\AcmeBundle\Provider\SuperHeroprovider arguments: - '@doctrine.orm.entity_manager' tags: - { name: elastica.provider, index: app, type: superheros }
更改批量大小
批量大小在向Elasticsearch提供数据时非常重要。请注意节点设置,设置合适的批量大小。默认批量大小为1000,您可以通过设置标签的批量条目来更改设置。
# AcmeBundle/Resources/config/services.yml services: acme_bundle.superhero_provider: class: GBProd\AcmeBundle\Provider\SuperHeroprovider calls: - ['changeBulkSize', 42] tags: - { name: elastica.provider, index: app, type: superheros }
或直接在Provider内部直接更改。
<?php namespace GBProd\AcmeBundle\Provider; use GBProd\ElasticaProviderBundle\Provider\BulkProvider; class SuperHeroprovider extends BulkProvider { public function __construct() { $this->changeBulkSize(42); } protected function populate() { // ... } }
关于count方法
实现count
方法不是强制性的,但它允许您在Provider运行时有一个不错的进度条。
<?php namespace GBProd\AcmeBundle\Provider; use GBProd\ElasticaProviderBundle\Provider\BulkProvider; class SuperHeroprovider extends BulkProvider { protected function populate() { // ... } public function count() { return 2; } }