Auto Index and Search Django Model Instances with RediSearch (using redis-search-django)
Tags: redishackathon, python, redis, django
Published: 2022-08-21 14:52:00

### Overview of My Submission

I built an **Installable Django Package** called **[`redis-search-django`](https://github.com/saadmk11/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](https://redis.io/docs/stack/search/)**.

`redis-search-django` internally uses [redis-om-python](https://github.com/redis/redis-om-python) and [redis-py](https://github.com/redis/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:**

{% embed https://github.com/saadmk11/redis-search-django/ %}

**Demo App Repository:**
(**README** file contains the details as per **Hackathon Requirements**)
{% embed https://github.com/saadmk11/try-redis-search-django/ %}


### Additional Resources / Info

#### Architecture Diagram


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6kkttmcx1qkc1vx2rb4p.png)

#### How to use it

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

**1. For Django Model:**

```python
# 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
```

**2. You can create a document class like this:**

```python
# documents.py

from redis_search_django.documents import JsonDocument

from .models import Category


class CategoryDocument(JsonDocument):
    class Django:
        model = Category
        fields = ["name", "slug"]
```

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

```bash
python manage.py index
```

**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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gcw5inn3h3ejc8lij71n.png)

- - - 


* _Check out [Redis OM](https://redis.io/docs/stack/get-started/clients/#high-level-client-libraries), client libraries for working with Redis as a multi-model database._
* _Use [RedisInsight](https://redis.info/redisinsight) to visualize your data in Redis._
* _Sign up for a [free Redis database](https://redis.info/try-free-dev-to)._