Client
There are several places to use Java client
:
- Perform standard index, GET, delete, and search in a cluster that exists
- Perform administrative tasks in the cluster
- Start all nodes when you want to run Elasticsearch nested within your application or when you want to run unit tests or set tests
Getting a client is very easy, and the most common steps are as follows:
- Create a nested node that acts as a node of the cluster
- Request a client from this nested node
Another way is to create one TransportClient
to connect to the cluster.
Important: The same version is recommended for both the client and the cluster side, and some incompatibilities may occur if the version is different.
Node Client
Instantiating a node's client is the simplest way to get a client. This client can perform elasticsearch related operations.
import static org.elasticsearch.node.NodeBuilder.*;// on startupNode node = nodeBuilder().node();Client client = node.client();// on shutdownnode.close();
When you start one node
, it joins the Elasticsearch cluster. You can cluster.name
have different clusters by simple setup or by explicitly using clusterName
methods.
You can define it in the file of your project /src/main/resources/elasticsearch.yml
cluster.name
. Just under the elasticsearch.yml
classpath directory, you can use it to start your node.
cluster.name: yourclustername
Or through Java:
Node node = nodeBuilder().clusterName("yourclustername").node();Client client = node.client();
The advantage of using the client is that the operation can be automatically routed to the node where these operations are executed, without the need to perform a double hop. For example, the index operation will be executed on the Shard where the operation eventually exists.
When you start a node, the most important decision is whether it will retain the data. In most cases, we just need to use clients instead of shards to allocate them. This can be done simply by setting it to node.data
false or by setting it to node.client
true.
import static org.elasticsearch.node.NodeBuilder.*;// on startupNode node = nodeBuilder().client(true).node();Client client = node.client();// on shutdownnode.close();
Another common use is to start node and then use the Client for unit/integration testing. In this case, we should start a "local" node. This is just a simple setting to start node. Note that "local" means running on the local JVM. Two local services running on the same JVM can discover each other and form a cluster.
import static org.elasticsearch.node.NodeBuilder.*;// on startupNode node = nodeBuilder().local(true).node();Client client = node.client();// on shutdownnode.close();
Transfer (transport) client
TransportClient
Connect a elasticsearch cluster remotely using the transport module. It does not join the cluster, it simply obtains one or more initialized transport addresses and communicates with them in a polling manner.
// on startupClient client = new TransportClient() .addTransportAddress(new InetSocketTransportAddress("host1", 9300)) .addTransportAddress(new InetSocketTransportAddress("host2", 9300));// on shutdownclient.close();
Note that if you have a elasticsearch
cluster different from the cluster, you can set the machine name.
Settings settings = ImmutableSettings.settingsBuilder() .put("cluster.name", "myClusterName").build();Client client = new TransportClient(settings);//Add transport addresses and do something with the client...
You can also use the elasticsearch.yml
file to set.
This client can sniff other parts of the cluster and add them to the list of machines. To turn on this feature, set client.transport.sniff
to True.
Settings settings = ImmutableSettings.settingsBuilder() .put("client.transport.sniff", true).build();TransportClient client = new TransportClient(settings);
The other transport client settings are as follows:
Parameter |
Description |
Client.transport.ignore_cluster_name |
True: Ignore cluster name validation for connection nodes |
Client.transport.ping_timeout |
Ping the response time of a node, which is 5s by default |
Client.transport.nodes_sampler_interval |
The time interval of the sample/ping node, which is 5s by default |
Elasticsearch Chinese API (ii)