27
27
import org .elasticsearch .action .bulk .BulkRequest ;
28
28
import org .elasticsearch .action .bulk .BulkResponse ;
29
29
import org .elasticsearch .action .index .IndexRequest ;
30
+ import org .elasticsearch .action .search .SearchRequest ;
31
+ import org .elasticsearch .action .search .SearchResponse ;
30
32
import org .elasticsearch .action .support .WriteRequest ;
31
33
import org .elasticsearch .client .ESRestHighLevelClientTestCase ;
32
34
import org .elasticsearch .client .RequestOptions ;
60
62
import org .elasticsearch .common .unit .TimeValue ;
61
63
import org .elasticsearch .rest .RestStatus ;
62
64
import org .elasticsearch .search .aggregations .bucket .histogram .DateHistogramInterval ;
65
+ import org .elasticsearch .search .aggregations .metrics .NumericMetricsAggregation ;
66
+ import org .elasticsearch .search .aggregations .metrics .max .MaxAggregationBuilder ;
67
+ import org .elasticsearch .search .builder .SearchSourceBuilder ;
63
68
import org .junit .Before ;
64
69
65
70
import java .io .IOException ;
72
77
import java .util .concurrent .TimeUnit ;
73
78
74
79
import static org .elasticsearch .common .xcontent .XContentFactory .jsonBuilder ;
80
+ import static org .hamcrest .Matchers .closeTo ;
75
81
import static org .hamcrest .Matchers .equalTo ;
76
82
import static org .hamcrest .Matchers .hasSize ;
77
83
import static org .hamcrest .Matchers .isOneOf ;
@@ -89,7 +95,7 @@ public void setUpDocs() throws IOException {
89
95
.field ("timestamp" , String .format (Locale .ROOT , "2018-01-01T00:%02d:00Z" , i ))
90
96
.field ("hostname" , 0 )
91
97
.field ("datacenter" , 0 )
92
- .field ("temperature" , 0 )
98
+ .field ("temperature" , i )
93
99
.field ("voltage" , 0 )
94
100
.field ("load" , 0 )
95
101
.field ("net_in" , 0 )
@@ -330,6 +336,56 @@ public void onFailure(Exception e) {
330
336
assertTrue (latch .await (30L , TimeUnit .SECONDS ));
331
337
}
332
338
339
+ public void testSearch () throws Exception {
340
+ // Setup a rollup index to query
341
+ testCreateRollupJob ();
342
+
343
+ RestHighLevelClient client = highLevelClient ();
344
+
345
+ // tag::search-request
346
+ SearchRequest request = new SearchRequest ();
347
+ request .source (new SearchSourceBuilder ()
348
+ .size (0 )
349
+ .aggregation (new MaxAggregationBuilder ("max_temperature" )
350
+ .field ("temperature" )));
351
+ // end::search-request
352
+
353
+ // tag::search-execute
354
+ SearchResponse response =
355
+ client .rollup ().search (request , RequestOptions .DEFAULT );
356
+ // end::search-execute
357
+
358
+ // tag::search-response
359
+ NumericMetricsAggregation .SingleValue maxTemperature =
360
+ response .getAggregations ().get ("max_temperature" );
361
+ assertThat (maxTemperature .value (), closeTo (49.0 , .00001 ));
362
+ // end::search-response
363
+
364
+ ActionListener <SearchResponse > listener ;
365
+ // tag::search-execute-listener
366
+ listener = new ActionListener <SearchResponse >() {
367
+ @ Override
368
+ public void onResponse (SearchResponse response ) {
369
+ // <1>
370
+ }
371
+
372
+ @ Override
373
+ public void onFailure (Exception e ) {
374
+ // <2>
375
+ }
376
+ };
377
+ // end::search-execute-listener
378
+
379
+ final CountDownLatch latch = new CountDownLatch (1 );
380
+ listener = new LatchedActionListener <>(listener , latch );
381
+
382
+ // tag::search-execute-async
383
+ client .rollup ().searchAsync (request , RequestOptions .DEFAULT , listener ); // <1>
384
+ // end::search-execute-async
385
+
386
+ assertTrue (latch .await (30L , TimeUnit .SECONDS ));
387
+ }
388
+
333
389
@ SuppressWarnings ("unused" )
334
390
public void testGetRollupCaps () throws Exception {
335
391
RestHighLevelClient client = highLevelClient ();
0 commit comments