Skip to content

Commit 12cb957

Browse files
authored
ESQL: Fix a test bug (elastic#128146)
When adding support for pushing `==` to `semantic_text` we were incorrectly asserting that all queries to that field used nested documents. That's normal for `semantic_text`, but sometimes we query indices that don't have any nested fields. Closes elastic#128122
1 parent e9a8efe commit 12cb957

File tree

1 file changed

+24
-15
lines changed
  • x-pack/plugin/esql/qa/server/single-node/src/javaRestTest/java/org/elasticsearch/xpack/esql/qa/single_node

1 file changed

+24
-15
lines changed

x-pack/plugin/esql/qa/server/single-node/src/javaRestTest/java/org/elasticsearch/xpack/esql/qa/single_node/PushQueriesIT.java

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import org.elasticsearch.client.Request;
1414
import org.elasticsearch.client.Response;
1515
import org.elasticsearch.client.ResponseException;
16+
import org.elasticsearch.common.collect.Iterators;
1617
import org.elasticsearch.test.ListMatcher;
1718
import org.elasticsearch.test.MapMatcher;
1819
import org.elasticsearch.test.TestClustersThreadFilter;
@@ -21,6 +22,7 @@
2122
import org.elasticsearch.xcontent.XContentType;
2223
import org.elasticsearch.xpack.esql.AssertWarnings;
2324
import org.elasticsearch.xpack.esql.qa.rest.RestEsqlTestCase;
25+
import org.hamcrest.Matcher;
2426
import org.junit.ClassRule;
2527

2628
import java.io.IOException;
@@ -39,6 +41,7 @@
3941
import static org.elasticsearch.xpack.esql.qa.rest.RestEsqlTestCase.runEsql;
4042
import static org.elasticsearch.xpack.esql.qa.single_node.RestEsqlIT.commonProfile;
4143
import static org.elasticsearch.xpack.esql.qa.single_node.RestEsqlIT.fixTypesOnProfile;
44+
import static org.hamcrest.Matchers.anyOf;
4245
import static org.hamcrest.Matchers.equalTo;
4346
import static org.hamcrest.Matchers.instanceOf;
4447
import static org.hamcrest.Matchers.startsWith;
@@ -79,7 +82,7 @@ public void testEquality() throws IOException {
7982
case "match_only_text", "semantic_text" -> true;
8083
default -> throw new UnsupportedOperationException("unknown type [" + type + "]");
8184
};
82-
testPushQuery(value, esqlQuery, luceneQuery, filterInCompute, true);
85+
testPushQuery(value, esqlQuery, List.of(luceneQuery), filterInCompute, true);
8386
}
8487

8588
public void testEqualityTooBigToPush() throws IOException {
@@ -93,7 +96,7 @@ public void testEqualityTooBigToPush() throws IOException {
9396
case "semantic_text" -> "FieldExistsQuery [field=_primary_term]";
9497
default -> throw new UnsupportedOperationException("unknown type [" + type + "]");
9598
};
96-
testPushQuery(value, esqlQuery, luceneQuery, true, true);
99+
testPushQuery(value, esqlQuery, List.of(luceneQuery), true, true);
97100
}
98101

99102
/**
@@ -111,7 +114,7 @@ public void testEqualityOrTooBig() throws IOException {
111114
case "semantic_text" -> "FieldExistsQuery [field=_primary_term]";
112115
default -> throw new UnsupportedOperationException("unknown type [" + type + "]");
113116
};
114-
testPushQuery(value, esqlQuery, luceneQuery, true, true);
117+
testPushQuery(value, esqlQuery, List.of(luceneQuery), true, true);
115118
}
116119

117120
public void testEqualityOrOther() throws IOException {
@@ -131,7 +134,7 @@ public void testEqualityOrOther() throws IOException {
131134
case "match_only_text", "semantic_text" -> true;
132135
default -> throw new UnsupportedOperationException("unknown type [" + type + "]");
133136
};
134-
testPushQuery(value, esqlQuery, luceneQuery, filterInCompute, true);
137+
testPushQuery(value, esqlQuery, List.of(luceneQuery), filterInCompute, true);
135138
}
136139

137140
public void testEqualityAndOther() throws IOException {
@@ -140,23 +143,23 @@ public void testEqualityAndOther() throws IOException {
140143
FROM test
141144
| WHERE test == "%value" AND foo == 1
142145
""";
143-
String luceneQuery = switch (type) {
144-
case "text", "auto" -> "#test.keyword:%value -_ignored:test.keyword #foo:[1 TO 1]";
145-
case "match_only_text" -> "foo:[1 TO 1]";
146+
List<String> luceneQueryOptions = switch (type) {
147+
case "text", "auto" -> List.of("#test.keyword:%value -_ignored:test.keyword #foo:[1 TO 1]");
148+
case "match_only_text" -> List.of("foo:[1 TO 1]");
146149
case "semantic_text" ->
147150
/*
148151
* single_value_match is here because there are extra documents hiding in the index
149152
* that don't have the `foo` field.
150153
*/
151-
"#foo:[1 TO 1] #single_value_match(foo)";
154+
List.of("#foo:[1 TO 1] #single_value_match(foo)", "foo:[1 TO 1]");
152155
default -> throw new UnsupportedOperationException("unknown type [" + type + "]");
153156
};
154157
boolean filterInCompute = switch (type) {
155158
case "text", "auto" -> false;
156159
case "match_only_text", "semantic_text" -> true;
157160
default -> throw new UnsupportedOperationException("unknown type [" + type + "]");
158161
};
159-
testPushQuery(value, esqlQuery, luceneQuery, filterInCompute, true);
162+
testPushQuery(value, esqlQuery, luceneQueryOptions, filterInCompute, true);
160163
}
161164

