getting-started.asciidoc 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 `6.0.0-beta1` version which depends on Lucene `7.0.0-snapshot-00142c9`, 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>http://s3.amazonaws.com/download.elasticsearch.org/lucenesnapshots/00142c9</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. url 'http://s3.amazonaws.com/download.elasticsearch.org/lucenesnapshots/00142c9'
  84. }
  85. --------------------------------------------------
  86. [[java-rest-high-getting-started-dependencies]]
  87. === Dependencies
  88. The High Level Java REST Client depends on the following artifacts and their
  89. transitive dependencies:
  90. - org.elasticsearch.client:elasticsearch-rest-client
  91. - org.elasticsearch:elasticsearch
  92. [[java-rest-high-getting-started-initialization]]
  93. === Initialization
  94. A `RestHighLevelClient` instance needs a <<java-rest-low-usage-initialization,REST low-level client builder>>
  95. to be built as follows:
  96. ["source","java",subs="attributes,callouts,macros"]
  97. --------------------------------------------------
  98. include-tagged::{doc-tests}/MainDocumentationIT.java[rest-high-level-client-init]
  99. --------------------------------------------------
  100. The high-level client will internally create the low-level client used to
  101. perform requests based on the provided builder, and manage its lifecycle.
  102. The high-level client instance needs to be closed when no longer needed so that
  103. all the resources used by it get properly released, as well as the underlying
  104. http client instance and its threads. This can be done through the `close`
  105. method, which will close the internal `RestClient` instance.
  106. ["source","java",subs="attributes,callouts,macros"]
  107. --------------------------------------------------
  108. include-tagged::{doc-tests}/MainDocumentationIT.java[rest-high-level-client-close]
  109. --------------------------------------------------
  110. In the rest of this documentation about the Java High Level Client, the `RestHighLevelClient` instance
  111. will be referenced as `client`.