views:

378

answers:

1

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?

+1  A: 

Sitecore's own reference code actually expects this behaviour. Reference: http://sdn.sitecore.net/upload/sdn/articles/administration/lucene-search-engine/lucene-search-engine.pdf

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.

Mark Cassidy
Another reason why I dislike Sitecore. Thanks for the help. Tried looking up the "ghost result" and it returned no results when logged in as an admin user in the Sitecore client.
Kyle