大海捞针搜索非主要领域
我希望有任何从两个不同字段搜索索引模型的选项。例如,有时按姓名搜索,有时按职业搜索。有人知道采取正确的方法吗?这是我当前的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正确的方法是使用带有过滤器的 SearchQuerySet: http://docs.haystacksearch.org/dev/ searchqueryset_api.html
在你的情况下,它看起来像:
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: