Shazwazza / Examine

A .NET indexing and search engine powered by Lucene.Net
https://shazwazza.github.io/Examine/
380 stars 123 forks source link

Examine GetFieldInternalQuery error #335

Closed jplatfordquba closed 11 months ago

jplatfordquba commented 1 year ago

A site of ours (Umbraco v10.2.0) experienced this error yesterday and we haven't been able to reproduce it on our development or test environments. A site reboot resolved the issue and it is yet to return. It appeared on all pages that use Examine for search. The error occurred regardless of search term/filter.

System.NullReferenceException: Object reference not set to an instance of an object. at Lucene.Net.QueryParsers.Classic.QueryParser.Jj_add_error_token(Int32 kind, Int32 pos) at Lucene.Net.QueryParsers.Classic.QueryParser.Jj_scan_token(Int32 kind) at Lucene.Net.QueryParsers.Classic.QueryParser.Jj_3R_2() at Lucene.Net.QueryParsers.Classic.QueryParser.Jj_3_1() at Lucene.Net.QueryParsers.Classic.QueryParser.Jj_2_1(Int32 xla) at Lucene.Net.QueryParsers.Classic.QueryParser.Clause(String field) at Lucene.Net.QueryParsers.Classic.QueryParser.Query(String field) at Lucene.Net.QueryParsers.Classic.QueryParser.TopLevelQuery(String field) at Lucene.Net.QueryParsers.Classic.QueryParserBase.Parse(String query) at Examine.Lucene.Search.LuceneSearchQueryBase.GetFieldInternalQuery(String fieldName, IExamineValue fieldValue, Boolean useQueryParser) at Examine.Lucene.Search.LuceneSearchQuery.Search(QueryOptions options)

nikcio commented 1 year ago

Hi @jplatfordquba if there's going to be any chance of figuring out the issue we need some more information. Examples:

jplatfordquba commented 1 year ago

Hi @nikcio

Snippet below:


var criteria = searcher.CreateQuery(IndexTypes.Content, BooleanOperation.And);
IBooleanOperation examineQuery;
examineQuery = criteria.GroupedOr(new string[] { "__NodeTypeAlias" }, searchableNodeTypes);
var results = examineQuery.Execute();
nzdev commented 1 year ago

@jplatfordquba the value / type of searchableNodeTypes needs to be included to help diagnose. Also the query string that can be obtained from the debugger by inspecting the value of examineQuery.

danlb2000 commented 12 months ago

We are seeing the same problem as @jplatfordquba. It is the exact same symptoms with the exact same stack trace in Umbraco 10.6.1. All queries will work fine for days, but all of a sudden queries that worked fine before now fail. Every query we do will fail until we restart the app pool and then everything will work fine again. We run two load balanced server and the failure will randomly happen on one or the other.

Shazwazza commented 12 months ago

As this error is coming from Lucene code, I might suggest reporting this issue on the Lucene repo https://github.com/apache/lucenenet

But like @nzdev mentions, without all of the information its pretty hard to diagnose the issue without steps to replicate.

danlb2000 commented 12 months ago

Unfortunately we are unable to determine what steps are needed to replicate it. It happens pretty randomly.

Shazwazza commented 12 months ago

Is it generally from the same query? If so, can you share the entire query and how you are creating it? I wonder if we can setup a test to continuously execute it and see if it eventually fails.

danlb2000 commented 12 months ago

Is it generally from the same query? If so, can you share the entire query and how you are creating it? I wonder if we can setup a test to continuously execute it and see if it eventually fails.

We query the index in a couple different areas of our site and once it starts failing it fails in all of them. This is probably the most straight forward way we query it. This is for a general index page so no user input is involved, all the parameters are statically determined. Note this code is slightly simplified.

if (!_examineManager.TryGetIndex(UmbracoIndexes.ExternalIndexName, out var index) || !(index is IUmbracoIndex umbIndex)) { throw new InvalidOperationException($"No index found by name ExternalIndex or is not of type {typeof(IUmbracoIndex)}"); } QueryOptions queryOptions = new QueryOptions(0, 3000); string[] docTypes = docTypeList.ToArray(); // Gets populated with a list of doc types var resultsAsSearchItems = index .Searcher .CreateQuery(IndexTypes.Content) .Field("__Published_en", "y") .And() .GroupedOr(new[] { "__NodeTypeAlias" }, docTypes) .Execute(queryOptions);

This is the stack trace it produces when the problem occurs. We have a more complex query that produces the exact stack trace that @jplatfordquba reported

System.NullReferenceException: Object reference not set to an instance of an object. at Lucene.Net.QueryParsers.Classic.ParseException.Initialize(Token currentToken, Int32[][] expectedTokenSequences, String[] tokenImage) at Lucene.Net.QueryParsers.Classic.QueryParser.GenerateParseException() at Lucene.Net.QueryParsers.Classic.QueryParser.Jj_consume_token(Int32 kind) at Lucene.Net.QueryParsers.Classic.QueryParser.Clause(String field) at Lucene.Net.QueryParsers.Classic.QueryParser.Query(String field) at Lucene.Net.QueryParsers.Classic.QueryParser.TopLevelQuery(String field) at Lucene.Net.QueryParsers.Classic.QueryParserBase.Parse(String query) at Examine.Lucene.Search.LuceneSearchQueryBase.GetFieldInternalQuery(String fieldName, IExamineValue fieldValue, Boolean useQueryParser) at Examine.Lucene.Search.LuceneSearchQuery.Search(QueryOptions options)

