Use Python Elasticsearch Client with Django Application

Bashayr Alabdullah
3 min readFeb 22, 2020

This post shows how to insert the information from Django app to Elasticsearch using Django command. We assume the Elasticsearch and Kibana have been installed, if not please click here to install Elasticsearch and here for Kibana installation.

Setting up Django requirements

Install elasticsearch-py:

pip3 install elasticsearch

My project hierarchy:

Django rest framework project

You can find the source code in my github.

Create python package management/commands to write Django command of inserting data to the Elasticsearch. To reduces overhead and can greatly increase indexing speed, we will use Bulk insert.

from __future__ import unicode_literals

from django.core.management import BaseCommand
from elasticsearch import Elasticsearch
from elasticsearch import helpers

from users.models import Users


class Command(BaseCommand):
help = ''' Insert Data to Elasticsearch '''

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.model_name = Users
self.es = Elasticsearch()

def handle(self, *args, **options):
# Bulk inserts
actions = [
{
"_index": "users",
"_type": "doc",
"_source": {
'id': user.id,
'username': user.username,
'first_name': user.first_name,
'last_name': user.last_name,
'email': user.email
}

}
for user in Users.objects.all()
]
helpers.bulk(self.es, actions)

Run Django Command

Don’t forget to run Elasticsearch and Kibana!

python manage.py es_indexes

Now, let’s take a look at how Elasticsearch has interpreted our document structure, by requesting the mapping for the users type:

GET /users/_mapping{
"users" : {
"mappings" : {
"properties" : {
"email" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"first_name" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"id" : {
"type" : "long"
},
"last_name" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"username" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
}
}
}
}

search about some user:

GET /users/_search?q=username=noor{
"took" : 17,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 1.9924302,
"hits" : [
{
"_index" : "users",
"_type" : "doc",
"_id" : "iN-obnABhvwnyK44MMto",
"_score" : 1.9924302,
"_source" : {
"id" : 2,
"username" : "noor",
"first_name" : "Noor",
"last_name" : "Ali",
"email" : "noor@example.com"
}
}
]
}
}

and more good stuff :)

I hope you find this post is helpful… have a nice day!

References:

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Bashayr Alabdullah
Bashayr Alabdullah

Written by Bashayr Alabdullah

Salam, I'm Tech Engineer 🚀. I blend technology insights, inspiration, and fun in my feed. #innovation #discovery 🥑🎶🎯🌻 *my personal -own- thoughts*

No responses yet

Write a response