Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sitecore 7 Search, cannot access a disposed object

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

like image 973
Ian Graham Avatar asked Feb 17 '14 11:02

Ian Graham


1 Answers

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.

like image 174
Jens Mikkelsen Avatar answered Dec 11 '22 00:12

Jens Mikkelsen