Apache Lucene open-source search software
Improve use of generics in DisjunctionMaxQuery construction (#14096)
Chris Hegarty204c39f8eb7f
diff --git a/lucene/core/src/java/org/apache/lucene/search/DisjunctionMaxQuery.java b/lucene/core/src/java/org/apache/lucene/search/DisjunctionMaxQuery.javaindex 7255a67419b..5fc62866b1f 100644--- a/lucene/core/src/java/org/apache/lucene/search/DisjunctionMaxQuery.java+++ b/lucene/core/src/java/org/apache/lucene/search/DisjunctionMaxQuery.java@@ -53,14 +53,14 @@ public final class DisjunctionMaxQuery extends Query implements Iterable<Query>/*** Creates a new DisjunctionMaxQuery*- * @param disjuncts a {@code Collection<Query>} of all the disjuncts to add+ * @param disjuncts a collection of all the disjunct queries to add* @param tieBreakerMultiplier the score of each non-maximum disjunct for a document is multiplied* by this weight and added into the final score. If non-zero, the value should be small, on* the order of 0.1, which says that 10 occurrences of word in a lower-scored field that is* also in a higher scored field is just as good as a unique word in the lower scored field* (i.e., one that is not in any higher scored field.*/- public DisjunctionMaxQuery(Collection<Query> disjuncts, float tieBreakerMultiplier) {+ public DisjunctionMaxQuery(Collection<? extends Query> disjuncts, float tieBreakerMultiplier) {Objects.requireNonNull(disjuncts, "Collection of Querys must not be null");if (tieBreakerMultiplier < 0 || tieBreakerMultiplier > 1) {throw new IllegalArgumentException("tieBreakerMultiplier must be in [0, 1]");diff --git a/lucene/core/src/test/org/apache/lucene/search/TestDisjunctionMaxQuery.java b/lucene/core/src/test/org/apache/lucene/search/TestDisjunctionMaxQuery.javaindex 1464aa84bd3..b5bb9c5e62d 100644--- a/lucene/core/src/test/org/apache/lucene/search/TestDisjunctionMaxQuery.java+++ b/lucene/core/src/test/org/apache/lucene/search/TestDisjunctionMaxQuery.java@@ -637,13 +637,28 @@ public class TestDisjunctionMaxQuery extends LuceneTestCase {dir.close();}+ // Ensure generics and type inference play nicely together+ public void testGenerics() {+ var query =+ new DisjunctionMaxQuery(+ Arrays.stream(new String[] {"term"}).map((term) -> tq("test", term)).toList(), 1.0f);+ assertEquals(1, query.getDisjuncts().size());++ var disjuncts =+ List.of(+ new RegexpQuery(new Term("field", "foobar")),+ new WildcardQuery(new Term("field", "foobar")));+ query = new DisjunctionMaxQuery(disjuncts, 1.0f);+ assertEquals(2, query.getDisjuncts().size());+ }+/** macro */- protected Query tq(String f, String t) {+ protected TermQuery tq(String f, String t) {return new TermQuery(new Term(f, t));}/** macro */- protected Query tq(String f, String t, float b) {+ protected BoostQuery tq(String f, String t, float b) {Query q = tq(f, t);return new BoostQuery(q, b);}