4343import org .elasticsearch .client .indexlifecycle .LifecyclePolicyMetadata ;
4444import org .elasticsearch .client .indexlifecycle .Phase ;
4545import org .elasticsearch .client .indexlifecycle .PutLifecyclePolicyRequest ;
46+ import org .elasticsearch .client .indexlifecycle .RemoveIndexLifecyclePolicyRequest ;
47+ import org .elasticsearch .client .indexlifecycle .RemoveIndexLifecyclePolicyResponse ;
4648import org .elasticsearch .client .indexlifecycle .RetryLifecyclePolicyRequest ;
4749import org .elasticsearch .client .indexlifecycle .RolloverAction ;
4850import org .elasticsearch .client .indexlifecycle .StartILMRequest ;
4951import org .elasticsearch .client .indexlifecycle .StopILMRequest ;
5052import org .elasticsearch .client .indexlifecycle .ShrinkAction ;
5153import org .elasticsearch .cluster .metadata .IndexMetaData ;
54+ import org .elasticsearch .common .Strings ;
5255import org .elasticsearch .common .collect .ImmutableOpenMap ;
5356import org .elasticsearch .common .settings .Settings ;
5457import org .elasticsearch .common .unit .ByteSizeUnit ;
5962import org .hamcrest .Matchers ;
6063
6164import java .io .IOException ;
65+ import java .util .ArrayList ;
6266import java .util .Collections ;
6367import java .util .HashMap ;
68+ import java .util .List ;
6469import java .util .Map ;
6570import java .util .concurrent .CountDownLatch ;
6671import java .util .concurrent .TimeUnit ;
@@ -163,19 +168,19 @@ public void testDeletePolicy() throws IOException, InterruptedException {
163168 assertTrue (putResponse .isAcknowledged ());
164169 }
165170
166- // tag::ilm-delete -lifecycle-policy-request
171+ // tag::ilm-remove -lifecycle-policy-request
167172 DeleteLifecyclePolicyRequest request =
168173 new DeleteLifecyclePolicyRequest ("my_policy" ); // <1>
169- // end::ilm-delete -lifecycle-policy-request
174+ // end::ilm-remove -lifecycle-policy-request
170175
171- // tag::ilm-delete -lifecycle-policy-execute
176+ // tag::ilm-remove -lifecycle-policy-execute
172177 AcknowledgedResponse response = client .indexLifecycle ()
173178 .deleteLifecyclePolicy (request , RequestOptions .DEFAULT );
174- // end::ilm-delete -lifecycle-policy-execute
179+ // end::ilm-remove -lifecycle-policy-execute
175180
176- // tag::ilm-delete -lifecycle-policy-response
181+ // tag::ilm-remove -lifecycle-policy-response
177182 boolean acknowledged = response .isAcknowledged (); // <1>
178- // end::ilm-delete -lifecycle-policy-response
183+ // end::ilm-remove -lifecycle-policy-response
179184
180185 assertTrue (acknowledged );
181186
@@ -186,7 +191,7 @@ public void testDeletePolicy() throws IOException, InterruptedException {
186191 assertTrue (putResponse .isAcknowledged ());
187192 }
188193
189- // tag::ilm-delete -lifecycle-policy-execute-listener
194+ // tag::ilm-remove -lifecycle-policy-execute-listener
190195 ActionListener <AcknowledgedResponse > listener =
191196 new ActionListener <AcknowledgedResponse >() {
192197 @ Override
@@ -199,16 +204,16 @@ public void onFailure(Exception e) {
199204 // <2>
200205 }
201206 };
202- // end::ilm-delete -lifecycle-policy-execute-listener
207+ // end::ilm-remove -lifecycle-policy-execute-listener
203208
204209 // Replace the empty listener by a blocking listener in test
205210 final CountDownLatch latch = new CountDownLatch (1 );
206211 listener = new LatchedActionListener <>(listener , latch );
207212
208- // tag::ilm-delete -lifecycle-policy-execute-async
213+ // tag::ilm-remove -lifecycle-policy-execute-async
209214 client .indexLifecycle ().deleteLifecyclePolicyAsync (request ,
210215 RequestOptions .DEFAULT , listener ); // <1>
211- // end::ilm-delete -lifecycle-policy-execute-async
216+ // end::ilm-remove -lifecycle-policy-execute-async
212217
213218 assertTrue (latch .await (30L , TimeUnit .SECONDS ));
214219 }
@@ -643,6 +648,91 @@ public void onFailure(Exception e) {
643648 assertTrue (latch .await (30L , TimeUnit .SECONDS ));
644649 }
645650
651+ public void testDeletePolicyFromIndex () throws Exception {
652+ RestHighLevelClient client = highLevelClient ();
653+
654+ // setup policy for index
655+ Map <String , Phase > phases = new HashMap <>();
656+ phases .put ("delete" , new Phase ("delete" , TimeValue .timeValueHours (10L ),
657+ Collections .singletonMap (DeleteAction .NAME , new DeleteAction ())));
658+ LifecyclePolicy policy = new LifecyclePolicy ("my_policy" , phases );
659+ PutLifecyclePolicyRequest putRequest = new PutLifecyclePolicyRequest (policy );
660+ client .indexLifecycle ().putLifecyclePolicy (putRequest , RequestOptions .DEFAULT );
661+ CreateIndexRequest createIndexRequest = new CreateIndexRequest ("my_index" ,
662+ Settings .builder ()
663+ .put (IndexMetaData .SETTING_NUMBER_OF_SHARDS , 1 )
664+ .put ("index.lifecycle.name" , "my_policy" )
665+ .build ());
666+ client .indices ().create (createIndexRequest , RequestOptions .DEFAULT );
667+ assertBusy (() -> assertTrue (client .indexLifecycle ()
668+ .explainLifecycle (new ExplainLifecycleRequest ("my_index" ), RequestOptions .DEFAULT )
669+ .getIndexResponses ().get ("my_index" ).managedByILM ()));
670+
671+ // tag::ilm-remove-lifecycle-policy-from-index-request
672+ List <String > indices = new ArrayList <>();
673+ indices .add ("my_index" );
674+ RemoveIndexLifecyclePolicyRequest request =
675+ new RemoveIndexLifecyclePolicyRequest (indices ); // <1>
676+ // end::ilm-remove-lifecycle-policy-from-index-request
677+
678+
679+ // tag::ilm-remove-lifecycle-policy-from-index-execute
680+ RemoveIndexLifecyclePolicyResponse response = client
681+ .indexLifecycle ()
682+ .removeIndexLifecyclePolicy (request , RequestOptions .DEFAULT );
683+ // end::ilm-remove-lifecycle-policy-from-index-execute
684+
685+ // tag::ilm-remove-lifecycle-policy-from-index-response
686+ boolean hasFailures = response .hasFailures (); // <1>
687+ List <String > failedIndexes = response .getFailedIndexes (); // <2>
688+ // end::ilm-remove-lifecycle-policy-from-index-response
689+
690+ {
691+ assertFalse (hasFailures );
692+ Map <String , Object > indexSettings = getIndexSettings ("my_index" );
693+ assertTrue (Strings .isNullOrEmpty ((String ) indexSettings .get ("index.lifecycle.name" )));
694+ }
695+
696+ // re-apply policy on index
697+ updateIndexSettings ("my_index" , Settings .builder ().put ("index.lifecycle.name" , "my_policy" ));
698+ assertBusy (() -> assertTrue (client .indexLifecycle ()
699+ .explainLifecycle (new ExplainLifecycleRequest ("my_index" ), RequestOptions .DEFAULT )
700+ .getIndexResponses ().get ("my_index" ).managedByILM ()));
701+
702+ // tag::ilm-remove-lifecycle-policy-from-index-execute-listener
703+ ActionListener <RemoveIndexLifecyclePolicyResponse > listener =
704+ new ActionListener <RemoveIndexLifecyclePolicyResponse >() {
705+ @ Override
706+ public void onResponse (
707+ RemoveIndexLifecyclePolicyResponse response ) {
708+ boolean hasFailures = response .hasFailures (); // <1>
709+ List <String > failedIndexes = response .getFailedIndexes ();
710+ }
711+
712+ @ Override
713+ public void onFailure (Exception e ) {
714+ // <2>
715+ }
716+ };
717+ // end::ilm-remove-lifecycle-policy-from-index-execute-listener
718+
719+ {
720+ Map <String , Object > indexSettings = getIndexSettings ("my_index" );
721+ assertTrue (Strings .isNullOrEmpty ((String ) indexSettings .get ("index.lifecycle.name" )));
722+ }
723+
724+ // Replace the empty listener by a blocking listener in test
725+ final CountDownLatch latch = new CountDownLatch (1 );
726+ listener = new LatchedActionListener <>(listener , latch );
727+
728+ // tag::ilm-remove-lifecycle-policy-from-index-execute-async
729+ client .indexLifecycle ().removeIndexLifecyclePolicyAsync (request ,
730+ RequestOptions .DEFAULT , listener ); // <1>
731+ // end::ilm-remove-lifecycle-policy-from-index-execute-async
732+
733+ assertTrue (latch .await (30L , TimeUnit .SECONDS ));
734+ }
735+
646736 static Map <String , Object > toMap (Response response ) throws IOException {
647737 return XContentHelper .convertToMap (JsonXContent .jsonXContent , EntityUtils .toString (response .getEntity ()), false );
648738 }
0 commit comments