2323import org .elasticsearch .action .bulk .BulkRequest ;
2424import org .elasticsearch .action .bulk .BulkResponse ;
2525import org .elasticsearch .action .index .IndexRequest ;
26+ import org .elasticsearch .action .index .IndexResponse ;
2627import org .elasticsearch .action .search .ClearScrollRequest ;
2728import org .elasticsearch .action .search .ClearScrollResponse ;
2829import org .elasticsearch .action .search .SearchRequest ;
5253import org .elasticsearch .search .builder .SearchSourceBuilder ;
5354import org .elasticsearch .search .fetch .subphase .highlight .HighlightBuilder ;
5455import org .elasticsearch .search .fetch .subphase .highlight .HighlightField ;
56+ import org .elasticsearch .search .profile .ProfileResult ;
57+ import org .elasticsearch .search .profile .ProfileShardResult ;
58+ import org .elasticsearch .search .profile .aggregation .AggregationProfileShardResult ;
59+ import org .elasticsearch .search .profile .query .CollectorResult ;
60+ import org .elasticsearch .search .profile .query .QueryProfileShardResult ;
5561import org .elasticsearch .search .sort .FieldSortBuilder ;
5662import org .elasticsearch .search .sort .ScoreSortBuilder ;
5763import org .elasticsearch .search .sort .SortOrder ;
7076
7177import static org .elasticsearch .index .query .QueryBuilders .matchQuery ;
7278import static org .hamcrest .Matchers .containsString ;
79+ import static org .hamcrest .Matchers .equalTo ;
7380import static org .hamcrest .Matchers .greaterThan ;
7481
7582/**
@@ -105,7 +112,7 @@ public void testSearch() throws IOException {
105112 Arrays .asList ("kimchy" , "tanguy" ), "innerObject" , Collections .singletonMap ("key" , "value" )));
106113 request .setRefreshPolicy (WriteRequest .RefreshPolicy .IMMEDIATE );
107114 BulkResponse bulkResponse = client .bulk (request );
108- assertSame (bulkResponse .status (), RestStatus . OK );
115+ assertSame (RestStatus . OK , bulkResponse .status ());
109116 assertFalse (bulkResponse .hasFailures ());
110117 }
111118 {
@@ -244,7 +251,7 @@ public void testSearchRequestAggregations() throws IOException {
244251 .source (XContentType .JSON , "company" , "Elastic" , "age" , 40 ));
245252 request .setRefreshPolicy (WriteRequest .RefreshPolicy .IMMEDIATE );
246253 BulkResponse bulkResponse = client .bulk (request );
247- assertSame (bulkResponse .status (), RestStatus . OK );
254+ assertSame (RestStatus . OK , bulkResponse .status ());
248255 assertFalse (bulkResponse .hasFailures ());
249256 }
250257 {
@@ -317,7 +324,7 @@ public void testSearchRequestSuggestions() throws IOException {
317324 request .add (new IndexRequest ("posts" , "doc" , "4" ).source (XContentType .JSON , "user" , "cbuescher" ));
318325 request .setRefreshPolicy (WriteRequest .RefreshPolicy .IMMEDIATE );
319326 BulkResponse bulkResponse = client .bulk (request );
320- assertSame (bulkResponse .status (), RestStatus . OK );
327+ assertSame (RestStatus . OK , bulkResponse .status ());
321328 assertFalse (bulkResponse .hasFailures ());
322329 }
323330 {
@@ -364,7 +371,7 @@ public void testSearchRequestHighlighting() throws IOException {
364371 Arrays .asList ("kimchy" , "tanguy" ), "innerObject" , Collections .singletonMap ("key" , "value" )));
365372 request .setRefreshPolicy (WriteRequest .RefreshPolicy .IMMEDIATE );
366373 BulkResponse bulkResponse = client .bulk (request );
367- assertSame (bulkResponse .status (), RestStatus . OK );
374+ assertSame (RestStatus . OK , bulkResponse .status ());
368375 assertFalse (bulkResponse .hasFailures ());
369376 }
370377 {
@@ -412,6 +419,74 @@ public void testSearchRequestHighlighting() throws IOException {
412419 }
413420 }
414421
422+ public void testSearchRequestProfiling () throws IOException {
423+ RestHighLevelClient client = highLevelClient ();
424+ {
425+ IndexRequest request = new IndexRequest ("posts" , "doc" , "1" )
426+ .source (XContentType .JSON , "tags" , "elasticsearch" , "comments" , 123 );
427+ request .setRefreshPolicy (WriteRequest .RefreshPolicy .WAIT_UNTIL );
428+ IndexResponse indexResponse = client .index (request );
429+ assertSame (RestStatus .CREATED , indexResponse .status ());
430+ }
431+ {
432+ SearchRequest searchRequest = new SearchRequest ();
433+ // tag::search-request-profiling
434+ SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder ();
435+ searchSourceBuilder .profile (true );
436+ // end::search-request-profiling
437+ searchSourceBuilder .query (QueryBuilders .termQuery ("tags" , "elasticsearch" ));
438+ searchSourceBuilder .aggregation (AggregationBuilders .histogram ("by_comments" ).field ("comments" ).interval (100 ));
439+ searchRequest .source (searchSourceBuilder );
440+
441+ SearchResponse searchResponse = client .search (searchRequest );
442+ // tag::search-request-profiling-get
443+ Map <String , ProfileShardResult > profilingResults = searchResponse .getProfileResults (); // <1>
444+ for (Map .Entry <String , ProfileShardResult > profilingResult : profilingResults .entrySet ()) { // <2>
445+ String key = profilingResult .getKey (); // <3>
446+ ProfileShardResult profileShardResult = profilingResult .getValue (); // <4>
447+ }
448+ // end::search-request-profiling-get
449+
450+ ProfileShardResult profileShardResult = profilingResults .values ().iterator ().next ();
451+ assertNotNull (profileShardResult );
452+
453+ // tag::search-request-profiling-queries
454+ List <QueryProfileShardResult > queryProfileShardResults = profileShardResult .getQueryProfileResults (); // <1>
455+ for (QueryProfileShardResult queryProfileResult : queryProfileShardResults ) { // <2>
456+
457+ }
458+ // end::search-request-profiling-queries
459+ assertThat (queryProfileShardResults .size (), equalTo (1 ));
460+
461+ for (QueryProfileShardResult queryProfileResult : queryProfileShardResults ) {
462+ // tag::search-request-profiling-queries-results
463+ for (ProfileResult profileResult : queryProfileResult .getQueryResults ()) { // <1>
464+ String queryName = profileResult .getQueryName (); // <2>
465+ long queryTimeInMillis = profileResult .getTime (); // <3>
466+ List <ProfileResult > profiledChildren = profileResult .getProfiledChildren (); // <4>
467+ }
468+ // end::search-request-profiling-queries-results
469+
470+ // tag::search-request-profiling-queries-collectors
471+ CollectorResult collectorResult = queryProfileResult .getCollectorResult (); // <1>
472+ String collectorName = collectorResult .getName (); // <2>
473+ Long collectorTimeInMillis = collectorResult .getTime (); // <3>
474+ List <CollectorResult > profiledChildren = collectorResult .getProfiledChildren (); // <4>
475+ // end::search-request-profiling-queries-collectors
476+ }
477+
478+ // tag::search-request-profiling-aggs
479+ AggregationProfileShardResult aggsProfileResults = profileShardResult .getAggregationProfileResults (); // <1>
480+ for (ProfileResult profileResult : aggsProfileResults .getProfileResults ()) { // <2>
481+ String aggName = profileResult .getQueryName (); // <3>
482+ long aggTimeInMillis = profileResult .getTime (); // <4>
483+ List <ProfileResult > profiledChildren = profileResult .getProfiledChildren (); // <5>
484+ }
485+ // end::search-request-profiling-aggs
486+ assertThat (aggsProfileResults .getProfileResults ().size (), equalTo (1 ));
487+ }
488+ }
489+
415490 public void testScroll () throws IOException {
416491 RestHighLevelClient client = highLevelClient ();
417492 {
@@ -424,7 +499,7 @@ public void testScroll() throws IOException {
424499 .source (XContentType .JSON , "title" , "The Future of Federated Search in Elasticsearch" ));
425500 request .setRefreshPolicy (WriteRequest .RefreshPolicy .IMMEDIATE );
426501 BulkResponse bulkResponse = client .bulk (request );
427- assertSame (bulkResponse .status (), RestStatus . OK );
502+ assertSame (RestStatus . OK , bulkResponse .status ());
428503 assertFalse (bulkResponse .hasFailures ());
429504 }
430505 {
0 commit comments