ppokatilo/elastica-entity-mapping-bundle

Automatically updates elasticsearch mappings

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

README

Latest Stable Version Total Downloads Latest Unstable Version License

An Symfony2 bundle that automatically updates your Elasticsearch mappings or notifies you of changes.

How it works

An elastica client service is modified to be constructed using a factory. The factory reads Composer's autoload files to know about all your dependencies. It will then scan each directory for a subdirectory called Entity and search the PHP files in that subdirectory for the @ElasticsearchMapping annotation.

When instantiating the elastica client service, the factory will first check if the mapping of registered entities on disk differs from that in Elasticsearch. If so, it will either throw an exception or try to update the mapping automatically.

Example usage

  • 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
    {
      // ...
    }