Skip to content

Commit e954938

Browse files
committed
Fix TermsSetQueryBuilder.doEquals() method (#29629)
Closes #29620
1 parent ea0c431 commit e954938

File tree

2 files changed

+59
-12
lines changed

2 files changed

+59
-12
lines changed

server/src/main/java/org/elasticsearch/index/query/TermsSetQueryBuilder.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@
1818
*/
1919
package org.elasticsearch.index.query;
2020

21-
import org.apache.lucene.index.DocValues;
2221
import org.apache.lucene.index.LeafReaderContext;
23-
import org.apache.lucene.index.NumericDocValues;
2422
import org.apache.lucene.index.SortedNumericDocValues;
2523
import org.apache.lucene.index.Term;
2624
import org.apache.lucene.search.BooleanQuery;
@@ -86,6 +84,11 @@ protected void doWriteTo(StreamOutput out) throws IOException {
8684
out.writeOptionalWriteable(minimumShouldMatchScript);
8785
}
8886

87+
// package protected for testing purpose
88+
String getFieldName() {
89+
return fieldName;
90+
}
91+
8992
public List<?> getValues() {
9093
return values;
9194
}
@@ -116,9 +119,10 @@ public TermsSetQueryBuilder setMinimumShouldMatchScript(Script minimumShouldMatc
116119

117120
@Override
118121
protected boolean doEquals(TermsSetQueryBuilder other) {
119-
return Objects.equals(fieldName, this.fieldName) && Objects.equals(values, this.values) &&
120-
Objects.equals(minimumShouldMatchField, this.minimumShouldMatchField) &&
121-
Objects.equals(minimumShouldMatchScript, this.minimumShouldMatchScript);
122+
return Objects.equals(fieldName, other.fieldName)
123+
&& Objects.equals(values, other.values)
124+
&& Objects.equals(minimumShouldMatchField, other.minimumShouldMatchField)
125+
&& Objects.equals(minimumShouldMatchScript, other.minimumShouldMatchScript);
122126
}
123127

124128
@Override

server/src/test/java/org/elasticsearch/index/query/TermsSetQueryBuilderTests.java

Lines changed: 50 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,9 @@
5959
import java.util.List;
6060
import java.util.Map;
6161
import java.util.function.Function;
62+
import java.util.function.Predicate;
6263

64+
import static java.util.Collections.emptyMap;
6365
import static org.hamcrest.Matchers.containsString;
6466
import static org.hamcrest.Matchers.equalTo;
6567
import static org.hamcrest.Matchers.instanceOf;
@@ -85,17 +87,13 @@ protected TermsSetQueryBuilder doCreateTestQueryBuilder() {
8587
do {
8688
fieldName = randomFrom(MAPPED_FIELD_NAMES);
8789
} while (fieldName.equals(GEO_POINT_FIELD_NAME) || fieldName.equals(GEO_SHAPE_FIELD_NAME));
88-
int numValues = randomIntBetween(0, 10);
89-
List<Object> randomTerms = new ArrayList<>(numValues);
90-
for (int i = 0; i < numValues; i++) {
91-
randomTerms.add(getRandomValueForFieldName(fieldName));
92-
}
90+
List<?> randomTerms = randomValues(fieldName);
9391
TermsSetQueryBuilder queryBuilder = new TermsSetQueryBuilder(STRING_FIELD_NAME, randomTerms);
9492
if (randomBoolean()) {
9593
queryBuilder.setMinimumShouldMatchField("m_s_m");
9694
} else {
9795
queryBuilder.setMinimumShouldMatchScript(
98-
new Script(ScriptType.INLINE, MockScriptEngine.NAME, "_script", Collections.emptyMap()));
96+
new Script(ScriptType.INLINE, MockScriptEngine.NAME, "_script", emptyMap()));
9997
}
10098
return queryBuilder;
10199
}
@@ -122,6 +120,41 @@ protected boolean builderGeneratesCacheableQueries() {
122120
return false;
123121
}
124122

123+
@Override
124+
public TermsSetQueryBuilder mutateInstance(final TermsSetQueryBuilder instance) throws IOException {
125+
String fieldName = instance.getFieldName();
126+
List<?> values = instance.getValues();
127+
String minimumShouldMatchField = null;
128+
Script minimumShouldMatchScript = null;
129+
130+
switch (randomIntBetween(0, 3)) {
131+
case 0:
132+
Predicate<String> predicate = s -> s.equals(instance.getFieldName()) == false && s.equals(GEO_POINT_FIELD_NAME) == false
133+
&& s.equals(GEO_SHAPE_FIELD_NAME) == false;
134+
fieldName = randomValueOtherThanMany(predicate, () -> randomFrom(MAPPED_FIELD_NAMES));
135+
values = randomValues(fieldName);
136+
break;
137+
case 1:
138+
values = randomValues(fieldName);
139+
break;
140+
case 2:
141+
minimumShouldMatchField = randomAlphaOfLengthBetween(1, 10);
142+
break;
143+
case 3:
144+
minimumShouldMatchScript = new Script(ScriptType.INLINE, MockScriptEngine.NAME, randomAlphaOfLength(10), emptyMap());
145+
break;
146+
}
147+
148+
TermsSetQueryBuilder newInstance = new TermsSetQueryBuilder(fieldName, values);
149+
if (minimumShouldMatchField != null) {
150+
newInstance.setMinimumShouldMatchField(minimumShouldMatchField);
151+
}
152+
if (minimumShouldMatchScript != null) {
153+
newInstance.setMinimumShouldMatchScript(minimumShouldMatchScript);
154+
}
155+
return newInstance;
156+
}
157+
125158
public void testBothFieldAndScriptSpecified() {
126159
TermsSetQueryBuilder queryBuilder = new TermsSetQueryBuilder("_field", Collections.emptyList());
127160
queryBuilder.setMinimumShouldMatchScript(new Script(""));
@@ -215,7 +248,7 @@ public void testDoToQuery_msmScriptField() throws Exception {
215248

216249
try (IndexReader ir = DirectoryReader.open(directory)) {
217250
QueryShardContext context = createShardContext();
218-
Script script = new Script(ScriptType.INLINE, MockScriptEngine.NAME, "_script", Collections.emptyMap());
251+
Script script = new Script(ScriptType.INLINE, MockScriptEngine.NAME, "_script", emptyMap());
219252
Query query = new TermsSetQueryBuilder("message", Arrays.asList("a", "b", "c", "d"))
220253
.setMinimumShouldMatchScript(script).doToQuery(context);
221254
IndexSearcher searcher = new IndexSearcher(ir);
@@ -228,6 +261,16 @@ public void testDoToQuery_msmScriptField() throws Exception {
228261
}
229262
}
230263

264+
private static List<?> randomValues(final String fieldName) {
265+
final int numValues = randomIntBetween(0, 10);
266+
final List<Object> values = new ArrayList<>(numValues);
267+
268+
for (int i = 0; i < numValues; i++) {
269+
values.add(getRandomValueForFieldName(fieldName));
270+
}
271+
return values;
272+
}
273+
231274
public static class CustomScriptPlugin extends MockScriptPlugin {
232275

233276
@Override

0 commit comments

Comments
 (0)