Sunspot/Solr/Rails:模型关联未在索引中更新 [英] Sunspot / Solr / Rails: Model Associations are not updating in the Index

查看:67
本文介绍了Sunspot/Solr/Rails:模型关联未在索引中更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序中有一个 Fieldnote 模型,它通过一个名为 :fieldnote_activities 的表附加了一个 Fieldnote 模型.然后我这样定义一个可搜索的索引:

I have a Fieldnote model in my app, which has_many :activities attached to it through a table called :fieldnote_activities. I then define a searchable index this way:

searchable :auto_index => true, :auto_remove => true do
  integer :id
  integer :user_id, :references => User

  integer :activity_ids, :multiple => true do
    activities.map(&:id)
  end

  text :observations
 end

然后我有一个搜索模型来存储/更新搜索.因此,搜索模型也与活动有自己的关联.然后我像这样执行我的搜索:

And then I have a Search model to store / update searches. The search model thus also has its own associations with activities. I then perform my searches like this:

@search = Search.find(params[:id])
@query  = Fieldnote.search do |query|
  query.keywords  @search.terms

  if @search.activities.map(&:id).empty? == false
    query.with    :activity_ids, @search.activities.map(&:id)
  end

end
@fieldnotes = @query.results

现在这一切都很好.问题是,如果我更改与 fieldnote 关联的活动,搜索结果不会更改,因为该 fieldnote 的索引似乎不会更改.我的印象是,当我定义可搜索索引时, :auto_index => true 和 :auto_remove => true 标志会跟踪新的关联(或删除的关联),但情况似乎并非如此.我该如何解决这个问题?

Now this all works GREAT. The problem is that if I change which activities that are associated with a fieldnote, the search results do not change because it appears the indices for that fieldnote do not change. I was under the impression that the :auto_index => true and :auto_remove => true flags when I define the searchable index would keep track of new associations (or deleted associations), but this appears not to be the case. How do I fix this?

推荐答案

你说得对,:auto_index:auto_remove 不适用于关联对象,只是指定它们的 searchable 对象.

You're right that :auto_index and :auto_remove don't apply to associated objects, just the searchable object they are specified on.

反规范化时,您应该在关联对象上使用 after_save 钩子以在必要时触发重新索引.在这种情况下,您希望更改 Activity 模型FieldnoteActivity 连接模型以触发对其关联的 Fieldnote保存或销毁时的对象.

When denormalizing, you should use after_save hooks on the associated objects to trigger a reindex where necessary. In this case, you want changes to the Activity model and the FieldnoteActivity join model to trigger a reindex of their associated Fieldnote objects when saved or destroyed.

class Fieldnote
  has_many :fieldnote_activities
  has_many :activities, :through => :fieldnote_activities

  searchable do
    # index denormalized data from activities
  end
end

class FieldnoteActivity
  has_many :fieldnotes
  has_many :activities

  after_save :reindex_fieldnotes
  before_destroy :reindex_fieldnotes

  def reindex_fieldnotes
    Sunspot.index(fieldnotes)
  end
end

class Activity
  has_many :fieldnote_activities
  has_many :fieldnotes, :through => :fieldnote_activities

  after_save :reindex_fieldnotes
  before_destroy :reindex_fieldnotes

  def reindex_fieldnotes
    Sunspot.index(fieldnotes)
  end
end

这篇关于Sunspot/Solr/Rails:模型关联未在索引中更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