kotman12 commented on code in PR #17032:
URL: https://github.com/apache/lucene/pull/17032#discussion_r4637967465
##########
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())) {
Review Comment:
This was moved from MPTFPS so you can argue it is leaking some pass-related
behavior into TFPS. However, behaviorally this only matters if you have
non-identity field mapper.
##########
lucene/monitor/src/java/org/apache/lucene/monitor/MultipassTermFilteredPresearcher.java:
##########
@@ -100,73 +141,60 @@ private static String field(String field, int pass) {
private class MultipassDocumentQueryBuilder implements DocumentQueryBuilder {
- BooleanQuery.Builder[] queries = new BooleanQuery.Builder[passes];
Map<String, BytesRefHash> terms = new HashMap<>();
-
- MultipassDocumentQueryBuilder() {
- for (int i = 0; i < queries.length; i++) {
- queries[i] = new BooleanQuery.Builder();
- }
- }
+ Map<String, List<BytesRef>> negatableTerms = new HashMap<>();
Review Comment:
It's a List<BytesRef> instead of a BytesRefHash (as in the case of terms)
because we don't need to deduplicate.
##########
lucene/monitor/src/java/org/apache/lucene/monitor/TermFilteredPresearcher.java:
##########
@@ -151,6 +180,42 @@ protected boolean accept() {
}
}
+ /** Adds the terms of a field that the term filter accepts, to select
queries */
+ private static void addTerms(
+ DocumentQueryBuilder queryBuilder,
+ String field,
+ TokenStream ts,
+ BiPredicate<String, BytesRef> termAcceptor)
+ throws IOException {
+ TermToBytesRefAttribute termAtt =
ts.addAttribute(TermToBytesRefAttribute.class);
+ while (ts.incrementToken()) {
+ BytesRef term = termAtt.getBytesRef();
+ if (termAcceptor.test(field, term)) {
+ queryBuilder.addTerm(field, BytesRef.deepCopyOf(term));
+ }
+ }
+ ts.close();
+ }
+
+ /**
+ * Adds the terms every document contains, which can exclude queries through
a negative disjunct
+ */
+ private static void addNegatableTerms(
+ DocumentQueryBuilder queryBuilder,
+ String field,
+ TermsEnum terms,
+ int maxDoc,
+ BiPredicate<String, BytesRef> termAcceptor)
+ throws IOException {
+ // read directly: TermsEnumTokenStream can merge invalid UTF-8 terms,
which must not exclude
+ String notField = field + NOT_SUFFIX;
+ for (BytesRef term = terms.next(); term != null; term = terms.next()) {
+ if (terms.docFreq() == maxDoc && termAcceptor.test(notField, term)) {
Review Comment:
Multi-doc batches don't work well with this feature as currently
implemented. I am not a user of the batch feature but analyzing this PR made me
realize that they don't work well with the current
MultiPassTermFilteredPresearcher either. More details here
https://github.com/apache/lucene/issues/17004. I think if we can address the
linked issue this won't really be a problem.
##########
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:
Potentially continuing conversation from
https://github.com/apache/lucene/pull/16998#discussion_r4498022341 @romseygeek
--
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]