维持Sunspot Solr指数的正确方法是什么?

Yev*_*niy 7 indexing solr sunspot mongoid

我对Solr索引机制感到困惑.也许有人可以对此有所了解.

所以,我们有2个rake命令:rake sunspot:solr:indexrake sunspot:solr:reindex

这是我的index任务看起来像(我为Mongoid重写它):

namespace :sunspot do
  namespace :solr do 
    desc "indexes searchable models"
    task :index => :environment do
      [Model1, Model2].each do |model|
        Sunspot.index!(model.all)
      end
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

据我所知,我的定义index是每次运行时都有效地重新索引集合.

我对吗?它是否会覆盖以前的索引,还是必须使用它reindex来删除旧索引并创建新索引?

我用的宝石sunspot v2.0.0,sunspot_mongo v1.0.1,sunspot_solr v2.0.0

ere*_*eit 8

reindex任务只调用solr_reindex每个模型,该方法的源代码如下(取自github).

   # Completely rebuild the index for this class. First removes all 
   # instances from the index, then loads records and indexes them.
   #   
   # See #index for information on options, etc.
   #   
   def solr_reindex(options = {}) 
     solr_remove_all_from_index
     solr_index(options)
   end 
Run Code Online (Sandbox Code Playgroud)

如果查看源代码solr_index,则注释表示该方法将"添加/更新Solr索引中的所有现有记录".

所以要回答你的问题,reindex任务基本上与index任务相同,只是reindex任务将首先删除现有索引.如果你的索引中有你不知道的项目,你应该打电话reindex.如果您知道只是在索引中添加或更新项目,那么您只需调用index并且不会因丢弃索引而遭受性能损失,然后从头开始重建.