Blog available for sell
This blog is available for sale. Please 'contact us' if interested.
Advertise with us
kibana elastic-search   0   10478
Elastic Search with Kibana and Django - part 2

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 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/ file.

from django.db import models

class Question(models.Model):
    author = models.CharField(max_length=100)
    question_text = models.CharField(max_length=1000)
    pub_date = models.DateTimeField('date published')

- Register your model in

from django.contrib import admin
from .models import Question

# Register your models here.

# Need to register Question so it shows up in the admin

- Add your app 'polls' in installed apps in file.

# Application definition


- Update your database configurations in file. We will be using MySQL. Create a database with name 'elasticsearchdb'.

    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'elasticsearchdb',
        'USER': 'root',
        'PASSWORD': 'root',
        'HOST': '',
        'PORT': '3306',

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

python makemigrations
python migrate

- Create a super user to login in admin panel.

python createsuperuser

- Now run server with below command

python runserver

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

elastic search with kibana and django part 2elastic search with kibana and django part 2

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. 

pip install elasticsearch-dsl

 - Create a new file  in your app 'polls'. Paste below content in it. Read comments to know what part of code is doing what?

from elasticsearch_dsl.connections import connections
from elasticsearch_dsl import DocType, Text, Date

# creates a global connection to elastic search

# defines what needs to index in elastic search
class QuestionsIndex(DocType):
    author = Text()
    question_text = Text()
    pub_date = Date()

    class Meta:
        # name of index. Will be used in search
        index = 'questions-index'

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 in polls app directory.

from .models import Question
from django.db.models.signals import post_save
from django.dispatch import receiver

@receiver(post_save, sender=Question)
def index_question(sender, instance, **kwargs):

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.

from django.db import models
from .elastic_search_connection import QuestionsIndex

class Question(models.Model):
    author = models.CharField(max_length=100)
    question_text = models.CharField(max_length=1000)
    pub_date = models.DateTimeField('date published')

    # indexing method of Question model
    def indexing(self):
       obj = QuestionsIndex(
            meta = {
            author =,
            pub_date = self.pub_date,
            question_text = self.question_text
       return obj.to_dict(include_meta=True)

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

from django.apps import AppConfig

class PollsConfig(AppConfig):
    name = 'polls'

    def ready(self):
        import polls.signals

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

default_app_config = 'polls.apps.PollsConfig'

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  in your browser.

elastic search with kibana and django part 2

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.

elastic search with kibana and django part 2

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.

elastic search with kibana and django part 2

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. 

elastic search with kibana and django part 2 Pie chart showing distribution of Authors (column in questions model)

Happy learning.

kibana elastic-search   0   10478
0 comments on 'Elastic Search With Kibana And Django - Part 2'
Login to comment

Related Articles:
Elastic Search with Kibana and Django
Using elastic search in django projects with the help of kibana. setting up elastic search with kibana and Django. Using indexed searching using elastic....
DigitalOcean Referral Badge

© 2022-2023 Python Circle   Contact   Sponsor   Archive   Sitemap