|
42 | 42 | import java.util.Comparator; |
43 | 43 | import java.util.List; |
44 | 44 | import java.util.Map; |
| 45 | +import java.util.TreeMap; |
45 | 46 |
|
46 | 47 | import static org.hamcrest.Matchers.equalTo; |
47 | 48 | import static org.hamcrest.Matchers.greaterThan; |
@@ -271,6 +272,51 @@ public void testDateHistogramAggregation() throws IOException { |
271 | 272 | assertTrue(matchResult.getMessage(), matchResult.isMatch()); |
272 | 273 | } |
273 | 274 |
|
| 275 | + public void testEsqlSource() throws IOException { |
| 276 | + int numberOfDocuments = ESTestCase.randomIntBetween(100, 200); |
| 277 | + final List<XContentBuilder> documents = generateDocuments(numberOfDocuments); |
| 278 | + |
| 279 | + indexDocuments(documents); |
| 280 | + |
| 281 | + final String query = "FROM $index METADATA _source, _id | KEEP _source, _id | LIMIT " + numberOfDocuments; |
| 282 | + final MatchResult matchResult = Matcher.matchSource() |
| 283 | + .mappings(getContenderMappings(), getBaselineMappings()) |
| 284 | + .settings(getContenderSettings(), getBaselineSettings()) |
| 285 | + .expected(getEsqlSourceResults(esqlBaseline(query))) |
| 286 | + .ignoringSort(true) |
| 287 | + .isEqualTo(getEsqlSourceResults(esqlContender(query))); |
| 288 | + assertTrue(matchResult.getMessage(), matchResult.isMatch()); |
| 289 | + } |
| 290 | + |
| 291 | + public void testEsqlTermsAggregation() throws IOException { |
| 292 | + int numberOfDocuments = ESTestCase.randomIntBetween(100, 200); |
| 293 | + final List<XContentBuilder> documents = generateDocuments(numberOfDocuments); |
| 294 | + |
| 295 | + indexDocuments(documents); |
| 296 | + |
| 297 | + final String query = "FROM $index | STATS count(*) BY host.name | SORT host.name | LIMIT " + numberOfDocuments; |
| 298 | + final MatchResult matchResult = Matcher.mappings(getContenderMappings(), getBaselineMappings()) |
| 299 | + .settings(getContenderSettings(), getBaselineSettings()) |
| 300 | + .expected(getEsqlStatsResults(esqlBaseline(query))) |
| 301 | + .ignoringSort(true) |
| 302 | + .isEqualTo(getEsqlStatsResults(esqlContender(query))); |
| 303 | + assertTrue(matchResult.getMessage(), matchResult.isMatch()); |
| 304 | + } |
| 305 | + |
| 306 | + public void testFieldCaps() throws IOException { |
| 307 | + int numberOfDocuments = ESTestCase.randomIntBetween(20, 50); |
| 308 | + final List<XContentBuilder> documents = generateDocuments(numberOfDocuments); |
| 309 | + |
| 310 | + indexDocuments(documents); |
| 311 | + |
| 312 | + final MatchResult matchResult = Matcher.mappings(getContenderMappings(), getBaselineMappings()) |
| 313 | + .settings(getContenderSettings(), getBaselineSettings()) |
| 314 | + .expected(getFields(fieldCapsBaseline())) |
| 315 | + .ignoringSort(true) |
| 316 | + .isEqualTo(getFields(fieldCapsContender())); |
| 317 | + assertTrue(matchResult.getMessage(), matchResult.isMatch()); |
| 318 | + } |
| 319 | + |
274 | 320 | @Override |
275 | 321 | public Response indexBaselineDocuments(CheckedSupplier<List<XContentBuilder>, IOException> documentsSupplier) throws IOException { |
276 | 322 | var response = super.indexBaselineDocuments(documentsSupplier); |
@@ -329,6 +375,40 @@ private static List<Map<String, Object>> getQueryHits(final Response response) t |
329 | 375 | .toList(); |
330 | 376 | } |
331 | 377 |
|
| 378 | + @SuppressWarnings("unchecked") |
| 379 | + private static Map<String, Object> getFields(final Response response) throws IOException { |
| 380 | + final Map<String, Object> map = XContentHelper.convertToMap(XContentType.JSON.xContent(), response.getEntity().getContent(), true); |
| 381 | + final Map<String, Object> fields = (Map<String, Object>) map.get("fields"); |
| 382 | + assertThat(fields.size(), greaterThan(0)); |
| 383 | + return new TreeMap<>(fields); |
| 384 | + } |
| 385 | + |
| 386 | + @SuppressWarnings("unchecked") |
| 387 | + private static List<Map<String, Object>> getEsqlSourceResults(final Response response) throws IOException { |
| 388 | + final Map<String, Object> map = XContentHelper.convertToMap(XContentType.JSON.xContent(), response.getEntity().getContent(), true); |
| 389 | + final List<List<Object>> values = (List<List<Object>>) map.get("values"); |
| 390 | + assertThat(values.size(), greaterThan(0)); |
| 391 | + |
| 392 | + // Results contain a list of [source, id] lists. |
| 393 | + return values.stream() |
| 394 | + .sorted(Comparator.comparingInt((List<Object> value) -> Integer.parseInt((String) value.get(1)))) |
| 395 | + .map(value -> (Map<String, Object>) value.get(0)) |
| 396 | + .toList(); |
| 397 | + } |
| 398 | + |
| 399 | + @SuppressWarnings("unchecked") |
| 400 | + private static List<Map<String, Object>> getEsqlStatsResults(final Response response) throws IOException { |
| 401 | + final Map<String, Object> map = XContentHelper.convertToMap(XContentType.JSON.xContent(), response.getEntity().getContent(), true); |
| 402 | + final List<List<Object>> values = (List<List<Object>>) map.get("values"); |
| 403 | + assertThat(values.size(), greaterThan(0)); |
| 404 | + |
| 405 | + // Results contain a list of [agg value, group name] lists. |
| 406 | + return values.stream() |
| 407 | + .sorted(Comparator.comparing((List<Object> value) -> (String) value.get(1))) |
| 408 | + .map(value -> Map.of((String) value.get(1), value.get(0))) |
| 409 | + .toList(); |
| 410 | + } |
| 411 | + |
332 | 412 | @SuppressWarnings("unchecked") |
333 | 413 | private static List<Map<String, Object>> getAggregationBuckets(final Response response, final String aggName) throws IOException { |
334 | 414 | final Map<String, Object> map = XContentHelper.convertToMap(XContentType.JSON.xContent(), response.getEntity().getContent(), true); |
|
0 commit comments