Skip to content

Commit

Permalink
Fix SynonymQuery equals implementation (apache#12260)
Browse files Browse the repository at this point in the history
The term member of TermAndBoost used to be a Term instance and became a
BytesRef with apache#11941, which means its equals impl won't take the field
name into account. The SynonymQuery equals impl needs to be updated
accordingly to take the field into account as well, otherwise synonym
queries with same term and boost across different fields are equal which
is a bug.
  • Loading branch information
javanna authored and alessandrobenedetti committed May 12, 2023
1 parent 533c6fc commit 9cb17b0
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public SynonymQuery build() {
*/
private SynonymQuery(TermAndBoost[] terms, String field) {
this.terms = Objects.requireNonNull(terms);
this.field = field;
this.field = Objects.requireNonNull(field);
}

public List<Term> getTerms() {
Expand Down Expand Up @@ -146,7 +146,9 @@ public int hashCode() {

@Override
public boolean equals(Object other) {
return sameClassAs(other) && Arrays.equals(terms, ((SynonymQuery) other).terms);
return sameClassAs(other)
&& field.equals(((SynonymQuery) other).field)
&& Arrays.equals(terms, ((SynonymQuery) other).terms);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,18 @@ public void testEquals() {
.addTerm(new Term("field", "c"), 0.2f)
.addTerm(new Term("field", "d"))
.build());

QueryUtils.checkUnequal(
new SynonymQuery.Builder("field").addTerm(new Term("field", "a"), 0.4f).build(),
new SynonymQuery.Builder("field").addTerm(new Term("field", "b"), 0.4f).build());

QueryUtils.checkUnequal(
new SynonymQuery.Builder("field").addTerm(new Term("field", "a"), 0.2f).build(),
new SynonymQuery.Builder("field").addTerm(new Term("field", "a"), 0.4f).build());

QueryUtils.checkUnequal(
new SynonymQuery.Builder("field1").addTerm(new Term("field1", "b"), 0.4f).build(),
new SynonymQuery.Builder("field2").addTerm(new Term("field2", "b"), 0.4f).build());
}

public void testBogusParams() {
Expand Down Expand Up @@ -127,6 +139,12 @@ public void testBogusParams() {
() -> {
new SynonymQuery.Builder("field1").addTerm(new Term("field1", "a"), -0f);
});

expectThrows(
NullPointerException.class,
() -> new SynonymQuery.Builder(null).addTerm(new Term("field1", "a"), -0f));

expectThrows(NullPointerException.class, () -> new SynonymQuery.Builder(null).build());
}

public void testToString() {
Expand Down

0 comments on commit 9cb17b0

Please sign in to comment.