Distributed search of elasticsearch Java APIs (1) -- interaction with clusters

Source: Internet
Author: User

This is the first tutorial on elasticsearch Java API. I will write some es experiences one after another.

Elasticsearch (elasticsearch for short) clusters can be connected in two ways. The first is to create an embedded elasticsearch node in your program to make it part of the elasticsearch cluster, then, the node is used to communicate with the es cluster. The second method is to use the transportclient interface to communicate with the es cluster.

Node Mode

You can create an embedded node as follows:

Import static Org. elasticsearch. node. nodebuilder. *; // Start Node node = nodebuilder (). node (); Client client = node. client (); // close the node. close ();

When you start a node, it automatically joins the elasticsearch cluster in the same network segment. The premise is that the elasticsearch cluster name (cluster. Name) parameter must be set to the same.

By default, a node is started, and the es cluster will automatically allocate some index shards to it. If you want this node to be used only as a client without storing data, you can set the node. set Data to false or node. set the client to true. The following is an example:

Node node = nodeBuilder().clusterName(clusterName).client(true).node(); 

Another case is that you do not want to add a node to a cluster. Instead, you only want to use it for unit testing, you need to start a local es, here, "local" refers to running at the JVM level, that is, two different es nodes will form a cluster when running in the same JVM. It needs to set the local parameter of the node to true. The following is an example:

Node node = nodeBuilder().local(true).node(); 

Transportclient Mode
Through the transportclient interface, we can communicate with the es cluster without starting the node. It needs to specify the IP address and port of one or more machines in the es cluster. The example is as follows:

Client client = new TransportClient()         .addTransportAddress(new InetSocketTransportAddress("host1", 9300))         .addTransportAddress(new InetSocketTransportAddress("host2", 9300));  client.close();


If you need to change the cluster name (elasticsearch by default), you need to set it as follows:

Settings settings = ImmutableSettings.settingsBuilder()                 .put("cluster.name", "myClusterName").build(); Client client = new TransportClient(settings);

You can set the client. transport. if the value of sniff is true, the client can sniff the status of the entire cluster and add the IP addresses of other machines in the cluster to the client, in this way, you generally do not need to manually set the IP addresses of all clusters in the cluster to the connected client. It will automatically help you add and automatically discover the machines that are newly added to the cluster. The code example is as follows:

Settings settings = ImmutableSettings.settingsBuilder()                 .put("client.transport.sniff", true).build(); TransportClient client = new TransportClient(settings);

References: http://www.searchtech.pro/articles/2013/02/18/1361194853254.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.