Elasticsearch python client

Elasticsearch python client and sample code for indices creation and deletion, document indexing, and searching. For elasticsearch installation, see post elk-getting-started-notes.

Installation

$ sudo pip install elasticsearch

Indices creation and deletion

from elasticsearch import Elasticsearch as ES

es = ES() # by default it connects to http://localhost:9200

# create index (i.e. database compared against RDBMS)
es.indices.create(index="my-index")
# ignore 400 status caused by IndexAlreadyExistsException
es.indices.create(index="my-index", ignore=400)

# delete index
# index = "*" will delete all indices
es.indices.delete(index="my-index")

Document indexing

data = {
  'summary': 'hey, elasticsearch rocks!',
  'description': 'a post introducing how elasticsearch works...',
  'date_creation': datetime.now(),
  'author': 'myself'
}

es = ES()

# index    => the index created earlier
# doc_type => i.e. table compared against RDMBS
# body     => the document to index: dict, map, or json like data
# id       => the document id, if not of interest, use uuid to generate random
es.index(index="my-index", doc_type='post', body=data, id=uuid.uuid1().hex)

Document searching

es = ES()

# q        => the query to search in elasticsearch, use "*" to search all
# Other options like:
# index    => the index to search in, leave empty means search in all indices
# doc_type => the doc_type to search in, leave empty means search in all types
results = es.search(q="some-query")

# get count of hits
hits_total = results['hits']['total']

# get list of hits, where each hit is a dict
hits_list = results['hits']['hits']

Other common operations

# list current elasticsearch cluster info
es.info()

# list all indices
es.cat.indices()

See Python Elasticsearch Client Docs

elasticsearch python