|
@@ -27,6 +27,8 @@ import org.elasticsearch.action.admin.indices.refresh.RefreshResponse;
|
|
|
import org.elasticsearch.action.bulk.BulkRequest;
|
|
|
import org.elasticsearch.action.bulk.BulkResponse;
|
|
|
import org.elasticsearch.action.index.IndexRequest;
|
|
|
+import org.elasticsearch.action.search.SearchRequest;
|
|
|
+import org.elasticsearch.action.search.SearchResponse;
|
|
|
import org.elasticsearch.action.support.WriteRequest;
|
|
|
import org.elasticsearch.client.ESRestHighLevelClientTestCase;
|
|
|
import org.elasticsearch.client.RequestOptions;
|
|
@@ -60,6 +62,9 @@ import org.elasticsearch.cluster.health.ClusterHealthStatus;
|
|
|
import org.elasticsearch.common.unit.TimeValue;
|
|
|
import org.elasticsearch.rest.RestStatus;
|
|
|
import org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramInterval;
|
|
|
+import org.elasticsearch.search.aggregations.metrics.NumericMetricsAggregation;
|
|
|
+import org.elasticsearch.search.aggregations.metrics.MaxAggregationBuilder;
|
|
|
+import org.elasticsearch.search.builder.SearchSourceBuilder;
|
|
|
import org.junit.Before;
|
|
|
|
|
|
import java.io.IOException;
|
|
@@ -72,6 +77,7 @@ import java.util.concurrent.CountDownLatch;
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
|
|
|
+import static org.hamcrest.Matchers.closeTo;
|
|
|
import static org.hamcrest.Matchers.equalTo;
|
|
|
import static org.hamcrest.Matchers.hasSize;
|
|
|
import static org.hamcrest.Matchers.isOneOf;
|
|
@@ -89,7 +95,7 @@ public class RollupDocumentationIT extends ESRestHighLevelClientTestCase {
|
|
|
.field("timestamp", String.format(Locale.ROOT, "2018-01-01T00:%02d:00Z", i))
|
|
|
.field("hostname", 0)
|
|
|
.field("datacenter", 0)
|
|
|
- .field("temperature", 0)
|
|
|
+ .field("temperature", i)
|
|
|
.field("voltage", 0)
|
|
|
.field("load", 0)
|
|
|
.field("net_in", 0)
|
|
@@ -330,6 +336,56 @@ public class RollupDocumentationIT extends ESRestHighLevelClientTestCase {
|
|
|
assertTrue(latch.await(30L, TimeUnit.SECONDS));
|
|
|
}
|
|
|
|
|
|
+ public void testSearch() throws Exception {
|
|
|
+ // Setup a rollup index to query
|
|
|
+ testCreateRollupJob();
|
|
|
+
|
|
|
+ RestHighLevelClient client = highLevelClient();
|
|
|
+
|
|
|
+ // tag::search-request
|
|
|
+ SearchRequest request = new SearchRequest();
|
|
|
+ request.source(new SearchSourceBuilder()
|
|
|
+ .size(0)
|
|
|
+ .aggregation(new MaxAggregationBuilder("max_temperature")
|
|
|
+ .field("temperature")));
|
|
|
+ // end::search-request
|
|
|
+
|
|
|
+ // tag::search-execute
|
|
|
+ SearchResponse response =
|
|
|
+ client.rollup().search(request, RequestOptions.DEFAULT);
|
|
|
+ // end::search-execute
|
|
|
+
|
|
|
+ // tag::search-response
|
|
|
+ NumericMetricsAggregation.SingleValue maxTemperature =
|
|
|
+ response.getAggregations().get("max_temperature");
|
|
|
+ assertThat(maxTemperature.value(), closeTo(49.0, .00001));
|
|
|
+ // end::search-response
|
|
|
+
|
|
|
+ ActionListener<SearchResponse> listener;
|
|
|
+ // tag::search-execute-listener
|
|
|
+ listener = new ActionListener<SearchResponse>() {
|
|
|
+ @Override
|
|
|
+ public void onResponse(SearchResponse response) {
|
|
|
+ // <1>
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onFailure(Exception e) {
|
|
|
+ // <2>
|
|
|
+ }
|
|
|
+ };
|
|
|
+ // end::search-execute-listener
|
|
|
+
|
|
|
+ final CountDownLatch latch = new CountDownLatch(1);
|
|
|
+ listener = new LatchedActionListener<>(listener, latch);
|
|
|
+
|
|
|
+ // tag::search-execute-async
|
|
|
+ client.rollup().searchAsync(request, RequestOptions.DEFAULT, listener); // <1>
|
|
|
+ // end::search-execute-async
|
|
|
+
|
|
|
+ assertTrue(latch.await(30L, TimeUnit.SECONDS));
|
|
|
+ }
|
|
|
+
|
|
|
@SuppressWarnings("unused")
|
|
|
public void testGetRollupCaps() throws Exception {
|
|
|
RestHighLevelClient client = highLevelClient();
|