pip install django-haystack python INSTALLED_APPS = [ ... 'haystack', ... ] python HAYSTACK_CONNECTIONS = { 'default': { 'ENGINE': 'haystack.backends.elasticsearch5_backend.Elasticsearch5SearchEngine', 'URL': 'http://localhost:9200/', 'INDEX_NAME': 'my_index', }, } python from django.db import models class Article(models.Model): title = models.CharField(max_length=100) content = models.TextField() python from haystack import indexes from .models import Article class ArticleIndex(indexes.SearchIndex, indexes.Indexable): text = indexes.CharField(document=True, use_template=True) title = indexes.CharField(model_attr='title') def get_model(self): return Article def index_queryset(self, using=None): return self.get_model().objects.all() {{ object.title }} {{ object.content }} python manage.py rebuild_index python from haystack.query import SearchQuerySet search_results = SearchQuerySet().filter(content=query) for result in search_results: print(result.title)


上一篇:
下一篇:
切换中文