Elasticsearch with Django – Part 4
In previous articles (Here you can read: part-1, part-2 and part-3) we have seen that how queries work on Elasticsearch engine via terminal.
Now let’s discuss about integration with Django application.
Let’s start with creating a new Django project.
Setting up a Django Project
First you create a virtual environment with virtualenv elastic_env and enter it with source elastic_env/bin/activate in order to keep everything contained. Then you install some packages:
pip3 install django
pip3 install elasticsearch-dsl
Now let’s create a new django project, run:
django-admin startproject django_elastic_project
cd django_elastic_project
python3 manage.py startapp elasticsearchapp
After creating the new django project, we need to create a model that we will use. I choose to go with car example. In models.py
place the following code:
Don’t forget to add elasticsearch app to INSTALLED_APPS in settings.py and register your new BookPublish model in admin.py like this:
Now apply python3 manage.py make migration
and python3 manage.py migrate
and then python3 manage.py create superuser
to create the database and an admin account.
Now Finally run python3 manage.py runserver
, now let’s check admin site, go to http://localhost:8000/admin/ and log in, and check our BookPublish model there. Now create data in that model.
Connect Elasticsearch with django
We can start this with creating a new search.py file in our elasticsearch app directory. Now create a connection from our django application to Elasticsearch Engine. Add this code in search.py file:
Now we have a global connection of Elasticsearch. Add this code in utils.py
Actually, DocType works as a wrapper to enable you to write an index like a model. Text and Date are the fields which are used for the correct format when they get indexed.
Now we need to actually create the mapping of our newly created BookPublishIndex in Elasticsearch using bulk_indexing()
method.
the bulk command is included in helpers and it is automatically added at the time of installation. Add this code in search.py :
Actually this function work as, when you create a new object in BookPublish then this will init by .indexing()
and index with Elasticsearch. It is important to index all the entries which are newly added in a table, then only Elasticsearch can search the data by own.
Now let’s add indexing()
method in models.py
:
Note: Before this, please make sure Elasticsearch engine is live.
Let’s try to indexing all the existing objects with Elasticsearch. By running python3 manage.py shell
you go into the Django shell and import your search.py
with from elasticsearchapp.search import *
and then run bulk_indexing()
to index all the BookPublish objects.
Simple Search
Now lets make a simple search function in search.py
to find all book published filtered by author name
:
Let’s try the search out. In the shell: from elasticsearchapp.search import *
and run bulk_indexing()
and then search(author="author_name")
:
>>> from elasticsearchapp.search import *
>>> bulk_indexing()
>>> search(author="author")
<Response: [<Hit(book-publish-index/book_publish_index/5): {'posted_date': '2018–06–21', 'text': 'latest dfgju', 'title...}>]>
Conclusion
I explained how to connect a Django model to Elasticsearch for indexing and simple searching, but there is so much more that Elasticsearch can do (We have seen in previous blogs). I recommend reading on their website and exploring what other possibilities exist.
Here I am finishing my Elasticsearch With Django blog series.
If you liked this article or have a comment or suggestions, please feel free to leave a message below. And stay tuned for more interesting stuff!
Click here for more details…
At BoTree Technologies, we build enterprise applications with our Django team of 20+ engineers.
We also specialize in RPA, AI, Python, Ruby on Rails, JavaScript and ReactJS.