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.
This commit is contained in:
Chris Hegarty 2025-01-03 10:04:38 +00:00 committed by GitHub
parent 8c4b3702f1
commit 204c39f8eb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 19 additions and 4 deletions

View File

@ -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]");

View File

@ -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);
}