kotman12 opened a new pull request, #16998:
URL: https://github.com/apache/lucene/pull/16998

   ### Description
   
   Hey all, I'm a Solr committer and also a frequent user of the lucene-monitor 
module. I've had this idea for a while which was greatly inspired by the very 
clever way that `MultiPassTermFilteredPresearcher` handles conjunctions. The 
same approach can actually be extended to more optimally handle negative 
queries. I never had the courage to actually implement this but in the age of 
agents you can speed-run the implementation hurdles that come with such a 
delicate change. This work was started during the CoC hackathon in Glasgow. 
Shout out to @romseygeek for hearing me out on this 😄 
   
   ### AI Usage
   
   Opus 5.5 generated 100% of the code here but I have reviewed it all and 
iterated many times to get the shape that I present today as the initial 
implementation was very rough around the edges. The issue and PR description 
are 100% written by me; if they are slop let it be know that it is human slop!
   
   ### Design
   
   The TermFilteredPresearcher indexes a query as a disjunction of terms and 
then an incoming document selects the query if it contains any of them. One can 
extend this idea and  add a "negative disjunct" to that disjunction, which a 
document satisfies by containing _none_ of its terms. A single negated term or 
several conjuncted, negated terms are indexed into a special `{field}__not_0` 
field, along with a `__negdisjunct_0:__T__` sentinel. The `_0` extension 
assigns an ordinal to the negative disjunct. In the first section I will assume 
we have only one negative disjunct but queries that need several are covered 
later.
   
   When converting documents to queries we append the following conjunction of 
double negations to the existing expansion:
   
   ```
   ... OR
       (__negdisjunct_0:__T__ AND -field0__not_0:{term00} AND ... 
           AND -field0__not_0:{term0M} AND -field1__not_0:{term10} AND ...
               AND -fieldN__not_0:{termN0} AND ... AND -fieldN__not_0:{termNK})
   ```
   
   At first glance it may not seem obvious how this provides any sort of 
advantage. After all, we are taking the union of this extra clause with the 
existing pre-filter which makes it look _less_ selective. To fully understand 
one must remember what happens currently when a negative sub-query is indexed:
   
   1. `*:* -field:value` → `{__anytokenfield:__ANYTOKEN__}`
   2. `field:a AND -field:b` → `{field:a}`
   
   Here, `__anytokenfield:__ANYTOKEN__` is the existing sentinel term that the 
presearcher appends to every document → query expansion, `... OR 
__anytokenfield:__ANYTOKEN__`. As a result, query 1 will be returned by every 
pre-search and run against _every_ document that is processed. Which also means 
that by _safely_ skipping the sentinel we have an immediate advantage as long 
as the negative pre-search filters out _anything_.
   
   Although example 2 has better selectivity (it filters out all documents 
missing value `a` in `field`), the extra information of the negative clause is 
lost during the pre-search. In many cases negative queries are quite selective 
so there is a performance gain to be captured here. For instance, if we made 
the multi-pass extension of the TermFilteredPresearcher negative-aware we can 
expand 2 in the following ways (assuming `passes=2`):
   
   query-as-document:
   `field:a AND -field:b` → `{field_0:a, __negdisjunct_1_0:__T__, 
field_1__not_0:b}`
   
   document-as-query:
   `{field:[a, b]}` →
   ```
   (
       (
           field_0:a OR field_0:b OR
           (__negdisjunct_0_0:__T__ AND -field_0__not_0:a AND -field_0__not_0:b)
       )
   
       AND
   
       (
           field_1:a OR field_1:b OR
           (__negdisjunct_1_0:__T__ AND -field_1__not_0:a AND -field_1__not_0:b)
       )
   )
   OR __anytokenfield:__ANYTOKEN__
   ```
   
   By handling negative queries we are able to successfully filter out this 
document which we could not do otherwise. In the current logic the second pass 
simply repeats the first pass as the pre-searcher cannot extract an indexable 
term from the negative clause. The end result is the false positive query:
   ```
   ((field_0:a OR field_0:b) AND (field_1:a OR field_1:b)) ...
   ```
   matches the document which unnecessarily forwards it to the expensive 
matching stage: `{field_0:a, field_1:a}`
   
   The motivation for the `__negdisjunct_*` sentinel is to optimally handle 
