Table of Contents

ElasticSearch Essential Commands Using Python - ElasticSearch-dsl

There are three main Packages in Python for executing Elasticsearch commands...

  • Pyelasticsearch
  • Elasticsearch
  • ElasticSearch-dsl

In this post, I will use Elasticsearch and Elasticsearch-dsl

Lets install the packages first...

pip install -U elasticsearch 
pip install -U elasticsearch_dsl

Mainly we will use elasticsearch_dsl because most of the commands can be executed as it is..

Lets make the connection first...

from elasticsearch_dsl import connections
from elasticsearch_dsl import Index
es = connections.create_connection(hosts='localhost:9200')

Elasticsearch Create Index

i = Index('index1')

To check if index created successfully, run following command...

Elasticsearch Check Index Exists


Before Creating the index we can also insert mapping for the fields. Mapping is defined to let Elasticsearch index Fields with specific types. For example suppose my data has three fields category, rating and title and I want to store my fields app, title and category as keywords and rating which is numeric field as float. Lets create a mapping dictionary below.

Elasticsearch Create Mapping

mapping = {'properties': {'category': {'type': 'keyword'},
   'rating': {'type': 'float'},
   'app': {'type':'keyword'},
   'title': {'type': 'keyword'}}}

Lets add the above mapping to our index1.


To remove the index use following command...


Now we have the Index created successfully.

Lets add a document to the index.

Elasticsearch Index Documents

doc = {'field1':field, 'field2':field}

To bulk Index, we will use the library Elasticsearch.

from elasticsearch import helpers

helpers.bulk can be used to bulk index the docs. Lets prepare the list of documents first.

docs = []
doc1 = {'field1':'test1','field2':'test2'}
doc2 =  {'field1':'test3','field2':'test4'}
newdocs = []
for doc in docs:
	newdoc = {'_index':'index1','_type':'doc_type1','_source':doc}
helpers.bulk(es, newdocs, index='index1')

How to do textual search in Elasticsearch

Lets import following packages...

from elasticsearch_dsl import Q
from elasticsearch_dsl import Search

Q is to build the query. Lets build a simple query.

q = Q("match", query='viber', fields=['package'])

Lets instantiate the Search object and then build our query.

s = Search(using=es)

At this point we have not executed the query. Let us execute the query now...

response = s.execute()

response is not a dictionary but elasticsearch DSL object. If we do "type" of response, we can find out.

In [330]: type(response)
Out[330]: elasticsearch_dsl.response.Response

response.hits has the list of all the documents that Elasticsearch returned. We can go over the list in a loop.

doc1 = response.hits[0]

As we see above, it is a list but elasticsearch dsl list object.

We can get our fields out as we do through the dictionary as shown below.


Related Posts