Django Stretch¶
Elasticsearch for Django, Made Friendly
Stretch lets you index and search Django models in Elasticsearch. It sits on top of the elasticsearch_dsl
package for easy access to most of Elasticsearch’s features.
See the complete Documentation
Installation¶
Currently you can only install django-stretch via git.
pip install git+git@github.com:cbredev/django-stretch.git
Quickstart¶
Make sure you have Elasticsearch installed
- Add to
INSTALLED_APPS
INSTALLED_APPS = ( ... 'stretch', )
- Add to
- Create a
stretch_indices.py
file in one of your apps from stretch.indices import StretchIndex from stretch.signals import RealtimeSignalHandler from elasticsearch_dsl import String from yourapp.models import Foo class FooIndex(StretchIndex): name = String(source='name') bar = String(source='get_bar') class Meta: # Specify the model to track with this index model = Foo index = 'foos' # (Optional) specify a custom index name doc_type = 'deal' # (Optional) specify a custom doc type # (Optional) Specify any custom index settings index_settings = { 'number_of_replicas': 1 } # Automatically track model updates in realtime or with celery signal_handler = RealtimeSignalHandler # The bar field is populated by this custom method def get_bar(self, obj): return 'custom value'
- Create a
- Create the index & add all the documents
./manage.py stretch_update_all
Your models are now being indexed in Elasticsearch!
Features¶
- Specify custom analyzers and other field attributes
- Dynamically update mapping and per index settings
- Django Rest Framework (DRF) Search Filter
- Management Commands to update indices and documents
- post_save / post_delete signals
Philosophy¶
Stretch sits on top of the Elasticsearch DSL python library. This makes it really easy to create document mappings and use custom analyzers in a declarative, pythonic syntax. It also includes an up to date interface for performing complex searches. This tool focuses on making it easy to manage your Elasticsearch indices over the long term as you add new fields and analyzers.
Why not [Haystack|Bungiesearch]?¶
Both Haystack and Bungieserach are great tools. Haystack works great as a general search engine wrapper, but makes it difficult to use the majority of Elasticsearch’s built in features. Bungiesearch does a slightly better job at this, but does not provide good tooling for adding new analyzers or updating index settings. We wanted a tool that let us easily manage indices as well as documents.
Settings¶
STRETCH_SETTINGS = {
'HOSTS': ['localhost'],
'APPS': None, # If not specified, searches all apps for indices
'EXCLUDED_INDICES': None, # Manually ignore an index. You can also set the index attribute to `IS_INDEX = False`
'SIGNAL_PROCESSOR': None, # Set a global default signal processor. None by default
'TEST': False # Uses a mock backend so you can run tests in your project without trying to make a real connection to Elasticsearch
'RAISE_EXCEPTIONS': False # By default, Stretch will use a `logging.exception` to notify your application of failures without crashing your threads. If you prefer to have your application fail on write errors, set this to `True`
}