kotman12 commented on code in PR #16998:
URL: https://github.com/apache/lucene/pull/16998#discussion_r4498022341


##########
lucene/monitor/src/java/org/apache/lucene/monitor/TermFilteredPresearcher.java:
##########
@@ -254,18 +383,99 @@ public final Document indexQuery(Query query, Map<String, 
String> metadata) {
 
   /** Builds a {@link Document} from the terms extracted from a query */
   protected Document buildQueryDocument(QueryTree querytree) {
-    Map<String, BytesRefHash> fieldTerms = collectTerms(querytree);
+    return toDocument(querytree, field -> field);
+  }
+
+  /** Indexes the terms and negative disjuncts of the current path through a 
{@link QueryTree} */
+  Document toDocument(QueryTree querytree, FieldMapper fields) {
     Document doc = new Document();
-    for (Map.Entry<String, BytesRefHash> entry : fieldTerms.entrySet()) {
-      doc.add(
-          new Field(
-              entry.getKey(),
-              new TermsEnumTokenStream(new 
BytesRefHashIterator(entry.getValue())),
-              QUERYFIELDTYPE));
+    CollectedTerms collected = collectTermsAndNegativeDisjuncts(querytree);
+    List<Set<Term>> negativeDisjuncts = collected.negativeDisjuncts();
+    if (negativeDisjuncts.size() > maxNegativeDisjuncts) {
+      // More negative disjuncts than the document side checks: the marker 
alone satisfies the path
+      // for
+      // every document, so its terms are not needed either
+      addField(doc, negativeDisjunctMarkerField(fields, 0), 
NEGATIVE_DISJUNCT_MARKER);
+      return doc;
+    }
+    for (Map.Entry<String, BytesRefHash> entry : collected.terms().entrySet()) 
{
+      if (maxNegativeDisjuncts > 0 && entry.getKey().contains(NOT_SUFFIX)) {
+        // The field could be indexed under the name of a negative disjunct 
field, whose clauses
+        // exclude
+        // queries, so select the query for every document instead
+        addField(doc, ANYTOKEN_FIELD, new BytesRef(ANYTOKEN));
+        continue;
+      }
+      String field = fields.map(entry.getKey());
+      addField(doc, field, entry.getValue());
+      if (!field.equals(entry.getKey())) {
+        // the term filter looks terms up under the document's field names, so 
a renamed field is
+        // also indexed under its own name
+        addField(doc, entry.getKey(), entry.getValue());
+      }
+    }
+    for (int i = 0; i < negativeDisjuncts.size(); i++) {
+      Map<String, BytesRefHash> byField = new HashMap<>();
+      for (Term term : negativeDisjuncts.get(i)) {
+        byField.computeIfAbsent(term.field(), _ -> new 
BytesRefHash()).add(term.bytes());
+      }
+      for (Map.Entry<String, BytesRefHash> entry : byField.entrySet()) {
+        addRawTerms(doc, negativeDisjunctField(fields.map(entry.getKey()), i), 
entry.getValue());
+        addRawTerms(doc, entry.getKey() + NOT_SUFFIX, entry.getValue());
+      }
+      addField(doc, negativeDisjunctMarkerField(fields, i), 
NEGATIVE_DISJUNCT_MARKER);
     }
     return doc;
   }
 
+  private void addField(Document doc, String field, BytesRef term) {
+    BytesRefHash terms = new BytesRefHash();
+    terms.add(term);
+    addField(doc, field, terms);
+  }
+
+  private void addField(Document doc, String field, BytesRefHash terms) {
+    doc.add(
+        new Field(
+            field, new TermsEnumTokenStream(new BytesRefHashIterator(terms)), 
QUERYFIELDTYPE));
+  }
+
+  /**
+   * Indexes exact bytes: TermsEnumTokenStream can merge invalid UTF-8 terms, 
which must not exclude
+   */
+  private static void addRawTerms(Document doc, String field, BytesRefHash 
terms) {
+    BytesRef scratch = new BytesRef();
+    for (int i = 0; i < terms.size(); i++) {

Review Comment:
   Adversarial review caught that folding non-UTF bytes into characters (which 
is possible with binary query terms) can lead to false negatives when negations 
are disabled. Say you fold some non-UTF8 bytes to some character which you then 
index ... when a document comes in with a matching character (but not matching 
bytes) it will be a false-positive match by the presearcher but the 
CandidateMatcher will filter it out so it is still correct. In the case of 
negative queries the same principle flips and you get a false negative. So you 
must faithfully index the bytes so that the document doesn't get incorrectly 
filtered out if it matches the folded character (but doesn't match the actual 
bytes in the query term). 
   
   I tried to see if we could just go full-binary when indexing positive and 
negative terms but it appears a lot of the existing machinery around regex 
handling (i.e. suffixing n-grams) relies on string encoded terms so I've left 
it on a separate path for negative query terms. 



-- 
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