2
2
== Migration Guide
3
3
4
4
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
6
6
of Elasticsearch.
7
7
8
8
=== Motivations around a new Java client
@@ -107,9 +107,6 @@ More importantly, the high-level client:
107
107
request constructors like `new IndexRequest()` to create requests
108
108
objects. The requests are then executed using synchronous or
109
109
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>>.
113
110
114
111
==== How to migrate the way requests are built
115
112
@@ -241,71 +238,6 @@ returned by the cluster.
241
238
<4> The `onFailure()` method is called when an error occurs
242
239
during the execution of the request.
243
240
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
-
309
241
[[java-rest-high-level-migration-cluster-health]]
310
242
==== Checking Cluster Health using the Low-Level REST Client
311
243
@@ -331,18 +263,18 @@ With the low-level client, the code can be changed to:
331
263
--------------------------------------------------
332
264
include-tagged::{doc-tests}/MigrationDocumentationIT.java[migration-cluster-health]
333
265
--------------------------------------------------
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
338
270
helper requires the content type of the response to be passed as an argument and returns
339
271
a `Map` of objects. Values in the map can be of any type, including inner `Map` that are
340
272
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`
342
274
object and use the `ClusterHealthStatus.fromString()` method to convert it as a `ClusterHealthStatus`
343
275
object. This method throws an exception if the value does not corresponds to a valid cluster
344
276
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
346
278
347
279
Note that for convenience this example uses Elasticsearch's helpers to parse the JSON response
348
280
body, but any other JSON parser could have been use instead.
0 commit comments