Elastic Search with Kibana and Django – part 2

elastic search with django

In previous part, we installed Elasticsearch and Kibana. Now we will create a Django project and will link the elastic search with the project.

Django Project:

You can follow the official Django tutorials to create and start a Django project.  Or you follow below steps. We will be following the official documentation, but only the necessary steps.

– Make sure python 3 is installed on your machine. Although not mandatory but it is recommended to use virtual environment.

Install Django. Setup a database. We will be using MySQL.

– Create a project.
$ django-admin startproject elasticsearchdjango

– Change directory to elasticsearch. Create a new app ‘poll’.
$ python manage.py startapp polls

– If you are using virtual environment than install the dependencies using pip install -r req.txt  file after saving below lines in req.txt  file.

– Create a model in polls/models.py file.

– Register your model in admin.py

– Add your app ‘polls’ in installed apps in settings.py file.

– Update your database configurations in settings.py file. We will be using MySQL. Create a database with name ‘elasticsearchdb’.

– Go to terminal, run below commands to create tables in DB.

– Create a super user to login in admin panel.

– Now run server with below command

– Go to your browser http://localhost:8000/admin/  , login to admin panel and add question in Questions table.

Now our Django project skeleton is ready. Lets connect Django Project to Elastic Search.

Connecting to Elastic Search:

– Install the python package elsaticsearch-dsl using pip.

– Create a new file elastic_search_connection.py  in your app ‘polls’. Paste below content in it. Read comments to know what part of code is doing what?

So we are creating a global connection to elastic-search here. Then we are defining what we are indexing in elastic-search. Name of the index will be 'questions-index' .

– Now we will create a signal which will be triggered on every save event of question model instance. For this create a file signals.py  in polls app directory.

This signal works as receiver on each post_save event where sender of signal is Question model. Everytime post_save event is triggered, it is received by this signal and it indexes the instance of Question model.

– We need to create indexing method in Question model for above code to work. Code in model class should look as below.

– We also need to inform Django project that we are using signals. Add below config code to app.py .

– Now we need to tell Django that we are using new config defined in app.py . Add below line in __init__.py file of polls app.

 

Now all the configurations are complete. On every save of model instance, data will be indexed in elastic-search. Restart the Django project server.

Now open a new terminal and start the elastic search as discussed in part 1 of this article.

Go to admin panel and save data in Question model. Now navigate to this url http://127.0.0.1:9200/questions-index/questions_index/6?pretty  in your browser.

elastic search index

questions-index is the name of index. questions_index is the name of class QustionsIndex in snake notation.

This complete code is available on Github.

Using Kibana to search, browse and visualise data:

We already installed and started the kibana in previous article. Go to the homepage of kibana which will be running on localhost:5601 .

Initially there is no index pattern configured and it will ask us to configure one.

kibana Django elastic search

Type the name of your index in index pattern input box. You can use wild cards. Once index patter is configured, you will see the data in matching index pattern.

index pattern kibana elastic search and django

Now you can start exploring your data.

– Search and browse your data interactively from the Discover page.
– Chart and map your data from the Visualise page.
– Create and view custom dashboards from the Dashboard page.

visualization in kibana, elastic search and django
Pie chart showing distribution of Authors (column in questions model)

 

Happy learning.

 

(Visited 956 times, 1 visits today)

You must read this :

2 thoughts on “Elastic Search with Kibana and Django – part 2”

Leave a Reply

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