微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

为什么 Django-Haystack 不提供 apache solr 的结果?

如何解决为什么 Django-Haystack 不提供 apache solr 的结果?

我正在使用 apache solr 和 django-haystack 和 pysolr 来实现我网站中的搜索功能。但是我的搜索无法在 haystack 查询集中使用自动完成功能

数据库MysqL

Indexes.py:

from haystack import indexes
from product.models import Product


class ProductIndex(indexes.SearchIndex,indexes.Indexable):
   text = indexes.CharField(document=True,use_template=True)
   title = indexes.CharField(model_attr='title')
   keywords = indexes.CharField(model_attr='keywords')

  title_auto = indexes.EdgeNgramField(model_attr='title')
  keywords_auto = indexes.EdgeNgramField(model_attr='keywords')
  suggestions = indexes.FacetCharField()

  def get_model(self):
     return Product

  def index_queryset(self,using=None):
     return self.get_model().objects.all()

  def prepare(self,obj):
     prepared_data = super(ProductIndex,self).prepare(obj)
     prepared_data['suggestions'] = prepared_data['text']
     return prepared_data

Views.py

def search_view(request):
   search_keyword = request.data['query']
   sqs = SearchQuerySet().autocomplete(keywords_auto=search_keyword)
   print(sqs.count())
   suggestions = [result.title for result in sqs]
   the_data = json.dumps({
      'results': suggestions
   })
   return HttpResponse(the_data,content_type='application/json')

当我测试它时,它给了我空白结果。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。