干草堆多字段搜索
作者:互联网
嗨,我正在用干草堆作为搜索引擎:
我的模型如下
class Person(models.Model):
personid = models.IntegerField(primary_key = True, db_column = 'PID')
firstname = models.CharField(max_length = 50, db_column = 'FIRSTNAME')
lastname = models.CharField(max_length = 50, db_column = 'LASTNAME')
class Meta:
db_table = '"TEST"."PERSON"'
managed = False
class TDoc(models.Model):
tdocid = models.IntegerField(primary_key = True, db_column = 'TDOCID')
person = models.ForeignKey(Person, db_column = 'PID')
content = models.TextField(db_column = 'CONTENT', blank = True)
filepath = models.TextField(db_column = 'FILEPATH', blank = True)
class Meta:
db_table = '"TEST"."TDOC"'
managed = False
search_index.py如下:
class TDocIndex(SearchIndex):
content = CharField(model_attr = 'content', document = True)
filepaht = CharField(model_attr = 'filepath')
person = CharField(model_attr = 'person')
def get_queryset(self):
return TDoc.objects.all()
def prepare_person(self, obj):
# Store a list of id's for filtering
return obj.person.lastname
site.register(TDoc, TDocIndex)
我的问题是我想进行多个归档搜索,例如
content:xxx AND person:SMITH
在干草堆上,它无法一次进行所有字段搜索,而我无法进行特定的字段搜索.
我怀疑我的索引已损坏,但是:
ix = open_dir("/testindex")
searcher = ix.searcher()
mparser = MultifieldParser(["content", "filepath", "person"], schema = ix.schema)
myquery = mparser.parse(content:xxx AND person:SMITH')
results = searcher.search(myquery)
for result in results:
print result
但它可以正常工作并返回正确的值.
我正在使用本教程中的标准haystack SearchView,search.html
(r'^search/', include('haystack.urls')),
解决方法:
在索引中,您应该使用document = True定义一个字段,这是干草堆将在其中搜索的文档.按照惯例,该字段称为文本.如果计划对它们的值进行过滤或排序,则可以添加其他字段.
执行搜索时考虑多个字段的方法是将文档定义为模板,并在文档字段上设置use_template.您的索引如下所示:
class TDocIndex(SearchIndex):
text = CharField(document=True, use_template=True)
#if you plan to filter by person
personid = IntegerField(model_attr='person__id')
site.register(TDoc, TDocIndex)
您将拥有一个search / indexes / tdoc_text.txt模板,例如:
{{ object.content }}
{{ object.filepath }}
{{ object.person.lastname }}
参见this答案.
标签:whoosh,search,python,django-haystack 来源: https://codeday.me/bug/20191105/1998477.html