Git Product home page Git Product logo

django-opensearch-dsl's Introduction

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

  • Based on opensearch-py so you can make queries with the Search object.
  • Management commands for creating, deleting, and populating indices and documents.
  • Opensearch auto mapping from Django models fields.
  • Complex field type support (ObjectField, NestedField).
  • Index fast using parallel indexing.

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:

  • pip install django-opensearch-dsl

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

django-opensearch-dsl's People

Contributors

sabricot avatar safwanrahman avatar qcoumes avatar grendel7 avatar barseghyanartur avatar dsanders11 avatar dannyaziz avatar juliensabre avatar david-guillot avatar saadmk11 avatar markotibold avatar oehrlein avatar dependabot[bot] avatar ghkdxofla avatar dannylagrouw avatar colin-seifer avatar arielpontes avatar chosak avatar alexgmin avatar jaywhy13 avatar alaminopu avatar noamkush avatar paulogiacomelli avatar cool-rr avatar rzschech avatar sir-sigurd avatar serkanozer avatar shauryashahi avatar tomfa avatar hirokbiswas avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.