Skip to content

Commit

Permalink
Improve use of generics in DisjunctionMaxQuery construction (#14096)
Browse files Browse the repository at this point in the history
This is a small usability improvement which allows construction of a DisjunctionMaxQuery to play nicer with type inference. Follows PECS.
  • Loading branch information
ChrisHegarty authored Jan 3, 2025
1 parent 8c4b370 commit 204c39f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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]");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit 204c39f

Please sign in to comment.