Skip to main content

Simple django - elasticsearch - drf integration

Project description

Django ES DRF

Work in progress

A simple integration layer between Django, Elasticsearch and Django rest framework

Model and ES Document example

To declare your own document, create a model and register it with a DjangoDocument

from django.db import models
from django_es_drf import registry, DjangoDocument
from rest_framework.serializers import ModelSerializer


class School(models.Model):
    name = models.CharField(max_length=100)
    address = models.TextField()


@registry.register(School)
class SchoolDocument(DjangoDocument):
    class Index:
        name = "schools"

If you want to split the file into models, serializers and documents, be free to do so but ensure that the file including documents is loaded at the startup - in app's ready function, imported at the bottom of the model etc.

Later on, use SchoolDocument (which is a subclass of elasticsearch-dsl document):

for s in SchoolDocument.search().filter('term', name='Blah'):
    print(s)
    s.name = s.name + '-test'
    s.save()  # will save django and index to ES

DRF example

The simplest case is:

class SchoolAPI(ESViewSet):
    document = SchoolDocument

Search, facets

Luqum search

Django Document and mapping

Custom declaration for fields

Any fields that are already present on the document will be keep as they are. In the example above, to set that name is a text and not keyword (the default), just declare:

import elasticsearch_dsl as e


@registry.register(School, SchoolSerializer)
class SchoolDocument(DjangoDocument):
    name = e.Text()

    class Index:
        name = "schools"

Excluding fields

To exclude a field from the automatic generation, add it to excluded_fields:

@registry.register(School, SchoolSerializer,
                   excluded_fields=['address'])
class SchoolDocument(DjangoDocument):
    class Index:
        name = "schools"

Custom mapping between serializer fields and ES fields

Add your own mapping - the key is the DRF field type, value is a function that takes the field and context and returns ES field: The context is an instance of RegistrationContext.

import elasticsearch_dsl as e


@registry.register(School, SchoolSerializer,
                   mapping={
                       TextField: lambda fld, ctx: e.Keyword()
                   })
class SchoolDocument(DjangoDocument):
    class Index:
        name = "schools"

Disabling the mapping

Add generate=False to decorator's parameters:

import elasticsearch_dsl as e


@registry.register(School, SchoolSerializer,
                   generate=False)
class SchoolDocument(DjangoDocument):
    # you need to provide your own mapping here

    class Index:
        name = "schools"

Relations

The framework does not generate code for relations - if you need this, do it in serializer and add your own mapping, or use a more complete library, such as django-elasticsearch-dsl-drf.

Serializer

The serializer is just a plain DRF serializer that converts django fields to document's fields. When autogenerated mapping is used, just use the plain empty serializer.

Note: see Relations section above if you need to serialize relations

Objects and nested

Viewsets

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-es-drf-1.0.0a1.tar.gz (12.1 kB view details)

Uploaded Source

Built Distribution

django_es_drf-1.0.0a1-py3-none-any.whl (14.9 kB view details)

Uploaded Python 3

File details

Details for the file django-es-drf-1.0.0a1.tar.gz.

File metadata

  • Download URL: django-es-drf-1.0.0a1.tar.gz
  • Upload date:
  • Size: 12.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.63.1 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.11

File hashes

Hashes for django-es-drf-1.0.0a1.tar.gz
Algorithm Hash digest
SHA256 cb55fc756cc8f234137748d9809d8cdfaf2f99446667e69174cce158533af7ed
MD5 fcc7c2b8a34a77d796681727a3047fbd
BLAKE2b-256 21058372f352559ec4e2c8d63bfe5e372519c9fa83bb7dc29ef0a7f520c1b7f2

See more details on using hashes here.

File details

Details for the file django_es_drf-1.0.0a1-py3-none-any.whl.

File metadata

  • Download URL: django_es_drf-1.0.0a1-py3-none-any.whl
  • Upload date:
  • Size: 14.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.63.1 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.11

File hashes

Hashes for django_es_drf-1.0.0a1-py3-none-any.whl
Algorithm Hash digest
SHA256 e5628a202c6cf5ca672a4c7666f386f85d6461d8cb1c6912f23f8e34f2517752
MD5 1c78f60b8700bb73b61af7d545e4756a
BLAKE2b-256 207ebb87ffef16497bfdf6f86c2f0069d21a8dcb5625751e3dc02a027ffe6670

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page