4141import org .elasticsearch .client .indexlifecycle .LifecyclePolicyMetadata ;
4242import org .elasticsearch .client .indexlifecycle .Phase ;
4343import org .elasticsearch .client .indexlifecycle .PutLifecyclePolicyRequest ;
44+ import org .elasticsearch .client .indexlifecycle .RemoveIndexLifecyclePolicyRequest ;
45+ import org .elasticsearch .client .indexlifecycle .RemoveIndexLifecyclePolicyResponse ;
4446import org .elasticsearch .client .indexlifecycle .RetryLifecyclePolicyRequest ;
4547import org .elasticsearch .client .indexlifecycle .RolloverAction ;
4648import org .elasticsearch .client .indexlifecycle .StartILMRequest ;
4749import org .elasticsearch .client .indexlifecycle .StopILMRequest ;
4850import org .elasticsearch .client .indexlifecycle .ShrinkAction ;
4951import org .elasticsearch .cluster .metadata .IndexMetaData ;
52+ import org .elasticsearch .common .Strings ;
5053import org .elasticsearch .common .collect .ImmutableOpenMap ;
5154import org .elasticsearch .common .settings .Settings ;
5255import org .elasticsearch .common .unit .ByteSizeUnit ;
5760import org .hamcrest .Matchers ;
5861
5962import java .io .IOException ;
63+ import java .util .ArrayList ;
6064import java .util .Collections ;
6165import java .util .HashMap ;
66+ import java .util .List ;
6267import java .util .Map ;
6368import java .util .concurrent .CountDownLatch ;
6469import java .util .concurrent .TimeUnit ;
@@ -161,19 +166,19 @@ public void testDeletePolicy() throws IOException, InterruptedException {
161166 assertTrue (putResponse .isAcknowledged ());
162167 }
163168
164- // tag::ilm-delete -lifecycle-policy-request
169+ // tag::ilm-remove -lifecycle-policy-request
165170 DeleteLifecyclePolicyRequest request =
166171 new DeleteLifecyclePolicyRequest ("my_policy" ); // <1>
167- // end::ilm-delete -lifecycle-policy-request
172+ // end::ilm-remove -lifecycle-policy-request
168173
169- // tag::ilm-delete -lifecycle-policy-execute
174+ // tag::ilm-remove -lifecycle-policy-execute
170175 AcknowledgedResponse response = client .indexLifecycle ()
171176 .deleteLifecyclePolicy (request , RequestOptions .DEFAULT );
172- // end::ilm-delete -lifecycle-policy-execute
177+ // end::ilm-remove -lifecycle-policy-execute
173178
174- // tag::ilm-delete -lifecycle-policy-response
179+ // tag::ilm-remove -lifecycle-policy-response
175180 boolean acknowledged = response .isAcknowledged (); // <1>
176- // end::ilm-delete -lifecycle-policy-response
181+ // end::ilm-remove -lifecycle-policy-response
177182
178183 assertTrue (acknowledged );
179184
@@ -184,7 +189,7 @@ public void testDeletePolicy() throws IOException, InterruptedException {
184189 assertTrue (putResponse .isAcknowledged ());
185190 }
186191
187- // tag::ilm-delete -lifecycle-policy-execute-listener
192+ // tag::ilm-remove -lifecycle-policy-execute-listener
188193 ActionListener <AcknowledgedResponse > listener =
189194 new ActionListener <AcknowledgedResponse >() {
190195 @ Override
@@ -197,16 +202,16 @@ public void onFailure(Exception e) {
197202 // <2>
198203 }
199204 };
200- // end::ilm-delete -lifecycle-policy-execute-listener
205+ // end::ilm-remove -lifecycle-policy-execute-listener
201206
202207 // Replace the empty listener by a blocking listener in test
203208 final CountDownLatch latch = new CountDownLatch (1 );
204209 listener = new LatchedActionListener <>(listener , latch );
205210
206- // tag::ilm-delete -lifecycle-policy-execute-async
211+ // tag::ilm-remove -lifecycle-policy-execute-async
207212 client .indexLifecycle ().deleteLifecyclePolicyAsync (request ,
208213 RequestOptions .DEFAULT , listener ); // <1>
209- // end::ilm-delete -lifecycle-policy-execute-async
214+ // end::ilm-remove -lifecycle-policy-execute-async
210215
211216 assertTrue (latch .await (30L , TimeUnit .SECONDS ));
212217 }
@@ -530,6 +535,91 @@ public void onFailure(Exception e) {
530535 assertTrue (latch .await (30L , TimeUnit .SECONDS ));
531536 }
532537
538+ public void testDeletePolicyFromIndex () throws Exception {
539+ RestHighLevelClient client = highLevelClient ();
540+
541+ // setup policy for index
542+ Map <String , Phase > phases = new HashMap <>();
543+ phases .put ("delete" , new Phase ("delete" , TimeValue .timeValueHours (10L ),
544+ Collections .singletonMap (DeleteAction .NAME , new DeleteAction ())));
545+ LifecyclePolicy policy = new LifecyclePolicy ("my_policy" , phases );
546+ PutLifecyclePolicyRequest putRequest = new PutLifecyclePolicyRequest (policy );
547+ client .indexLifecycle ().putLifecyclePolicy (putRequest , RequestOptions .DEFAULT );
548+ CreateIndexRequest createIndexRequest = new CreateIndexRequest ("my_index" ,
549+ Settings .builder ()
550+ .put (IndexMetaData .SETTING_NUMBER_OF_SHARDS , 1 )
551+ .put ("index.lifecycle.name" , "my_policy" )
552+ .build ());
553+ client .indices ().create (createIndexRequest , RequestOptions .DEFAULT );
554+ assertBusy (() -> assertTrue (client .indexLifecycle ()
555+ .explainLifecycle (new ExplainLifecycleRequest ().indices ("my_index" ), RequestOptions .DEFAULT )
556+ .getIndexResponses ().get ("my_index" ).managedByILM ()));
557+
558+ // tag::ilm-remove-lifecycle-policy-from-index-request
559+ List <String > indices = new ArrayList <>();
560+ indices .add ("my_index" );
561+ RemoveIndexLifecyclePolicyRequest request =
562+ new RemoveIndexLifecyclePolicyRequest (indices ); // <1>
563+ // end::ilm-remove-lifecycle-policy-from-index-request
564+
565+
566+ // tag::ilm-remove-lifecycle-policy-from-index-execute
567+ RemoveIndexLifecyclePolicyResponse response = client
568+ .indexLifecycle ()
569+ .removeIndexLifecyclePolicy (request , RequestOptions .DEFAULT );
570+ // end::ilm-remove-lifecycle-policy-from-index-execute
571+
572+ // tag::ilm-remove-lifecycle-policy-from-index-response
573+ boolean hasFailures = response .hasFailures (); // <1>
574+ List <String > failedIndexes = response .getFailedIndexes (); // <2>
575+ // end::ilm-remove-lifecycle-policy-from-index-response
576+
577+ {
578+ assertFalse (hasFailures );
579+ Map <String , Object > indexSettings = getIndexSettings ("my_index" );
580+ assertTrue (Strings .isNullOrEmpty ((String ) indexSettings .get ("index.lifecycle.name" )));
581+ }
582+
583+ // re-apply policy on index
584+ updateIndexSettings ("my_index" , Settings .builder ().put ("index.lifecycle.name" , "my_policy" ));
585+ assertBusy (() -> assertTrue (client .indexLifecycle ()
586+ .explainLifecycle (new ExplainLifecycleRequest ().indices ("my_index" ), RequestOptions .DEFAULT )
587+ .getIndexResponses ().get ("my_index" ).managedByILM ()));
588+
589+ // tag::ilm-remove-lifecycle-policy-from-index-execute-listener
590+ ActionListener <RemoveIndexLifecyclePolicyResponse > listener =
591+ new ActionListener <RemoveIndexLifecyclePolicyResponse >() {
592+ @ Override
593+ public void onResponse (
594+ RemoveIndexLifecyclePolicyResponse response ) {
595+ boolean hasFailures = response .hasFailures (); // <1>
596+ List <String > failedIndexes = response .getFailedIndexes ();
597+ }
598+
599+ @ Override
600+ public void onFailure (Exception e ) {
601+ // <2>
602+ }
603+ };
604+ // end::ilm-remove-lifecycle-policy-from-index-execute-listener
605+
606+ {
607+ Map <String , Object > indexSettings = getIndexSettings ("my_index" );
608+ assertTrue (Strings .isNullOrEmpty ((String ) indexSettings .get ("index.lifecycle.name" )));
609+ }
610+
611+ // Replace the empty listener by a blocking listener in test
612+ final CountDownLatch latch = new CountDownLatch (1 );
613+ listener = new LatchedActionListener <>(listener , latch );
614+
615+ // tag::ilm-remove-lifecycle-policy-from-index-execute-async
616+ client .indexLifecycle ().removeIndexLifecyclePolicyAsync (request ,
617+ RequestOptions .DEFAULT , listener ); // <1>
618+ // end::ilm-remove-lifecycle-policy-from-index-execute-async
619+
620+ assertTrue (latch .await (30L , TimeUnit .SECONDS ));
621+ }
622+
533623 static Map <String , Object > toMap (Response response ) throws IOException {
534624 return XContentHelper .convertToMap (JsonXContent .jsonXContent , EntityUtils .toString (response .getEntity ()), false );
535625 }
0 commit comments