像 QuerySet 一样过滤 Django Haystack 结果?

发布于 2024-08-19 17:12:52 字数 297 浏览 5 评论 0原文

是否可以将 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 技术交流群。

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

发布评论

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

评论(3

无名指的心愿 2024-08-26 17:12:52

您可以根据 Haystack 搜索的结果,使用对象的 PK 来过滤您的查询集:

def view(request):
  if request.GET.get('q'):
    from haystack import ModelSearchForm
    form = ModelSearchForm(request.GET, searchqueryset=None, load_all=True)
    searchqueryset = form.search()
    results = [ r.pk for r in searchqueryset ]

    docs = Document.objects.filter(pk__in=results)
    # do something with your plain old regular queryset

    return render_to_response('results.html', {'documents': docs});

不确定这是如何扩展的,但对于小型结果集(在我的例子中是几百个),这工作得很好。

You could filter your queryset based on the results of a Haystack search, using the objects' PKs:

def view(request):
  if request.GET.get('q'):
    from haystack import ModelSearchForm
    form = ModelSearchForm(request.GET, searchqueryset=None, load_all=True)
    searchqueryset = form.search()
    results = [ r.pk for r in searchqueryset ]

    docs = Document.objects.filter(pk__in=results)
    # do something with your plain old regular queryset

    return render_to_response('results.html', {'documents': docs});

Not sure how this scales, but for small resultsets (a few hundred, in my case), this works fine.

愚人国度 2024-08-26 17:12:52

来自文档:

SearchQuerySet.load_all(self)

有效地填充搜索结果中的对象。不使用
这种方法,数据库查找是在每个对象的基础上完成的,结果是
许多个人访问数据库。如果使用load_all,则
SearchQuerySet 会将相似的对象分组到单个查询中,
导致的查询数量与不同对象类型的数量相同
返回。

http://django-haystack.readthedocs.org/en/latest /searchqueryset_api.html#load-all

因此,在过滤了 SQS 后,您可以对其执行 load_all() 操作,然后仅通过 SearchResult.object 访问数据库对象。例如

sqs = SearchQuerySet()
# filter as needed, then load_all
sqs = sqs.load_all()

for result in sqs:
    my_obj = result.object
    # my_obj is a your model object

From the docs:

SearchQuerySet.load_all(self)

Efficiently populates the objects in the search results. Without using
this method, DB lookups are done on a per-object basis, resulting in
many individual trips to the database. If load_all is used, the
SearchQuerySet will group similar objects into a single query,
resulting in only as many queries as there are different object types
returned.

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.

sqs = SearchQuerySet()
# filter as needed, then load_all
sqs = sqs.load_all()

for result in sqs:
    my_obj = result.object
    # my_obj is a your model object
打小就很酷 2024-08-26 17:12:52

如果你想跟上相关性,你必须通过“object”从数据库访问对象:

模板中的示例:

{% for result in results %}
    {{ result.object.title }}
    {{ result.objects.author }}
{% endfor %}

但这真的很糟糕,因为 haystack 会发出一个额外的请求,例如“SELECT * FROM blah WHERE id =每个结果为 42"。

似乎您正在尝试从数据库中获取这些对象,因为您没有在索引中添加一些额外的字段,不是吗?如果您在 SearchIndex 中添加标题和作者,那么您可以只使用您的结果:

{% for result in results %}
    {{ result.title }}
    {{ result.author }}
{% endfor %}

并避免一些额外的查询。

If you want to keep up with the pertinence, you have to access the object from the database through "object" :

example in your template:

{% for result in results %}
    {{ result.object.title }}
    {{ result.objects.author }}
{% endfor %}

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:

{% for result in results %}
    {{ result.title }}
    {{ result.author }}
{% endfor %}

and avoid some extra queries.

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