Codoc-os / django-opensearch-dsl

This is a package that allows indexing of django models in opensearch with opensearch-py.
Other
26 stars 16 forks source link

Django Opensearch DSL

PyPI Version Documentation Status Tests Python 3.9+ Django 3.2+ OpenSearch 1.3+, 2.7+ License Apache 2 codecov CodeFactor

Django Opensearch DSL is a package that allows the indexing of Django models in opensearch. It is built as a thin wrapper around opensearch-py so you can use all the features developed by the opensearch-py team.

You can view the full documentation at https://django-opensearch-dsl.readthedocs.io.

Features

Requirements

django-opensearch-dsl only support the supported version of each dependency (mainstream & lts).

Installation and Configuration

The easiest way to install django-opensearch-dsl is through pip:

Then add django_opensearch_dsl to your INSTALLED_APPS settings.

You must then define OPENSEARCH_DSL in your Django settings.

For example:

OPENSEARCH_DSL = {
    'default': {
        'hosts': 'localhost:9200'
    },
    'secure': {
        'hosts': [{"scheme": "https", "host": "192.30.255.112", "port": 9201}],
        'http_auth': ("admin", "password"),
        'timeout': 120,
    },
}

OPENSEARCH_DSL is then passed to opensearchpy.connection.connections.configure .

Create Document Classes

To index instances of the following model :

# models.py

class Car(models.Model):
    name = models.CharField()
    color = models.CharField()
    description = models.TextField()
    type = models.IntegerField(choices=[
        (1, "Sedan"),
        (2, "Truck"),
        (4, "SUV"),
    ])

First create a subclass of django_opensearch_dsl.Document containing the subclasses Index (which define the index' settings) and Django (which contains settings related to your django Model). Finally, register the class using registry.register_document() decorator.

It is required to define Document classes inside a file named documents.py in your apps' directory.

# documents.py

from django_opensearch_dsl import Document
from django_opensearch_dsl.registries import registry
from .models import Car

@registry.register_document
class CarDocument(Document):
    class Index:
        name = 'cars'  # Name of the Opensearch index
        settings = {  # See Opensearch Indices API reference for available settings
            'number_of_shards': 1,
            'number_of_replicas': 0
        }
        # Configure how the index should be refreshed after an update.
        # See Opensearch documentation for supported options.
        # This per-Document setting overrides settings.OPENSEARCH_DSL_AUTO_REFRESH.
        auto_refresh = False

    class Django:
        model = Car  # The model associated with this Document        
        fields = [  # The fields of the model you want to be indexed in Opensearch
            'name',
            'color',
            'description',
            'type',
        ]
        # Paginate the django queryset used to populate the index with the specified size
        # This per-Document setting overrides settings.OPENSEARCH_DSL_QUERYSET_PAGINATION.
        queryset_pagination = 5000