Sitecore solr索引重建和刷新之间的区别

时间:2015-05-15 08:41:44

标签: indexing solr sitecore sitecore7.2

我们想知道Sitecore full index rebuildindex refresh之间的区别。当我们想要在索引中刷新项目时花了~2分钟。完全重建主索引需要~10分钟。为什么索引刷新是如此耗时?

我们使用此代码刷新项目:

Sitecore.Data.Item item = GetItem();
var masterIndex = ContentSearchManager.GetIndex("sitecore_master_index");
masterIndex.Refresh(new SitecoreIndexableItem(item));

1 个答案:

答案 0 :(得分:1)

我正在查看Sitecore.ContentSearch程序集的反编译源代码,看起来Refresh方法最终调用RefreshTree类上的IndexCustodian方法。 RefreshTree将为要编制索引的项创建一个新的对象数组,然后迭代所有可用的索引(即使您已从特定索引调用它)并为每个索引创建刷新作业(将其添加到队列中进行处理):

public static IEnumerable<Job> RefreshTree(IIndexable startItem)
{
    Assert.ArgumentNotNull(startItem, "startItem");
    CrawlingLog.Log.Debug(string.Format("IndexCustodian. RefreshTree triggered on item {0}.", startItem.AbsolutePath), null);
    return 
        from index in ContentSearchManager.Indexes
        select IndexCustodian.Refresh(index, startItem);
}

public static Job Refresh(ISearchIndex index, IIndexable indexable)
{
    Assert.ArgumentNotNull(index, "index");
    Assert.ArgumentNotNull(indexable, "indexable");
    object[] objArray = new object[] { indexable };
    JobOptions jobOptions = IndexCustodian.GetJobOptions(index, "Refresh", objArray, true);
    jobOptions.CustomData = indexable;
    CrawlingLog.Log.Debug(string.Format("IndexCustodian. Refresh triggered on index {0}. Data={1}", index.Name, jobOptions.CustomData), null);
    return JobManager.Start(jobOptions);
}

FullRebuild方法(也在IndexCustodian中)调用CreateRebuildJob,直接创建一个要处理的作业:

public static Job FullRebuild(ISearchIndex index, bool start = true)
    {
        return IndexCustodian.CreateRebuildJob(index, new EventHandler<JobStartedEventArgs>(IndexCustodian.RebuildStartedHandler), new EventHandler<JobFinishedEventArgs>(IndexCustodian.RebuildFinishedHandler), start, null);
    }

private static Job CreateRebuildJob(ISearchIndex index, EventHandler<JobStartedEventArgs> startedHandler = null, EventHandler<JobFinishedEventArgs> finishedHandler = null, bool start = true, object[] parameters = null)
    {
        Assert.ArgumentNotNull(index, "index");
        JobOptions jobOptions = IndexCustodian.GetJobOptions(index, "Rebuild", parameters, false);
        Job job = new Job(jobOptions);
        if (startedHandler != null)
        {
            job.Started += startedHandler;
        }
        if (finishedHandler != null)
        {
            job.Finished += finishedHandler;
        }
        if (start)
        {
            CrawlingLog.Log.Warn(string.Format("IndexCustodian. FullRebuild triggered on index {0}.", index.Name), null);
            JobManager.Start(job);
        }
        return job;
    }

因此,Refresh方法通过迭代索引会产生一些开销,然后依赖于JobManager来排队和处理刷新作业(如果找到了多个索引,则会有多个)。