Integration of Elastic Search and Django(A Python Framework) – Part three

Share On Facebook
Share On Google Plus
Share On Linkedin
Contact us

In the previous tutorials I made a simple django application, and made a custom command(seed_db) that populate the database with the dummy data, also take count as argument of how many products will be created. Now In this tutorial we will learn, how to index our database with the elastic search.

Elasticsearch, in simple words it is like a search engine provide capabilities like full text searching, filtering, faceting and many more with fast speed on non relational JSON like documents. So its like a private search for your web application.

Upto this level we had a simple django based application consist of the five models, one is built in model, i.e. User model from django.contrib.auth.models, others are Product, Category, Subcategory and the UserProfile model. We also implemented a custom command to populate database with the dummy data. So we can have any number of dummy records in our db, to make it indexed in elasticsearch. So thats the reason to make seed_db command.

Now I am going to make one more custom command i.e. index_db, to index the database to the elasticsearch. Before proceeding in this tutorial, make sure you have elasticserver(Elasticsearch 5.2)installed in your system amd running at localhost:9200. Refer here to install it. You might need JDK on your system installed to run elastic server. Just download the archive file of the elasticsearch, extract it and run command in your extracted elastic search folder.

your_elasticsearch_folder$ ./bin/elasticsearch
start elastic search server

Go to localhost:9200 in your browser to see it running.

elastic search running

Once you make it upto, then, install elasticsearch client in django by run command in your venv…

install elastic search in django

This is the client at the django side that will talk to the elasticserver running at localhost:9200. We need one mode module i.e. requests, which is needed by the elastic client at django for HTTP based communication to the elesticserver.

install requests

Add Settings to the elasticdjango/settings.py file

MAke a one mode sample command i.e.

remane the sample.py file in management folder to say index_db.py.

tree structure

Add following code to the index_db.py file.

Run command

index db

This way we index the db with the elasticsearch. Lets have a look in the code of index_db. I have function that checks for index if it is already exixting in the elasticsearch, if yes than it delete the exixting index and recreate it. To check out index go to http://localhost:9200/ecommerce/ and you will see the index info.

elastic server running

See there are two more functions one is push_db_to_index, it does the same work as its name implies, it prepare the data dict using function create_for_bulk. create_for_bulk function uses the Product instance method, i.e. es_repr(), es_repr() returns the object in the required json form, also I add the meta info to the respective record of the Product, this meta information like es_index_name, es_type_name is also I defined in the Meta of class Product in the code/models.py. I also use the django options like

and assign the values in the Class Meta of the Class Product as:

pay attention to es_mapping which is used to recreate the index, and also provide the desied format for the Product in the elastic search index.
This is the basic integration of the elastic search with the Django so far we are reading from the past three tutorials.

 Thanks for reading this tutorial. For the full code go to my github repo. For more tutorials Keep visiting this site and also check my Git repositories from https://github.com/tusharcoder?tab=repositories.

Leave a Reply

Your email address will not be published. Required fields are marked *