Optimizing Search: Integrating Elasticsearch in Django Applications

Blog
Spread the love

What is Elasticsearch?

Elasticsearch is a powerful search engine built upon the Lucene library. This software offers a distributed, multi-tenant capable platform for conducting full-text searches through HTTP web interfaces, handling JSON documents without strict schemas. Developed primarily in Java, Elasticsearch has gained immense popularity since its launch in 2010 and is widely recognized as a leading search engine.

Its applications encompass a variety of functions, including log analytics, full-text search, security intelligence, business analytics, and operational intelligence. Esteemed companies like eBay, Facebook, Uber, and GitHub utilise Elasticsearch to construct their products, integrating features such as search, aggregation, analytics, and more.

What is Elasticsearch used for?

Elasticsearch allows you to store, search, and analyse huge volumes of data quickly and in near real-time and give back answers in milliseconds. It’s able to achieve fast search responses because instead of searching the text directly, it searches an index.

Elasticsearch – some basic concepts:

  • Index – a collection of different types of documents and document properties. For example, a document set may contain the data of a social networking application.
  • Type/Mapping − a collection of documents sharing a set of standard fields in the same index. For example, an index contains data from a social networking application; there can be a specific type for user profile data, a different kind for messaging data, and yet another for comments data.
  • Document − a collection of fields defined in the JSON format in a specific manner. Every document belongs to a type and resides inside an index. Every document is associated with a unique identifier called the UID.
  • Field – Elasticsearch fields can include multiple values of the same type (essentially a list). In SQL, on the other hand, a column can contain exactly one value of the said type.

Using Elasticsearch with Django:

To take advantage of Elasticsearch with Django, we’ll use a few beneficial packages:

  • Elasticsearch DSL – a high-level library that helps write and run queries against Elasticsearch. It’s built on top of the official low-level client (elasticsearch-py).
  • Django Elasticsearch DSL – a package that allows easy integration and configuration of Elasticsearch with Django. It’s built as a thin wrapper around elasticsearch-dsl-py so that you can use all the features developed by the elasticsearch-dsl-py team.
  • Django Elasticsearch DSL DRF – integrates Elasticsearch DSL and the Django REST framework. It provides us with Elasticsearch using API most efficiently.

Download and install the .zip package

C:\elasticsearch-8.9.0\bin>elasticsearch.bat

fter running the file for the very first time, we will get a password and certificate. In our project, which is for demonstration purposes or local development, it is not needed. So, we will disable the security-related configuration by editing a file named elasticsearch.yml in the config directory. For this, we need to look for an entry ‘xpack.security.enabled,’ which will be true by default. We need to change this to false. After this modification, there will be no issues related to security while connecting to Elasticsearch. This can be verified by visiting the Elasticsearch URL:

https://localhost:9200.

Installing Elasticsearch and Django-Elasticsearch Integration:

Step 1:

  • Install Elasticsearch and the Python client for Elasticsearch:pip install elasticsearch
  • Install Django Elasticsearch DSL:pip install django-elasticsearch-dsl

Configuring Django Settings:

Step 2:

  • Then add django_elasticsearch_dsl to the INSTALLED_APPS

Step 3:

  • You must define ELASTICSEARCH_DSL in your django settings.

Step 4:

  • Define the Model

  • Don’t forget to run migrations and migrate:
  • python manage.py makemigrations
  • python manage.py migrate
  • To make this model work with Elasticsearch, create a subclass of django_elasticsearch_dsl.Document, create a class Index inside the Document class to define your Elasticsearch indices, names, settings etc and at last register the class using registry.register_document decorator. It is required to define the Document class in documents.py in your app directory

Step 5:Indexing Data in document.py

  • Create a file named documents.py in the blog app to define the Elasticsearch document:

  • Don’t forget to run migrations and migrate:
  • python manage.py makemigrations
  • python manage.py migrate
  • To make this model work with Elasticsearch, create a subclass of django_elasticsearch_dsl.Document, create a class Index inside the Document class to define your Elasticsearch indices, names, settings etc and at last register the class using registry.register_document decorator. It is required to define the Document class in documents.py in your app directory

Populate:

  • To create and populate the Elasticsearch index and mapping use the search_index command: $python manage.py search_index — rebuild
  • For more help use $python manage.py search_index — help command

Step 6: Search Data in Elasticsearch

  • You can now search for blog posts using Elasticsearch. Here’s a simple search function in views.py:

Step 8: URL Configuration (urls.py)

After making a search API request with the query “powerful,” the resulting JSON output with matched titles is structured as follows:.

Output:

The output includes a total of 2 hits with the respective titles, content, and publication dates matching the query “powerful.” Each hit provides information about the title, content, and metadata of the posts in the Elasticsearch index.

Manisha Kumari

Manisha Kumari

2024-05-16

0

Leave a Reply

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

Find More Blogs

Customize lead qualification for a seamless experience

Spread the love When the 2024 Release Wave 2 was launched,

Create a Custom Console Command in Spryker

Spread the love In Spryker, developers can create custom

SALESFORCE

Spread the love Salesforce is a cloud-based software

CRUD Operation in Spryker Backoffice with a Custom Module

Spread the love Spryker is a highly flexible and modular

How to create custom module on Spryker for Backoffice?

Spread the love Creating a custom module in Spryker

How to Install Spryker Project with Docker on Local Machine

Spread the love Spryker is a flexible and scalable commerce

How to Add a Custom Tab to the Customer Order Detail Page in Magento 2

Spread the love Adding a custom tab to the Order Detail

Exception Handling in Java

Spread the love Introduction – Exception:An unwanted

How to add custom less file in Magento2

Spread the love Source files *.css included into layout

Spring Boot Microservices

Spread the love Microservices architecture has become

bluethinkinc Blogs