【问题标题】:Haystack index on attribute inherited by model模型继承的属性的干草堆索引
【发布时间】:2014-07-09 21:00:12
【问题描述】:

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

例如,使用代码:

django 模型.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(Resource).filter(is_active=True) 的搜索将返回这两个实例,而只需要第一个实例... 我对 Haystack 没有太多经验,但在我看来,它甚至没有考虑索引。 例如。 SearchQuerySet().models(Resource).filter(is_active='something that even not a Boolean') 也将返回两个结果。我已经用rebuild_index 刷新了我的索引。

任何帮助将不胜感激!

【问题讨论】:

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


    【解决方案1】:

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

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

    【讨论】:

    • 谢谢阿米尔。问题依旧,haystack好像没有过滤
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-02
      • 1970-01-01
      • 1970-01-01
      • 2021-11-29
      • 1970-01-01
      • 2011-11-25
      相关资源
      最近更新 更多