Django haystack过滤器在搜索索引中

时间:2022-09-12 23:18:47

I have a search working fine on my project. But in my models I have a boolean field named is_active.

在我的项目中,我的搜索工作进行得很顺利。但在我的模型中,我有一个名为is_active的布尔字段。

I want the search occurs only when is_active is True, but I've been testing this without any satisfatory response.

我希望搜索只在is_active为真时发生,但我一直在测试它,没有得到任何满意的响应。

my search_indexes.py:

我的search_indexes.py:

    from haystack.indexes import *
    from haystack.sites import site
    from core.models import AnuncioSolucao

    class AnuncioSolucaoIndex(RealTimeSearchIndex):
        text = CharField(document=True,use_template=True)

    site.register(AnuncioSolucao,AnuncioSolucaoIndex)

That way it works, but also bring me all the is_active == False. Any thoughts?

这样,它就可以工作了,但是还可以为我带来is_active == False。任何想法吗?

1 个解决方案

#1


2  

There's a method called read_queryset on the SearchIndex API. I just had to override this:

在SearchIndex API上有一个名为read_queryset的方法。我必须重写这个

class AnuncioSolucaoIndex(RealTimeSearchIndex):
    text = CharField(document=True,use_template=True)
    def read_queryset(self):
        super(AnuncioSolucaoIndex,self).read_queryset()
        return AnuncioSolucao.objects.filter(is_active=True)

#1


2  

There's a method called read_queryset on the SearchIndex API. I just had to override this:

在SearchIndex API上有一个名为read_queryset的方法。我必须重写这个

class AnuncioSolucaoIndex(RealTimeSearchIndex):
    text = CharField(document=True,use_template=True)
    def read_queryset(self):
        super(AnuncioSolucaoIndex,self).read_queryset()
        return AnuncioSolucao.objects.filter(is_active=True)