index.asciidoc 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. [[java-api]]
  2. = Java API
  3. include::../Versions.asciidoc[]
  4. [preface]
  5. == Preface
  6. deprecated[7.0.0, The `TransportClient` is deprecated in favour of the {java-rest}/java-rest-high.html[Java High Level REST Client] and will be removed in Elasticsearch 8.0. The {java-rest}/java-rest-high-level-migration.html[migration guide] describes all the steps needed to migrate.]
  7. This section describes the Java API that Elasticsearch provides. All
  8. Elasticsearch operations are executed using a
  9. <<client,Client>> object. All
  10. operations are completely asynchronous in nature (either accepts a
  11. listener, or returns a future).
  12. Additionally, operations on a client may be accumulated and executed in
  13. <<java-docs-bulk,Bulk>>.
  14. Note, all the APIs are exposed through the
  15. Java API (actually, the Java API is used internally to execute them).
  16. == Javadoc
  17. The javadoc for the transport client can be found at {transport-client-javadoc}/index.html.
  18. == Maven Repository
  19. Elasticsearch is hosted on
  20. http://search.maven.org/#search%7Cga%7C1%7Ca%3A%22elasticsearch%22[Maven
  21. Central].
  22. For example, you can define the latest version in your `pom.xml` file:
  23. ["source","xml",subs="attributes"]
  24. --------------------------------------------------
  25. <dependency>
  26. <groupId>org.elasticsearch.client</groupId>
  27. <artifactId>transport</artifactId>
  28. <version>{version}</version>
  29. </dependency>
  30. --------------------------------------------------
  31. [[java-transport-usage-maven-lucene]]
  32. === Lucene Snapshot repository
  33. The very first releases of any major version (like a beta), might have been built on top of a Lucene Snapshot version.
  34. In such a case you will be unable to resolve the Lucene dependencies of the client.
  35. For example, if you want to use the `6.0.0-beta1` version which depends on Lucene `7.0.0-snapshot-00142c9`, you must
  36. define the following repository.
  37. For Maven:
  38. ["source","xml",subs="attributes"]
  39. --------------------------------------------------
  40. <repository>
  41. <id>elastic-lucene-snapshots</id>
  42. <name>Elastic Lucene Snapshots</name>
  43. <url>http://s3.amazonaws.com/download.elasticsearch.org/lucenesnapshots/00142c9</url>
  44. <releases><enabled>true</enabled></releases>
  45. <snapshots><enabled>false</enabled></snapshots>
  46. </repository>
  47. --------------------------------------------------
  48. For Gradle:
  49. ["source","groovy",subs="attributes"]
  50. --------------------------------------------------
  51. maven {
  52. url 'http://s3.amazonaws.com/download.elasticsearch.org/lucenesnapshots/00142c9'
  53. }
  54. --------------------------------------------------
  55. === Log4j 2 Logger
  56. You need to also include Log4j 2 dependencies:
  57. ["source","xml",subs="attributes"]
  58. --------------------------------------------------
  59. <dependency>
  60. <groupId>org.apache.logging.log4j</groupId>
  61. <artifactId>log4j-core</artifactId>
  62. <version>2.9.1</version>
  63. </dependency>
  64. --------------------------------------------------
  65. And also provide a Log4j 2 configuration file in your classpath.
  66. For example, you can add in your `src/main/resources` project dir a `log4j2.properties` file like:
  67. ["source","properties",subs="attributes"]
  68. --------------------------------------------------
  69. appender.console.type = Console
  70. appender.console.name = console
  71. appender.console.layout.type = PatternLayout
  72. appender.console.layout.pattern = [%d{ISO8601}][%-5p][%-25c] %marker%m%n
  73. rootLogger.level = info
  74. rootLogger.appenderRef.console.ref = console
  75. --------------------------------------------------
  76. === Using another Logger
  77. If you want to use another logger than Log4j 2, you can use http://www.slf4j.org/[SLF4J] bridge to do that:
  78. ["source","xml",subs="attributes"]
  79. --------------------------------------------------
  80. <dependency>
  81. <groupId>org.apache.logging.log4j</groupId>
  82. <artifactId>log4j-to-slf4j</artifactId>
  83. <version>2.9.1</version>
  84. </dependency>
  85. <dependency>
  86. <groupId>org.slf4j</groupId>
  87. <artifactId>slf4j-api</artifactId>
  88. <version>1.7.24</version>
  89. </dependency>
  90. --------------------------------------------------
  91. http://www.slf4j.org/manual.html[This page] lists implementations you can use. Pick your favorite logger
  92. and add it as a dependency. As an example, we will use the `slf4j-simple` logger:
  93. ["source","xml",subs="attributes"]
  94. --------------------------------------------------
  95. <dependency>
  96. <groupId>org.slf4j</groupId>
  97. <artifactId>slf4j-simple</artifactId>
  98. <version>1.7.21</version>
  99. </dependency>
  100. --------------------------------------------------
  101. == Dealing with JAR dependency conflicts
  102. If you want to use Elasticsearch in your Java application, you may have to deal with version conflicts with third party
  103. dependencies like Guava and Joda. For instance, perhaps Elasticsearch uses Joda 2.8, while your code uses Joda 2.1.
  104. You have two choices:
  105. * The simplest solution is to upgrade. Newer module versions are likely to have fixed old bugs.
  106. The further behind you fall, the harder it will be to upgrade later. Of course, it is possible that you are using a
  107. third party dependency that in turn depends on an outdated version of a package, which prevents you from upgrading.
  108. * The second option is to relocate the troublesome dependencies and to shade them either with your own application
  109. or with Elasticsearch and any plugins needed by the Elasticsearch client.
  110. The https://www.elastic.co/blog/to-shade-or-not-to-shade["To shade or not to shade" blog post] describes
  111. all the steps for doing so.
  112. == Embedding jar with dependencies
  113. If you want to create a single jar containing your application and all dependencies, you should not
  114. use `maven-assembly-plugin` for that because it can not deal with `META-INF/services` structure which is
  115. required by Lucene jars.
  116. Instead, you can use `maven-shade-plugin` and configure it as follow:
  117. [source,xml]
  118. --------------------------------------------------
  119. <plugin>
  120. <groupId>org.apache.maven.plugins</groupId>
  121. <artifactId>maven-shade-plugin</artifactId>
  122. <version>2.4.1</version>
  123. <executions>
  124. <execution>
  125. <phase>package</phase>
  126. <goals><goal>shade</goal></goals>
  127. <configuration>
  128. <transformers>
  129. <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
  130. </transformers>
  131. </configuration>
  132. </execution>
  133. </executions>
  134. </plugin>
  135. --------------------------------------------------
  136. Note that if you have a `main` class you want to automatically call when running `java -jar yourjar.jar`, just add
  137. it to the `transformers`:
  138. [source,xml]
  139. --------------------------------------------------
  140. <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
  141. <mainClass>org.elasticsearch.demo.Generate</mainClass>
  142. </transformer>
  143. --------------------------------------------------
  144. :client-tests: {docdir}/../../server/src/test/java/org/elasticsearch/client/documentation
  145. include::client.asciidoc[]
  146. include::docs.asciidoc[]
  147. include::search.asciidoc[]
  148. include::aggs.asciidoc[]
  149. include::query-dsl.asciidoc[]
  150. include::admin/index.asciidoc[]