|
@@ -18,6 +18,7 @@
|
|
|
*/
|
|
|
package org.elasticsearch.client;
|
|
|
|
|
|
+import org.elasticsearch.ElasticsearchStatusException;
|
|
|
import org.elasticsearch.action.admin.indices.refresh.RefreshRequest;
|
|
|
import org.elasticsearch.action.admin.indices.refresh.RefreshResponse;
|
|
|
import org.elasticsearch.action.bulk.BulkItemResponse;
|
|
@@ -31,6 +32,8 @@ import org.elasticsearch.client.rollup.GetRollupJobRequest;
|
|
|
import org.elasticsearch.client.rollup.GetRollupJobResponse;
|
|
|
import org.elasticsearch.client.rollup.GetRollupJobResponse.IndexerState;
|
|
|
import org.elasticsearch.client.rollup.GetRollupJobResponse.JobWrapper;
|
|
|
+import org.elasticsearch.client.rollup.DeleteRollupJobRequest;
|
|
|
+import org.elasticsearch.client.rollup.DeleteRollupJobResponse;
|
|
|
import org.elasticsearch.client.rollup.PutRollupJobRequest;
|
|
|
import org.elasticsearch.client.rollup.PutRollupJobResponse;
|
|
|
import org.elasticsearch.client.rollup.job.config.DateHistogramGroupConfig;
|
|
@@ -46,6 +49,7 @@ import org.elasticsearch.search.aggregations.metrics.MaxAggregationBuilder;
|
|
|
import org.elasticsearch.search.aggregations.metrics.MinAggregationBuilder;
|
|
|
import org.elasticsearch.search.aggregations.metrics.SumAggregationBuilder;
|
|
|
import org.elasticsearch.search.aggregations.metrics.ValueCountAggregationBuilder;
|
|
|
+import org.junit.Before;
|
|
|
|
|
|
import java.util.Arrays;
|
|
|
import java.util.Collections;
|
|
@@ -60,19 +64,35 @@ import static org.hamcrest.Matchers.greaterThan;
|
|
|
import static org.hamcrest.Matchers.hasKey;
|
|
|
import static org.hamcrest.Matchers.hasSize;
|
|
|
import static org.hamcrest.Matchers.empty;
|
|
|
+import static org.hamcrest.Matchers.is;
|
|
|
import static org.hamcrest.Matchers.lessThan;
|
|
|
|
|
|
public class RollupIT extends ESRestHighLevelClientTestCase {
|
|
|
|
|
|
+ double sum = 0.0d;
|
|
|
+ int max = Integer.MIN_VALUE;
|
|
|
+ int min = Integer.MAX_VALUE;
|
|
|
private static final List<String> SUPPORTED_METRICS = Arrays.asList(MaxAggregationBuilder.NAME, MinAggregationBuilder.NAME,
|
|
|
SumAggregationBuilder.NAME, AvgAggregationBuilder.NAME, ValueCountAggregationBuilder.NAME);
|
|
|
|
|
|
- @SuppressWarnings("unchecked")
|
|
|
- public void testPutAndGetRollupJob() throws Exception {
|
|
|
- double sum = 0.0d;
|
|
|
- int max = Integer.MIN_VALUE;
|
|
|
- int min = Integer.MAX_VALUE;
|
|
|
+ private String id;
|
|
|
+ private String indexPattern;
|
|
|
+ private String rollupIndex;
|
|
|
+ private String cron;
|
|
|
+ private int pageSize;
|
|
|
+ private int numDocs;
|
|
|
+
|
|
|
+ @Before
|
|
|
+ public void init() throws Exception {
|
|
|
+ id = randomAlphaOfLength(10);
|
|
|
+ indexPattern = randomFrom("docs", "d*", "doc*");
|
|
|
+ rollupIndex = randomFrom("rollup", "test");
|
|
|
+ cron = "*/1 * * * * ?";
|
|
|
+ numDocs = indexDocs();
|
|
|
+ pageSize = randomIntBetween(numDocs, numDocs * 10);
|
|
|
+ }
|
|
|
|
|
|
+ public int indexDocs() throws Exception {
|
|
|
final BulkRequest bulkRequest = new BulkRequest();
|
|
|
bulkRequest.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE);
|
|
|
for (int minute = 0; minute < 60; minute++) {
|
|
@@ -112,12 +132,33 @@ public class RollupIT extends ESRestHighLevelClientTestCase {
|
|
|
|
|
|
RefreshResponse refreshResponse = highLevelClient().indices().refresh(new RefreshRequest("docs"), RequestOptions.DEFAULT);
|
|
|
assertEquals(0, refreshResponse.getFailedShards());
|
|
|
+ return numDocs;
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
- final String id = randomAlphaOfLength(10);
|
|
|
- final String indexPattern = randomFrom("docs", "d*", "doc*");
|
|
|
- final String rollupIndex = randomFrom("rollup", "test");
|
|
|
- final String cron = "*/1 * * * * ?";
|
|
|
- final int pageSize = randomIntBetween(numDocs, numDocs * 10);
|
|
|
+ public void testDeleteRollupJob() throws Exception {
|
|
|
+ final GroupConfig groups = new GroupConfig(new DateHistogramGroupConfig("date", DateHistogramInterval.DAY));
|
|
|
+ final List<MetricConfig> metrics = Collections.singletonList(new MetricConfig("value", SUPPORTED_METRICS));
|
|
|
+ final TimeValue timeout = TimeValue.timeValueSeconds(randomIntBetween(30, 600));
|
|
|
+ PutRollupJobRequest putRollupJobRequest =
|
|
|
+ new PutRollupJobRequest(new RollupJobConfig(id, indexPattern, rollupIndex, cron, pageSize, groups, metrics, timeout));
|
|
|
+ final RollupClient rollupClient = highLevelClient().rollup();
|
|
|
+ PutRollupJobResponse response = execute(putRollupJobRequest, rollupClient::putRollupJob, rollupClient::putRollupJobAsync);
|
|
|
+ DeleteRollupJobRequest deleteRollupJobRequest = new DeleteRollupJobRequest(id);
|
|
|
+ DeleteRollupJobResponse deleteRollupJobResponse = highLevelClient().rollup()
|
|
|
+ .deleteRollupJob(deleteRollupJobRequest, RequestOptions.DEFAULT);
|
|
|
+ assertTrue(deleteRollupJobResponse.isAcknowledged());
|
|
|
+ }
|
|
|
+
|
|
|
+ public void testDeleteMissingRollupJob() {
|
|
|
+ DeleteRollupJobRequest deleteRollupJobRequest = new DeleteRollupJobRequest(randomAlphaOfLength(10));
|
|
|
+ ElasticsearchStatusException responseException = expectThrows(ElasticsearchStatusException.class,() -> highLevelClient().rollup()
|
|
|
+ .deleteRollupJob(deleteRollupJobRequest, RequestOptions.DEFAULT));
|
|
|
+ assertThat(responseException.status().getStatus(), is(404));
|
|
|
+ }
|
|
|
+
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
+ public void testPutAndGetRollupJob() throws Exception {
|
|
|
// TODO expand this to also test with histogram and terms?
|
|
|
final GroupConfig groups = new GroupConfig(new DateHistogramGroupConfig("date", DateHistogramInterval.DAY));
|
|
|
final List<MetricConfig> metrics = Collections.singletonList(new MetricConfig("value", SUPPORTED_METRICS));
|
|
@@ -134,9 +175,6 @@ public class RollupIT extends ESRestHighLevelClientTestCase {
|
|
|
Response startResponse = client().performRequest(new Request("POST", "/_xpack/rollup/job/" + id + "/_start"));
|
|
|
assertEquals(RestStatus.OK.getStatus(), startResponse.getHttpResponse().getStatusLine().getStatusCode());
|
|
|
|
|
|
- int finalMin = min;
|
|
|
- int finalMax = max;
|
|
|
- double finalSum = sum;
|
|
|
assertBusy(() -> {
|
|
|
SearchResponse searchResponse = highLevelClient().search(new SearchRequest(rollupIndex), RequestOptions.DEFAULT);
|
|
|
assertEquals(0, searchResponse.getFailedShards());
|
|
@@ -154,13 +192,13 @@ public class RollupIT extends ESRestHighLevelClientTestCase {
|
|
|
for (String name : metric.getMetrics()) {
|
|
|
Number value = (Number) source.get(metric.getField() + "." + name + ".value");
|
|
|
if ("min".equals(name)) {
|
|
|
- assertEquals(finalMin, value.intValue());
|
|
|
+ assertEquals(min, value.intValue());
|
|
|
} else if ("max".equals(name)) {
|
|
|
- assertEquals(finalMax, value.intValue());
|
|
|
+ assertEquals(max, value.intValue());
|
|
|
} else if ("sum".equals(name)) {
|
|
|
- assertEquals(finalSum, value.doubleValue(), 0.0d);
|
|
|
+ assertEquals(sum, value.doubleValue(), 0.0d);
|
|
|
} else if ("avg".equals(name)) {
|
|
|
- assertEquals(finalSum, value.doubleValue(), 0.0d);
|
|
|
+ assertEquals(sum, value.doubleValue(), 0.0d);
|
|
|
Number avgCount = (Number) source.get(metric.getField() + "." + name + "._count");
|
|
|
assertEquals(numDocs, avgCount.intValue());
|
|
|
} else if ("value_count".equals(name)) {
|
|
@@ -191,4 +229,5 @@ public class RollupIT extends ESRestHighLevelClientTestCase {
|
|
|
GetRollupJobResponse getResponse = execute(getRollupJobRequest, rollupClient::getRollupJob, rollupClient::getRollupJobAsync);
|
|
|
assertThat(getResponse.getJobs(), empty());
|
|
|
}
|
|
|
+
|
|
|
}
|