pip install django-haystack
python
HAYSTACK_CONNECTIONS = {
'default': {
'ENGINE': 'haystack.backends.elasticsearch_backend.ElasticsearchSearchEngine',
'URL': 'http://localhost:9200/',
'INDEX_NAME': 'my_index',
},
}
python
from haystack import indexes
from .models import MyModel
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
HAYSTACK_SIGNAL_PROCESSOR = 'haystack.signals.RealtimeSignalProcessor'
python
{{ object.field_name }}
python
HAYSTACK_DEFAULT_ANALYZER = 'haystack.backends.jieba.ChineseAnalyzer'
python
class MyModelIndex(indexes.SearchIndex, indexes.Indexable):
DEFAULT_SORT_BY = '-publish_date'
python
HAYSTACK_SIGNAL_PROCESSOR = 'haystack.signals.RealtimeSignalProcessor'
HAYSTACK_INCLUDE_SPELLING = True
python
from haystack.query import SearchQuerySet
from django.http import JsonResponse
def search_suggestions(request):
term = request.GET.get('term', '')
sqs = SearchQuerySet().autocomplete(text_auto=term)
suggestions = [result.text_auto for result in sqs]
return JsonResponse(suggestions, safe=False)