$ pip install django-haystack
python
INSTALLED_APPS = [
...
'haystack',
...
]
HAYSTACK_CONNECTIONS = {
'default': {
'ENGINE': 'haystack.backends.elasticsearch5_backend.Elasticsearch5SearchEngine',
},
}
HAYSTACK_SIGNAL_PROCESSOR = 'haystack.signals.RealtimeSignalProcessor'
$ python manage.py rebuild_index
python
from django.db import models
from django.contrib.auth.models import User
from haystack import indexes
python
class MyModelIndex(indexes.SearchIndex, indexes.Indexable):
text = indexes.CharField(document=True, use_template=True)
def get_model(self):
return MyModel
def index_queryset(self, using=None):
return self.get_model().objects.all()
python
from haystack.query import SearchQuerySet
python
{% load haystack %}
{% for result in results %}
<h3>{{ result.object.title }}</h3>
<p>{{ result.object.content }}</p>
{% empty %}
{% endfor %}