Skip to content

Apache Lucene open-source search software

LUCENE-6415: Make TermsQuery.extractTerms a no-op instead of throwing an UOE.

Adrien Grand9b060c407e0d
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index db052492ad6..16953e5edc5 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -82,6 +82,9 @@ Bug Fixes
* LUCENE-6378: Fix all RuntimeExceptions to throw the underlying root cause.
(Varun Thacker, Adrien Grand, Mike McCandless)
+* LUCENE-6415: TermsQuery.extractTerms is a no-op (used to throw an
+ UnsupportedOperationException). (Adrien Grand)
+
API Changes
* LUCENE-6377: SearcherFactory#newSearcher now accepts the previous reader
diff --git a/lucene/queries/src/java/org/apache/lucene/queries/TermsQuery.java b/lucene/queries/src/java/org/apache/lucene/queries/TermsQuery.java
index 5c02a173b64..b7a02f7f2c3 100644
--- a/lucene/queries/src/java/org/apache/lucene/queries/TermsQuery.java
+++ b/lucene/queries/src/java/org/apache/lucene/queries/TermsQuery.java
@@ -24,6 +24,7 @@ import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
+import java.util.Set;
import org.apache.lucene.index.Fields;
import org.apache.lucene.index.LeafReader;
@@ -53,9 +54,21 @@ import org.apache.lucene.util.ToStringUtils;
* Specialization for a disjunction over many terms that behaves like a
* {@link ConstantScoreQuery} over a {@link BooleanQuery} containing only
* {@link org.apache.lucene.search.BooleanClause.Occur#SHOULD} clauses.
- * This query creates a bit set and sets bits that matches any of the wrapped
- * terms. While this might help performance when there are many terms, it would
- * be slower than a {@link BooleanQuery} when there are few terms to match.
+ * <p>For instance in the following example, both @{code q1} and {@code q2}
+ * would yield the same scores:
+ * <pre class="prettyprint">
+ * Query q1 = new TermsQuery(new Term("field", "foo"), new Term("field", "bar"));
+ *
+ * BooleanQuery bq = new BooleanQuery();
+ * bq.add(new TermQuery(new Term("field", "foo")), Occur.SHOULD);
+ * bq.add(new TermQuery(new Term("field", "bar")), Occur.SHOULD);
+ * Query q2 = new ConstantScoreQuery(bq);
+ * </pre>
+ * <p>This query creates a bit set and sets bits that match any of the
+ * wrapped terms. While this might help performance when there are many terms,
+ * it would be slower than a {@link BooleanQuery} when there are few terms to
+ * match.
+ * <p>NOTE: This query produces scores that are equal to its boost
*/
public class TermsQuery extends Query implements Accountable {
@@ -249,6 +262,14 @@ public class TermsQuery extends Query implements Accountable {
return Collections.emptyList();
}
+ @Override
+ public void extractTerms(Set<Term> terms) {
+ // no-op
+ // This query is for abuse cases when the number of terms is too high to
+ // run efficiently as a BooleanQuery. So likewise we hide its terms in
+ // order to protect highlighters
+ }
+
private static final class TermsAndField implements Accountable {
private static final long BASE_RAM_BYTES_USED =