ppokatilo/elastica-entity-mapping-bundle

自动更新 Elasticsearch 映射

v0.5.0 2014-12-21 05:51 UTC

README

Latest Stable Version Total Downloads Latest Unstable Version License

一个 Symfony2 扩展包,可以自动更新您的 Elasticsearch 映射或者通知您有变化。

工作原理

将 elastica 客户端服务修改为使用工厂构造。工厂读取 Composer 的自动加载文件以了解所有依赖项。然后,它会扫描每个目录中的名为 Entity 的子目录,并在该子目录中的 PHP 文件中搜索 @ElasticsearchMapping 注释。

在实例化 elastica 客户端服务时,工厂将首先检查磁盘上注册的实体映射与 Elasticsearch 中的映射是否不同。如果是,它将抛出异常或尝试自动更新映射。

示例用法

  • app/AppKernel.php

    class AppKernel extends Kernel
    {
      public function registerBundles()
      {
          $bundles = array(
            // ...
          );
    
          if (in_array($this->getEnvironment(), array('dev', 'test'))) {
              $bundles[] = new SHyx0rmZ\ElasticaEntityMapping\ElasticaEntityMappingBundle();
          }
    
          return $bundles;
      }
    }
    
    // ...
  • app/config/config_dev.yml

    elastica_entity_mapping:
      clients:
        -
          service: elastica.client
          update: false # throw exceptions instead of updating mapping automatically
          indices:
            -
              name: %elastica_index_name%
              alias: dev
              settings: vendor/example/entitybundle/settings.json
            -
              name: my_other_index
              alias: other
  • vendor/example/entitybundle/ExampleEntity.php

    /**
     * @ElasticsearchMapping(file="./example_entity.json", indices="dev,other")
     **/
    class ExampleEntity
    {
      // ...
    }