danlb2000 commented 11 months ago

Another interesting piece of information on this. We removed IndexTypes.Content from CreateQuery(IndexTypes.Content) since in our scenario it really wasn't needed and when we did this searches no longer fail. I was able to confirm this since we missed that change in one place and when the problem re-occurred it only failed on the function where we didn't remove it, it worked on in other areas.

Shazwazza commented 11 months ago

Ok thanks for the feedback! At least this gives us something to try to use to test/replicate and then figure out. Will need to setup some tests to see if/how we can try go get this issue to occur.

Musealali commented 11 months ago

Seems like this is also reported in the CMS Github issue tracker - if you need more details @Shazwazza

https://github.com/umbraco/Umbraco-CMS/issues/12646#issuecomment-1176075691

Shazwazza commented 11 months ago

Thanks all - Since this happens intermittently this sounds like an issue with something holding state and not being thread safe. My feeling is that maybe its a QueryParser instance being re-used and the QueryParser is not thread safe. I can see in the docs that "Note that QueryParser is not thread-safe." so potentially could be that.

But that said, there is no concurrency test that I can seem to write that will replicate this locally.

Could it be that folks are trying to re-use the instance of IQuery?:

IQuery criteria = searcher.CreateQuery(IndexTypes.Content, BooleanOperation.And);

If that is attempted to be re-used, things will break because it is not thread safe. Each instance of IQuery creates a new instance of the query parser.

Shazwazza commented 11 months ago

I think I have an idea of where/how this is happening but I'm unsure how this is being caused outside of the Examine Source. Based on the call stack, I can see that this goes into (in bold)

at Lucene.Net.QueryParsers.Classic.QueryParserBase.Parse(String query) at Examine.Lucene.Search.LuceneSearchQueryBase.GetFieldInternalQuery(String fieldName, IExamineValue fieldValue, Boolean useQueryParser)

The only way this occurs if if the boolean useQueryParser is false. But there is nowhere in the Examine code that can make this false. But I discovered that behind that query parser is a static instance of a query parser and as I mentioned above this is not thread safe. So my assumption is this is the problem, but I don't know how it gets caused.

Changing to a non-static instance is easy, so I can make that change and publish it as part of 3.2 (in hopes that it 'just works')

Shazwazza commented 11 months ago

I've pushed Examine 3.2.0 RTM, hopefully that resolves the issue.

danlb2000 commented 11 months ago

I've pushed Examine 3.2.0 RTM, hopefully that resolves the issue.

Thanks for the update. We are tied to the version that Umbraco is using so we don't have any way to test this.

Shazwazza commented 11 months ago

You can update to the latest Examine externally of Umbraco, unless you are on an older version

biapar commented 6 months ago

Umbraco version 13.1.1

Lucene.Net.QueryParsers.Classic.ParseException: Cannot parse '(id: OR __Path:-1,,)': Encountered " "OR "" at line 1, column 5. Was expecting one of:

... "(" ... "*" ... ... ... ... ... ... "[" ... "{" ... ... ---> Lucene.Net.QueryParsers.Classic.ParseException: Encountered " "OR "" at line 1, column 5. Was expecting one of: ... "(" ... "*" ... ... ... ... ... ... "[" ... "{" ... ... at Lucene.Net.QueryParsers.Classic.QueryParser.Jj_consume_token(Int32 kind) at Lucene.Net.QueryParsers.Classic.QueryParser.Clause(String field) at Lucene.Net.QueryParsers.Classic.QueryParser.Query(String field) at Lucene.Net.QueryParsers.Classic.QueryParser.Clause(String field) at Lucene.Net.QueryParsers.Classic.QueryParser.Query(String field) at Lucene.Net.QueryParsers.Classic.QueryParserBase.Parse(String query) --- End of inner exception stack trace --- at Lucene.Net.QueryParsers.Classic.QueryParserBase.Parse(String query) at Examine.Lucene.Search.LuceneSearchQueryBase.NativeQuery(String query) at Umbraco.Cms.Infrastructure.Examine.DeliveryApiContentIndex.PerformDeleteFromIndex(IEnumerable`1 itemIds, Action`1 onComplete) at Umbraco.Cms.Infrastructure.Examine.Deferred.DeliveryApiContentIndexHandleContentChanges.Reindex(IContent content, IIndex index) at Umbraco.Cms.Infrastructure.Examine.Deferred.DeliveryApiContentIndexHandleContentChanges.b__7_0(CancellationToken _) at Umbraco.Cms.Infrastructure.HostedServices.QueuedHostedService.BackgroundProcessing(CancellationToken stoppingToken)
biapar commented 6 months ago

I have this error when I save and publish.

Shazwazza commented 6 months ago

@biapar What Examine version are you using and have you tried the latest version?

biapar commented 6 months ago

3.2.0

Shazwazza commented 3 months ago

@biapar I have responded to the Umbraco issue you opened https://github.com/umbraco/Umbraco-CMS/issues/16223#issuecomment-2293712114

biapar commented 3 months ago

Thanks.