SearchManager 与 ContentSearchManager?

SearchManager vs ContentSearchManager?

我一直在深入研究 sitecore 的搜索功能,直到现在我一直使用以前的开发人员使用的功能。我正在研究一个问题,其中某些谓词似乎没有在下面的代码段中起作用:

public IEnumerable<IndexedEvent> SearchItems(Expression<Func<IndexedEvent, bool>> predicate)
{
   using (IProviderSearchContext _context = ContentSearchManager.GetIndex(indexName).CreateSearchContext())
   {
       IEnumerable<IndexedEvent> results = _context
                .GetQueryable<IndexedEvent>()
                .Where(predicate);

            return results;

   }
}

上面的不是我写的,我只是用而已

当我遇到这个示例问题 Very basic usage of sitecore search 时,我正在调查我的问题,其中包含代码:

// use id of from the index configuration
using (IndexSearchContext indexSearchContext = SearchManager.GetIndex("my-custom-index").CreateSearchContext())
{
    // MatchAllDocsQuery will return everything. Use proper query from the link below
    SearchHits hits = indexSearchContext.Search(new MatchAllDocsQuery(), int.MaxValue);
    // Get Sitecore items from the results of the query
    List<Item> items = hits.FetchResults(0, int.MaxValue).Select(result => result.GetObject<Item>()).Where(item => item != null).ToList();
}

现在这似乎使用了一种完全不同的方法来查询索引,IndexSearchContext 而我的代码(不是我写的)使用 IProviderSearchContext。我找不到关于这两者的任何文档,它们位于完全不同的程序集中。

那么问题来了,什么时候用IndexSearchContext,什么时候用IProviderSearchContext?这里有任何根本的区别,还是只是实现相同净结果的两种方法?

您使用 SearchManagerIndexSearchContext 引用的问题和代码来自 Sitecore 6。带有 ContentSearchManagerIProviderSearchContext 的代码适用于 Sitecore 7或 8(好吧,就是 7+)。

因此,如果您的代码如您的标签和代码示例所建议的那样适用于 Sitecore8,那么 ContentSearchManager 是最佳选择。