我正在尝试写一个小的搜索引擎与django-haystac和呼呼。我改编了他们的教程,我已经从一个JSON文件创建了我的索引,并用QueryParser成功地查询了它,现在我正在尝试使用他们的视图。
当我尝试访问位于:http://127.0.0.1:8000/my_search/的搜索url时,我得到以下错误:
索引'PaperIndex‘必须有一个(且只有一个)带有document=True的SearchField。
如果我删除search_indexes.py,我可以访问搜索页面,但它当然不会工作,因为它没有任何要搜索的信息。
通过调试,它似乎没有拾取任何字段,但它确实看到了这个类。
我试了几种方法,但都不管用。
我的search_indexes.py:
from haystack import indexes
from my_search.models import Paper
class PaperIndex(indexes.SearchIndex, indexes.Indexable):
"""
This is a search index
"""
title = indexes.CharField(model_attr='title'),
abstract = indexes.CharField(document=True,
use_template=False, model_attr='abstract'),
def get_model(self):
return Paper
def index_queryset(self, using=None):
"""Used when the entire index for model is updated."""
return self.get_model().objects # .filter(
pub_date__lte=datetime.datetime.now())我的models.py:
from django.db import models
class Paper(models.Model):
paper_url = models.CharField(max_length=200),
title = models.CharField(max_length=200),
abstract = models.TextField()
authors = models.CharField(max_length=200),
date = models.DateTimeField(max_length=200),
def __unicode__(self):
return self.title谢谢!
发布于 2020-12-30 07:05:37
Haystack为document=True字段使用了一个额外的字段。text = indexes.CharField(document=True)不在模型中,并且haystack在其中转储了一堆可搜索的文本。
Haystack提供了一个辅助方法prepare_text()来填充此字段。或者,也可以使用模板方法,它只是一个带有django模板样式模型属性的txt文件。
class PaperIndex(indexes.SearchIndex, indexes.Indexable):
"""
This is a search index
"""
text = indexes.CharField(document=True)
title = indexes.CharField(model_attr='title'),
abstract = indexes.CharField(model_attr='abstract'),
def get_model(self):
return Paper
def index_queryset(self, using=None):
"""Used when the entire index for model is updated."""
return self.get_model().objects # .filter(
pub_date__lte=datetime.datetime.now())https://stackoverflow.com/questions/65498163
复制相似问题