3
votes

We recently upgraded to Sitecore 6.6 and are running into issues with the search and crawl functionality from Lucene since 6.6 uses a newer version and has some of the methods/functions have been made obsolete.

The code below used to work fine with the previous version of Lucene.NET 2.3 but isn't working in 2.9. Can you tell us what we are doing wrong and help us rectify this piece of code? The error we get while compiling is

`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?) 

This error happens on this line - Sitecore.Search.SearchHits hits = new SearchHits(context.Searcher.Search(query,sort));. I'm guessing that it will be a simple fix but I'm unsure of how to go about fixing it.

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;
}

Thanks

2

2 Answers

4
votes

Sitecore 6.6 uses Lucene 2.9. The code below is your code updated to support the newer version of Lucene. There are 2 major changes:

  1. Search method is executed with 2 additional parameters (Filter which is set to null and maxDocs which is set to int.MaxValue).
  2. SearchHits constructor takes IndexReader instance as the second parameter.

Code below should work exactly as you expect.

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);
}
3
votes

Not terribly familiar with Sitecore, but Searcher.search(Query, Sort) was deprecated in Lucene 2.9, and looks like wasn't present at all in Lucene.Net. Instead, call Searcher.search(Query, Filter, int, Sort). The second argument (Filter) can be null, and the third (int) indicates the number of documents to return from the search.