Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Fetching contributors…
Cannot retrieve contributors at this time
197 lines (122 sloc) 5.18 KB

elasticutils.contrib.django: Using with Django


Django helpers are all located in elasticutils.contrib.django.

This chapter covers using ElasticUtils Django bits.


ElasticUtils depends on the following settings:


The get_es() in the Django contrib will helpfully cache your ES objects thread-local.

It is built with the settings from your django.conf.settings.


get_es() only caches the ES if you don't pass in any override arguments. If you pass in override arguments, it doesn't cache it, but instead creates a new one.

Using with Django ORM models


The elasticutils.contrib.django.S class takes a model in the constructor. That model is a Django ORM Models derivative. For example:

from elasticutils.contrib.django import S
from myapp.models import MyModel

searcher = S(MyModel)

Further, you can have your model extend :class:`elasticutils.contrib.django.models.SearchMixin` and get a bunch of functionality that makes indexing data easier.

Two things to know:

  1. The doctype for the model is cls._meta.db_table.
  2. The index that's searched is settings.ES_INDEXES[doctype] and if that doesn't exist, it defaults to settings.ES_INDEXES['default']

Other helpers

Requirements:Django, Celery

You can then utilize things such as :func:`~elasticutils.contrib.django.tasks.index_objects` to automatically index all new items.

Writing tests

Requirements:Django, test_utils, nose

In elasticutils.contrib.django.estestcase, is ESTestCase which can be subclassed in your app's test cases.

It does the following:

  • If ES_HOSTS is empty it raises a SkipTest.
  • is available from the ESTestCase class and any subclasses.
  • At the end of the test case the index is wiped.


from elasticutils.djangolib import ESTestCase

class TestQueries(ESTestCase):
    def test_query(self):

    def test_locked_filters(self):


From Rob Hudson (with some minor editing):

I recently discovered a nice tool for helping solve ElasticSearch problems that I thought I'd share...

While scanning the code of pyes I discovered that it has an option to dump the commands it is sending to the ES backend to whatever you give it that has a write() method [1]. I also discovered that elasticutils will pass this through to pyes based on the settings.ES_DUMP_CURL [2].

I threw together a quick and ugly class just to dump output while debugging an ES problem:

class CurlDumper(object):
    def write(self, s):
        print s
ES_DUMP_CURL = CurlDumper()

This is pretty great when running a test with output enabled, or even in the runserver output. But to my surprise, when running tests with output not enabled I see the curl dump for only tests that fail, which has turned out to be very useful information.

Jump to Line
Something went wrong with that request. Please try again.