usage.asciidoc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. == Getting started
  2. === Maven Repository
  3. The low-level Java REST client is hosted on
  4. http://search.maven.org/#search%7Cga%7C1%7Cg%3A%22org.elasticsearch.client%22[Maven
  5. Central]. The minimum Java version required is `1.7`.
  6. The low-level REST client is subject to the same release cycle as
  7. elasticsearch. Replace the version with the desired client version, first
  8. released with `5.0.0-alpha4`. There is no relation between the client version
  9. and the elasticsearch version that the client can communicate with. The
  10. low-level REST client is compatible with all elasticsearch versions.
  11. ==== Maven configuration
  12. Here is how you can configure the dependency using maven as a dependency manager.
  13. Add the following to your `pom.xml` file:
  14. ["source","xml",subs="attributes"]
  15. --------------------------------------------------
  16. <dependency>
  17. <groupId>org.elasticsearch.client</groupId>
  18. <artifactId>rest</artifactId>
  19. <version>{version}</version>
  20. </dependency>
  21. --------------------------------------------------
  22. ==== Gradle configuration
  23. Here is how you can configure the dependency using gradle as a dependency manager.
  24. Add the following to your `build.gradle` file:
  25. ["source","groovy",subs="attributes"]
  26. --------------------------------------------------
  27. dependencies {
  28. compile 'org.elasticsearch.client:rest:{version}'
  29. }
  30. --------------------------------------------------
  31. === Dependencies
  32. The low-level Java REST client internally uses the
  33. http://hc.apache.org/httpcomponents-asyncclient-dev/[Apache Http Async Client]
  34. to send http requests. It depends on the following artifacts, namely the async
  35. http client and its own transitive dependencies:
  36. - org.apache.httpcomponents:httpasyncclient
  37. - org.apache.httpcomponents:httpcore-nio
  38. - org.apache.httpcomponents:httpclient
  39. - org.apache.httpcomponents:httpcore
  40. - commons-codec:commons-codec
  41. - commons-logging:commons-logging
  42. === Initialization
  43. A `RestClient` instance can be built through the corresponding
  44. `RestClientBuilder` class, created via `RestClient#builder(HttpHost...)`
  45. static method. The only required argument is one or more hosts that the
  46. client will communicate with, provided as instances of
  47. https://hc.apache.org/httpcomponents-core-ga/httpcore/apidocs/org/apache/http/HttpHost.html[HttpHost]
  48. as follows:
  49. [source,java]
  50. --------------------------------------------------
  51. RestClient restClient = RestClient.builder(
  52. new HttpHost("localhost", 9200, "http"),
  53. new HttpHost("localhost", 9201, "http")).build();
  54. --------------------------------------------------
  55. The `RestClient` class is thread-safe and ideally has the same lifecycle as
  56. the application that uses it. It is important that it gets closed when no
  57. longer needed so that all the resources used by it get properly released,
  58. as well as the underlying http client instance and its threads:
  59. [source,java]
  60. --------------------------------------------------
  61. restClient.close();
  62. --------------------------------------------------
  63. `RestClientBuilder` also allows to optionally set the following configuration
  64. parameters while building the `RestClient` instance:
  65. `setDefaultHeaders`:: default headers that need to be sent with each request,
  66. to prevent having to specify them with each single request
  67. `setMaxRetryTimeoutMillis`:: the timeout that should be honoured in case
  68. multiple attempts are made for the same request. The default value is 10
  69. seconds, same as the default socket timeout. In case the socket timeout is
  70. customized, the maximum retry timeout should be adjusted accordingly
  71. `setFailureListener`:: a listener that gets notified every time a node
  72. fails, in case actions need to be taken. Used internally when sniffing on
  73. failure is enabled
  74. `setRequestConfigCallback`:: callback that allows to modify the default
  75. request configuration (e.g. request timeouts, authentication, or anything that
  76. the https://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/client/config/RequestConfig.Builder.html[`org.apache.http.client.config.RequestConfig.Builder`]
  77. allows to set)
  78. `setHttpClientConfigCallback`:: callback that allows to modify the http client
  79. configuration (e.g. encrypted communication over ssl, or anything that the
  80. http://hc.apache.org/httpcomponents-asyncclient-dev/httpasyncclient/apidocs/org/apache/http/impl/nio/client/HttpAsyncClientBuilder.html[`org.apache.http.impl.nio.client.HttpAsyncClientBuilder`]
  81. allows to set)
  82. === Performing requests
  83. Once the `RestClient` has been created, requests can be sent by calling one of
  84. the available `performRequest` or `performRequestAsync` method variants.
  85. The `performRequest` methods are synchronous and they return the `Response`
  86. directly, meaning that the client will block and wait for a response to be returned.
  87. The `performRequestAsync` variants, which return `void` and accept an extra
  88. `ResponseListener` as an argument, are executed asynchronously. The provided
  89. listener will be notified upon completion or failure.
  90. [source,java]
  91. --------------------------------------------------
  92. // Synchronous variants
  93. Response performRequest(String method, String endpoint,
  94. Header... headers)
  95. throws IOException;
  96. Response performRequest(String method, String endpoint,
  97. Map<String, String> params, Header... headers)
  98. throws IOException;
  99. Response performRequest(String method, String endpoint,
  100. Map<String, String> params,
  101. HttpEntity entity,
  102. Header... headers)
  103. throws IOException;
  104. Response performRequest(String method, String endpoint,
  105. Map<String, String> params,
  106. HttpEntity entity,
  107. HttpAsyncResponseConsumerFactory responseConsumerFactory,
  108. Header... headers)
  109. throws IOException;
  110. // Asynchronous variants
  111. void performRequestAsync(String method, String endpoint,
  112. ResponseListener responseListener,
  113. Header... headers);
  114. void performRequestAsync(String method, String endpoint,
  115. Map<String, String> params,
  116. ResponseListener responseListener,
  117. Header... headers);
  118. void performRequestAsync(String method, String endpoint,
  119. Map<String, String> params,
  120. HttpEntity entity,
  121. ResponseListener responseListener,
  122. Header... headers);
  123. void performRequestAsync(String method, String endpoint,
  124. Map<String, String> params,
  125. HttpEntity entity,
  126. HttpAsyncResponseConsumerFactory responseConsumerFactory,
  127. ResponseListener responseListener,
  128. Header... headers);
  129. --------------------------------------------------
  130. ==== Request Arguments
  131. The following are the arguments accepted by the different methods:
  132. `method`:: the http method or verb
  133. `endpoint`:: the request path, which identifies the Elasticsearch API to
  134. call (e.g. `/_cluster/health`)
  135. `params`:: the optional parameters to be sent as querystring parameters
  136. `entity`:: the optional request body enclosed in an
  137. `org.apache.http.HttpEntity` object
  138. `responseConsumerFactory`:: the optional factory that is used to create an
  139. http://hc.apache.org/httpcomponents-core-ga/httpcore-nio/apidocs/org/apache/http/nio/protocol/HttpAsyncResponseConsumer.html[`org.apache.http.nio.protocol.HttpAsyncResponseConsumer`]
  140. callback instance per request attempt. Controls how the response body gets
  141. streamed from a non-blocking HTTP connection on the client side. When not
  142. provided, the default implementation is used which buffers the whole response
  143. body in heap memory, up to 100 MB
  144. `responseListener`:: the listener to be notified upon asynchronous
  145. request success or failure
  146. `headers`:: optional request headers
  147. === Reading responses
  148. The `Response` object, either returned by the synchronous `performRequest` methods or
  149. received as an argument in `ResponseListener#onSuccess(Response)`, wraps the
  150. response object returned by the http client and exposes the following information:
  151. `getRequestLine`:: information about the performed request
  152. `getHost`:: the host that returned the response
  153. `getStatusLine`:: the response status line
  154. `getHeaders`:: the response headers, which can also be retrieved by name
  155. though `getHeader(String)`
  156. `getEntity`:: the response body enclosed in an
  157. https://hc.apache.org/httpcomponents-core-ga/httpcore/apidocs/org/apache/http/HttpEntity.html[`org.apache.http.HttpEntity`]
  158. object
  159. When performing a request, an exception is thrown (or received as an argument
  160. in `ResponseListener#onFailure(Exception)` in the following scenarios:
  161. `IOException`:: communication problem (e.g. SocketTimeoutException etc.)
  162. `ResponseException`:: a response was returned, but its status code indicated
  163. an error (not `2xx`). A `ResponseException` originates from a valid
  164. http response, hence it exposes its corresponding `Response` object which gives
  165. access to the returned response.
  166. NOTE: A `ResponseException` is **not** thrown for `HEAD` requests that return
  167. a `404` status code because it is an expected `HEAD` response that simply
  168. denotes that the resource is not found. All other HTTP methods (e.g., `GET`)
  169. throw a `ResponseException` for `404` responses unless the `ignore` parameter
  170. contains `404`. `ignore` is a special client parameter that doesn't get sent
  171. to Elasticsearch and contains a comma separated list of error status codes.
  172. It allows to control whether some error status code should be treated as an
  173. expected response rather than as an exception. This is useful for instance
  174. with the get api as it can return `404` when the document is missing, in which
  175. case the response body will not contain an error but rather the usual get api
  176. response, just without the document as it was not found.
  177. === Example requests
  178. Here are a couple of examples:
  179. [source,java]
  180. --------------------------------------------------
  181. Response response = restClient.performRequest("GET", "/",
  182. Collections.singletonMap("pretty", "true"));
  183. System.out.println(EntityUtils.toString(response.getEntity()));
  184. //index a document
  185. HttpEntity entity = new NStringEntity(
  186. "{\n" +
  187. " \"user\" : \"kimchy\",\n" +
  188. " \"post_date\" : \"2009-11-15T14:12:12\",\n" +
  189. " \"message\" : \"trying out Elasticsearch\"\n" +
  190. "}", ContentType.APPLICATION_JSON);
  191. Response indexResponse = restClient.performRequest(
  192. "PUT",
  193. "/twitter/tweet/1",
  194. Collections.<String, String>emptyMap(),
  195. entity);
  196. --------------------------------------------------
  197. IMPORTANT: The `ContentType` that you specify for the `HttpEntity` is important
  198. because it will be used to set the `Content-Type` header so that Elasticsearch
  199. can properly parse the content.
  200. Note that the low-level client doesn't expose any helper for json marshalling
  201. and un-marshalling. Users are free to use the library that they prefer for that
  202. purpose.
  203. The underlying Apache Async Http Client ships with different
  204. https://hc.apache.org/httpcomponents-core-ga/httpcore/apidocs/org/apache/http/HttpEntity.html[`org.apache.http.HttpEntity`]
  205. implementations that allow to provide the request body in different formats
  206. (stream, byte array, string etc.). As for reading the response body, the
  207. `HttpEntity#getContent` method comes handy which returns an `InputStream`
  208. reading from the previously buffered response body. As an alternative, it is
  209. possible to provide a custom
  210. http://hc.apache.org/httpcomponents-core-ga/httpcore-nio/apidocs/org/apache/http/nio/protocol/HttpAsyncResponseConsumer.html[`org.apache.http.nio.protocol.HttpAsyncResponseConsumer`]
  211. that controls how bytes are read and buffered.
  212. The following is a basic example of how async requests can be sent:
  213. [source,java]
  214. --------------------------------------------------
  215. int numRequests = 10;
  216. final CountDownLatch latch = new CountDownLatch(numRequests);
  217. for (int i = 0; i < numRequests; i++) {
  218. restClient.performRequestAsync(
  219. "PUT",
  220. "/twitter/tweet/" + i,
  221. Collections.<String, String>emptyMap(),
  222. //assume that the documents are stored in an entities array
  223. entities[i],
  224. new ResponseListener() {
  225. @Override
  226. public void onSuccess(Response response) {
  227. System.out.println(response);
  228. latch.countDown();
  229. }
  230. @Override
  231. public void onFailure(Exception exception) {
  232. latch.countDown();
  233. }
  234. }
  235. );
  236. }
  237. //wait for all requests to be completed
  238. latch.await();
  239. --------------------------------------------------
  240. === Logging
  241. The Java REST client uses the same logging library that the Apache Async Http
  242. Client uses: https://commons.apache.org/proper/commons-logging/[Apache Commons Logging],
  243. which comes with support for a number of popular logging implementations. The
  244. java packages to enable logging for are `org.elasticsearch.client` for the
  245. client itself and `org.elasticsearch.client.sniffer` for the sniffer.
  246. The request tracer logging can also be enabled to log every request and
  247. corresponding response in curl format. That comes handy when debugging, for
  248. instance in case a request needs to be manually executed to check whether it
  249. still yields the same response as it did. Enable trace logging for the `tracer`
  250. package to have such log lines printed out. Do note that this type of logging is
  251. expensive and should not be enabled at all times in production environments,
  252. but rather temporarily used only when needed.