Skip to content

Commit 711d703

Browse files
committed
[TEST] add missing doc tests for HLRC GetRollupIndexCaps API
1 parent 2da239f commit 711d703

File tree

1 file changed

+116
-0
lines changed

1 file changed

+116
-0
lines changed

client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/RollupDocumentationIT.java

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838
import org.elasticsearch.client.rollup.DeleteRollupJobResponse;
3939
import org.elasticsearch.client.rollup.GetRollupCapsRequest;
4040
import org.elasticsearch.client.rollup.GetRollupCapsResponse;
41+
import org.elasticsearch.client.rollup.GetRollupIndexCapsRequest;
42+
import org.elasticsearch.client.rollup.GetRollupIndexCapsResponse;
4143
import org.elasticsearch.client.rollup.GetRollupJobRequest;
4244
import org.elasticsearch.client.rollup.GetRollupJobResponse;
4345
import org.elasticsearch.client.rollup.GetRollupJobResponse.JobWrapper;
@@ -406,6 +408,120 @@ public void onFailure(Exception e) {
406408
assertTrue(latch.await(30L, TimeUnit.SECONDS));
407409
}
408410

411+
@SuppressWarnings("unused")
412+
public void testGetRollupIndexCaps() throws Exception {
413+
RestHighLevelClient client = highLevelClient();
414+
415+
DateHistogramGroupConfig dateHistogram =
416+
new DateHistogramGroupConfig("timestamp", DateHistogramInterval.HOUR, new DateHistogramInterval("7d"), "UTC"); // <1>
417+
TermsGroupConfig terms = new TermsGroupConfig("hostname", "datacenter");
418+
HistogramGroupConfig histogram = new HistogramGroupConfig(5L, "load", "net_in", "net_out");
419+
GroupConfig groups = new GroupConfig(dateHistogram, histogram, terms);
420+
List<MetricConfig> metrics = new ArrayList<>(); // <1>
421+
metrics.add(new MetricConfig("temperature", Arrays.asList("min", "max", "sum")));
422+
metrics.add(new MetricConfig("voltage", Arrays.asList("avg", "value_count")));
423+
424+
//tag::x-pack-rollup-get-rollup-index-caps-setup
425+
final String indexPattern = "docs";
426+
final String rollupIndexName = "rollup";
427+
final String cron = "*/1 * * * * ?";
428+
final int pageSize = 100;
429+
final TimeValue timeout = null;
430+
431+
String id = "job_1";
432+
RollupJobConfig config = new RollupJobConfig(id, indexPattern, rollupIndexName, cron,
433+
pageSize, groups, metrics, timeout);
434+
435+
PutRollupJobRequest request = new PutRollupJobRequest(config);
436+
PutRollupJobResponse response = client.rollup().putRollupJob(request, RequestOptions.DEFAULT);
437+
438+
boolean acknowledged = response.isAcknowledged();
439+
//end::x-pack-rollup-get-rollup-index-caps-setup
440+
assertTrue(acknowledged);
441+
442+
ClusterHealthRequest healthRequest = new ClusterHealthRequest(config.getRollupIndex()).waitForYellowStatus();
443+
ClusterHealthResponse healthResponse = client.cluster().health(healthRequest, RequestOptions.DEFAULT);
444+
assertFalse(healthResponse.isTimedOut());
445+
assertThat(healthResponse.getStatus(), isOneOf(ClusterHealthStatus.YELLOW, ClusterHealthStatus.GREEN));
446+
447+
// Now that the job is created, we should have a rollup index with metadata.
448+
// We can test out the caps API now.
449+
450+
//tag::x-pack-rollup-get-rollup-index-caps-request
451+
GetRollupIndexCapsRequest getRollupIndexCapsRequest = new GetRollupIndexCapsRequest("rollup");
452+
//end::x-pack-rollup-get-rollup-index-caps-request
453+
454+
//tag::x-pack-rollup-get-rollup-index-caps-execute
455+
GetRollupIndexCapsResponse capsResponse = client.rollup()
456+
.getRollupIndexCapabilities(getRollupIndexCapsRequest, RequestOptions.DEFAULT);
457+
//end::x-pack-rollup-get-rollup-index-caps-execute
458+
459+
//tag::x-pack-rollup-get-rollup-index-caps-response
460+
Map<String, RollableIndexCaps> rolledPatterns = capsResponse.getJobs();
461+
462+
RollableIndexCaps docsPattern = rolledPatterns.get("rollup");
463+
464+
// indexName will be "rollup", the target index we requested
465+
String indexName = docsPattern.getIndexName();
466+
467+
// Each index pattern can have multiple jobs that rolled it up, so `getJobCaps()`
468+
// returns a list of jobs that rolled up the pattern
469+
List<RollupJobCaps> rollupJobs = docsPattern.getJobCaps();
470+
RollupJobCaps jobCaps = rollupJobs.get(0);
471+
472+
// jobID is the identifier we used when we created the job (e.g. `job1`)
473+
String jobID = jobCaps.getJobID();
474+
475+
// rollupIndex is the location that the job stored it's rollup docs (e.g. `rollup`)
476+
String rollupIndex = jobCaps.getRollupIndex();
477+
478+
// Finally, fieldCaps are the capabilities of individual fields in the config
479+
// The key is the field name, and the value is a RollupFieldCaps object which
480+
// provides more info.
481+
Map<String, RollupJobCaps.RollupFieldCaps> fieldCaps = jobCaps.getFieldCaps();
482+
483+
// If we retrieve the "timestamp" field, it returns a list of maps. Each list
484+
// item represents a different aggregation that can be run against the "timestamp"
485+
// field, and any additional details specific to that agg (interval, etc)
486+
List<Map<String, Object>> timestampCaps = fieldCaps.get("timestamp").getAggs();
487+
assert timestampCaps.get(0).toString().equals("{agg=date_histogram, delay=7d, interval=1h, time_zone=UTC}");
488+
489+
// In contrast to the timestamp field, the temperature field has multiple aggs configured
490+
List<Map<String, Object>> temperatureCaps = fieldCaps.get("temperature").getAggs();
491+
assert temperatureCaps.toString().equals("[{agg=min}, {agg=max}, {agg=sum}]");
492+
//end::x-pack-rollup-get-rollup-index-caps-response
493+
494+
assertThat(indexName, equalTo("rollup"));
495+
assertThat(jobID, equalTo("job_1"));
496+
assertThat(rollupIndex, equalTo("rollup"));
497+
assertThat(fieldCaps.size(), equalTo(8));
498+
499+
// tag::x-pack-rollup-get-rollup-index-caps-execute-listener
500+
ActionListener<GetRollupIndexCapsResponse> listener = new ActionListener<GetRollupIndexCapsResponse>() {
501+
@Override
502+
public void onResponse(GetRollupIndexCapsResponse response) {
503+
504+
// <1>
505+
}
506+
507+
@Override
508+
public void onFailure(Exception e) {
509+
// <2>
510+
}
511+
};
512+
// end::x-pack-rollup-get-rollup-index-caps-execute-listener
513+
514+
// Replace the empty listener by a blocking listener in test
515+
final CountDownLatch latch = new CountDownLatch(1);
516+
listener = new LatchedActionListener<>(listener, latch);
517+
518+
// tag::x-pack-rollup-get-rollup-index-caps-execute-async
519+
client.rollup().getRollupIndexCapabilitiesAsync(getRollupIndexCapsRequest, RequestOptions.DEFAULT, listener); // <1>
520+
// end::x-pack-rollup-get-rollup-index-caps-execute-async
521+
522+
assertTrue(latch.await(30L, TimeUnit.SECONDS));
523+
}
524+
409525
@SuppressWarnings("unused")
410526
public void testDeleteRollupJob() throws Exception {
411527
RestHighLevelClient client = highLevelClient();

0 commit comments

Comments
 (0)