7

我对 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

据我了解,我的定义index是每次运行时有效地重新索引集合。

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

我正在使用宝石sunspot v2.0.0,,sunspot_mongo v1.0.1sunspot_solr v2.0.0

4

1 回答 1

8

重新索引任务只调用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 

如果您查看 的来源solr_index,评论说该方法将“添加/更新 Solr 索引中的所有现有记录”。

所以回答你的问题,reindex任务本质上与index任务相同,只是reindex任务将首先删除现有索引。如果您的索引中有您知道不应该存在的项目,您应该调用reindex. 如果您知道您只是在索引中添加或更新项目,您可以调用index而不会遭受删除索引然后从头开始重建它的性能损失。

于 2013-04-22T19:58:37.810 回答