问题描述
我正在尝试使用 django-haystac 和 whoosh 编写一个小搜索引擎。 我改编了他们的教程,我从一个 JSON 文件创建了我的索引并使用 QueryParser 成功查询它,现在我正在尝试使用他们的视图。
当我尝试访问搜索网址时:http://127.0.0.1:8000/my_search/ 我收到错误:
索引 '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
谢谢!
解决方法
Haystack 为 struct illedetayyip: View {
@State private var iliskiDurumlari: [String] = ["Evli","Bekar","Ayrı","anan"]
@State private var iliskiVisible = false
@State private var iliski = ""
var body: some View {
VStack {
TextField("İlişki Durumu Seçiniz..",text: $iliski)
.frame(width: 300,height: 50,alignment: .center)
.padding(5)
.font(Font.system(size: 15,weight: .medium,design: .serif))
.overlay(
RoundedRectangle(cornerRadius: 30)
.stroke(Color(red: 45 / 255,green: 0 / 255,blue: 112 / 255),lineWidth: 1)
)
.actionSheet(isPresented: $iliskiVisible,content: actionSheet)
.onTapGesture {
self.iliskiVisible.toggle()
}
}
}
func actionSheet() -> ActionSheet {
ActionSheet(
title: Text("Bir İlişki Durumu Seçiniz"),message: Text("Aşagıda"),buttons: iliskiDurumlari.map { value in
ActionSheet.Button.default(Text(value),action: {
self.iliski = value
})
}
)
}
}
字段使用了一个额外的字段。
document=True
不在模型上,haystack 在其中转储了一堆可搜索的文本。
Haystack 提供了一个辅助方法 text = indexes.CharField(document=True)
来填充此字段。或者,可以使用模板方法,它只是一个带有 django 模板样式模型属性的 txt 文件。
prepare_text()