我们最近升级到 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;
}
谢谢
最佳答案
Sitecore 6.6 使用 Lucene 2.9。下面的代码是您更新后的代码以支持较新版本的 Lucene。有 2 个主要变化:
Search
方法使用 2 个附加参数执行(Filter
设置为null
和maxDocs
设置为int.MaxValue
)。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);
}
关于lucene - Sitecore 6.6 和 Lucene 升级问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19012273/