Skip to content

Commit 41148e4

Browse files
authored
Docs: Update HighLevelRestClient migration docs (#30544)
The High Level REST Client's documentation suggested that users should use the Low Level REST Client for index management activities. This change removes that suggestion because the high level REST client supports those APIs now. This also changes the examples in the migration docs to that still use the Low Level REST Client to use the non-deprecated varieats of `performRequest`.
1 parent b8bf480 commit 41148e4

File tree

2 files changed

+16
-119
lines changed

2 files changed

+16
-119
lines changed

client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/MigrationDocumentationIT.java

+9-44
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.elasticsearch.action.index.IndexRequest;
3131
import org.elasticsearch.action.index.IndexResponse;
3232
import org.elasticsearch.client.ESRestHighLevelClientTestCase;
33+
import org.elasticsearch.client.Request;
3334
import org.elasticsearch.client.Response;
3435
import org.elasticsearch.client.RestHighLevelClient;
3536
import org.elasticsearch.cluster.health.ClusterHealthStatus;
@@ -66,58 +67,22 @@
6667
* --------------------------------------------------
6768
*/
6869
public class MigrationDocumentationIT extends ESRestHighLevelClientTestCase {
69-
70-
public void testCreateIndex() throws IOException {
71-
RestHighLevelClient client = highLevelClient();
72-
{
73-
//tag::migration-create-index
74-
Settings indexSettings = Settings.builder() // <1>
75-
.put(SETTING_NUMBER_OF_SHARDS, 1)
76-
.put(SETTING_NUMBER_OF_REPLICAS, 0)
77-
.build();
78-
79-
String payload = Strings.toString(XContentFactory.jsonBuilder() // <2>
80-
.startObject()
81-
.startObject("settings") // <3>
82-
.value(indexSettings)
83-
.endObject()
84-
.startObject("mappings") // <4>
85-
.startObject("doc")
86-
.startObject("properties")
87-
.startObject("time")
88-
.field("type", "date")
89-
.endObject()
90-
.endObject()
91-
.endObject()
92-
.endObject()
93-
.endObject());
94-
95-
HttpEntity entity = new NStringEntity(payload, ContentType.APPLICATION_JSON); // <5>
96-
97-
Response response = client.getLowLevelClient().performRequest("PUT", "my-index", emptyMap(), entity); // <6>
98-
if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
99-
// <7>
100-
}
101-
//end::migration-create-index
102-
assertEquals(200, response.getStatusLine().getStatusCode());
103-
}
104-
}
105-
10670
public void testClusterHealth() throws IOException {
10771
RestHighLevelClient client = highLevelClient();
10872
{
10973
//tag::migration-cluster-health
110-
Map<String, String> parameters = singletonMap("wait_for_status", "green");
111-
Response response = client.getLowLevelClient().performRequest("GET", "/_cluster/health", parameters); // <1>
74+
Request request = new Request("GET", "/_cluster/health");
75+
request.addParameter("wait_for_status", "green"); // <1>
76+
Response response = client.getLowLevelClient().performRequest(request); // <2>
11277

11378
ClusterHealthStatus healthStatus;
114-
try (InputStream is = response.getEntity().getContent()) { // <2>
115-
Map<String, Object> map = XContentHelper.convertToMap(XContentType.JSON.xContent(), is, true); // <3>
116-
healthStatus = ClusterHealthStatus.fromString((String) map.get("status")); // <4>
79+
try (InputStream is = response.getEntity().getContent()) { // <3>
80+
Map<String, Object> map = XContentHelper.convertToMap(XContentType.JSON.xContent(), is, true); // <4>
81+
healthStatus = ClusterHealthStatus.fromString((String) map.get("status")); // <5>
11782
}
11883

119-
if (healthStatus == ClusterHealthStatus.GREEN) {
120-
// <5>
84+
if (healthStatus != ClusterHealthStatus.GREEN) {
85+
// <6>
12186
}
12287
//end::migration-cluster-health
12388
assertSame(ClusterHealthStatus.GREEN, healthStatus);

docs/java-rest/high-level/migration.asciidoc

+7-75
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
== Migration Guide
33

44
This section describes how to migrate existing code from the `TransportClient`
5-
to the new Java High Level REST Client released with the version 5.6.0
5+
to the Java High Level REST Client released with the version 5.6.0
66
of Elasticsearch.
77

88
=== Motivations around a new Java client
@@ -107,9 +107,6 @@ More importantly, the high-level client:
107107
request constructors like `new IndexRequest()` to create requests
108108
objects. The requests are then executed using synchronous or
109109
asynchronous dedicated methods like `client.index()` or `client.indexAsync()`.
110-
- does not provide indices or cluster management APIs. Management
111-
operations can be executed by external scripts or
112-
<<java-rest-high-level-migration-manage-indices, using the low-level client>>.
113110

114111
==== How to migrate the way requests are built
115112

@@ -241,71 +238,6 @@ returned by the cluster.
241238
<4> The `onFailure()` method is called when an error occurs
242239
during the execution of the request.
243240

244-
[[java-rest-high-level-migration-manage-indices]]
245-
==== Manage Indices using the Low-Level REST Client
246-
247-
The low-level client is able to execute any kind of HTTP requests, and can
248-
therefore be used to call the APIs that are not yet supported by the high level client.
249-
250-
For example, creating a new index with the `TransportClient` may look like this:
251-
252-
[source,java]
253-
--------------------------------------------------
254-
Settings settings = Settings.builder() // <1>
255-
.put(SETTING_NUMBER_OF_SHARDS, 1)
256-
.put(SETTING_NUMBER_OF_REPLICAS, 0)
257-
.build();
258-
259-
String mappings = XContentFactory.jsonBuilder() // <2>
260-
.startObject()
261-
.startObject("doc")
262-
.startObject("properties")
263-
.startObject("time")
264-
.field("type", "date")
265-
.endObject()
266-
.endObject()
267-
.endObject()
268-
.endObject()
269-
.string();
270-
271-
CreateIndexResponse response = transportClient.admin().indices() // <3>
272-
.prepareCreate("my-index")
273-
.setSettings(indexSettings)
274-
.addMapping("doc", docMapping, XContentType.JSON)
275-
.get();
276-
277-
if (response.isAcknowledged() == false) {
278-
// <4>
279-
}
280-
--------------------------------------------------
281-
<1> Define the settings of the index
282-
<2> Define the mapping for document of type `doc` using a
283-
`XContentBuilder`
284-
<3> Create the index with the previous settings and mapping
285-
using the `prepareCreate()` method. The execution is synchronous
286-
and blocks on the `get()` method until the remote cluster returns
287-
a response.
288-
<4> Handle the situation where the index has not been created
289-
290-
The same operation executed with the low-level client could be:
291-
292-
["source","java",subs="attributes,callouts,macros"]
293-
--------------------------------------------------
294-
include-tagged::{doc-tests}/MigrationDocumentationIT.java[migration-create-index]
295-
--------------------------------------------------
296-
<1> Define the settings of the index
297-
<2> Define the body of the HTTP request using a `XContentBuilder` with JSON format
298-
<3> Include the settings in the request body
299-
<4> Include the mappings in the request body
300-
<5> Convert the request body from `String` to a `HttpEntity` and
301-
set its content type (here, JSON)
302-
<6> Execute the request using the low-level client. The execution is synchronous
303-
and blocks on the `performRequest()` method until the remote cluster returns
304-
a response. The low-level client can be retrieved from an existing `RestHighLevelClient`
305-
instance through the `getLowLevelClient` getter method.
306-
<7> Handle the situation where the index has not been created
307-
308-
309241
[[java-rest-high-level-migration-cluster-health]]
310242
==== Checking Cluster Health using the Low-Level REST Client
311243

@@ -331,18 +263,18 @@ With the low-level client, the code can be changed to:
331263
--------------------------------------------------
332264
include-tagged::{doc-tests}/MigrationDocumentationIT.java[migration-cluster-health]
333265
--------------------------------------------------
334-
<1> Call the cluster's health REST endpoint and wait for the cluster health to become green,
335-
then get back a `Response` object.
336-
<2> Retrieve an `InputStream` object in order to read the response's content
337-
<3> Parse the response's content using Elasticsearch's helper class `XContentHelper`. This
266+
<1> Set up the request to wait for the cluster's health to become green if it isn't already.
267+
<2> Make the request and the get back a `Response` object.
268+
<3> Retrieve an `InputStream` object in order to read the response's content
269+
<4> Parse the response's content using Elasticsearch's helper class `XContentHelper`. This
338270
helper requires the content type of the response to be passed as an argument and returns
339271
a `Map` of objects. Values in the map can be of any type, including inner `Map` that are
340272
used to represent the JSON object hierarchy.
341-
<4> Retrieve the value of the `status` field in the response map, casts it as a a `String`
273+
<5> Retrieve the value of the `status` field in the response map, casts it as a a `String`
342274
object and use the `ClusterHealthStatus.fromString()` method to convert it as a `ClusterHealthStatus`
343275
object. This method throws an exception if the value does not corresponds to a valid cluster
344276
health status.
345-
<5> Handle the situation where the cluster's health is not green
277+
<6> Handle the situation where the cluster's health is not green
346278

347279
Note that for convenience this example uses Elasticsearch's helpers to parse the JSON response
348280
body, but any other JSON parser could have been use instead.

0 commit comments

Comments
 (0)