getting-started.asciidoc 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. [[java-rest-high-getting-started]]
  2. == Getting started
  3. This section describes how to get started with the high-level REST client from
  4. getting the artifact to using it in an application.
  5. [[java-rest-high-compatibility]]
  6. === Compatibility
  7. The Java High Level REST Client requires Java 1.8 and depends on the Elasticsearch
  8. core project. The client version is the same as the Elasticsearch version that the
  9. client was developed for. It accepts the same request arguments as the `TransportClient`
  10. and returns the same response objects. See the <<java-rest-high-level-migration>>
  11. if you need to migrate an application from `TransportClient` to the new REST client.
  12. The High Level Client is guaranteed to be able to communicate with any Elasticsearch
  13. node running on the same major version and greater or equal minor version. It
  14. doesn't need to be in the same minor version as the Elasticsearch nodes it
  15. communicates with, as it is forward compatible meaning that it supports
  16. communicating with later versions of Elasticsearch than the one it was developed for.
  17. The 6.0 client is able to communicate with any 6.x Elasticsearch node, while the 6.1
  18. client is for sure able to communicate with 6.1, 6.2 and any later 6.x version, but
  19. there may be incompatibility issues when communicating with a previous Elasticsearch
  20. node version, for instance between 6.1 and 6.0, in case the 6.1 client supports new
  21. request body fields for some APIs that are not known by the 6.0 node(s).
  22. It is recommended to upgrade the High Level Client when upgrading the Elasticsearch
  23. cluster to a new major version, as REST API breaking changes may cause unexpected
  24. results depending on the node that is hit by the request, and newly added APIs will
  25. only be supported by the newer version of the client. The client should always be
  26. updated last, once all of the nodes in the cluster have been upgraded to the new
  27. major version.
  28. [[java-rest-high-javadoc]]
  29. === Javadoc
  30. The javadoc for the REST high level client can be found at {rest-high-level-client-javadoc}/index.html.
  31. [[java-rest-high-getting-started-maven]]
  32. === Maven Repository
  33. The high-level Java REST client is hosted on
  34. http://search.maven.org/#search%7Cga%7C1%7Cg%3A%22org.elasticsearch.client%22[Maven
  35. Central]. The minimum Java version required is `1.8`.
  36. The High Level REST Client is subject to the same release cycle as
  37. Elasticsearch. Replace the version with the desired client version.
  38. If you are looking for a SNAPSHOT version, the Elastic Maven Snapshot repository is available
  39. at https://snapshots.elastic.co/maven/.
  40. [[java-rest-high-getting-started-maven-maven]]
  41. ==== Maven configuration
  42. Here is how you can configure the dependency using maven as a dependency manager.
  43. Add the following to your `pom.xml` file:
  44. ["source","xml",subs="attributes"]
  45. --------------------------------------------------
  46. <dependency>
  47. <groupId>org.elasticsearch.client</groupId>
  48. <artifactId>elasticsearch-rest-high-level-client</artifactId>
  49. <version>{version}</version>
  50. </dependency>
  51. --------------------------------------------------
  52. [[java-rest-high-getting-started-maven-gradle]]
  53. ==== Gradle configuration
  54. Here is how you can configure the dependency using gradle as a dependency manager.
  55. Add the following to your `build.gradle` file:
  56. ["source","groovy",subs="attributes"]
  57. --------------------------------------------------
  58. dependencies {
  59. compile 'org.elasticsearch.client:elasticsearch-rest-high-level-client:{version}'
  60. }
  61. --------------------------------------------------
  62. [[java-rest-high-getting-started-maven-lucene]]
  63. ==== Lucene Snapshot repository
  64. The very first releases of any major version (like a beta), might have been built on top of a Lucene Snapshot version.
  65. In such a case you will be unable to resolve the Lucene dependencies of the client.
  66. For example, if you want to use the `7.0.0-beta1` version which depends on Lucene `8.0.0-snapshot-83f9835`, you must
  67. define the following repository.
  68. For Maven:
  69. ["source","xml",subs="attributes"]
  70. --------------------------------------------------
  71. <repository>
  72. <id>elastic-lucene-snapshots</id>
  73. <name>Elastic Lucene Snapshots</name>
  74. <url>https://s3.amazonaws.com/download.elasticsearch.org/lucenesnapshots/83f9835</url>
  75. <releases><enabled>true</enabled></releases>
  76. <snapshots><enabled>false</enabled></snapshots>
  77. </repository>
  78. --------------------------------------------------
  79. For Gradle:
  80. ["source","groovy",subs="attributes"]
  81. --------------------------------------------------
  82. maven {
  83. name 'lucene-snapshots'
  84. url 'https://s3.amazonaws.com/download.elasticsearch.org/lucenesnapshots/83f9835'
  85. }
  86. --------------------------------------------------
  87. [[java-rest-high-getting-started-dependencies]]
  88. === Dependencies
  89. The High Level Java REST Client depends on the following artifacts and their
  90. transitive dependencies:
  91. - org.elasticsearch.client:elasticsearch-rest-client
  92. - org.elasticsearch:elasticsearch
  93. [[java-rest-high-getting-started-initialization]]
  94. === Initialization
  95. A `RestHighLevelClient` instance needs a <<java-rest-low-usage-initialization,REST low-level client builder>>
  96. to be built as follows:
  97. ["source","java",subs="attributes,callouts,macros"]
  98. --------------------------------------------------
  99. include-tagged::{doc-tests}/MiscellaneousDocumentationIT.java[rest-high-level-client-init]
  100. --------------------------------------------------
  101. The high-level client will internally create the low-level client used to
  102. perform requests based on the provided builder. That low-level client
  103. maintains a pool of connections and starts some threads so you should
  104. close the high-level client when you are well and truly done with
  105. it and it will in turn close the internal low-level client to free those
  106. resources. This can be done through the `close`:
  107. ["source","java",subs="attributes,callouts,macros"]
  108. --------------------------------------------------
  109. include-tagged::{doc-tests}/MiscellaneousDocumentationIT.java[rest-high-level-client-close]
  110. --------------------------------------------------
  111. In the rest of this documentation about the Java High Level Client, the `RestHighLevelClient` instance
  112. will be referenced as `client`.
  113. [[java-rest-high-getting-started-request-options]]
  114. === RequestOptions
  115. All APIs in the `RestHighLevelClient` accept a `RequestOptions` which you can
  116. use to customize the request in ways that won't change how Elasticsearch
  117. executes the request. For example, this is the place where you'd specify a
  118. `NodeSelector` to control which node receives the request. See the
  119. <<java-rest-low-usage-request-options,low level client documentation>> for
  120. more examples of customizing the options.
  121. === Asynchronous usage
  122. All of the the methods across the different clients exist in a traditional synchronous and
  123. asynchronous variant. The difference is that the asynchronous ones use asynchronous requests
  124. in the REST Low Level Client. This is useful if you are doing multiple requests or are using e.g.
  125. rx java, Kotlin co-routines, or similar frameworks.
  126. The asynchronous methods are recognizable by the fact that they have the word "Async" in their name
  127. and return a `Cancellable` instance. The asynchronous methods accept the same request object
  128. as the synchronous variant and accept a generic `ActionListener<T>` where `T` is the return
  129. type of the synchronous method.
  130. All asynchronous methods return a `Cancellable` object with a `cancel` method that you may call
  131. in case you want to abort the request. Cancelling
  132. no longer needed requests is a good way to avoid putting unnecessary
  133. load on Elasticsearch.
  134. Using the `Cancellable` instance is optional and you can safely ignore this if you have
  135. no need for this. A use case for this would be using this with e.g. Kotlin's `suspendCancellableCoRoutine`.