client.asciidoc 7.1 KB

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