client.asciidoc 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. [[client]]
  2. == Client
  3. You can use the *Java client* in multiple ways:
  4. * Perform standard <<index_,index>>, <<get,get>>,
  5. <<delete,delete>> and <<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 link:#nodeclient[`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 link:#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. dir 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,java]
  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. Node node = nodeBuilder().client(true).node();
  69. Client client = node.client();
  70. // on shutdown
  71. node.close();
  72. --------------------------------------------------
  73. Another common usage is to start the `Node` and use the `Client` in
  74. unit/integration tests. In such a case, we would like to start a "local"
  75. `Node` (with a "local" discovery and transport). Again, this is just a
  76. matter of a simple setting when starting the `Node`. Note, "local" here
  77. means local on the JVM (well, actually class loader) level, meaning that
  78. two *local* servers started within the same JVM will discover themselves
  79. and form a cluster.
  80. [source,java]
  81. --------------------------------------------------
  82. import static org.elasticsearch.node.NodeBuilder.*;
  83. // on startup
  84. Node node = nodeBuilder().local(true).node();
  85. Client client = node.client();
  86. // on shutdown
  87. node.close();
  88. --------------------------------------------------
  89. [[transport-client]]
  90. === Transport Client
  91. The `TransportClient` connects remotely to an elasticsearch cluster
  92. using the transport module. It does not join the cluster, but simply
  93. gets one or more initial transport addresses and communicates with them
  94. in round robin fashion on each action (though most actions will probably
  95. be "two hop" operations).
  96. [source,java]
  97. --------------------------------------------------
  98. // on startup
  99. Client client = new TransportClient()
  100. .addTransportAddress(new InetSocketTransportAddress("host1", 9300))
  101. .addTransportAddress(new InetSocketTransportAddress("host2", 9300));
  102. // on shutdown
  103. client.close();
  104. --------------------------------------------------
  105. Note that you have to set the cluster name if you use one different than
  106. "elasticsearch":
  107. [source,java]
  108. --------------------------------------------------
  109. Settings settings = ImmutableSettings.settingsBuilder()
  110. .put("cluster.name", "myClusterName").build();
  111. Client client = new TransportClient(settings);
  112. //Add transport addresses and do something with the client...
  113. --------------------------------------------------
  114. Or using `elasticsearch.yml` file as shown in the link:#nodeclient[Node
  115. Client section]
  116. The client allows to sniff the rest of the cluster, and add those into
  117. its list of machines to use. In this case, note that the IP addresses
  118. used will be the ones that the other nodes were started with (the
  119. "publish" address). In order to enable it, set the
  120. `client.transport.sniff` to `true`:
  121. [source,java]
  122. --------------------------------------------------
  123. Settings settings = ImmutableSettings.settingsBuilder()
  124. .put("client.transport.sniff", true).build();
  125. TransportClient client = new TransportClient(settings);
  126. --------------------------------------------------
  127. Other transport client level settings include:
  128. [cols="<,<",options="header",]
  129. |=======================================================================
  130. |Parameter |Description
  131. |`client.transport.ignore_cluster_name` |Set to `true` to ignore cluster
  132. name validation of connected nodes. (since 0.19.4)
  133. |`client.transport.ping_timeout` |The time to wait for a ping response
  134. from a node. Defaults to `5s`.
  135. |`client.transport.nodes_sampler_interval` |How often to sample / ping
  136. the nodes listed and connected. Defaults to `5s`.
  137. |=======================================================================