Skip to main content
# 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

## settings.py

In your Django settings file, define these variables:

```python

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

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

Add elasticmodels to INSTALLED_APPS:

```python

INSTALLED_APPS = (
...
'elasticmodels',
)
```

## app/search_indexes.py

In a Django app, create a search_indexes.py file, like so:

```python

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 {
"pk": obj.pk,
"content": render_to_string("files/search.txt", {"object": obj}),
"tags": [ft.tag.name 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

./manage.py 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

```python

from elasticmodels import make_searchable

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

make_searchable(f)

```

## 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.

See:
http://elasticutils.readthedocs.org/en/latest/searching.html
http://elasticutils.readthedocs.org/en/latest/searching.html#filters-filter
http://elasticutils.readthedocs.org/en/latest/searching.html#queries-query
http://elasticutils.readthedocs.org/en/latest/searching.html#advanced-filters-f-and-filter-raw

```python

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.pk, result.content
```

Metadata

Release files for elasticmodels 0.0.10

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for elasticmodels 0.0.10
File Size Uploaded
elasticmodels-0.0.10.tar.gz 6.1 kB Details

Release files / elasticmodels-0.0.10.tar.gz

Download URL elasticmodels-0.0.10.tar.gz
Size 6.1 kB
Tags Source
SHA-256 checksum
How to use checksums
c1e0e6a66b0a3908e49641259a63bf92baa9582909ee3b5509ed9cdfd9a94f81
BLAKE2b-256 checksum
How to use checksums
c803988c7669e84266cb4e08babc60be9fece663a30e914a333e2deb133d92c2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.0.10 This release

1 release file

0.0.9

1 release file

0.0.8

1 release file

0.0.7

1 release file

0.0.6

1 release file

0.0.5

1 release file

0.0.4

1 release file

0.0.3

1 release file

0.0.2

1 release file

0.0.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page