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


##########
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:
   So it looks like registering a literal `TermToBytesRefAttribute` registers a 
`CharTermAttribute` under the hood? I'm assuming you meant a 
`BytesTermAttribute`... The one issue I ran into with that idea is that it 
breaks existing users of `CustomQueryHandler::wrapTermStream`, who implicitly 
rely on this being a `CharTermAttribute`. If we switched to `Bytes*` they would 
stop seeing the terms an anything they'd write would get dropped as well. A 
straight switch would have to be a breaking change. The other tricky part is 
that it may make things less efficient for them, because some filtering 
probably relies on char-based input. For instance, the existing 
regex-supporting n-gram token stream takes substrings, which would be tricky to 
do on raw bytes. You'd probably want to round-trip `bytes->chars->bytes` in 
that case (which is what happens currently, but in the new model you'd do it 
for every filter). I imagine there's a good chance that anyone who has 
implemented a custom handler re
 lies on char input.
   
   A possible workaround, though a bit heavyweight, is a custom attribute 
implementing both `BytesTermAttribute` and `CharTermAttribute`, which starts as 
raw bytes and "downgrades" to chars whenever a filter touches the chars. For 
Monitors without custom handlers (probably a good majority) it _could_ stay 
bytes-only. But not sure if this is worth the effort.



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