Skip to main content

Django model manager like class for elasticsearch integration

Project description

# elasticmanager
A manager for translating an ORM into Elasticsearch

*This package is still in active development. Currently it is being built as an mixin for the Django ORM. However, once stable, this will be abstracted away to be able to plugin to other ORMs.*

This pacakge requires `elasticsearch-dsl` which you can get:

pip install elasticsearch-dsl

## Getting Started

1. To get started, install from pip.

pip install elasticmanager

1. Add `elasticmanager` to your `` (this enables management commands).
1. Put the name of your index in ``: `ELASTICSEARCH_INDEX = 'myindex'`
1. Subclass `elasticmanager.ElasticModel`.

from django.db import models
from elasticmanager.models import ElasticModel
class Visitor(ElasticModel, models.Model):
name = models.CharField(max_lendth=100)
created = models.DateTimeField(auto_now_add=True, editable=False)

1. Create a `` in the same app as the corresponding ``.
Add a `DocType` to this file with the same name as your model. (See [Elasticsearch DSL documenation for more information on creating a `DocType`](

from django.conf import settings
from elasticmanager.doctypes import BaseDocType
from . import models
class Visitor(BaseDocType):

pk = field.Keyword()
name = field.Keyword()
created = field.Date()

class Meta:
model = models.Visitor
index = settings.ELASTICSEARCH_INDEX

Note that the `Meta` information for the `doctype` should link to the `model` and the `index`.

1. Run `./ rebuild_mapping`
1. Run `./ rebuild_indexing` (if there are items in the DB that need to be inexed)

There is also another management command (`./ remove_index <NAME>`) that can be used to delete an entire index. Helpful during development stages.

Currently, all calls to the `.save()` method on an instance of the model will trigger the `.save()` on the doctype, and therefore will keep the index in Elasticsearch up to date.

## Making calls to query and filter

Calls somewhat approximate the default Django syntax.

**Get all instances**

visitors = Visitor.elastic.all() # Return everything from Elasticsearch

**Get one instance**

visitor = Visitor.elastic.get(pk=123) # Return an instance from Elasticsearch given a specific key
visitor = Visitor.elastic.first() # Return first instance in a queryset
visitor = Visitor.elastic.last() # Return last instance in a queryset

**Count the number of instances**

count = Visitor.elastic.count() # Count the number of instances in a queryset

**Filter/query** *This lines up with the [query]( and [filter]( methods. See the linked documentation for more information.*

johns = Visitor.elastic.filter(name="John") # Return everything from Elasticsearch

**Chaining applies**

first_john = Visitor.elastic.filter(name="John").first()

Under the hood, there is an `execute()` method that is committing the search. This can be called by itself, and probably should be if you are (for example) manipulating a queryset. But, you should not need to call that if you are, for example, calling `count()` or if you are iterating over the result set.

johns = Visitor.elastic.filter(name="John")
for john in johns:

# or

johns = Visitor.elastic.filter(name="John")

*This requirement will be removed with some more fine tuning to work more intuitively.*

## Plans for the future

- Abstraction from using `models.Manager`
- Fix the class factory so that a `Model` can be automatically transformed into a `DocType` without having to define it in ``.
- Tests
- Aggregations
- Additional Exception handling
- More management commands
- More powerful API

If you have any questions, thoughts, complaints, compliments, let me know.

Project details

Download files

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

Files for elasticmanager, version 0.2.6
Filename, size File type Python version Upload date Hashes
Filename, size elasticmanager-0.2.6.tar.gz (8.8 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page