像 QuerySet 一样过滤 Django Haystack 结果?
是否可以将 Django Haystack 搜索与“内置”QuerySet 过滤操作结合起来,特别是使用 Q() 实例进行过滤以及 SearchQuerySet 不支持的查找类型?无论哪种顺序:
haystack-searched -> queryset-filtered
或
queryset-filtered -> haystack-searched
浏览 Django Haystack 文档都没有给出任何指示如何执行此操作。
Is it possible to combine a Django Haystack search with "built-in" QuerySet filter operations, specifically filtering with Q() instances and lookup types not supported by SearchQuerySet? In either order:
haystack-searched -> queryset-filtered
or
queryset-filtered -> haystack-searched
Browsing the Django Haystack documentation didn't give any directions how to do this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以根据 Haystack 搜索的结果,使用对象的 PK 来过滤您的查询集:
不确定这是如何扩展的,但对于小型结果集(在我的例子中是几百个),这工作得很好。
You could filter your queryset based on the results of a Haystack search, using the objects' PKs:
Not sure how this scales, but for small resultsets (a few hundred, in my case), this works fine.
来自文档:
SearchQuerySet.load_all(self)
http://django-haystack.readthedocs.org/en/latest /searchqueryset_api.html#load-all
因此,在过滤了 SQS 后,您可以对其执行 load_all() 操作,然后仅通过 SearchResult.object 访问数据库对象。例如
From the docs:
SearchQuerySet.load_all(self)
http://django-haystack.readthedocs.org/en/latest/searchqueryset_api.html#load-all
Therefore, after you have a filtered SQS, you can do a load_all() on it and just access the database objects via SearchResult.object. E.g.
如果你想跟上相关性,你必须通过“object”从数据库访问对象:
模板中的示例:
但这真的很糟糕,因为 haystack 会发出一个额外的请求,例如“SELECT * FROM blah WHERE id =每个结果为 42"。
似乎您正在尝试从数据库中获取这些对象,因为您没有在索引中添加一些额外的字段,不是吗?如果您在 SearchIndex 中添加标题和作者,那么您可以只使用您的结果:
并避免一些额外的查询。
If you want to keep up with the pertinence, you have to access the object from the database through "object" :
example in your template:
But this is really bad since haystack will make an extra request like "SELECT * FROM blah WHERE id = 42" on each results.
Seems you're trying to get those object from your database because you didn't put some extra fields in your index ins't it ? If you add the title AND the author in your SearchIndex, then you can just use your results:
and avoid some extra queries.