DEV Community

Cover image for Auto Index and Search Django Model Instances with RediSearch (using redis-search-django)
Maksudul Haque
Maksudul Haque

Posted on

Auto Index and Search Django Model Instances with RediSearch (using redis-search-django)

Overview of My Submission

I built an Installable Django Package called redis-search-django as a part of Redis Hackathon on DEV. redis-search-django is a Django package that provides auto indexing and searching capabilities for Django Model instances using RediSearch.

redis-search-django internally uses redis-om-python and redis-py

redis-search-django is Available on Python Package Index (PyPI): https://pypi.org/project/redis-search-django/

You can install it by running: pip install redis-search-django

Features

  • Management Command to create, update and populate the RediSearch Index.
  • Auto Index on Model object Create, Update and Delete.
  • Auto Index on Related Model object Add, Update, Remove and Delete.
  • Easy to create Document classes (Uses Django Model Form Class like structure).
  • Index nested models (e.g: OneToOneField, ForeignKey and ManyToManyField).
  • Search documents using redis-om.
  • Search Result Pagination.
  • Search Result Sorting.
  • RediSearch Result to Django QuerySet.
  • Faceted Search.

Submission Category:

Wacky Wildcards Or, Microservice Mavens
(Because a Django Package does not fit into other Categories)

Language Used

  • Python

Link to Code

Django Package Repository:

GitHub logo saadmk11 / redis-search-django

Django package that provides auto indexing and searching capabilities for Django model instances using RediSearch.

redis-search-django

Pypi Version Supported Python Versions Supported Django Versions License

Django Tests Codecov pre-commit.ci Changelog-CI Code Style

About

A Django package that provides auto indexing and searching capabilities for Django model instances using RediSearch.

Features

  • Management Command to create, update and populate the RediSearch Index.
  • Auto Index on Model object Create, Update and Delete.
  • Auto Index on Related Model object Add, Update, Remove and Delete.
  • Easy to create Document classes (Uses Django Model Form Class like structure).
  • Index nested models (e.g: OneToOneField, ForeignKey and ManyToManyField).
  • Search documents using redis-om.
  • Search Result Pagination.
  • Search Result Sorting.
  • RediSearch Result to Django QuerySet.
  • Faceted Search.

Requirements

  • Python: 3.7, 3.8, 3.9, 3.10
  • Django: 3.2, 4.0, 4.1
  • redis-om: >= 0.0.27

Redis

Downloading Redis

The latest version of Redis is available from Redis.io. You can also install Redis with your operating system's package manager.

RediSearch and RedisJSON

redis-search-django relies on the RediSearch and RedisJSON Redis modules to support rich queries and embedded models You need these Redis…

Demo App Repository:
(README file contains the details as per Hackathon Requirements)

Auto Index and Search Django Model Instances with RediSearch (using redis-search-django)

Description

I built an Installable Django Package called redis-search-django as a part of Redis Hackathon on DEV redis-search-django is a package that provides auto indexing and searching capabilities for Django model instances using RediSearch.

This is a Demo App that uses redis-search-django package to Showcase a Product Search Engine with auto indexing and searching.

redis-search-django Documentation: https://github.com/saadmk11/redis-search-django

Features

  • Management Command to create, update and populate the RediSearch Index.
  • Auto Index on Model object Create, Update and Delete.
  • Auto Index on Related Model object Add, Update, Remove and Delete.
  • Easy to create Document classes (Uses Django Model Form Class like structure).
  • Index nested models (e.g: OneToOneField, ForeignKey and ManyToManyField).
  • Search documents using redis-om.
  • Search Result Pagination.
  • Search Result Sorting.
  • RediSearch Result to Django QuerySet.
  • Faceted Search.

App Screenshot

RediSearch Django

How it works

Create Search

Additional Resources / Info

Architecture Diagram

Image description

How to use it

Create Search Document from Django Model. (Using redis-search-django)

1. For Django Model:

# models.py

from django.db import models


class Category(models.Model):
    name = models.CharField(max_length=32)
    slug = models.SlugField(max_length=64)

    def __str__(self) -> str:
        return self.name
Enter fullscreen mode Exit fullscreen mode

2. You can create a document class like this:

# documents.py

from redis_search_django.documents import JsonDocument

from .models import Category


class CategoryDocument(JsonDocument):
    class Django:
        model = Category
        fields = ["name", "slug"]
Enter fullscreen mode Exit fullscreen mode

3. Run Django Management Command Index to create the index on Redis (Only need to run once to generate Index Schema on Redis):

python manage.py index
Enter fullscreen mode Exit fullscreen mode

Note: This will also populate the index with existing data from the database

Now category objects will be indexed automatically using Redis on create/update/delete events.

More Complex Examples Can be found here: https://github.com/saadmk11/redis-search-django

Screenshot of the Demo App:

Image description


Top comments (0)