Surround query parser's Query instances accumulate clause count on rewrite() causing TooManyBasicQueries and hashCode/equals changes [LUCENE-9282]
- Dominant language
- Java
- Stars
- 3.6k
- Forks
- 1.4k
- Avg merge
- 2d 11h
- Merged PRs (30d)
- 88
Description
I was surprised to discover that queries produced by the surround query parser (span queries) behave in a non-deterministic way over multiple calls to IndexSearcher.search() methods.
The problem is that SQP produces classes referencing an internal BasicQueryFactory that accumulates primitive clause count on rewrite, throwing TooManyBasicQueries if a given threshold is exceeded. This is fine but leads to an odd situation in which a loop like this:
```java
Query q = QueryParser.parse("...")
for (int i = 0; i < 10000; i++) {
indexSearcher.search(q, 10);
}
```
would execute the query successfully up until the threshold is reached, only after that throwing an exception. What's even weirder, the hashCode/ equals changes on q over time, disrespecting the Query class contract:
https://github.com/apache/lucene-solr/blob/fbd05167f455e3ce2b2ead50336e2b9c2521cd6c/lucene/queryparser/src/java/org/apache/lucene/queryparser/surround/query/RewriteQuery.java#L67
and:
https://github.com/apache/lucene-solr/blob/fbd05167f455e3ce2b2ead50336e2b9c2521cd6c/lucene/queryparser/src/java/org/apache/lucene/queryparser/surround/query/BasicQueryFactory.java#L76-L91
This seems like a bug to me but I wanted to make sure if this behavior is relied on anywhere?
My take on fixing this would be to pass an independent query counter inside rewrite so that the original Query itself remains immutable (including hash code and equals) and rewrite can be called any number of times (always with the same result).
---
Migrated from [LUCENE-9282](https://issues.apache.org/jira/browse/LUCENE-9282) by Dawid Weiss (@dweiss)
Contributor guide
Assessment
This issue has not been assessed yet.