@@ -350,6 +350,71 @@ public void testClusterState() throws Exception {
350350
351351 }
352352
353+ public void testShrink () throws IOException {
354+ String shrunkenIndex = index + "_shrunk" ;
355+ int numDocs ;
356+ if (runningAgainstOldCluster ) {
357+ XContentBuilder mappingsAndSettings = jsonBuilder ();
358+ mappingsAndSettings .startObject ();
359+ {
360+ mappingsAndSettings .startObject ("mappings" );
361+ mappingsAndSettings .startObject ("doc" );
362+ mappingsAndSettings .startObject ("properties" );
363+ {
364+ mappingsAndSettings .startObject ("field" );
365+ mappingsAndSettings .field ("type" , "text" );
366+ mappingsAndSettings .endObject ();
367+ }
368+ mappingsAndSettings .endObject ();
369+ mappingsAndSettings .endObject ();
370+ mappingsAndSettings .endObject ();
371+ }
372+ mappingsAndSettings .endObject ();
373+ client ().performRequest ("PUT" , "/" + index , Collections .emptyMap (),
374+ new StringEntity (mappingsAndSettings .string (), ContentType .APPLICATION_JSON ));
375+
376+ numDocs = randomIntBetween (512 , 1024 );
377+ indexRandomDocuments (numDocs , true , true , i -> {
378+ return JsonXContent .contentBuilder ().startObject ()
379+ .field ("field" , "value" )
380+ .endObject ();
381+ });
382+
383+ String updateSettingsRequestBody = "{\" settings\" : {\" index.blocks.write\" : true}}" ;
384+ Response rsp = client ().performRequest ("PUT" , "/" + index + "/_settings" , Collections .emptyMap (),
385+ new StringEntity (updateSettingsRequestBody , ContentType .APPLICATION_JSON ));
386+ assertEquals (200 , rsp .getStatusLine ().getStatusCode ());
387+
388+ String shrinkIndexRequestBody = "{\" settings\" : {\" index.number_of_shards\" : 1}}" ;
389+ rsp = client ().performRequest ("PUT" , "/" + index + "/_shrink/" + shrunkenIndex , Collections .emptyMap (),
390+ new StringEntity (shrinkIndexRequestBody , ContentType .APPLICATION_JSON ));
391+ assertEquals (200 , rsp .getStatusLine ().getStatusCode ());
392+
393+ rsp = client ().performRequest ("POST" , "/_refresh" );
394+ assertEquals (200 , rsp .getStatusLine ().getStatusCode ());
395+ } else {
396+ numDocs = countOfIndexedRandomDocuments ();
397+ }
398+
399+ Map <?, ?> response = toMap (client ().performRequest ("GET" , "/" + index + "/_search" ));
400+ assertNoFailures (response );
401+ int totalShards = (int ) XContentMapValues .extractValue ("_shards.total" , response );
402+ assertThat (totalShards , greaterThan (1 ));
403+ int successfulShards = (int ) XContentMapValues .extractValue ("_shards.successful" , response );
404+ assertEquals (totalShards , successfulShards );
405+ int totalHits = (int ) XContentMapValues .extractValue ("hits.total" , response );
406+ assertEquals (numDocs , totalHits );
407+
408+ response = toMap (client ().performRequest ("GET" , "/" + shrunkenIndex + "/_search" ));
409+ assertNoFailures (response );
410+ totalShards = (int ) XContentMapValues .extractValue ("_shards.total" , response );
411+ assertEquals (1 , totalShards );
412+ successfulShards = (int ) XContentMapValues .extractValue ("_shards.successful" , response );
413+ assertEquals (1 , successfulShards );
414+ totalHits = (int ) XContentMapValues .extractValue ("hits.total" , response );
415+ assertEquals (numDocs , totalHits );
416+ }
417+
353418 void assertBasicSearchWorks (int count ) throws IOException {
354419 logger .info ("--> testing basic search" );
355420 Map <String , Object > response = toMap (client ().performRequest ("GET" , "/" + index + "/_search" ));
0 commit comments