You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by ch...@apache.org on 2025/01/03 10:04:44 UTC

(lucene) branch main updated: Improve use of generics in DisjunctionMaxQuery construction (#14096)

This is an automated email from the ASF dual-hosted git repository.

chegar pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/lucene.git


The following commit(s) were added to refs/heads/main by this push:
     new 204c39f8eb7 Improve use of generics in DisjunctionMaxQuery construction (#14096)
204c39f8eb7 is described below

commit 204c39f8eb7fb5fd26a3b9ff41ef7d18fae1c844
Author: Chris Hegarty <62...@users.noreply.github.com>
AuthorDate: Fri Jan 3 10:04:38 2025 +0000

    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.
---
 .../org/apache/lucene/search/DisjunctionMaxQuery.java |  4 ++--
 .../apache/lucene/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 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.java
index 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);
   }