| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399 | [[java-rest-low-usage]]== Getting startedThis section describes how to get started with the low-level REST client fromgetting the artifact to using it in an application.[[java-rest-low-javadoc]]=== JavadocThe javadoc for the low level REST client can be found at {rest-client-javadoc}/index.html.[[java-rest-low-usage-maven]]=== Maven RepositoryThe low-level Java REST client is hosted onhttp://search.maven.org/#search%7Cga%7C1%7Cg%3A%22org.elasticsearch.client%22[MavenCentral]. The minimum Java version required is `1.7`.The low-level REST client is subject to the same release cycle asElasticsearch. Replace the version with the desired client version, firstreleased with `5.0.0-alpha4`. There is no relation between the client versionand the Elasticsearch version that the client can communicate with. Thelow-level REST client is compatible with all Elasticsearch versions.If you are looking for a SNAPSHOT version, the Elastic Maven Snapshot repository is availableat https://snapshots.elastic.co/maven/.[[java-rest-low-usage-maven-maven]]==== Maven configurationHere is how you can configure the dependency using maven as a dependency manager.Add the following to your `pom.xml` file:["source","xml",subs="attributes"]--------------------------------------------------<dependency>    <groupId>org.elasticsearch.client</groupId>    <artifactId>elasticsearch-rest-client</artifactId>    <version>{version}</version></dependency>--------------------------------------------------[[java-rest-low-usage-maven-gradle]]==== Gradle configurationHere is how you can configure the dependency using gradle as a dependency manager.Add the following to your `build.gradle` file:["source","groovy",subs="attributes"]--------------------------------------------------dependencies {    compile 'org.elasticsearch.client:elasticsearch-rest-client:{version}'}--------------------------------------------------[[java-rest-low-usage-dependencies]]=== DependenciesThe low-level Java REST client internally uses thehttp://hc.apache.org/httpcomponents-asyncclient-dev/[Apache Http Async Client] to send http requests. It depends on the following artifacts, namely the async http client and its own transitive dependencies:- org.apache.httpcomponents:httpasyncclient- org.apache.httpcomponents:httpcore-nio- org.apache.httpcomponents:httpclient- org.apache.httpcomponents:httpcore- commons-codec:commons-codec- commons-logging:commons-logging[[java-rest-low-usage-shading]]=== ShadingIn order to avoid version conflicts, the dependencies can be shaded and packagedwithin the client in a single JAR file (sometimes called an "uber JAR" or "fatJAR"). Shading a dependency consists of taking its content (resources files andJava class files) and renaming some of its packages before putting them in thesame JAR file as the low-level Java REST client. Shading a JAR can beaccomplished by 3rd-party plugins for Gradle and Maven.Be advised that shading a JAR also has implications. Shading the Commons Logginglayer, for instance, means that 3rd-party logging backends need to be shaded aswell.[[java-rest-low-usage-shading-maven]]==== Maven configurationHere is a configuration using the Mavenhttps://maven.apache.org/plugins/maven-shade-plugin/index.html[Shade]plugin. Add the following to your `pom.xml` file:["source","xml",subs="attributes"]--------------------------------------------------<build>    <plugins>        <plugin>            <groupId>org.apache.maven.plugins</groupId>            <artifactId>maven-shade-plugin</artifactId>            <version>3.1.0</version>            <executions>                <execution>                    <phase>package</phase>                    <goals><goal>shade</goal></goals>                    <configuration>                        <relocations>                            <relocation>                                <pattern>org.apache.http</pattern>                                <shadedPattern>hidden.org.apache.http</shadedPattern>                            </relocation>                            <relocation>                                <pattern>org.apache.logging</pattern>                                <shadedPattern>hidden.org.apache.logging</shadedPattern>                            </relocation>                            <relocation>                                <pattern>org.apache.commons.codec</pattern>                                <shadedPattern>hidden.org.apache.commons.codec</shadedPattern>                            </relocation>                            <relocation>                                <pattern>org.apache.commons.logging</pattern>                                <shadedPattern>hidden.org.apache.commons.logging</shadedPattern>                            </relocation>                        </relocations>                    </configuration>                </execution>            </executions>        </plugin>    </plugins></build>--------------------------------------------------[[java-rest-low-usage-shading-gradle]]==== Gradle configurationHere is a configuration using the Gradlehttps://github.com/johnrengelman/shadow[ShadowJar] plugin. Add the following toyour `build.gradle` file:["source","groovy",subs="attributes"]--------------------------------------------------shadowJar {    relocate 'org.apache.http', 'hidden.org.apache.http'    relocate 'org.apache.logging', 'hidden.org.apache.logging'    relocate 'org.apache.commons.codec', 'hidden.org.apache.commons.codec'    relocate 'org.apache.commons.logging', 'hidden.org.apache.commons.logging'}--------------------------------------------------[[java-rest-low-usage-initialization]]=== InitializationA `RestClient` instance can be built through the corresponding`RestClientBuilder` class, created via `RestClient#builder(HttpHost...)`static method. The only required argument is one or more hosts that theclient will communicate with, provided as instances ofhttps://hc.apache.org/httpcomponents-core-ga/httpcore/apidocs/org/apache/http/HttpHost.html[HttpHost] as follows:["source","java",subs="attributes,callouts,macros"]--------------------------------------------------include-tagged::{doc-tests}/RestClientDocumentation.java[rest-client-init]--------------------------------------------------The `RestClient` class is thread-safe and ideally has the same lifecycle asthe application that uses it. It is important that it gets closed when nolonger needed so that all the resources used by it get properly released,as well as the underlying http client instance and its threads:["source","java",subs="attributes,callouts,macros"]--------------------------------------------------include-tagged::{doc-tests}/RestClientDocumentation.java[rest-client-close]--------------------------------------------------`RestClientBuilder` also allows to optionally set the following configurationparameters while building the `RestClient` instance:["source","java",subs="attributes,callouts,macros"]--------------------------------------------------include-tagged::{doc-tests}/RestClientDocumentation.java[rest-client-init-default-headers]--------------------------------------------------<1> Set the default headers that need to be sent with each request, toprevent having to specify them with each single request["source","java",subs="attributes,callouts,macros"]--------------------------------------------------include-tagged::{doc-tests}/RestClientDocumentation.java[rest-client-init-max-retry-timeout]--------------------------------------------------<1> Set the timeout that should be honoured in case multiple attempts are madefor the same request. The default value is 30 seconds, same as the defaultsocket timeout. In case the socket timeout is customized, the maximum retrytimeout should be adjusted accordingly["source","java",subs="attributes,callouts,macros"]--------------------------------------------------include-tagged::{doc-tests}/RestClientDocumentation.java[rest-client-init-failure-listener]--------------------------------------------------<1> Set a listener that gets notified every time a node fails,  in case actionsneed to be taken. Used internally when sniffing on failure is enabled.["source","java",subs="attributes,callouts,macros"]--------------------------------------------------include-tagged::{doc-tests}/RestClientDocumentation.java[rest-client-init-request-config-callback]--------------------------------------------------<1> Set a callback that allows to modify the default request configuration(e.g. request timeouts, authentication, or anything that thehttps://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/client/config/RequestConfig.Builder.html[`org.apache.http.client.config.RequestConfig.Builder`] allows to set)["source","java",subs="attributes,callouts,macros"]--------------------------------------------------include-tagged::{doc-tests}/RestClientDocumentation.java[rest-client-init-client-config-callback]--------------------------------------------------<1> Set a callback that allows to modify the http client configuration(e.g. encrypted communication over ssl, or anything that thehttp://hc.apache.org/httpcomponents-asyncclient-dev/httpasyncclient/apidocs/org/apache/http/impl/nio/client/HttpAsyncClientBuilder.html[`org.apache.http.impl.nio.client.HttpAsyncClientBuilder`] allows to set)[[java-rest-low-usage-requests]]=== Performing requestsOnce the `RestClient` has been created, requests can be sent by calling one ofthe available `performRequest` or `performRequestAsync` method variants.The `performRequest` methods are synchronous and return the `Response` directly,meaning that the client will block and wait for a response to be returned.The `performRequestAsync` variants return `void` and accept an extra`ResponseListener` as an argument instead, meaning that they are executedasynchronously. The provided listener will be notified upon request completionor failure.["source","java",subs="attributes,callouts,macros"]--------------------------------------------------include-tagged::{doc-tests}/RestClientDocumentation.java[rest-client-verb-endpoint]--------------------------------------------------<1> Send a request by providing only the verb and the endpoint, minimum setof required arguments["source","java",subs="attributes,callouts,macros"]--------------------------------------------------include-tagged::{doc-tests}/RestClientDocumentation.java[rest-client-verb-endpoint-params]--------------------------------------------------<1> Send a request by providing the verb, the endpoint, and some querystringparameter["source","java",subs="attributes,callouts,macros"]--------------------------------------------------include-tagged::{doc-tests}/RestClientDocumentation.java[rest-client-verb-endpoint-params-body]--------------------------------------------------<1> Send a request by providing the verb, the endpoint, optional querystringparameters and the request body enclosed in an `org.apache.http.HttpEntity`objectIMPORTANT: The `ContentType` specified for the `HttpEntity` is importantbecause it will be used to set the `Content-Type` header so that Elasticsearchcan properly parse the content.["source","java",subs="attributes,callouts,macros"]--------------------------------------------------include-tagged::{doc-tests}/RestClientDocumentation.java[rest-client-response-consumer]--------------------------------------------------<1> Send a request by providing the verb, the endpoint, optional querystringparameters, optional request body and the optional factory that is used tocreate an http://hc.apache.org/httpcomponents-core-ga/httpcore-nio/apidocs/org/apache/http/nio/protocol/HttpAsyncResponseConsumer.html[`org.apache.http.nio.protocol.HttpAsyncResponseConsumer`]callback instance per request attempt. Controls how the response body getsstreamed from a non-blocking HTTP connection on the client side. When notprovided, the default implementation is used which buffers the whole responsebody in heap memory, up to 100 MB.["source","java",subs="attributes,callouts,macros"]--------------------------------------------------include-tagged::{doc-tests}/RestClientDocumentation.java[rest-client-verb-endpoint-async]--------------------------------------------------<1> Define what needs to happen when the request is successfully performed<2> Define what needs to happen when the request fails, meaning wheneverthere's a connection error or a response with error status code is returned.<3> Send an async request by providing only the verb, the endpoint, and theresponse listener to be notified once the request is completed, minimum setof required arguments["source","java",subs="attributes,callouts,macros"]--------------------------------------------------include-tagged::{doc-tests}/RestClientDocumentation.java[rest-client-verb-endpoint-params-async]--------------------------------------------------<1> Send an async request by providing the verb, the endpoint, some querystringparameter and the response listener to be notified once the request is completed["source","java",subs="attributes,callouts,macros"]--------------------------------------------------include-tagged::{doc-tests}/RestClientDocumentation.java[rest-client-verb-endpoint-params-body-async]--------------------------------------------------<1> Send an async request by providing the verb, the endpoint, optionalquerystring parameters, the request body enclosed in an`org.apache.http.HttpEntity` object and the response listener to benotified once the request is completed["source","java",subs="attributes,callouts,macros"]--------------------------------------------------include-tagged::{doc-tests}/RestClientDocumentation.java[rest-client-response-consumer-async]--------------------------------------------------<1> Send an async request by providing the verb, the endpoint, optionalquerystring parameters, optional request body and the optional factory that isused to create an http://hc.apache.org/httpcomponents-core-ga/httpcore-nio/apidocs/org/apache/http/nio/protocol/HttpAsyncResponseConsumer.html[`org.apache.http.nio.protocol.HttpAsyncResponseConsumer`]callback instance per request attempt. Controls how the response body getsstreamed from a non-blocking HTTP connection on the client side. When notprovided, the default implementation is used which buffers the whole responsebody in heap memory, up to 100 MB.The following is a basic example of how async requests can be sent:["source","java",subs="attributes,callouts,macros"]--------------------------------------------------include-tagged::{doc-tests}/RestClientDocumentation.java[rest-client-async-example]--------------------------------------------------<1> Process the returned response<2> Handle the returned exception, due to communication error or a responsewith status code that indicates an errorEach of the above listed method supports sending headers along with therequest through a `Header` varargs argument as in the following examples:["source","java",subs="attributes,callouts,macros"]--------------------------------------------------include-tagged::{doc-tests}/RestClientDocumentation.java[rest-client-headers]--------------------------------------------------["source","java",subs="attributes,callouts,macros"]--------------------------------------------------include-tagged::{doc-tests}/RestClientDocumentation.java[rest-client-headers-async]--------------------------------------------------[[java-rest-low-usage-responses]]=== Reading responsesThe `Response` object, either returned by the synchronous `performRequest` methods orreceived as an argument in `ResponseListener#onSuccess(Response)`, wraps theresponse object returned by the http client and exposes some additional information.["source","java",subs="attributes,callouts,macros"]--------------------------------------------------include-tagged::{doc-tests}/RestClientDocumentation.java[rest-client-response2]--------------------------------------------------<1> Information about the performed request<2> The host that returned the response<3> The response status line, from which you can for instance retrieve the status code<4> The response headers, which can also be retrieved by name though `getHeader(String)`<5> The response body enclosed in an https://hc.apache.org/httpcomponents-core-ga/httpcore/apidocs/org/apache/http/HttpEntity.html[`org.apache.http.HttpEntity`] objectWhen performing a request, an exception is thrown (or received as an argument in `ResponseListener#onFailure(Exception)` in the following scenarios:`IOException`:: communication problem (e.g. SocketTimeoutException)`ResponseException`:: a response was returned, but its status code indicatedan error (not `2xx`). A `ResponseException` originates from a validhttp response, hence it exposes its corresponding `Response` object which givesaccess to the returned response.NOTE: A `ResponseException` is **not** thrown for `HEAD` requests that returna `404` status code because it is an expected `HEAD` response that simplydenotes that the resource is not found. All other HTTP methods (e.g., `GET`)throw a `ResponseException` for `404` responses unless the `ignore` parametercontains `404`. `ignore` is a special client parameter that doesn't get sentto Elasticsearch and contains a comma separated list of error status codes.It allows to control whether some error status code should be treated as anexpected response rather than as an exception. This is useful for instancewith the get api as it can return `404` when the document is missing, in whichcase the response body will not contain an error but rather the usual get apiresponse, just without the document as it was not found.Note that the low-level client doesn't expose any helper for json marshallingand un-marshalling. Users are free to use the library that they prefer for thatpurpose.The underlying Apache Async Http Client ships with differenthttps://hc.apache.org/httpcomponents-core-ga/httpcore/apidocs/org/apache/http/HttpEntity.html[`org.apache.http.HttpEntity`] implementations that allow to provide the request body in different formats(stream, byte array, string etc.). As for reading the response body, the`HttpEntity#getContent` method comes handy which returns an `InputStream`reading from the previously buffered response body. As an alternative, it ispossible to provide a customhttp://hc.apache.org/httpcomponents-core-ga/httpcore-nio/apidocs/org/apache/http/nio/protocol/HttpAsyncResponseConsumer.html[`org.apache.http.nio.protocol.HttpAsyncResponseConsumer`] that controls how bytes are read and buffered.[[java-rest-low-usage-logging]]=== LoggingThe Java REST client uses the same logging library that the Apache Async HttpClient uses: https://commons.apache.org/proper/commons-logging/[Apache Commons Logging], which comes with support for a number of popular logging implementations. Thejava packages to enable logging for are `org.elasticsearch.client` for theclient itself and `org.elasticsearch.client.sniffer` for the sniffer.The request tracer logging can also be enabled to log every request andcorresponding response in curl format. That comes handy when debugging, forinstance in case a request needs to be manually executed to check whether itstill yields the same response as it did. Enable trace logging for the `tracer`package to have such log lines printed out. Do note that this type of logging isexpensive and should not be enabled at all times in production environments,but rather temporarily used only when needed.
 |