2013-09-25 60 views
3

我們最近升級到Sitecore 6.6,並且由於6.6使用更新的版本並且已經使一些方法/功能過時而遇到來自Lucene的搜索和爬取功能的問題。Sitecore 6.6和Lucene升級問題

以下代碼用於與以前版本的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; 
} 

感謝

回答

4

Sitecore 6.6使用Lucene 2.9。下面的代碼是您的代碼已更新,以支持更新版本的Lucene。有2度主要的改變:用2個額外的參數(Filter被設置爲nullmaxDocs被設定爲int.MaxValue)執行

  1. Search方法。
  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); 
} 
+0

發生了變化。在洞裏射擊! :) – Gabbar

+0

隨着你的代碼,我得到了以下錯誤 - prefixCoded字符串中的無效移位值(編碼值真的是INT?) – Gabbar

+0

任何stacktrace?剛剛在谷歌上找到一個和你有同樣問題的人,他的回答是「我爲數字字段索引了一個double,但我的Sortfield被設置爲一個浮點數。」 –

3

並不十分熟悉Sitecore的,但Searcher.search(Query, Sort)被棄用的Lucene 2.9,看起來就像是不存在Lucene.Net的。相反,請致電Searcher.search(Query, Filter, int, Sort)。第二個參數(Filter)可以爲空,第三個參數(int)指示從搜索返回的文檔數量。