Sitecore 7 Search, cannot access a disposed object

lucene, search, sitecore

Solution

It seems that `SearchResults<T>` holds reference to `SearchHit` and the `LuceneSearchProvider` doesn't hold a reader open. The new version of Lucene automatically closes the reader. I think you might be returning the wrong type. You should probably do like this:

var query = context.GetQueryable<SearchResultItem>().Where(predicate);
return query.ToList();

However make sure, that don't return too many. You should probably use take() etc.

Problem

I've been working with some Sitecore 7 search code. Example below. ``` using (var context = Index.CreateSearchContext()) { // ....Build predicates var query = context.GetQueryable<SearchResultItem>().Where(predicate); return query.GetResults(); } ``` This works fine in SOLR, but when used with standard Lucene, whenever I reference a property in the `SearchResults<SearchResultItem>` returned by `GetResults()`, Sitecore errors with "`Cannot access a disposed object`". It appears that `GetResults()` doesn't enumerate and still hangs on to the searchcontext. Anyone come across this before and know how to fix? I've seen some articles suggesting having the `SearchContext` in application state, but ideally I want to avoid this. Thanks Ian

Original source