index.asciidoc 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. [[java-api]]
  2. = Java API
  3. :ref: http://www.elastic.co/guide/en/elasticsearch/reference/master
  4. :version: 6.0.0-alpha1
  5. [preface]
  6. == Preface
  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. == Maven Repository
  17. Elasticsearch is hosted on
  18. http://search.maven.org/#search%7Cga%7C1%7Ca%3A%22elasticsearch%22[Maven
  19. Central].
  20. For example, you can define the latest version in your `pom.xml` file:
  21. ["source","xml",subs="attributes"]
  22. --------------------------------------------------
  23. <dependency>
  24. <groupId>org.elasticsearch.client</groupId>
  25. <artifactId>transport</artifactId>
  26. <version>{version}</version>
  27. </dependency>
  28. --------------------------------------------------
  29. === Log4j 2 Logger
  30. You need to also include Log4j 2 dependencies:
  31. ["source","xml",subs="attributes"]
  32. --------------------------------------------------
  33. <dependency>
  34. <groupId>org.apache.logging.log4j</groupId>
  35. <artifactId>log4j-api</artifactId>
  36. <version>2.7</version>
  37. </dependency>
  38. <dependency>
  39. <groupId>org.apache.logging.log4j</groupId>
  40. <artifactId>log4j-core</artifactId>
  41. <version>2.7</version>
  42. </dependency>
  43. --------------------------------------------------
  44. And also provide a Log4j 2 configuration file in your classpath.
  45. For example, you can add in your `src/main/resources` project dir a `log4j2.properties` file like:
  46. ["source","properties",subs="attributes"]
  47. --------------------------------------------------
  48. appender.console.type = Console
  49. appender.console.name = console
  50. appender.console.layout.type = PatternLayout
  51. appender.console.layout.pattern = [%d{ISO8601}][%-5p][%-25c] %marker%m%n
  52. rootLogger.level = info
  53. rootLogger.appenderRef.console.ref = console
  54. --------------------------------------------------
  55. === Using another Logger
  56. If you want to use another logger than Log4j 2, you can use http://www.slf4j.org/[SLF4J] bridge to do that:
  57. ["source","xml",subs="attributes"]
  58. --------------------------------------------------
  59. <dependency>
  60. <groupId>org.apache.logging.log4j</groupId>
  61. <artifactId>log4j-to-slf4j</artifactId>
  62. <version>2.7</version>
  63. </dependency>
  64. <dependency>
  65. <groupId>org.slf4j</groupId>
  66. <artifactId>slf4j-api</artifactId>
  67. <version>1.7.21</version>
  68. </dependency>
  69. --------------------------------------------------
  70. http://www.slf4j.org/manual.html[This page] lists implementations you can use. Pick your favorite logger
  71. and add it as a dependency. As an example, we will use the `slf4j-simple` logger:
  72. ["source","xml",subs="attributes"]
  73. --------------------------------------------------
  74. <dependency>
  75. <groupId>org.slf4j</groupId>
  76. <artifactId>slf4j-simple</artifactId>
  77. <version>1.7.21</version>
  78. </dependency>
  79. --------------------------------------------------
  80. == Dealing with JAR dependency conflicts
  81. If you want to use Elasticsearch in your Java application, you may have to deal with version conflicts with third party
  82. dependencies like Guava and Joda. For instance, perhaps Elasticsearch uses Joda 2.8, while your code uses Joda 2.1.
  83. You have two choices:
  84. * The simplest solution is to upgrade. Newer module versions are likely to have fixed old bugs.
  85. The further behind you fall, the harder it will be to upgrade later. Of course, it is possible that you are using a
  86. third party dependency that in turn depends on an outdated version of a package, which prevents you from upgrading.
  87. * The second option is to relocate the troublesome dependencies and to shade them either with your own application
  88. or with Elasticsearch and any plugins needed by the Elasticsearch client.
  89. The https://www.elastic.co/blog/to-shade-or-not-to-shade["To shade or not to shade" blog post] describes
  90. all the steps for doing so.
  91. == Embedding jar with dependencies
  92. If you want to create a single jar containing your application and all dependencies, you should not
  93. use `maven-assembly-plugin` for that because it can not deal with `META-INF/services` structure which is
  94. required by Lucene jars.
  95. Instead, you can use `maven-shade-plugin` and configure it as follow:
  96. [source,xml]
  97. --------------------------------------------------
  98. <plugin>
  99. <groupId>org.apache.maven.plugins</groupId>
  100. <artifactId>maven-shade-plugin</artifactId>
  101. <version>2.4.1</version>
  102. <executions>
  103. <execution>
  104. <phase>package</phase>
  105. <goals><goal>shade</goal></goals>
  106. <configuration>
  107. <transformers>
  108. <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
  109. </transformers>
  110. </configuration>
  111. </execution>
  112. </executions>
  113. </plugin>
  114. --------------------------------------------------
  115. Note that if you have a `main` class you want to automatically call when running `java -jar yourjar.jar`, just add
  116. it to the `transformers`:
  117. [source,xml]
  118. --------------------------------------------------
  119. <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
  120. <mainClass>org.elasticsearch.demo.Generate</mainClass>
  121. </transformer>
  122. --------------------------------------------------
  123. == Deploying in JBoss EAP6 module
  124. Elasticsearch and Lucene classes need to be in the same JBoss module.
  125. You should define a `module.xml` file like this:
  126. [source,xml]
  127. --------------------------------------------------
  128. <?xml version="1.0" encoding="UTF-8"?>
  129. <module xmlns="urn:jboss:module:1.1" name="org.elasticsearch">
  130. <resources>
  131. <!-- Elasticsearch -->
  132. <resource-root path="elasticsearch-2.0.0.jar"/>
  133. <!-- Lucene -->
  134. <resource-root path="lucene-core-5.1.0.jar"/>
  135. <resource-root path="lucene-analyzers-common-5.1.0.jar"/>
  136. <resource-root path="lucene-queries-5.1.0.jar"/>
  137. <resource-root path="lucene-memory-5.1.0.jar"/>
  138. <resource-root path="lucene-highlighter-5.1.0.jar"/>
  139. <resource-root path="lucene-queryparser-5.1.0.jar"/>
  140. <resource-root path="lucene-sandbox-5.1.0.jar"/>
  141. <resource-root path="lucene-suggest-5.1.0.jar"/>
  142. <resource-root path="lucene-misc-5.1.0.jar"/>
  143. <resource-root path="lucene-join-5.1.0.jar"/>
  144. <resource-root path="lucene-grouping-5.1.0.jar"/>
  145. <resource-root path="lucene-spatial-5.1.0.jar"/>
  146. <resource-root path="lucene-expressions-5.1.0.jar"/>
  147. <!-- Insert other resources here -->
  148. </resources>
  149. <dependencies>
  150. <module name="sun.jdk" export="true" >
  151. <imports>
  152. <include path="sun/misc/Unsafe" />
  153. </imports>
  154. </module>
  155. <module name="org.apache.log4j"/>
  156. <module name="org.apache.commons.logging"/>
  157. <module name="javax.api"/>
  158. </dependencies>
  159. </module>
  160. --------------------------------------------------
  161. include::client.asciidoc[]
  162. include::docs.asciidoc[]
  163. include::search.asciidoc[]
  164. include::aggs.asciidoc[]
  165. include::query-dsl.asciidoc[]
  166. include::indexed-scripts.asciidoc[]
  167. include::admin/index.asciidoc[]