queries without a negative disjunct. Otherwise, the additional clause meant to 
target negative queries would incidentally pick up positive ones, which would 
likely lead to performance loss relative to the existing implementation. For 
instance, `x:a OR y:b` → `{x:a, y:b}` would get matched by the final clause in:
   
   `{x:c, y:d}` → `(__anytokenfield:__ANYTOKEN__ OR x:c OR y:d OR (*:* AND 
-x__not_0:c AND -y__not_0:d))`.
   
   By intersecting the last clause with `__negdisjunct_0:__T__` instead of 
`*:*` we correctly mask queries that have no negative disjunct in their indexed 
representation.
   
   One has to remember that if a `query` matches a `document` then 
`presearcher.buildQuery(document)` must match `presearcher.indexQuery(query)`, 
i.e. a false negatives are not allowed. It is also true that a query which 
imposes additional conditions apart from the existence of terms (i.e. 
positional queries), once negated, results in a query that imposes _fewer_ 
conditions than the nonexistence of those same terms. Thus, negations of 
positional queries cannot be handled by the proposed, negative-aware 
pre-searcher as they would violate the aforementioned invariant.
   
   ### Multiple Negative Disjuncts
   
   So far every query needed a single negative disjunct, but there is a bit of 
friction when negated terms are themselves disjoined, similar to the friction 
of positive _conjunctions_ but with many properties reversed. If you recall, 
our document→query conversion results in an additional query clause which is 
the intersection of each document term encoded in a negative disjunct field. 
Put differently we filter out all stored queries which have any of these 
document terms indexed in their negative disjunct. But once we have a 
disjunction of two negative terms this idea falls flat. In this case it is 
possible to have a matching document where none of the terms match the first 
negated query term _or_ none of the document terms match the second negated 
term. Thus, during pre-search we have to check for the nonexistence of each 
term _independently_. We would not get a correct result by filtering out all 
stored queries which do not have _any_ of these terms.
   
   The solution to this is to give each branch of the disjunction a negative 
disjunct of its own, similar to how multi-pass layers the indexed 
representations of positive conjunctions. We have a budget of negative 
disjuncts (similar to `passes` in `MTFP`), and each branch uses one. Thus,
   
   `(*:* -myField:term0) OR (*:* -otherField:term1)` → `{__negdisjunct_0:__T__, 
myField__not_0:term0, __negdisjunct_1:__T__, otherField__not_1:term1}`
   
   Similarly, a document would be transformed as follows:
   `{myField:term2, otherField:term1}` →
   ```
    ... OR
       (__negdisjunct_0:__T__ AND -myField__not_0:term2 AND 
-otherField__not_0:term1)
           OR (__negdisjunct_1:__T__ AND -myField__not_1:term2 AND 
-otherField__not_1:term1)
   ```
   
   which correctly matches the indexed query despite matching only a single 
branch of the disjunction. We can only filter out if the document contains 
_both_ negated terms:
   `{myField:term0, otherField:term1}` →
   ```
    ... OR
       (__negdisjunct_0:__T__ AND -myField__not_0:term0 AND 
-otherField__not_0:term1)
           OR (__negdisjunct_1:__T__ AND -myField__not_1:term0 AND 
-otherField__not_1:term1)
   ```
   In this case, the document is correctly filtered out from the result set by 
the pre-searcher (assuming a budget of 2 negative disjuncts). 
   
   A key difference between expanding positive conjunctions into passes and 
negated disjunctions into negative disjuncts is when we run out of 
pre-configured budget. In the case of positive conjunctions it is safe to 
simply stop and keep the existing passes that fit into the budget. On the other 
hand, a pass that needs more negative disjuncts than the budget allows must 
select the query for every document, otherwise you open the door to false 
negatives. For that pass we index only the `__negdisjunct_0:__T__` sentinel. 
When a negative disjunct clause in the document→query conversion has no indexed 
terms to exclude on, it will match every document (although the remaining 
passes can still filter).
   
   Another important difference is that each subsequent pass in the positive 
query case _increases_ selectivity while each additional negative disjunct 
_decreases_ it, instead. Thus, in practice one may run with a budget of only 
one, or perhaps two negative disjuncts. I leave it in the implementation 
because it isn't too much extra work and may turn out to be useful. As a 
default I would probably recommend one.
   
   ### Usage
   
   I imagine that this enhancement will be most useful with a 
`MultiPassTermFilteredPresearcher` as standalone negative queries (`*:* 
-field:b`) queries are rare and when you have a positive branch it is typically 
more selective than a negative branch. But once you can index more than one 
branch (with `MPTFPS`, being able to use negation as a pre-search filter 
becomes more enticing. For what it's worth I find the `MPTFPS` to be the best 
`Presearcher` in practice as its default behavior is quite selective and 
requires little to no tuning. 


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to