I seem to keep butting up against lock or already closed exception errors, with Examine search functionality. I am using an Azure Web App, that I have to keep at one instance, which seemed to work for a week or so, and then I started getting the Lucene "AlreadyClosedException" errors in the log, and my search functionality stopped.
Here's my code for a predictive search function:
private UmbracoHelper _uHelper { get; set; }
public SearchHelper(UmbracoHelper uHelper)
{
_uHelper = uHelper;
}
public SearchViewModel GetSearchResults(string query, string culture, string[] allKeys, string fieldName = "__Published")
{
IIndex index;
SearchViewModel searchResponse = new SearchViewModel()
{
Results = new List<SearchResultsModel>(),
ResultCount = 0,
SearchTerm = query
};
if(!ExamineManager.Instance.TryGetIndex("ExternalIndex", out index) || !(index is IUmbracoIndex))
throw new InvalidOperationException("The required index ExternalIndex was not found");
string[] fields = new string[8]
{
"basePartNumber",
"productName",
"name",
"bodyText",
"productFeatures",
"specifications",
"tags",
"associations"
};
getAndSetSearchResults(searchResponse, query, culture, index, fields, fieldName, false);
return searchResponse;
}
private void getAndSetSearchResults(SearchViewModel searchResponse, string query, string culture, IIndex index, string[] fields, string fieldName, bool triedConfigSKU)
{
var booleanOperation = index.GetSearcher().CreateQuery("content", BooleanOperation.And).ManagedQuery(query.Length > 2 ? query.ToLower() + "*" : query.ToLower(), fields).And().Field(fieldName, "y");
long totalRecords;
IEnumerable<PublishedSearchResult> publishedContentSearchResults = _uHelper.ContentQuery.Search((IQueryExecutor)booleanOperation.Not().Field("hideFromSearch", "1"), 0, 25, out totalRecords);
searchResponse.ResultCount = Convert.ToInt32(totalRecords);
var hyphenProductName = ContainsHyphenProductName(query.ToString().ToUpper());
if(totalRecords == 0 && triedConfigSKU == false)
{
// Try it as a configured SKU by searching up to the hyphen
publishedContentSearchResults = null;
getAndSetSearchResults(searchResponse, !String.IsNullOrEmpty(hyphenProductName) ? hyphenProductName : (query.Contains('-') ? query.Substring(0, query.IndexOf('-')) : query), culture, index, fields, fieldName, true);
}
foreach(PublishedSearchResult publishedSearchResult in publishedContentSearchResults.OrderByDescending(x => x.Score))
{
IPublishedContent content = publishedSearchResult.Content;
if(!searchResponse.Results.Where(x => x.Id == content.Id).Any() && !content.Ancestors().Where(x => x.Name.ToUpper() == "DISCONTINUED").Any())
{
if(content.Value<Boolean>("UmbracoNavhide") == false)
{
searchResponse.Results.Add(new SearchResultsModel()
{
Id = content.Id,
Name = content.Name(culture),
Url = content.Url(culture, UrlMode.Auto),
BodyText = content.GetProperty("bodyText"),
ProductName = content.Value<string>("productName"),
ProductImageUrl = content.Value<IPublishedContent>("listingPhoto", (string)null, (string)null, new Fallback(), (IPublishedContent)null)?.Url,
DocType = content.ContentType.Alias,
Tags = content.Value<String[]>("tags")
});
}
}
}
}
The error I see is:
Lucene.Net.Store.AlreadyClosedException: this IndexReader is closed
at Lucene.Net.Index.IndexReader.EnsureOpen() in d:\Lucene.Net\FullRepo\trunk\src\core\Index\IndexReader.cs:line 204
at Lucene.Net.Index.DirectoryReader.GetFieldNames(FieldOption fieldNames) in d:\Lucene.Net\FullRepo\trunk\src\core\Index\DirectoryReader.cs:line 1055
at Examine.LuceneEngine.Providers.LuceneSearcher.GetAllIndexedFields() in C:\projects\examine-qvx04\src\Examine\LuceneEngine\Providers\LuceneSearcher.cs:line 101
at Examine.LuceneEngine.Providers.BaseLuceneSearcher.CreateQuery(String category, BooleanOperation defaultOperation, Analyzer luceneAnalyzer, LuceneSearchOptions searchOptions) in C:\projects\examine-qvx04\src\Examine\LuceneEngine\Providers\BaseLuceneSearcher.cs:line 64
at Examine.LuceneEngine.Providers.BaseLuceneSearcher.CreateQuery(String category, BooleanOperation defaultOperation) in C:\projects\examine-qvx04\src\Examine\LuceneEngine\Providers\BaseLuceneSearcher.cs:line 49
at USD.CMS.Helpers.SearchHelper.getAndSetSearchResults(SearchViewModel searchResponse, String query, String culture, IIndex index, String[] fields, String fieldName, Boolean triedConfigSKU) in C:\Users\jason.delaplain\Source\Repos\USD\USD-Umbraco\USD.CMS\Helpers\SearchHelper.cs:line 59
at USD.CMS.Helpers.SearchHelper.GetSearchResults(String query, String culture, String[] allKeys, String fieldName) in C:\Users\jason\Source\Repos\USD\USD-Umbraco\USD.CMS\Helpers\SearchHelper.cs:line 51
at USD.CMS.Controllers.SearchController.SubmitSearchForm(SearchViewModel model) in C:\Users\jason\Source\Repos\USD\USD-Umbraco\USD.CMS\Controllers\SearchController.cs:line 44
at lambda_method(Closure , ControllerBase , Object[] )
at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters)
at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c.<BeginInvokeSynchronousActionMethod>b__9_0(IAsyncResult asyncResult, ActionInvocation innerInvokeState)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`2.CallEndDelegate(IAsyncResult asyncResult)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.End()
at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult)
at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<>c__DisplayClass11_0.<InvokeActionMethodFilterAsynchronouslyRecursive>b__0()
at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<>c__DisplayClass11_2.<InvokeActionMethodFilterAsynchronouslyRecursive>b__2()
at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<>c__DisplayClass11_2.<InvokeActionMethodFilterAsynchronouslyRecursive>b__2()
at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<>c__DisplayClass11_2.<InvokeActionMethodFilterAsynchronouslyRecursive>b__2()
at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<>c__DisplayClass11_2.<InvokeActionMethodFilterAsynchronouslyRecursive>b__2()
at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass7_0.<BeginInvokeActionMethodWithFilters>b__1(IAsyncResult asyncResult)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.End()
at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult)
at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass3_6.<BeginInvokeAction>b__4()
at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass3_1.<BeginInvokeAction>b__1(IAsyncResult asyncResult)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.End()
at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult)
at System.Web.Mvc.Controller.<>c.<BeginExecuteCore>b__152_1(IAsyncResult asyncResult, ExecuteCoreState innerState)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.End()
at System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult)
at System.Web.Mvc.Controller.<>c.<BeginExecute>b__151_2(IAsyncResult asyncResult, Controller controller)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.End()
at System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult)
at System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult)
at System.Web.Mvc.MvcHandler.<>c.<BeginProcessRequest>b__20_1(IAsyncResult asyncResult, ProcessRequestState innerState)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.End()
at System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult)
at System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result)
at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
at System.Web.HttpApplication.ExecuteStepImpl(IExecutionStep step)
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
We're wanting (trying) to go live with the entire backend of our website switched out to Umbraco, but this could be a deal breaker. Especially since I'm not seeing much on the forums in the terms of replies or fixes.
Ultimately, It appeared that we had to upgrade Umbraco from 8.6 to 8.16. I also upgraded Examine. That coupled with a slight refactoring of the ContentQuery instance, and it's doing great.
Lucene.Net.Store.AlreadyClosedException
I seem to keep butting up against lock or already closed exception errors, with Examine search functionality. I am using an Azure Web App, that I have to keep at one instance, which seemed to work for a week or so, and then I started getting the Lucene "AlreadyClosedException" errors in the log, and my search functionality stopped.
Here's my code for a predictive search function:
The error I see is:
We're wanting (trying) to go live with the entire backend of our website switched out to Umbraco, but this could be a deal breaker. Especially since I'm not seeing much on the forums in the terms of replies or fixes.
Hi, have you seen this https://github.com/umbraco/Umbraco-CMS/issues/9386 ?
Hi Ken,
That does seem related.
Ultimately, It appeared that we had to upgrade Umbraco from 8.6 to 8.16. I also upgraded Examine. That coupled with a slight refactoring of the ContentQuery instance, and it's doing great.
is working on a reply...