From 204c39f8eb7fb5fd26a3b9ff41ef7d18fae1c844 Mon Sep 17 00:00:00 2001 From: Chris Hegarty <62058229+ChrisHegarty@users.noreply.github.com> Date: Fri, 3 Jan 2025 10:04:38 +0000 Subject: [PATCH] Improve use of generics in DisjunctionMaxQuery construction (#14096) This is a small usability improvement which allows construction of a DisjunctionMaxQuery to play nicer with type inference. Follows PECS. --- .../lucene/search/DisjunctionMaxQuery.java | 4 ++-- .../search/TestDisjunctionMaxQuery.java | 19 +++++++++++++++++-- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/lucene/core/src/java/org/apache/lucene/search/DisjunctionMaxQuery.java b/lucene/core/src/java/org/apache/lucene/search/DisjunctionMaxQuery.java index 7255a67419bf..5fc62866b1fc 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 /** * Creates a new DisjunctionMaxQuery * - * @param disjuncts a {@code Collection} 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 disjuncts, float tieBreakerMultiplier) { + public DisjunctionMaxQuery(Collection 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.java index 1464aa84bd32..b5bb9c5e62d5 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 @@ private void doTestRandomTopDocs(int numFields, double... freqs) throws IOExcept 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); }