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


##########
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:
   Is this a bug in TermsEnumTokenStream?  From a quick look, the current 
implementation seems to be doing a fairly pointless round-trip to a char[] when 
we could be using a TermToBytesRefAttribute and operating entirely on bytes.



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