Skip to content

Commit 34da953

Browse files
authored
[TEST] Add coverage for field caps and ES|QL to LogsDB QA testing (#114505)
* Add coverage for field caps and ES|QL to LogsDB QA testing * address comments * address comments * address comments
1 parent 8a38621 commit 34da953

File tree

2 files changed

+107
-0
lines changed

2 files changed

+107
-0
lines changed

modules/data-streams/src/javaRestTest/java/org/elasticsearch/datastreams/logsdb/qa/AbstractChallengeRestTest.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,33 @@ private Response query(final SearchSourceBuilder search, final Supplier<String>
276276
return client.performRequest(request);
277277
}
278278

279+
public Response esqlBaseline(final String query) throws IOException {
280+
return esql(query, this::getBaselineDataStreamName);
281+
}
282+
283+
public Response esqlContender(final String query) throws IOException {
284+
return esql(query, this::getContenderDataStreamName);
285+
}
286+
287+
private Response esql(final String query, final Supplier<String> dataStreamNameSupplier) throws IOException {
288+
final Request request = new Request("POST", "/_query");
289+
request.setJsonEntity("{\"query\": \"" + query.replace("$index", dataStreamNameSupplier.get()) + "\"}");
290+
return client.performRequest(request);
291+
}
292+
293+
public Response fieldCapsBaseline() throws IOException {
294+
return fieldCaps(this::getBaselineDataStreamName);
295+
}
296+
297+
public Response fieldCapsContender() throws IOException {
298+
return fieldCaps(this::getContenderDataStreamName);
299+
}
300+
301+
private Response fieldCaps(final Supplier<String> dataStreamNameSupplier) throws IOException {
302+
final Request request = new Request("GET", "/" + dataStreamNameSupplier.get() + "/_field_caps?fields=*");
303+
return client.performRequest(request);
304+
}
305+
279306
public String getBaselineDataStreamName() {
280307
return baselineDataStreamName;
281308
}

modules/data-streams/src/javaRestTest/java/org/elasticsearch/datastreams/logsdb/qa/StandardVersusLogsIndexModeChallengeRestIT.java

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import java.util.Comparator;
4343
import java.util.List;
4444
import java.util.Map;
45+
import java.util.TreeMap;
4546

4647
import static org.hamcrest.Matchers.equalTo;
4748
import static org.hamcrest.Matchers.greaterThan;
@@ -271,6 +272,51 @@ public void testDateHistogramAggregation() throws IOException {
271272
assertTrue(matchResult.getMessage(), matchResult.isMatch());
272273
}
273274

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+
274320
@Override
275321
public Response indexBaselineDocuments(CheckedSupplier<List<XContentBuilder>, IOException> documentsSupplier) throws IOException {
276322
var response = super.indexBaselineDocuments(documentsSupplier);
@@ -329,6 +375,40 @@ private static List<Map<String, Object>> getQueryHits(final Response response) t
329375
.toList();
330376
}
331377

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+
332412
@SuppressWarnings("unchecked")
333413
private static List<Map<String, Object>> getAggregationBuckets(final Response response, final String aggName) throws IOException {
334414
final Map<String, Object> map = XContentHelper.convertToMap(XContentType.JSON.xContent(), response.getEntity().getContent(), true);

0 commit comments

Comments
 (0)