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)
..
..
# It's optional to add 'async_' arg.
@es_indexing
def indexing(self, async_=True):
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, async_=True)
elif not created:
index_name = es_indices.ProgramIndex._index._name
delete_es_document(document.id, index_name, async_=True)
def delete_program(sender, instance, **kwargs):
index_name = es_indices.ProgramIndex._index._name
delete_es_document(instance.id, index_name, async_=True)
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.
Metadata
Release files for django-es-utils 0.0.5
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| django_es_utils-0.0.5.tar.gz | 6.6 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| django_es_utils-0.0.5-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 14.4 kB
Release files / django_es_utils-0.0.5.tar.gz
| Download URL | django_es_utils-0.0.5.tar.gz |
|---|---|
| Size | 6.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
ab06fa24d2b3807b31c0281b2e383c4756fdd65b1e8eee8200aed0c2afe56a3f
|
|
BLAKE2b-256 checksum How to use checksums |
1ac0ca2ea501e411356d303f5f36c1b77b4882951448ffd7ace62f60a57fb24e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7
|
Release files / django_es_utils-0.0.5-py3-none-any.whl
| Download URL | django_es_utils-0.0.5-py3-none-any.whl |
|---|---|
| Size | 7.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
242b690d9ff0abefb0628f49ea92b13611cca2fd4393556455c8a3e3b40067ba
|
|
BLAKE2b-256 checksum How to use checksums |
1ded386091c729469594694884abe752da03d051046a9a5d7e7340912f342990
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7
|