【发布时间】: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