关于模型继承的属性的干草堆索引

时间:2014-07-09 21:00:13

标签: python django django-models elasticsearch django-haystack

当model_attr引用继承属性时,我在Haystack(Elasticsearch)中对索引进行过滤时遇到问题。

例如,使用代码:

django models.py

Parent(models.Model):

    is_active = models.BooleanField(default=False)

Child(Parent):

    title = models.CharField(max_length=255)

search_index.py

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

    text = indexes.EdgeNgramField(document=True, use_template=True)
    title = indexes.CharField(model_attr='title')
    is_active = indexes.BooleanField(model_attr='is_active')

    def get_model(self):
        return Child

    def index_queryset(self, using=None):
        return self.get_model().objects.all()

以及以下实例

Child.objects.create(title='matches keyword', is_active=True)
Child.objects.create(title='also matches keyword but not active', is_active=False)

使用"关键字"进行搜索和SearchQuerySet()。models(资源).filter(is_active = True)将返回两个实例,只有第一个预期... 我对Haystack没有多少经验,但在我看来,它甚至没有考虑索引。 例如。 SearchQuerySet()。models(Resource).filter(is_active ='甚至不是布尔值的东西)也将返回两个结果。我用rebuild_index刷新了我的索引。

非常感谢任何帮助!

2 个答案:

答案 0 :(得分:0)

在Haystack中,您将布尔字段过滤为:

SearchQuerySet().models(Resource).filter(is_active='true') # or 'false'

答案 1 :(得分:0)

问题实际上与继承无关,但是正如Aamir所建议的那样,查询集的过滤方式也是如此。 解决方案是SearchQuerySet()。models(Resource).filter(is_active = 1)

相关问题