Skip to main content

Elasticsearch utilities for Django projects.

Reason this release was yanked:

Wrong version number.

Project description

Elasticsearch Utilities

Elasticsearch utilities for Django projects to analyze queries, create documents, sync data and connect to ES.

Required configurations

Add the following configurations to Django project settings:

  • ES_SYNC_FEATURE (Required: Boolean)
  • ELASTICSEARCH_HOST (Required: String)
  • ELASTICSEARCH_USERNAME (Optional: String)
  • ELASTICSEARCH_PASSWORD (Optional: String)

Syncing data with Elasticsearch

Steps to index a Django model into Elasticsearch:

1- create a new file inside any Django app called es_indices.py and insert this code into it with your modifications:

from es_utils.analyzers import arabic_analyzer
from es_utils.document import ESDocument
from elasticsearch_dsl import (
    connections,
    Document,
    Text,
    Completion
)


class ProgramIndex(ESDocument):
    """
    This class is used to map the programs with Elasticsearch index.
    """

    class Index:
        name = 'programs'

    # the fields you want to index with their types

    id = Text()

    # support autocomplete for this field
    name_en = Text(fields={'completion': Completion()})

    # add arabic analyzer to this field
    about_ar = Text(analyzer=arabic_analyzer)
    about_en = Text()

    # nested object
    category = Object(properties={
        'name_en': Text(),
        'name_ar': Text()
    })

    # ...
    # ...

2- Add the indexing function to your model in models.py file:

from cms.djangoapps.program.es_indices import ProgramIndex
from es_utils.helpers import es_indexing


class Program(models.Model):
    name_en = models.CharField(max_length=255)
    about_en = models.CharField(max_length=255)
    ..
    ..

    @es_indexing
    def indexing(self):
        attrs = dict(
            meta={'id': self.id},
            metadata={
                'filters': {'type': self.program_type, 'status': self.status},
                'sorting': {'name': self.name_en, 'type': self.program_type, 'start_date': self.start}
            },
            id=self.id,
            slug=self.slug,
            name_en=self.name_en,
            type=self.program_type,
            about_en=self.about_en,

            # nested dict
            category=dict(
                name_en=self.category.name_en,
                name_ar=self.category.name_ar
            ),
        )
        return ProgramIndex, attrs

3- Add a post_save and post_delete signals and connect them with the model:

from cms.djangoapps.program.models import Program
from cms.djangoapps.program import es_indices
from django.db.models.signals import post_save, post_delete
from elasticsearch_dsl import Search
from es_utils.connection import connection
from es_utils.helpers import delete_es_document


def index_program(sender, instance, created, **kwargs):
    if instance.can_index():
        instance.indexing(new=created)
    elif not created:
        index_name = es_indices.ProgramIndex._index._name
        delete_es_document(document.id, index_name)


def delete_program(sender, instance, **kwargs):
    index_name = es_indices.ProgramIndex._index._name
    delete_es_document(instance.id, index_name)


for cls_name in settings.ES_SYNC_MODELS:
    Cls = getattr(models, cls_name)
    signals.post_save.connect(index_program, sender=Cls)
    signals.post_delete.connect(delete_program, sender=Cls)

Compatibility

  • Django >= 1.8.
  • Python3.

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_utils-1.0.3.tar.gz (5.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_es_utils-1.0.3-py3-none-any.whl (7.7 kB view details)

Uploaded Python 3

File details

Details for the file django_es_utils-1.0.3.tar.gz.

File metadata

  • Download URL: django_es_utils-1.0.3.tar.gz
  • Upload date:
  • Size: 5.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.20.1 setuptools/40.5.0 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.5

File hashes

Hashes for django_es_utils-1.0.3.tar.gz
Algorithm Hash digest
SHA256 5a94538c7964d5c94aad68312f5a89556320af6520c909d7ca4533cee3246176
MD5 0387fdfed2eff3dcca8ba6ca750c13b7
BLAKE2b-256 f4b85c96ac08a29a213c1df7ff9954735cda348aad13e0ce0c32f0e4cdef0e49

See more details on using hashes here.

File details

Details for the file django_es_utils-1.0.3-py3-none-any.whl.

File metadata

  • Download URL: django_es_utils-1.0.3-py3-none-any.whl
  • Upload date:
  • Size: 7.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.20.1 setuptools/40.5.0 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.5

File hashes

Hashes for django_es_utils-1.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 c1d4d91fc70759c4a857d022feb07936781ad848ca53e788e1f62d564adee15b
MD5 9fe5f236598582557c339cdfcaa7520a
BLAKE2b-256 cfea213855b7b272c094f9d76f087e3c4a875e61bab9b43ea63271bc8304f2c4

See more details on using hashes here.

Supported by

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