Description
Description
TermFilteredPresearcher
may fail to return stored queries if those queries contain the filter field in the query itself (not just in the metadata).
When building the presearcher query (here) , TermFilteredPresearcher
removes filter field tokens from the main clause of the presearcher query (via acceptor here) , presumably under the assumption that they will be added back to the query in a separate clause for filter fields, ANDed with the first clause (here).
We end up with a presearcher query that is something like
+(some_other_field_in_query_index: value) #(+(my_filter_field:value))
However, if an indexed query contains that filter field, and if that field was the only indexed field for the query's associated document in the query index, the first of the AND'd clauses cannot match (because the filter field was omitted), so the overall AND'd presearcher query cannot match, and the presearcher fails to return the query.
A user can work around this by using an additional dedicated field for the filter field (i.e. adding it on both the query metadata and the document), but this seems like an easy trap to fall into.
My question here: is this intentional? Is the idea of a "filter field" that it appears in documents and in MonitorQuery metadata but must not appear in a itself query? I'm aware of another issue about the intended behavior of Monitor filter fields (#11607), so I'm unsure.
If intentional, I think we should document that more directly. If unintentional, we might consider removing the check on filter fields (here) when building the first part of the presearcher query.
I've set up a test project here to demonstrate the problem (specifically in this test file, where testWithEmptyMetadata
works but testWithMetadata
fails).
Version and environment details
Tested with Lucene 10.1.0 on macOS Sequoia 15.3.2 (but I think the problem has been around since at least Lucene version 8, if not earlier).