I found this through the highlighting code in luwak, where we rewrite queries into their Span equivalents. MultiTermQueries are wrapped in SpanMultiTermQueryWrapper, which alters the inner query's RewriteMethod. If the MTQ is within a deeply-nested BooleanQuery then this invalidates the query's hashcode, and the assertion trips the next time the query is used.
I can see a few ways to solve this:
- make MTQ immutable, and rework SpanMTQWrapper so that it uses its inner query's TermsEnum to rewrite itself rather than altering the inner query's rewrite method directly
- make MTQ cloneable, and use a copy of the query in SpanMTQWrapper rather than altering the query directly
- stop caching the hashcode on BooleanQuery
I think we probably need to stop the caching anyway, because there's no way to guarantee that any query passed to BQ is itself immutable, but it's also worth making our core queries immutable if we can.