2017-09-06 20:06:41 +08:00
[[java-rest-high-getting-started]]
2017-07-04 16:58:57 +08:00
== Getting started
This section describes how to get started with the high-level REST client from
getting the artifact to using it in an application.
2017-02-24 20:52:36 +08:00
2017-08-01 16:38:56 +08:00
[[java-rest-high-compatibility]]
=== Compatibility
2020-10-28 00:52:02 +08:00
The Java High Level REST Client requires at least Java 1.8 and depends on the Elasticsearch
2017-08-01 16:38:56 +08:00
core project. The client version is the same as the Elasticsearch version that the
client was developed for. It accepts the same request arguments as the `TransportClient`
2017-08-21 20:11:55 +08:00
and returns the same response objects. See the <<java-rest-high-level-migration>>
if you need to migrate an application from `TransportClient` to the new REST client.
The High Level Client is guaranteed to be able to communicate with any Elasticsearch
node running on the same major version and greater or equal minor version. It
doesn't need to be in the same minor version as the Elasticsearch nodes it
communicates with, as it is forward compatible meaning that it supports
communicating with later versions of Elasticsearch than the one it was developed for.
2022-05-19 02:29:10 +08:00
The 7.0 client is able to communicate with any 7.x Elasticsearch node, while the 7.1
client is for sure able to communicate with 7.1, 7.2 and any later 7.x version, but
2017-08-21 20:11:55 +08:00
there may be incompatibility issues when communicating with a previous Elasticsearch
2022-05-19 02:29:10 +08:00
node version, for instance between 7.1 and 7.0, in case the 7.1 client supports new
request body fields for some APIs that are not known by the 7.0 node(s).
2017-08-21 20:11:55 +08:00
It is recommended to upgrade the High Level Client when upgrading the Elasticsearch
cluster to a new major version, as REST API breaking changes may cause unexpected
results depending on the node that is hit by the request, and newly added APIs will
only be supported by the newer version of the client. The client should always be
updated last, once all of the nodes in the cluster have been upgraded to the new
major version.
2017-08-01 16:38:56 +08:00
2022-05-19 02:29:10 +08:00
*Compatibility with Elasticsearch 8.x*
The High Level Client version 7.16 and higher can communicate with Elasticsearch version 8.x after enabling API compatibility mode. When this mode is enabled, the client will send HTTP headers that instruct Elasticsearch 8.x to honor 7.x request/responses.
Compatibility mode is enabled as follows:
["source","java",subs="attributes"]
--------------------------------------------------
RestHighLevelClient esClient = new RestHighLevelClientBuilder(restClient)
.setApiCompatibilityMode(true)
.build()
--------------------------------------------------
When compatibility mode is enabled, the client can also communicate with Elasticsearch version 7.11 and higher.
2017-07-31 21:41:21 +08:00
[[java-rest-high-javadoc]]
=== Javadoc
The javadoc for the REST high level client can be found at {rest-high-level-client-javadoc}/index.html.
2017-09-06 20:06:41 +08:00
[[java-rest-high-getting-started-maven]]
2017-07-04 16:58:57 +08:00
=== Maven Repository
2017-02-24 20:52:36 +08:00
The high-level Java REST client is hosted on
2020-08-01 04:16:31 +08:00
https://search.maven.org/search?q=g:org.elasticsearch.client[Maven
2017-02-24 20:52:36 +08:00
Central]. The minimum Java version required is `1.8`.
2017-07-03 16:48:16 +08:00
The High Level REST Client is subject to the same release cycle as
Elasticsearch. Replace the version with the desired client version.
2017-02-24 20:52:36 +08:00
2020-08-07 00:29:21 +08:00
If you are looking for a SNAPSHOT version, you should add our snapshot repository to your Maven config:
["source","xml",subs="attributes"]
--------------------------------------------------
<repositories>
<repository>
<id>es-snapshots</id>
<name>elasticsearch snapshot repo</name>
<url>https://snapshots.elastic.co/maven/</url>
</repository>
2020-08-18 21:09:10 +08:00
</repositories>
2020-08-07 00:29:21 +08:00
--------------------------------------------------
or in Gradle:
["source","groovy",subs="attributes"]
--------------------------------------------------
maven {
url "https://snapshots.elastic.co/maven/"
}
--------------------------------------------------
2018-02-10 00:59:04 +08:00
2017-09-06 20:06:41 +08:00
[[java-rest-high-getting-started-maven-maven]]
2017-07-04 16:58:57 +08:00
==== Maven configuration
2017-02-24 20:52:36 +08:00
Here 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>
2017-07-13 15:44:25 +08:00
<artifactId>elasticsearch-rest-high-level-client</artifactId>
2017-02-24 20:52:36 +08:00
<version>{version}</version>
</dependency>
--------------------------------------------------
2017-09-06 20:06:41 +08:00
[[java-rest-high-getting-started-maven-gradle]]
2017-07-04 16:58:57 +08:00
==== Gradle configuration
2017-02-24 20:52:36 +08:00
Here 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 {
2017-07-13 15:44:25 +08:00
compile 'org.elasticsearch.client:elasticsearch-rest-high-level-client:{version}'
2017-02-24 20:52:36 +08:00
}
--------------------------------------------------
2017-09-06 20:06:41 +08:00
[[java-rest-high-getting-started-maven-lucene]]
Document how to import Lucene Snapshot libs when elasticsearch clients (#26113)
When using the High Level Rest Client 6.0.0-beta1, we are missing some transitive dependencies for Lucene as Lucene 7 has not been released yet. See the following `pom.xml`:
```xml
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-client</artifactId>
<version>6.0.0-beta1</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>6.0.0-beta1</version>
</dependency>
```
It gives:
```
[ERROR] Failed to execute goal on project fscrawler: Could not resolve dependencies for project fr.pilato.elasticsearch.crawler:fscrawler:jar:2.4-SNAPSHOT: The following artifacts could not be resolved: org.apache.lucene:lucene-analyzers-common:jar:7.0.0-snapshot-00142c9, org.apache.lucene:lucene-backward-codecs:jar:7.0.0-snapshot-00142c9, org.apache.lucene:lucene-grouping:jar:7.0.0-snapshot-00142c9, org.apache.lucene:lucene-highlighter:jar:7.0.0-snapshot-00142c9, org.apache.lucene:lucene-join:jar:7.0.0-snapshot-00142c9, org.apache.lucene:lucene-memory:jar:7.0.0-snapshot-00142c9, org.apache.lucene:lucene-misc:jar:7.0.0-snapshot-00142c9, org.apache.lucene:lucene-queries:jar:7.0.0-snapshot-00142c9, org.apache.lucene:lucene-queryparser:jar:7.0.0-snapshot-00142c9, org.apache.lucene:lucene-sandbox:jar:7.0.0-snapshot-00142c9, org.apache.lucene:lucene-spatial:jar:7.0.0-snapshot-00142c9, org.apache.lucene:lucene-spatial-extras:jar:7.0.0-snapshot-00142c9, org.apache.lucene:lucene-spatial3d:jar:7.0.0-snapshot-00142c9, org.apache.lucene:lucene-suggest:jar:7.0.0-snapshot-00142c9: Failure to find org.apache.lucene:lucene-analyzers-common:jar:7.0.0-snapshot-00142c9 in https://artifacts.elastic.co/maven/ was cached in the local repository, resolution will not be reattempted until the update interval of elastic-download-service has elapsed or updates are forced -
```
We need to add some temporary documentation on how to add the missing repository to a gradle or maven project:
```xml
<repository>
<id>elastic-lucene-snapshots</id>
<name>Elastic Lucene Snapshots</name>
<url>http://s3.amazonaws.com/download.elasticsearch.org/lucenesnapshots/00142c9</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>false</enabled></snapshots>
</repository>
```
This also applies to the transport client.
Closes #26106.
2017-08-10 20:54:07 +08:00
==== Lucene Snapshot repository
The very first releases of any major version (like a beta), might have been built on top of a Lucene Snapshot version.
In such a case you will be unable to resolve the Lucene dependencies of the client.
2019-02-15 22:55:41 +08:00
For example, if you want to use the `7.0.0-beta1` version which depends on Lucene `8.0.0-snapshot-83f9835`, you must
Document how to import Lucene Snapshot libs when elasticsearch clients (#26113)
When using the High Level Rest Client 6.0.0-beta1, we are missing some transitive dependencies for Lucene as Lucene 7 has not been released yet. See the following `pom.xml`:
```xml
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-client</artifactId>
<version>6.0.0-beta1</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>6.0.0-beta1</version>
</dependency>
```
It gives:
```
[ERROR] Failed to execute goal on project fscrawler: Could not resolve dependencies for project fr.pilato.elasticsearch.crawler:fscrawler:jar:2.4-SNAPSHOT: The following artifacts could not be resolved: org.apache.lucene:lucene-analyzers-common:jar:7.0.0-snapshot-00142c9, org.apache.lucene:lucene-backward-codecs:jar:7.0.0-snapshot-00142c9, org.apache.lucene:lucene-grouping:jar:7.0.0-snapshot-00142c9, org.apache.lucene:lucene-highlighter:jar:7.0.0-snapshot-00142c9, org.apache.lucene:lucene-join:jar:7.0.0-snapshot-00142c9, org.apache.lucene:lucene-memory:jar:7.0.0-snapshot-00142c9, org.apache.lucene:lucene-misc:jar:7.0.0-snapshot-00142c9, org.apache.lucene:lucene-queries:jar:7.0.0-snapshot-00142c9, org.apache.lucene:lucene-queryparser:jar:7.0.0-snapshot-00142c9, org.apache.lucene:lucene-sandbox:jar:7.0.0-snapshot-00142c9, org.apache.lucene:lucene-spatial:jar:7.0.0-snapshot-00142c9, org.apache.lucene:lucene-spatial-extras:jar:7.0.0-snapshot-00142c9, org.apache.lucene:lucene-spatial3d:jar:7.0.0-snapshot-00142c9, org.apache.lucene:lucene-suggest:jar:7.0.0-snapshot-00142c9: Failure to find org.apache.lucene:lucene-analyzers-common:jar:7.0.0-snapshot-00142c9 in https://artifacts.elastic.co/maven/ was cached in the local repository, resolution will not be reattempted until the update interval of elastic-download-service has elapsed or updates are forced -
```
We need to add some temporary documentation on how to add the missing repository to a gradle or maven project:
```xml
<repository>
<id>elastic-lucene-snapshots</id>
<name>Elastic Lucene Snapshots</name>
<url>http://s3.amazonaws.com/download.elasticsearch.org/lucenesnapshots/00142c9</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>false</enabled></snapshots>
</repository>
```
This also applies to the transport client.
Closes #26106.
2017-08-10 20:54:07 +08:00
define the following repository.
For Maven:
["source","xml",subs="attributes"]
--------------------------------------------------
<repository>
<id>elastic-lucene-snapshots</id>
<name>Elastic Lucene Snapshots</name>
2019-05-04 20:28:30 +08:00
<url>https://s3.amazonaws.com/download.elasticsearch.org/lucenesnapshots/83f9835</url>
Document how to import Lucene Snapshot libs when elasticsearch clients (#26113)
When using the High Level Rest Client 6.0.0-beta1, we are missing some transitive dependencies for Lucene as Lucene 7 has not been released yet. See the following `pom.xml`:
```xml
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-client</artifactId>
<version>6.0.0-beta1</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>6.0.0-beta1</version>
</dependency>
```
It gives:
```
[ERROR] Failed to execute goal on project fscrawler: Could not resolve dependencies for project fr.pilato.elasticsearch.crawler:fscrawler:jar:2.4-SNAPSHOT: The following artifacts could not be resolved: org.apache.lucene:lucene-analyzers-common:jar:7.0.0-snapshot-00142c9, org.apache.lucene:lucene-backward-codecs:jar:7.0.0-snapshot-00142c9, org.apache.lucene:lucene-grouping:jar:7.0.0-snapshot-00142c9, org.apache.lucene:lucene-highlighter:jar:7.0.0-snapshot-00142c9, org.apache.lucene:lucene-join:jar:7.0.0-snapshot-00142c9, org.apache.lucene:lucene-memory:jar:7.0.0-snapshot-00142c9, org.apache.lucene:lucene-misc:jar:7.0.0-snapshot-00142c9, org.apache.lucene:lucene-queries:jar:7.0.0-snapshot-00142c9, org.apache.lucene:lucene-queryparser:jar:7.0.0-snapshot-00142c9, org.apache.lucene:lucene-sandbox:jar:7.0.0-snapshot-00142c9, org.apache.lucene:lucene-spatial:jar:7.0.0-snapshot-00142c9, org.apache.lucene:lucene-spatial-extras:jar:7.0.0-snapshot-00142c9, org.apache.lucene:lucene-spatial3d:jar:7.0.0-snapshot-00142c9, org.apache.lucene:lucene-suggest:jar:7.0.0-snapshot-00142c9: Failure to find org.apache.lucene:lucene-analyzers-common:jar:7.0.0-snapshot-00142c9 in https://artifacts.elastic.co/maven/ was cached in the local repository, resolution will not be reattempted until the update interval of elastic-download-service has elapsed or updates are forced -
```
We need to add some temporary documentation on how to add the missing repository to a gradle or maven project:
```xml
<repository>
<id>elastic-lucene-snapshots</id>
<name>Elastic Lucene Snapshots</name>
<url>http://s3.amazonaws.com/download.elasticsearch.org/lucenesnapshots/00142c9</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>false</enabled></snapshots>
</repository>
```
This also applies to the transport client.
Closes #26106.
2017-08-10 20:54:07 +08:00
<releases><enabled>true</enabled></releases>
<snapshots><enabled>false</enabled></snapshots>
</repository>
--------------------------------------------------
For Gradle:
["source","groovy",subs="attributes"]
--------------------------------------------------
maven {
2019-05-07 18:34:51 +08:00
name 'lucene-snapshots'
2019-05-04 20:28:30 +08:00
url 'https://s3.amazonaws.com/download.elasticsearch.org/lucenesnapshots/83f9835'
Document how to import Lucene Snapshot libs when elasticsearch clients (#26113)
When using the High Level Rest Client 6.0.0-beta1, we are missing some transitive dependencies for Lucene as Lucene 7 has not been released yet. See the following `pom.xml`:
```xml
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-client</artifactId>
<version>6.0.0-beta1</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>6.0.0-beta1</version>
</dependency>
```
It gives:
```
[ERROR] Failed to execute goal on project fscrawler: Could not resolve dependencies for project fr.pilato.elasticsearch.crawler:fscrawler:jar:2.4-SNAPSHOT: The following artifacts could not be resolved: org.apache.lucene:lucene-analyzers-common:jar:7.0.0-snapshot-00142c9, org.apache.lucene:lucene-backward-codecs:jar:7.0.0-snapshot-00142c9, org.apache.lucene:lucene-grouping:jar:7.0.0-snapshot-00142c9, org.apache.lucene:lucene-highlighter:jar:7.0.0-snapshot-00142c9, org.apache.lucene:lucene-join:jar:7.0.0-snapshot-00142c9, org.apache.lucene:lucene-memory:jar:7.0.0-snapshot-00142c9, org.apache.lucene:lucene-misc:jar:7.0.0-snapshot-00142c9, org.apache.lucene:lucene-queries:jar:7.0.0-snapshot-00142c9, org.apache.lucene:lucene-queryparser:jar:7.0.0-snapshot-00142c9, org.apache.lucene:lucene-sandbox:jar:7.0.0-snapshot-00142c9, org.apache.lucene:lucene-spatial:jar:7.0.0-snapshot-00142c9, org.apache.lucene:lucene-spatial-extras:jar:7.0.0-snapshot-00142c9, org.apache.lucene:lucene-spatial3d:jar:7.0.0-snapshot-00142c9, org.apache.lucene:lucene-suggest:jar:7.0.0-snapshot-00142c9: Failure to find org.apache.lucene:lucene-analyzers-common:jar:7.0.0-snapshot-00142c9 in https://artifacts.elastic.co/maven/ was cached in the local repository, resolution will not be reattempted until the update interval of elastic-download-service has elapsed or updates are forced -
```
We need to add some temporary documentation on how to add the missing repository to a gradle or maven project:
```xml
<repository>
<id>elastic-lucene-snapshots</id>
<name>Elastic Lucene Snapshots</name>
<url>http://s3.amazonaws.com/download.elasticsearch.org/lucenesnapshots/00142c9</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>false</enabled></snapshots>
</repository>
```
This also applies to the transport client.
Closes #26106.
2017-08-10 20:54:07 +08:00
}
--------------------------------------------------
2017-09-06 20:06:41 +08:00
[[java-rest-high-getting-started-dependencies]]
2017-07-04 16:58:57 +08:00
=== Dependencies
2017-02-24 20:52:36 +08:00
2017-07-03 16:48:16 +08:00
The High Level Java REST Client depends on the following artifacts and their
2017-02-24 20:52:36 +08:00
transitive dependencies:
2017-07-27 17:17:48 +08:00
- org.elasticsearch.client:elasticsearch-rest-client
2017-02-24 20:52:36 +08:00
- org.elasticsearch:elasticsearch
2017-09-06 20:06:41 +08:00
[[java-rest-high-getting-started-initialization]]
2017-07-04 16:58:57 +08:00
=== Initialization
2017-02-24 20:52:36 +08:00
2022-05-19 02:29:10 +08:00
A `RestHighLevelClient` instance needs a
2021-10-05 17:20:51 +08:00
{java-api-client}/java-rest-low-usage-initialization.html[REST low-level client builder]
2017-02-24 20:52:36 +08:00
to be built as follows:
2017-08-24 15:39:41 +08:00
["source","java",subs="attributes,callouts,macros"]
2017-02-24 20:52:36 +08:00
--------------------------------------------------
2018-03-15 02:27:01 +08:00
include-tagged::{doc-tests}/MiscellaneousDocumentationIT.java[rest-high-level-client-init]
2017-08-24 15:39:41 +08:00
--------------------------------------------------
The high-level client will internally create the low-level client used to
2018-07-13 04:22:42 +08:00
perform requests based on the provided builder. That low-level client
maintains a pool of connections and starts some threads so you should
close the high-level client when you are well and truly done with
it and it will in turn close the internal low-level client to free those
resources. This can be done through the `close`:
2017-08-24 15:39:41 +08:00
["source","java",subs="attributes,callouts,macros"]
--------------------------------------------------
2018-03-15 02:27:01 +08:00
include-tagged::{doc-tests}/MiscellaneousDocumentationIT.java[rest-high-level-client-close]
2017-02-24 20:52:36 +08:00
--------------------------------------------------
2017-07-03 16:48:16 +08:00
In the rest of this documentation about the Java High Level Client, the `RestHighLevelClient` instance
2017-02-24 20:52:36 +08:00
will be referenced as `client`.
2018-06-12 05:07:27 +08:00
2019-01-07 21:44:12 +08:00
[[java-rest-high-getting-started-request-options]]
2018-06-12 05:07:27 +08:00
=== RequestOptions
All APIs in the `RestHighLevelClient` accept a `RequestOptions` which you can
use to customize the request in ways that won't change how Elasticsearch
executes the request. For example, this is the place where you'd specify a
`NodeSelector` to control which node receives the request. See the
2022-05-19 02:29:10 +08:00
{java-api-client}/java-rest-low-usage-requests.html#java-rest-low-usage-request-options[low level client documentation]
2021-10-05 18:29:10 +08:00
for more examples of customizing the options.
2020-02-07 22:40:16 +08:00
[[java-rest-high-getting-started-asynchronous-usage]]
2019-08-28 16:59:33 +08:00
=== Asynchronous usage
2022-05-19 02:29:10 +08:00
All of the methods across the different clients exist in a traditional synchronous and
asynchronous variant. The difference is that the asynchronous ones use asynchronous requests
2019-08-28 16:59:33 +08:00
in the REST Low Level Client. This is useful if you are doing multiple requests or are using e.g.
rx java, Kotlin co-routines, or similar frameworks.
2022-05-19 02:29:10 +08:00
The asynchronous methods are recognizable by the fact that they have the word "Async" in their name
and return a `Cancellable` instance. The asynchronous methods accept the same request object
as the synchronous variant and accept a generic `ActionListener<T>` where `T` is the return
type of the synchronous method.
2019-08-28 16:59:33 +08:00
2022-05-19 02:29:10 +08:00
All asynchronous methods return a `Cancellable` object with a `cancel` method that you may call
2019-08-28 16:59:33 +08:00
in case you want to abort the request. Cancelling
2022-05-19 02:29:10 +08:00
no longer needed requests is a good way to avoid putting unnecessary
2019-08-28 16:59:33 +08:00
load on Elasticsearch.
2022-05-19 02:29:10 +08:00
Using the `Cancellable` instance is optional and you can safely ignore this if you have
no need for this. A use case for this would be using this with e.g. Kotlin's `suspendCancellableCoRoutine`.
2019-08-28 16:59:33 +08:00