Django Haystack:根据列表中的多个项目过滤查询。

发布于 2025-01-01 18:06:53 字数 1473 浏览 2 评论 0原文

我有可以属于一个或多个组织模型实例的事件模型实例。我已经实现了 haystack 2.0.0 来索引我的所有事件。这是一个搜索索引示例。

class EventIndex(indexes.SearchIndex, indexes.Indexable):

    text = indexes.CharField(document=True, use_template=True)
    organization_slug = indexes.CharField(model_attr='organization__slug',
                        weight=5.0)
    organization_name = indexes.CharField(model_attr='organization__name', 
                        weight=5.0)
    name = indexes.CharField(model_attr='name', weight=10.0)

    ....    

    def get_model(self):
        return Event

    def index_queryset(self):
        return Event.objects.filter()

我的问题是如何构建一个 SearchQuerySet 查询来根据一个或多个组织过滤事件。例如,我想查找属于“orgnization1”和“organization3”的所有事件(其中列表组织可以是任意长度)

作为 Django 查询,它可能看起来像这样:

Event.objects.filter(organization__in=[orgnization1, organization3]).filter(...)

How do Itranslate this to a haystack query?这是我的尝试,但我真的不知道我在做什么...

organization_list = [organization1.slug, organization2.slug]
SearchQuerySet().filter_or(organization__contains=organization_list)

这是我的模型外观的示例:

class Event(models.Model):
    name = models.CharField(max_length=64) 
    organization = models.ForeignKey('mymodule.Organization')
    ...

class Organization(models.Model):
    slug = models.SlugField(max_length=64)
    name = models.CharField(max_length=64)
    ... 

非常感谢任何帮助。

I have Event model instances that can belong to one or more Organization model instances. I've implemented haystack 2.0.0 to index all of my Events. Here is an example search index.

class EventIndex(indexes.SearchIndex, indexes.Indexable):

    text = indexes.CharField(document=True, use_template=True)
    organization_slug = indexes.CharField(model_attr='organization__slug',
                        weight=5.0)
    organization_name = indexes.CharField(model_attr='organization__name', 
                        weight=5.0)
    name = indexes.CharField(model_attr='name', weight=10.0)

    ....    

    def get_model(self):
        return Event

    def index_queryset(self):
        return Event.objects.filter()

My question is how do I construct a SearchQuerySet query that filters Events based on one or several Organizations. For example, I want find all Events that belong to "orgnization1" and "organization3" (where the list of organizations can be any length long)

As a Django query it might look something like this:

Event.objects.filter(organization__in=[orgnization1, organization3]).filter(...)

How do I translate this to a haystack query? This is my attempt, but I don't really know what I'm doing...

organization_list = [organization1.slug, organization2.slug]
SearchQuerySet().filter_or(organization__contains=organization_list)

Here is an example of how my models look:

class Event(models.Model):
    name = models.CharField(max_length=64) 
    organization = models.ForeignKey('mymodule.Organization')
    ...

class Organization(models.Model):
    slug = models.SlugField(max_length=64)
    name = models.CharField(max_length=64)
    ... 

Any help is much appreciated.

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

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

发布评论

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

评论(1

巷雨优美回忆 2025-01-08 18:06:53

我想我已经找到了解决方案。只是分享而已。显然,Haystack 有一个名为 SQ() 的对象,其功能类似于 Django 的 Q() 对象。我发现您可以在 Django 的 Q 对象实例上调用 add() 方法来添加更多查询参数。看来SQ的工作原理是一样的。

from haystack.forms import SearchForm
from haystack.query import SQ, SearchQuerySet
from haystack.views import SearchView

class CustomSerchView(SearchView):


    def __call__(self, request):

        self.request = request

        ########### Custom stuff
        user = request.user
        organization_list = [organization1.slug, organization2.slug, ....]

        sq = SQ()
        for slug in organization_list:
            sq.add(SQ(organization_slug=slug), SQ.OR)
        sqs = SearchQuerySet().filter(sq)        
        ##########

        self.form = self.build_form(form_kwargs={'searchqueryset':sqs})
        self.query = self.get_query()
        self.results = self.get_results()
        return self.create_response()

I think I've found a solution. Just sharing it. Apparently, Haystack has an object called a SQ(), which functions similar to Django's Q() object. I found somewhere that you can invoke the add() method on Django's Q object instances to add more query parameters. Seems to work the same way with SQ.

from haystack.forms import SearchForm
from haystack.query import SQ, SearchQuerySet
from haystack.views import SearchView

class CustomSerchView(SearchView):


    def __call__(self, request):

        self.request = request

        ########### Custom stuff
        user = request.user
        organization_list = [organization1.slug, organization2.slug, ....]

        sq = SQ()
        for slug in organization_list:
            sq.add(SQ(organization_slug=slug), SQ.OR)
        sqs = SearchQuerySet().filter(sq)        
        ##########

        self.form = self.build_form(form_kwargs={'searchqueryset':sqs})
        self.query = self.get_query()
        self.results = self.get_results()
        return self.create_response()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文