client.asciidoc 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. [[client]]
  2. == Client
  3. You can use the *Java client* in multiple ways:
  4. * Perform standard <<java-docs-index,index>>, <<java-docs-get,get>>,
  5. <<java-docs-delete,delete>> and <<java-search,search>> operations on an
  6. existing cluster
  7. * Perform administrative tasks on a running cluster
  8. * Start full nodes when you want to run Elasticsearch embedded in your
  9. own application or when you want to launch unit or integration tests
  10. Obtaining an elasticsearch `Client` is simple. The most common way to
  11. get a client is by:
  12. 1. Creating an embedded <<node-client,`Node`>> that acts as a node
  13. within a cluster.
  14. 2. Requesting a `Client` from your embedded `Node`.
  15. Another manner is by creating a <<transport-client,`TransportClient`>>
  16. that connects to a cluster.
  17. *Important:*
  18. ______________________________________________________________________________________________________________________________________________________________
  19. Please note that you are encouraged to use the same version on client
  20. and cluster sides. You may hit some incompatibility issues when mixing
  21. major versions.
  22. ______________________________________________________________________________________________________________________________________________________________
  23. [[node-client]]
  24. === Node Client
  25. Instantiating a node based client is the simplest way to get a `Client`
  26. that can execute operations against elasticsearch.
  27. [source,java]
  28. --------------------------------------------------
  29. import static org.elasticsearch.node.NodeBuilder.*;
  30. // on startup
  31. Node node = nodeBuilder().node();
  32. Client client = node.client();
  33. // on shutdown
  34. node.close();
  35. --------------------------------------------------
  36. When you start a `Node`, it joins an elasticsearch cluster. You can have
  37. different clusters by simply setting the `cluster.name` setting, or
  38. explicitly using the `clusterName` method on the builder.
  39. You can define `cluster.name` in the `/src/main/resources/elasticsearch.yml`
  40. file in your project. As long as `elasticsearch.yml` is present in the
  41. classpath, it will be used when you start your node.
  42. [source,yaml]
  43. --------------------------------------------------
  44. cluster.name: yourclustername
  45. --------------------------------------------------
  46. Or in Java:
  47. [source,java]
  48. --------------------------------------------------
  49. Node node = nodeBuilder().clusterName("yourclustername").node();
  50. Client client = node.client();
  51. --------------------------------------------------
  52. The benefit of using the `Client` is the fact that operations are
  53. automatically routed to the node(s) the operations need to be executed
  54. on, without performing a "double hop". For example, the index operation
  55. will automatically be executed on the shard that it will end up existing
  56. at.
  57. When you start a `Node`, the most important decision is whether it
  58. should hold data or not. In other words, should indices and shards be
  59. allocated to it. Many times we would like to have the clients just be
  60. clients, without shards being allocated to them. This is simple to
  61. configure by setting either `node.data` setting to `false` or
  62. `node.client` to `true` (the `NodeBuilder` respective helper methods on
  63. it):
  64. [source,java]
  65. --------------------------------------------------
  66. import static org.elasticsearch.node.NodeBuilder.*;
  67. // on startup
  68. // Embedded node clients behave just like standalone nodes,
  69. // which means that they will leave the HTTP port open!
  70. Node node =
  71. nodeBuilder()
  72. .settings(Settings.settingsBuilder().put("http.enabled", false))
  73. .client(true)
  74. .node();
  75. Client client = node.client();
  76. // on shutdown
  77. node.close();
  78. --------------------------------------------------
  79. Another common usage is to start the `Node` and use the `Client` in
  80. unit/integration tests. In such a case, we would like to start a "local"
  81. `Node` (with a "local" discovery and transport). Again, this is just a
  82. matter of a simple setting when starting the `Node`. Note, "local" here
  83. means local on the JVM (well, actually class loader) level, meaning that
  84. two *local* servers started within the same JVM will discover themselves
  85. and form a cluster.
  86. [source,java]
  87. --------------------------------------------------
  88. import static org.elasticsearch.node.NodeBuilder.*;
  89. // on startup
  90. Node node = nodeBuilder().local(true).node();
  91. Client client = node.client();
  92. // on shutdown
  93. node.close();
  94. --------------------------------------------------
  95. [[node-client-downsides]]
  96. ==== Node Client Downsides
  97. Embedding a node client into your application is the easiest way to connect
  98. to an Elasticsearch cluster, but it carries some downsides.
  99. - Frequently starting and stopping one or more node clients creates unnecessary
  100. noise across the cluster.
  101. - Embedded node client will respond to outside requests, just like any other client.
  102. ** You almost always want to disable HTTP for an _embedded_ node client.
  103. [[transport-client]]
  104. === Transport Client
  105. The `TransportClient` connects remotely to an Elasticsearch cluster
  106. using the transport module. It does not join the cluster, but simply
  107. gets one or more initial transport addresses and communicates with them
  108. in round robin fashion on each action (though most actions will probably
  109. be "two hop" operations).
  110. [source,java]
  111. --------------------------------------------------
  112. // on startup
  113. Client client = TransportClient.builder().build()
  114. .addTransportAddress(new InetSocketTransportAddress("host1", 9300))
  115. .addTransportAddress(new InetSocketTransportAddress("host2", 9300));
  116. // on shutdown
  117. client.close();
  118. --------------------------------------------------
  119. Note that you have to set the cluster name if you use one different than
  120. "elasticsearch":
  121. [source,java]
  122. --------------------------------------------------
  123. Settings settings = Settings.settingsBuilder()
  124. .put("cluster.name", "myClusterName").build();
  125. Client client = TransportClient.builder().settings(settings).build();
  126. //Add transport addresses and do something with the client...
  127. --------------------------------------------------
  128. Or using `elasticsearch.yml` file as shown in <<node-client>>
  129. The client allows sniffing the rest of the cluster, which adds data nodes
  130. into its list of machines to use. In this case, note that the IP addresses
  131. used will be the ones that the other nodes were started with (the
  132. "publish" address). In order to enable it, set the
  133. `client.transport.sniff` to `true`:
  134. [source,java]
  135. --------------------------------------------------
  136. Settings settings = Settings.settingsBuilder()
  137. .put("client.transport.sniff", true).build();
  138. TransportClient client = TransportClient.builder().settings(settings).build();
  139. --------------------------------------------------
  140. Other transport client level settings include:
  141. [cols="<,<",options="header",]
  142. |=======================================================================
  143. |Parameter |Description
  144. |`client.transport.ignore_cluster_name` |Set to `true` to ignore cluster
  145. name validation of connected nodes. (since 0.19.4)
  146. |`client.transport.ping_timeout` |The time to wait for a ping response
  147. from a node. Defaults to `5s`.
  148. |`client.transport.nodes_sampler_interval` |How often to sample / ping
  149. the nodes listed and connected. Defaults to `5s`.
  150. |=======================================================================