c# - SearchManager 与 ContentSearchManager?

标签 c# lucene sitecore sitecore8

我非常认真地研究了 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?这里有任何根本的区别,还是只是实现相同净结果的两种方式?

最佳答案

您在 SearchManager 中提到的问题和代码和 IndexSearchContext来自 Sitecore 6。带有 ContentSearchManager 的代码和 IProviderSearchContext适用于 Sitecore 7 或 8(好吧,就是 7+)。

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

关于c# - SearchManager 与 ContentSearchManager?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40288582/

相关文章:

lucene - 将lucene索引分成两半

sitecore - 在 Sitecore 中修补 web.config

c# - Web 浏览器 Hook 和自动化

C# 变量作用域和 "switch"语句?

c# - 如何获取在 asp.net 中使用我的 web api 的请求 url?

search - Elasticsearch-为什么更新文档后我没有得到相同的搜索结果?

c# - 将对象设置为 null 而不将其作为 ref 传递

elasticsearch - Elasticsearch中的SpanWithInQuery和SpanContainingQuery有什么区别?

sitecore - 自动取消发布 sitecore 中的项目

sitecore - 从媒体库中选择图像时默认为 "Browse"选项卡?