大海捞针搜索非主要领域

发布于 2024-10-15 11:02:57 字数 584 浏览 2 评论 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)

I would like to have any option of searching an indexed model from two different fields. For example, sometimes search by name and other times search by occupation. Does someone know the right approach to take? This is my current search_indexes.py file:

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

遮了一弯 2024-10-22 11:02:57

正确的方法是使用带有过滤器的 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")

The right approach is to use a SearchQuerySet with filters: http://docs.haystacksearch.org/dev/searchqueryset_api.html

In your case, it'd look something like:

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")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文