Skip to content

Commit

Permalink
Fix SynonymQuery equals implementation (#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 #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 committed May 3, 2023
1 parent 246ac4b commit f94cd17
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 @@ -114,7 +114,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 @@ -147,7 +147,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 f94cd17

Please sign in to comment.