【问题标题】:Sitecore 6.6 and Lucene upgrade issueSitecore 6.6 和 Lucene 升级问题
【发布时间】:2013-09-25 18:16:21
【问题描述】:

我们最近升级到 Sitecore 6.6,但在 Lucene 的搜索和爬网功能方面遇到了问题,因为 6.6 使用更新的版本,并且一些方法/功能已过时。

以下代码在以前版本的 Lucene.NET 2.3 中可以正常工作,但在 2.9 中无法正常工作。你能告诉我们我们做错了什么并帮助我们纠正这段代码吗?我们在编译时遇到的错误是

`Lucene.Net.Search.IndexSearcher` does not contain a definition for 'Search'
and no extension method 'Search' accepting a first argument of type 
`Lucene.Net.Search.IndexSearcher` could be found (are you missing a using 
directive or an assembly reference?) 

此错误发生在这一行 - Sitecore.Search.SearchHits hits = new SearchHits(context.Searcher.Search(query,sort));。我猜这将是一个简单的修复,但我不确定如何修复它。

private static SearchResultCollection GetSearchResults(Query query, Sort sort, int startingIndex, int getCount, out int totalHits)
{
    SearchResultCollection retVal = new SearchResultCollection();
    Sitecore.Search.Index searchIndex = Sitecore.Search.SearchManager.GetIndex("content");
    using (Sitecore.Search.IndexSearchContext context = searchIndex.CreateSearchContext())
    {
        Sitecore.Search.SearchHits hits = new SearchHits(context.Searcher.Search(query,sort));
        totalHits = hits.Length;
        //since index is zero based... adjust the numbers 
        startingIndex = (startingIndex - 1) * getCount;
        getCount = (getCount > totalHits || totalHits < startingIndex + getCount) 
            ? hits.Length - startingIndex : getCount;
        retVal = hits.FetchResults(startingIndex, getCount);
    }
    return retVal;
}

谢谢

【问题讨论】:

    标签: lucene sitecore lucene.net sitecore6


    【解决方案1】:

    Sitecore 6.6 使用 Lucene 2.9。下面的代码是您更新的代码以支持较新版本的 Lucene。有 2 个主要变化:

    1. Search 方法使用 2 个附加参数执行(Filter 设置为 nullmaxDocs 设置为 int.MaxValue)。
    2. SearchHits 构造函数将IndexReader 实例作为第二个参数。

    下面的代码应该完全按照您的预期工作。

    using (Sitecore.Search.IndexSearchContext context = searchIndex.CreateSearchContext())
    {
        TopFieldDocs docs = context.Searcher.Search(query, null, int.MaxValue, sort);
        Sitecore.Search.SearchHits hits = new SearchHits(docs, context.Searcher.GetIndexReader());
        totalHits = hits.Length;
        startingIndex = (startingIndex - 1) * getCount;
        getCount = (getCount > totalHits || totalHits < startingIndex + getCount) ? hits.Length - startingIndex : getCount;
        retVal = hits.FetchResults(startingIndex, getCount);
    }
    

    【讨论】:

    • 进行了更改。洞里开火! :)
    • 使用您的代码,我收到以下错误 - prefixCoded 字符串中的移位值无效(编码值真的是 INT 吗?)
    • 任何堆栈跟踪?刚刚在谷歌上发现一个和你有同样问题的人,他的回答是“我为 Numeric 字段索引了一个双精度字段,但我的 Sortfield 设置为浮点数。”
    • 我能够使用您的代码使其正常工作。我收到的错误是在未正确索引的字段上。一旦我解决了这个问题,一切就开始工作了。再次感谢您的输入。
    【解决方案2】:

    对 Sitecore 不是很熟悉,但 Searcher.search(Query, Sort) 在 Lucene 2.9 中已被弃用,并且看起来在 Lucene.Net 中根本不存在。相反,请致电Searcher.search(Query, Filter, int, Sort)。第二个参数 (Filter) 可以为 null,第三个参数 (int) 表示要从搜索中返回的文档数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多