Sunspot Rails-防止在保存时建立索引

alp*_*eus 4 solr ruby-on-rails sunspot sunspot-rails sunspot-solr

缺省情况下,sunspot solr gem作为保存回调的一部分向solr服务器发出索引命令。这种行为在我的大多数应用程序中都是可以接受的,但是在它的某些部分(尤其是用于大量处理的rake任务中),我希望在不与solr服务器进行任何交互的情况下保存我的模型实例。我该如何实现?

zrl*_*3dx 5

根据文档,我认为您应该添加auto_index: false到您的可搜索块中,即:

class Foo < ActiveRecord::Base
  searchable auto_index: false do
    # your search fields here
  end
end
Run Code Online (Sandbox Code Playgroud)

然后,您可以使用以下方法手动重新索引记录:

# On a class itself
Person.reindex
Sunspot.commit # or commit(true) for a soft commit (Solr4)

# On mixed objects
Sunspot.index [post1, item2]
Sunspot.index person3
Sunspot.commit # or commit(true) for a soft commit (Solr4)

# With autocommit
Sunspot.index! [post1, item2, person3]
Run Code Online (Sandbox Code Playgroud)