Package: elasticsearch-fabric
This package provides a unified command line interface to Elasticsearch
in Fabric.InstallationThe current release, published on PyPI, can be installed using the
following command:$pipinstallelasticsearch-fabricConfigurationTasksIf you plan to use the built-in tasks, include the module in your
fabfile module (e.g. fabfile.py). Most likely you might want to assign
an alias for the task namespace:fromesfabricimporttasksasesEnvironmentelasticsearch_clients: Customize elasticsearch client
configurations.elasticsearch_alias: Default Elasticsearch client alias in
elasticsearch_clients. default “default”elasticsearch_dest_alias: Reindex dest Elasticsearch client alias
in elasticsearch_clients. default elasticsearch_aliasExamples# cat fabfile.pyfromfabric.apiimportenvfromelasticsearchimportElasticsearchfromesfabricimporttasksasesenv.elasticsearch_clients={"default":Elasticsearch(**{"host":"localhost","port":9200,"send_get_body_as":"POST"}),"example":Elasticsearch(**{"host":"search.example.org","port":443,"send_get_body_as":"POST","use_ssl":True,"verify_certs":True})}Elasticsearch with ShieldYou can configure the client to use basic authentication:# cat fabfile.pyfromfabric.apiimportenvfromelasticsearchimportElasticsearchfromesfabricimporttasksasesenv.elasticsearch_clients={"default":Elasticsearch(**{"host":"localhost","port":9200,"send_get_body_as":"POST","http_auth":('user','secret')})}Running on AWS with IAM# cat fabfile.pyfromfabric.apiimportenvfromelasticsearchimportElasticsearchfromelasticsearchimportRequestsHttpConnectionfromrequests_aws4authimportAWS4Authfromesfabricimporttasksasesawsauth=AWS4Auth(YOUR_ACCESS_KEY,YOUR_SECRET_KEY,REGION,'es')env.elasticsearch_clients={"default":Elasticsearch(**{"host":"YOURHOST.us-east-1.es.amazonaws.com","port":443,"send_get_body_as":"POST","http_auth":awsauth,"use_ssl":True,"verify_certs":True,"connection_class":RequestsHttpConnection})}Checking the setupFor checking if everything is set up properly, you can run the included
taskinfo. For example, running$fabes.infoyou can show a similar result:{"cluster_name":"elasticsearch","tagline":"You Know, for Search","version":{"lucene_version":"5.5.0","build_hash":"218bdf10790eef486ff2c41a3df5cfa32dadcfde","number":"2.3.3","build_timestamp":"2016-05-17T15:40:04Z","build_snapshot":false},"name":"Ares"}Done.Example useYou can do this for example with the following command:# -------------------------------------------------------------
# by Default connect to localhost:9200$fabes.info{"cluster_name":"elasticsearch","tagline":"You Know, for Search","version":{"lucene_version":"5.5.0","build_hash":"218bdf10790eef486ff2c41a3df5cfa32dadcfde","number":"2.3.3","build_timestamp":"2016-05-17T15:40:04Z","build_snapshot":false},"name":"Ares"}Done.# -------------------------------------------------------------
# index a document
#
# $ cat doc.json
# {
#   "title": "Hello Elasticsearch",
#   "description": "elasticsearch fabric test"
# }$catdoc.json|fabes.index:index=blog,doc_type=posts,id=1{"_type":"posts","created":true,"_shards":{"successful":1,"failed":0,"total":2},"_version":1,"_index":"blog","_id":"1"}Done.# -------------------------------------------------------------
# get the document.$fabes.get:index=blog,doc_type=posts,id=1{"_type":"posts","_source":{"description":"elasticsearch fabric test","title":"Hello Elasticsearch"},"_index":"blog","_version":1,"found":true,"_id":"1"}Done.# -------------------------------------------------------------
# simple query search.$fabes.search:q="title:hello"{"hits":{"hits":[{"_score":0.19178301,"_type":"posts","_id":"1","_source":{"description":"elasticsearch fabric test","title":"Hello Elasticsearch"},"_index":"blog"}],"total":1,"max_score":0.19178301},"_shards":{"successful":26,"failed":0,"total":26},"took":4,"timed_out":false}Done.# -------------------------------------------------------------
# request body search.
#
# $ cat query.json
# {
#   "query": {
#     "match": {
#       "title": "hello"
#     }
#   }
# }$catquery.json|fabes.search{"hits":{"hits":[{"_score":0.19178301,"_type":"posts","_id":"1","_source":{"description":"elasticsearch fabric test","title":"Hello Elasticsearch"},"_index":"blog"}],"total":1,"max_score":0.19178301},"_shards":{"successful":26,"failed":0,"total":26},"took":8,"timed_out":false}Done.# -------------------------------------------------------------
# change number of replicas
#
# cat indices$fabes.cat.indiceshealthstatusindexprirepdocs.countdocs.deletedstore.sizepri.store.sizeyellowopenblog51103.9kb3.9kb# change number of replicas$fabes.helpers.change_replicas:index=blog,number_of_replicas=0{"acknowledged":true}# cat indices$fabes.cat.indices:v=1healthstatusindexprirepdocs.countdocs.deletedstore.sizepri.store.sizegreenopenblog50103.9kb3.9kb# -------------------------------------------------------------
# reindex blog to blog2$fabes.helpers.reindex:source_index=blog,dest_index=blog2{"dest":{"index":"blog2","host":"http://localhost:9200"},"source":{"index":"blog","host":"http://localhost:9200"},"errors":0,"success":1}Done.# cat indices$fabes.cat.indices:v=1healthstatusindexprirepdocs.countdocs.deletedstore.sizepri.store.sizeyellowopenblog251103.7kb3.7kbgreenopenblog50103.9kb3.9kbClient selection# fabfile.pyfromesfabricimporttasksasesfromesfabric.tasksimportclient_selectionenv.elasticsearch_clients={"client1":Elasticsearch(**{...}),"client2":Elasticsearch(**{...})}$fabc:client2es.infocis client_selection aliasLoggingyou can enable the elasticsearch.trace logger and have it log a shell
transcript of your session using curl:# fabfile.pyimportloggingtracer=logging.getLogger('elasticsearch.trace')tracer.setLevel(logging.DEBUG)tracer.addHandler(logging.FileHandler('/tmp/elasticsearch-py.sh'))Custom Taskfromesfabricimporttasksasesfromfabric.apiimportexecute,task@taskdefchange_replicas(number_of_replicas=1):execute(es.cat.indices,v=1)execute(es.helpers.change_replicas,number_of_replicas=number_of_replicas)execute(es.cat.indices,v=1)run task:$fabchange_replicas:10Available commandsThe following command will show a list of avaliable commands.$fab-lAvailable commandses.bulkes.ces.clear_scrolles.client_selectiones.countes.count_percolatees.createes.deletees.delete_by_queryes.delete_scriptes.delete_templatees.existses.explaines.field_statses.getes.get_scriptes.get_sourcees.get_templatees.indexes.infoes.mgetes.mpercolatees.msearches.msearch_templatees.mtermvectorses.percolatees.pinges.put_scriptes.put_templatees.reindexes.reindex_rethrottlees.render_search_templatees.scrolles.searches.search_shardses.search_templatees.suggestes.termvectorses.updatees.update_by_queryes.cat.aliaseses.cat.allocationes.cat.countes.cat.fielddataes.cat.healthes.cat.indiceses.cat.masteres.cat.nodeattrses.cat.nodeses.cat.pending_taskses.cat.pluginses.cat.recoveryes.cat.repositorieses.cat.segmentses.cat.shardses.cat.snapshotses.cat.thread_pooles.cluster.allocation_explaines.cluster.get_settingses.cluster.healthes.cluster.pending_taskses.cluster.put_settingses.cluster.reroutees.cluster.statees.cluster.statses.helpers.bulkes.helpers.change_replicases.helpers.reindexes.helpers.scanes.indices.analyzees.indices.clear_cachees.indices.closees.indices.createes.indices.deletees.indices.delete_aliases.indices.delete_templatees.indices.existses.indices.exists_aliases.indices.exists_templatees.indices.exists_typees.indices.flushes.indices.flush_syncedes.indices.forcemergees.indices.getes.indices.get_aliases.indices.get_field_mappinges.indices.get_mappinges.indices.get_settingses.indices.get_templatees.indices.get_upgradees.indices.openes.indices.put_aliases.indices.put_mappinges.indices.put_settingses.indices.put_templatees.indices.recoveryes.indices.refreshes.indices.rolloveres.indices.segmentses.indices.shard_storeses.indices.shrinkes.indices.statses.indices.update_aliaseses.indices.upgradees.indices.validate_queryes.nodes.hot_threadses.nodes.infoes.nodes.statses.snapshot.createes.snapshot.create_repositoryes.snapshot.deletees.snapshot.delete_repositoryes.snapshot.getes.snapshot.get_repositoryes.snapshot.restorees.snapshot.statuses.snapshot.verify_repository