pip install django-haystack
python
import haystack
python
INSTALLED_APPS = [
...
'haystack',
...
]
python
HAYSTACK_CONNECTIONS = {
'default': {
'ENGINE': 'haystack.backends.elasticsearch2_backend.Elasticsearch2SearchEngine',
'URL': 'http://localhost:9200/',
'INDEX_NAME': 'my_index',
},
}
python
python manage.py rebuild_index
python
from haystack import indexes
from .models import YourModel
class YourModelIndex(indexes.SearchIndex, indexes.Indexable):
text = indexes.CharField(document=True, use_template=True)
def get_model(self):
return YourModel
def index_queryset(self, using=None):
return self.get_model().objects.all()
python
from your_app.search_indexes import *
python
from haystack.query import SearchQuerySet
def search(request):
return render(request, 'search_results.html', {'results': results})