Skip to main content

Elasticmodels helps you index and query your Django models using elasticsearch

Project description

# Elasticmodels

Elasticmodels helps you index and query your Django models using elasticsearch.
It is designed to be an alternative to django-haystack when you need more control over
your index creation, and you are always going to use elasticsearch.

# Install

pip install elasticmodels

# Setup


In your Django settings file, define these variables:


"urls": ["http://localhost:9200/"],
"index": "the_name_of_your_es_index",
# "http_auth": "username:password",

# these are used when your index is created
"settings": {
"analysis": {
"analyzer": {
"snowball": {
"type": "snowball",
"stopwords": "_none_"

Add elasticmodels to INSTALLED_APPS:



## app/

In a Django app, create a file, like so:


from elasticmodels import Indexable
from django.template.loader import render_to_string
from .models import File, FileTag

class FileIndex(Indexable):
# specify the model class this index is for
model = File

def mapping(self):
Return the elasticsearch mapping for this model type
return {
"properties": {
"pk": {"type": "integer", "index": "not_analyzed"},
"content": {"type": "string", "analyzer": "snowball"},
"tags": {"type": "string", "analyzer": "keyword"},
"org_id": {"type": "integer", "index": "not_analyzed"},
"type": {"type": "integer", "analyzer": "keyword"},
"uploaded_by_id": {"type": "integer", "analyzer": "keyword"},

def prepare(self, obj):
Return obj transformed into a dict that corresponds to the mapping
you defined. This is what will be indexed by elasticsearch.
return {
"content": render_to_string("files/search.txt", {"object": obj}),
"tags": [ for ft in FileTag.objects.filter(file=obj).select_related("tag")],
"org_id": obj.org_id,
"type": obj.type,
"uploaded_by_id": obj.uploaded_by_id,

# Usage

## Deleting and recreating your index

./ rebuild_index

**This will delete the entire elasticsearch index** and recreate it. All your
model objects will be re-indexed.

## Adding an individual object to the index


from elasticmodels import make_searchable

f = File(name="Foo", type=1)



## Querying

Your subclass of elasticmodels.Indexable has a class attribute called `objects`
which returns an elasticutils `S` instance. You can then use whatever methods are
available in elasticutils on the S instance.



from elasticutils import F
from .search_indexes import FileIndex

results = FileIndex.objects.filter(F(type=1) | F(type=2)).query(content__match="foo")
for result in results:
print, result.content

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

elasticmodels-0.0.10.tar.gz (6.1 kB view hashes)

Uploaded source

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