162165
public void testInequality() throws IOException {
@@ -171,7 +174,7 @@ public void testInequality() throws IOException {
171174
case "semantic_text" -> "FieldExistsQuery [field=_primary_term]";
172175
default -> throw new UnsupportedOperationException("unknown type [" + type + "]");
173176
};
174-
testPushQuery(value, esqlQuery, luceneQuery, true, true);
177+
testPushQuery(value, esqlQuery, List.of(luceneQuery), true, true);
175178
}
176179

177180
public void testInequalityTooBigToPush() throws IOException {
@@ -185,7 +188,7 @@ public void testInequalityTooBigToPush() throws IOException {
185188
case "semantic_text" -> "FieldExistsQuery [field=_primary_term]";
186189
default -> throw new UnsupportedOperationException("unknown type [" + type + "]");
187190
};
188-
testPushQuery(value, esqlQuery, luceneQuery, true, false);
191+
testPushQuery(value, esqlQuery, List.of(luceneQuery), true, false);
189192
}
190193

191194
public void testCaseInsensitiveEquality() throws IOException {
@@ -199,10 +202,10 @@ public void testCaseInsensitiveEquality() throws IOException {
199202
case "semantic_text" -> "FieldExistsQuery [field=_primary_term]";
200203
default -> throw new UnsupportedOperationException("unknown type [" + type + "]");
201204
};
202-
testPushQuery(value, esqlQuery, luceneQuery, true, true);
205+
testPushQuery(value, esqlQuery, List.of(luceneQuery), true, true);
203206
}
204207

205-
private void testPushQuery(String value, String esqlQuery, String luceneQuery, boolean filterInCompute, boolean found)
208+
private void testPushQuery(String value, String esqlQuery, List<String> luceneQueryOptions, boolean filterInCompute, boolean found)
206209
throws IOException {
207210
indexValue(value);
208211
String differentValue = randomValueOtherThan(value, () -> randomAlphaOfLength(value.isEmpty() ? 1 : value.length()));
@@ -222,6 +225,12 @@ private void testPushQuery(String value, String esqlQuery, String luceneQuery, b
222225
matchesList().item(matchesMap().entry("name", "test").entry("type", "text")),
223226
equalTo(found ? List.of(List.of(value)) : List.of())
224227
);
228+
Matcher<String> luceneQueryMatcher = anyOf(
229+
() -> Iterators.map(
230+
luceneQueryOptions.iterator(),
231+
(String s) -> equalTo(s.replaceAll("%value", value).replaceAll("%different_value", differentValue))
232+
)
233+
);
225234

226235
@SuppressWarnings("unchecked")
227236
List<Map<String, Object>> profiles = (List<Map<String, Object>>) ((Map<String, Object>) result.get("profile")).get("drivers");
@@ -232,7 +241,7 @@ private void testPushQuery(String value, String esqlQuery, String luceneQuery, b
232241
@SuppressWarnings("unchecked")
233242
List<Map<String, Object>> operators = (List<Map<String, Object>>) p.get("operators");
234243
for (Map<String, Object> o : operators) {
235-
sig.add(checkOperatorProfile(o, luceneQuery.replaceAll("%value", value).replaceAll("%different_value", differentValue)));
244+
sig.add(checkOperatorProfile(o, luceneQueryMatcher));
236245
}
237246
String description = p.get("description").toString();
238247
switch (description) {
@@ -317,7 +326,7 @@ private void indexValue(String value) throws IOException {
317326

318327
private static final Pattern TO_NAME = Pattern.compile("\\[.+", Pattern.DOTALL);
319328

320-
private static String checkOperatorProfile(Map<String, Object> o, String query) {
329+
private static String checkOperatorProfile(Map<String, Object> o, Matcher<String> query) {
321330
String name = (String) o.get("operator");
322331
name = TO_NAME.matcher(name).replaceAll("");
323332
if (name.equals("LuceneSourceOperator")) {

0 commit comments

Comments
 (0)