0

我想要从两个不同的字段中搜索索引模型的任何选项。例如,有时按姓名搜索,有时按职业搜索。有人知道正确的方法吗?这是我当前的 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)
4

1 回答 1

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")
于 2011-02-03T06:29:15.293 回答