by Ben Stinton - 17th April 2015
Elasticsearch is an incredibly powerful tool which can really help speed up listings and search in your web application. However, as your app grows and your types become more complex it becomes even more important to improve your testing and make further development simpler. Aliases allow you to point different indexes for different environments, and also to seamlessly switch between them.
For this post I'll assume you are using Symfony 2.5.*, FOSElastica bundle 3.0.0alpha6 and Elasticsearch 0.9 to 1.4. For other versions these steps may need checking. I'll also assume that you have setup a test database and bootstrap that works with PHPUnit.
The first step we need to do is create the main indexes, I will use 'index_v1' for prod and dev environments to start with, and 'index_test_v1' for the test environment. i'm going to use the following commands rather than the FOSElastica populate commands:
$ curl -XPUT 'http://localhost:9200/index_v1' $ curl -XPUT 'http://localhost:9200/index_test_v1'
# app/config/fos_elastica.yml fos_elastica: clients: default: { host: 127.0.0.1, port: 9200 } serializer: callback_class: FOS\ElasticaBundle\Serializer\Callback serializer: serializer indexes: index: index_name: index_%kernel.environment% client: default types: user:
$ curl -XPOST 'http://localhost:9200/_aliases' -d '{"actions" : [{ "add" : { "index" : "index_v1", "alias" : "index_prod" } } ]}' $ curl -XPOST 'http://localhost:9200/_aliases' -d '{"actions" : [{ "add" : { "index" : "index_v1", "alias" : "index_dev" } } ]}'
$ curl -XPOST 'http://localhost:9200/_aliases' -d '{"actions" : [{ "add" : { "index" : "index_test_v1", "alias" : "index_test" } } ]}'
$ curl -XGET 'localhost:9200/index_v1/_alias/*' $ curl -XGET 'localhost:9200/index_test_v1/_alias/*'
# for each type defined $ php app/console fos:elastica:populate --index="index" --type="user" --env="prod" --no-debug
# make sure the environment is set to test
$ curl -XPUT 'http://localhost:9200/index_v2' $ curl -XPUT 'http://localhost:9200/index_test_v2'
$ curl -XPOST 'http://localhost:9200/_aliases' -d '{ "actions" : [ { "remove" : { "index" : "index_v1", "alias" : "index_dev" } }, { "add" : { "index" : "index_v2", "alias" : "index_dev" } } ] }'
$ curl -XPOST 'http://localhost:9200/_aliases' -d '{ "actions" : [ { "remove" : { "index" : "index_test_v1", "alias" : "index_test" } }, { "add" : { "index" : "index_test_v2", "alias" : "index_test" } } ] }'
$ curl -XDELETE 'http://localhost:9200/index_v1'
Comments: Send us an Email