Fix SynonymQuery equals implementation (#12260)

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.
This commit is contained in:
Luca Cavanna 2023-05-03 11:27:33 +02:00 committed by GitHub
parent 3c163745bb
commit caeabf3930
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 2 deletions

View File

@ -113,7 +113,7 @@ public final class SynonymQuery extends Query {
*/ */
private SynonymQuery(TermAndBoost[] terms, String field) { private SynonymQuery(TermAndBoost[] terms, String field) {
this.terms = Objects.requireNonNull(terms); this.terms = Objects.requireNonNull(terms);
this.field = field; this.field = Objects.requireNonNull(field);
} }
public List<Term> getTerms() { public List<Term> getTerms() {
@ -146,7 +146,9 @@ public final class SynonymQuery extends Query {
@Override @Override
public boolean equals(Object other) { 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 @Override

View File

@ -73,6 +73,18 @@ public class TestSynonymQuery extends LuceneTestCase {
.addTerm(new Term("field", "c"), 0.2f) .addTerm(new Term("field", "c"), 0.2f)
.addTerm(new Term("field", "d")) .addTerm(new Term("field", "d"))
.build()); .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() { public void testBogusParams() {
@ -127,6 +139,12 @@ public class TestSynonymQuery extends LuceneTestCase {
() -> { () -> {
new SynonymQuery.Builder("field1").addTerm(new Term("field1", "a"), -0f); 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() { public void testToString() {