Skip to content

Commit da117fa

Browse files
committed
Adds HLRC docs for put lifecycle policy (#35457)
* Adds HLRC docs for put lifecycle policy * Adds link to docs in client javadocs * Fixes checkstyle * Make the documentation use the right ack response
1 parent 468ecb7 commit da117fa

File tree

4 files changed

+178
-2
lines changed

4 files changed

+178
-2
lines changed

client/rest-high-level/src/main/java/org/elasticsearch/client/IndexLifecycleClient.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ public void getLifecyclePolicyAsync(GetLifecyclePolicyRequest request, RequestOp
7676
}
7777

7878
/**
79-
* Create or modify a lifecycle definition
80-
* See <a href="https://fix-me-when-we-have-docs.com">
79+
* Create or modify a lifecycle definition See <a href=
80+
* "https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-high-ilm-ilm-put-lifecycle-policy.html">
8181
* the docs</a> for more.
8282
* @param request the request
8383
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.elasticsearch.client.documentation;
21+
22+
import org.apache.http.util.EntityUtils;
23+
import org.elasticsearch.action.ActionListener;
24+
import org.elasticsearch.action.LatchedActionListener;
25+
import org.elasticsearch.client.ESRestHighLevelClientTestCase;
26+
import org.elasticsearch.client.RequestOptions;
27+
import org.elasticsearch.client.Response;
28+
import org.elasticsearch.client.RestHighLevelClient;
29+
import org.elasticsearch.client.core.AcknowledgedResponse;
30+
import org.elasticsearch.client.indexlifecycle.DeleteAction;
31+
import org.elasticsearch.client.indexlifecycle.DeleteLifecyclePolicyRequest;
32+
import org.elasticsearch.client.indexlifecycle.LifecycleAction;
33+
import org.elasticsearch.client.indexlifecycle.LifecyclePolicy;
34+
import org.elasticsearch.client.indexlifecycle.Phase;
35+
import org.elasticsearch.client.indexlifecycle.PutLifecyclePolicyRequest;
36+
import org.elasticsearch.client.indexlifecycle.RolloverAction;
37+
import org.elasticsearch.common.unit.ByteSizeUnit;
38+
import org.elasticsearch.common.unit.ByteSizeValue;
39+
import org.elasticsearch.common.unit.TimeValue;
40+
import org.elasticsearch.common.xcontent.XContentHelper;
41+
import org.elasticsearch.common.xcontent.json.JsonXContent;
42+
43+
import java.io.IOException;
44+
import java.util.Collections;
45+
import java.util.HashMap;
46+
import java.util.Map;
47+
import java.util.concurrent.CountDownLatch;
48+
import java.util.concurrent.TimeUnit;
49+
50+
public class ILMDocumentationIT extends ESRestHighLevelClientTestCase {
51+
52+
public void testPutLifecyclePolicy() throws Exception {
53+
RestHighLevelClient client = highLevelClient();
54+
55+
// tag::ilm-put-lifecycle-policy-request
56+
Map<String, Phase> phases = new HashMap<>();
57+
Map<String, LifecycleAction> hotActions = new HashMap<>();
58+
hotActions.put(RolloverAction.NAME, new RolloverAction(
59+
new ByteSizeValue(50, ByteSizeUnit.GB), null, null));
60+
phases.put("hot", new Phase("hot", TimeValue.ZERO, hotActions)); // <1>
61+
62+
Map<String, LifecycleAction> deleteActions =
63+
Collections.singletonMap(DeleteAction.NAME, new DeleteAction());
64+
phases.put("delete", new Phase("delete",
65+
new TimeValue(90, TimeUnit.DAYS), deleteActions)); // <2>
66+
67+
LifecyclePolicy policy = new LifecyclePolicy("my_policy",
68+
phases); // <3>
69+
PutLifecyclePolicyRequest request =
70+
new PutLifecyclePolicyRequest(policy);
71+
// end::ilm-put-lifecycle-policy-request
72+
73+
// tag::ilm-put-lifecycle-policy-execute
74+
AcknowledgedResponse response = client.indexLifecycle().
75+
putLifecyclePolicy(request, RequestOptions.DEFAULT);
76+
// end::ilm-put-lifecycle-policy-execute
77+
78+
// tag::ilm-put-lifecycle-policy-response
79+
boolean acknowledged = response.isAcknowledged(); // <1>
80+
// end::ilm-put-lifecycle-policy-response
81+
82+
assertTrue(acknowledged);
83+
84+
// Delete the policy so it can be added again
85+
{
86+
DeleteLifecyclePolicyRequest deleteRequest =
87+
new DeleteLifecyclePolicyRequest("my_policy");
88+
AcknowledgedResponse deleteResponse = client.indexLifecycle()
89+
.deleteLifecyclePolicy(deleteRequest,
90+
RequestOptions.DEFAULT);
91+
assertTrue(deleteResponse.isAcknowledged());
92+
}
93+
94+
// tag::ilm-put-lifecycle-policy-execute-listener
95+
ActionListener<AcknowledgedResponse> listener =
96+
new ActionListener<AcknowledgedResponse>() {
97+
@Override
98+
public void onResponse(AcknowledgedResponse response) {
99+
boolean acknowledged = response.isAcknowledged(); // <1>
100+
}
101+
102+
@Override
103+
public void onFailure(Exception e) {
104+
// <2>
105+
}
106+
};
107+
// end::ilm-put-lifecycle-policy-execute-listener
108+
109+
// Replace the empty listener by a blocking listener in test
110+
final CountDownLatch latch = new CountDownLatch(1);
111+
listener = new LatchedActionListener<>(listener, latch);
112+
113+
// tag::ilm-put-lifecycle-policy-execute-async
114+
client.indexLifecycle().putLifecyclePolicyAsync(request,
115+
RequestOptions.DEFAULT, listener); // <1>
116+
// end::ilm-put-lifecycle-policy-execute-async
117+
118+
assertTrue(latch.await(30L, TimeUnit.SECONDS));
119+
120+
}
121+
122+
static Map<String, Object> toMap(Response response) throws IOException {
123+
return XContentHelper.convertToMap(JsonXContent.jsonXContent, EntityUtils.toString(response.getEntity()), false);
124+
}
125+
126+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
--
2+
:api: ilm-put-lifecycle-policy
3+
:request: PutLifecyclePolicyRequest
4+
:response: AcknowledgedResponse
5+
--
6+
7+
[id="{upid}-{api}"]
8+
=== Put Lifecycle Policy API
9+
10+
11+
[id="{upid}-{api}-request"]
12+
==== Request
13+
14+
The Put Lifecycle Policy API allows you to add an Index Lifecycle Management
15+
Policy to the cluster.
16+
17+
["source","java",subs="attributes,callouts,macros"]
18+
--------------------------------------------------
19+
include-tagged::{doc-tests-file}[{api}-request]
20+
--------------------------------------------------
21+
<1> Adds a hot phase with a rollover action
22+
<2> Adds a delete phase that will delete in the index 90 days after rollover
23+
<3> Creates the policy with the defined phases and the name `my_policy`
24+
25+
[id="{upid}-{api}-response"]
26+
==== Response
27+
28+
The returned +{response}+ indicates if the put lifecycle policy request was received.
29+
30+
["source","java",subs="attributes,callouts,macros"]
31+
--------------------------------------------------
32+
include-tagged::{doc-tests-file}[{api}-response]
33+
--------------------------------------------------
34+
<1> Whether or not the put lifecycle policy was acknowledge.
35+
36+
include::../execution.asciidoc[]
37+
38+

docs/java-rest/high-level/supported-apis.asciidoc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,3 +413,15 @@ The Java High Level REST Client supports the following CCR APIs:
413413

414414
include::ccr/put_follow.asciidoc[]
415415
include::ccr/pause_follow.asciidoc[]
416+
417+
== Index Lifecycle Management APIs
418+
419+
:upid: {mainid}-ilm
420+
:doc-tests-file: {doc-tests}/ILMDocumentationIT.java
421+
422+
The Java High Level REST Client supports the following Index Lifecycle
423+
Management APIs:
424+
425+
* <<{upid}-ilm-put-lifecycle-policy>>
426+
427+
include::ilm/put_lifecycle_policy.asciidoc[]

0 commit comments

Comments
 (0)