我想从两个不同的字段中搜索索引模型。例如,有时按名称搜索,有时按职业搜索。有人知道正确的方法吗?这是我当前的search_indexes.py文件:
class JobIndex(indexes.SearchIndex):
text = indexes.CharField(document=True)
name = indexes.CharField(model_attr='name')
occupation = indexes.CharField(model_attr='occupation')
def prepare(self, obj):
self.prepared_data = super(JobIndex, self).prepare(obj)
self.prepared_data['text'] = obj.name
return self.prepared_data
def get_queryset(self):
return Job.objects.filter(status='open')
site.register(Job, JobIndex)
答案 0 :(得分:0)
正确的方法是使用带有过滤器的SearchQuerySet:http://docs.haystacksearch.org/dev/searchqueryset_api.html
在你的情况下,它看起来像:
from haystack.query import SearchQuerySet
sqs = SearchQuerySet()
# Find people named Bob
sqs.filter(name="Bob")
# Find people who are developers
sqs.filter(occupation="developer")
# Or chain searches: Find developers named Bob
sqs.filter(occupation="developer").filter(name="Bob")