干草堆多字段搜索 [英] haystack multiple field search
问题描述
您好,我将干草堆和woosh用作搜索引擎:
Hi i m using haystack with a woosh as search engine:
我的模型如下所示
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如下:
The search_index.py is as follows:
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)
我的问题是我想进行多次归档搜索,例如
My problem is i would like to do multiple filed searches like
content:xxx AND person:SMITH
content:xxx AND person:SMITH
在干草堆上一次搜索所有这些对象我无法进行特定领域的搜索。
我怀疑索引已损坏,但:
On haystack it search all of them at once i can't do specific field search. I suspected that my index is corrupt but:
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
,但是它可以正常工作并返回正确的值。
我使用标准的干草堆SearchView,search.html来自教程
but it works and return's the correct value. I m using the standard haystack SearchView,search.html from the tutorial
(r'^search/', include('haystack.urls')),
推荐答案
在您的索引应该使用 document = True
定义一个字段,这是干草堆将在其中搜索的文档。按照约定,该字段被命名为 text
。如果打算对它们的值进行过滤或排序,则可以添加其他字段。
In your index you should define one field with document=True
, which is the document haystack will search on. By convention this field is named text
. You add extra fields if you plan to do filtering or ordering on their values.
执行搜索时考虑多个字段的方法是定义文档作为模板,然后在文档字段中设置 use_template
。您的索引如下所示:
The way to take several fields in account when you perform a search, is to define the document as a template, and set use_template
on your document field. Your Index would look like:
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模板,例如:
And you'd have a search/indexes/tdoc_text.txt template like:
{{ object.content }}
{{ object.filepath }}
{{ object.person.lastname }}
请参见这个答案。
这篇关于干草堆多字段搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!