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.
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:
- The doctype for the model is
- The index that's searched is
settings.ES_INDEXES[doctype]and if that doesn't exist, it defaults to
You can then utilize things such as :func:`~elasticutils.contrib.django.tasks.index_objects` to automatically index all new items.
|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.
- self.es 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 . I also discovered that elasticutils will pass this through to pyes based on the
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.