Kyle
Kyle

Reputation: 4366

Lucene.net IndexSearcher.search no results

I'm using the Sitecore WCMS and have a piece of C# code that uses Lucene.net to search an index based on some criteria. I'm using the IndexSearcher class as follows:

Database webDB = Sitecore.Context.Database;
Sitecore.Data.Indexing.Index index = webDB.Indexes["CampusNewsIndex"];
IndexSearcher indexSearcher = index.GetSearcher(webDB);
BooleanQuery completeQuery = new BooleanQuery();
// build completeQuery
Hits hits = indexSearcher.Search(completeQuery, sort);

for (int i = 0; i < hits.length(); i++)
{
    returnItems[i] = Sitecore.Data.Indexing.Index.GetItem(hits.Doc(i), Sitecore.Context.Database);
}

This code works fine if results are returned. However, if "hits" has no results, hits.length() returns 1 even though it makes logical sense for it to return 0. Does anybody know how I can tell if the query returned no results?

Upvotes: 1

Views: 1790

Answers (1)

Mark Cassidy
Mark Cassidy

Reputation: 5860

Sitecore's own reference code actually expects this behaviour. Reference

Item item = Index.GetItem(hits.Doc(i), db);
if (item != null)

There could be any number of reasons why you are getting results back, but not getting them resolved via the item resolver. The indexed item could be in another database, it could be unavailable to the current user, it could be available in a different language than the current context language - being the most common ones.

I suggest you get hold of the ID of the "ghost result" you are getting, and searching for it inside the Sitecore Client, see what it is. Might shed some more light on matters.

Upvotes: 3

Related Questions