Sunspot:更新子模型时强制父模型的索引
我正在使用 Sunspot 生成很多应用程序索引和概述。
在这个应用程序中,我有 2 个模型,它们具有父/子一对多关系。使用 Sunspot,我对父级拥有的子级数量进行索引,因此可用于排序、范围界定等。
但是,当我更改子模型时,父级模型不会自动重新索引(因为它没有更改)。通过子进程上的call_back 强制parent.save 也不会强制索引。
因此,在我开始破解之前:
当更改/添加子模型时,在 Sunspot 中强制对父类执行索引操作的最佳方法是什么?
I am using Sunspot to generate alot of my apps indexes and overviews.
In this app i have 2 models which have a parent/child one-to-many relationship. With Sunspot I index the number of childs a parent has, so this is available for sorting, scoping etc.
However, when I change the child model the parent model does not automatically get reindexed (as it hasn't changed). Forcing a parent.save through a call_back on the child doesn't force the index either.
So before I start hacking away:
What would be the best way to force an index action on the parent class in Sunspot when a child model gets changed/added?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我现在也遇到了同样的问题。在查看了 Sunspot 的 API 文档之后,Sunspot 似乎通过以下方式扩展了模型:方法
index()
强制实例重新索引。考虑到这一点,应该只需要挂接到子模型的 after_save 回调中,以便在将其存储到数据库时重新索引父模型:
I was having the same problem right now. After looking into the API documentation for Sunspot, it seems that Sunspot extends models with a method
index()
that forces an instance to be reindexed.With this in mind, it should be just a matter of hooking into the after_save callback of the child model, to reindex the parent when this is stored onto the database: