Fundamentals 5 min read

Master Elasticsearch with Django: From Basics to Real‑World Integration

Learn what Elasticsearch is, its core concepts, and how to integrate it into a Django project, including installation, configuration, defining indices and documents, populating data, and performing searches, all illustrated with code examples for seamless full‑text search capabilities.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Master Elasticsearch with Django: From Basics to Real‑World Integration

What is Elasticsearch?

Elasticsearch is a search engine built on the Lucene library. It provides a distributed, multi‑tenant full‑text search engine with an HTTP web interface and schema‑free JSON documents, and it is developed in Java.

What are the uses of Elasticsearch?

Elasticsearch enables fast, near‑real‑time storage, search, and analysis of large volumes of data, returning results in milliseconds by searching indexes directly rather than raw text.

Basic concepts of Elasticsearch

Index – a collection of documents and their fields, e.g., data from a social‑network app.

Type/Mapping – a set of common fields shared by documents within the same index, such as separate types for user profiles, messages, and comments.

Document – a JSON‑formatted set of fields; each document belongs to a type and resides in an index, identified by a unique UID.

Field – an Elasticsearch field can hold multiple values of the same type (essentially a list), unlike a SQL column which holds a single value of its type.

Using Elasticsearch in Django

Install and configure the Django Elasticsearch DSL package: $ pip install django-elasticsearch-dsl Add django_elasticsearch_dsl to INSTALLED_APPS and define ELASTICSEARCH_DSL in Django settings, for example:

ELASTICSEARCH_DSL = {
    'default': {
        'hosts': 'localhost:9200'
    },
}

Declare the data to be indexed and create a model:

# models.py

class Category(models.Model):
    name = models.CharField(max_length=30)
    desc = models.CharField(max_length=100, blank=True)

    def __str__(self):
        return '%s' % (self.name)

To use this model with Elasticsearch, create a subclass of django_elasticsearch_dsl.Document, define an Index class for the index name and settings, and register the document with the registry.register_document decorator in documents.py:

from django_elasticsearch_dsl import Document
from django_elasticsearch_dsl.registries import registry
from .models import Category

@registry.register_document
class CategoryDocument(Document):
    class Index:
        name = 'category'
    settings = {
        'number_of_shards': 1,
        'number_of_replicas': 0
    }
    class Django:
        model = Category
        fields = [
            'name',
            'desc',
        ]

Create and populate the Elasticsearch index and mapping: $ python manage.py search_index --rebuild Search examples:

# Term filter
s = CategoryDocument.search().filter("term", name="computer")

# Match query
s = CategoryDocument.search().query("match", description="abc")

for hit in s:
    print("Category name : {}, description {}".format(hit.name, hit.desc))

Convert Elasticsearch results to a real Django queryset (incurring one additional SQL query to fetch model instances):

s = CategoryDocument.search().filter("term", name="computer")[:30]
qs = s.to_queryset()
for cat in qs:
    print(cat.name)

Feel free to leave comments if you have any questions.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

IntegrationElasticsearchDjangoFull‑Text Search
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.