【发布时间】:2020-11-12 09:49:26
【问题描述】:
我的 SearchIndex 中有这个:
class UserIndex(SearchIndex, Indexable):
text = CharField(document=True, use_template=True)
likes = IntegerField()
def index_queryset(self, using=None):
return self.get_model().objects.all()
def get_model(self):
return User
def prepare_likes(self, obj):
# Logging here just because it's the first "prepare" function.
log.debug("Indexing %s: %d" % (obj.__class__.__name__, obj.pk))
return obj.get_all_likes()
我的数据库中有一个对象。当我运行update_index 时,log.debug 被打印 3 次以用于 pk 1 的对象。
我做错了什么?
【问题讨论】:
-
也许是基类在初始
prepare_likes之后调用了prepare方法?另一个想法可能是 haystack 设置中有多个索引? -
@user1464664 谢谢!除非我误解,否则第一个假设有点奇怪?奇怪的是,无论基类做什么,对象都会被多次准备……现在它需要的时间是需要的 3 倍。第二个假设...是的
UserIndex不是我唯一的索引,但其他索引是针对其他模型的。这是你的意思吗? -
对于多索引建议,我指的是 settings.py 中可能包含的内容。 Haystack 将为在
HAYSTACK_CONNECTIONS设置中找到的每个索引循环。我已经在多语言项目中看到了这一点。 -
@user1464664 啊好的。不,我只有一个连接到一台 Elasticsearch 服务器。还有什么想法吗?这是唯一一个表现出这种行为的
SearchIndex。 -
@user1464664 没关系,请忽略:
prepare_likes,记录发生的地方,被另一个准备函数调用。 ??????????♂️