Elasticsearch's JAVAAPI Client

Source: Internet
Author: User

translated in original : Http://www.elasticsearch.org/guide/en/elasticsearch/client/java-api/current/client.html#node-client

Translation of Elasticsearch's JAVAAPI client

This section describes the Java APIs provided by Elasticsearch, and all elasticsearch operations are run using the client object.

The whole operation is essentially completely asynchronous (accepting a listener, or returning a future).

In addition, the operation of the client can be processed in batches.

Note: All the APIs is exposed through the Java API

Maven Repository

For example, you can add the following lines to the Pom.xml file

<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>${es.version}</version>
</dependency>

You can use the Java client in several ways:

For an existing cluster index, run the standard get,delete and search operations

Perform management tasks in one execution cluster

Execute the Elasticsearch embedded in your own application, or when you want to execute the unit and integrate the test, start the node

Getting a Elasticsearch client is easy. The following are the most frequently used methods:
1. Create a node ( acts as a node within a cluster)
2. Requesting the client from the node (fromyour embedded node )

There is also a way to connect to the cluster by creating a transportclient.

Important:

Note that we recommend that you use the same version number for both the client and the cluster. Because you may encounter some incompatibility issues when mixing different version numbers.

Node Client:

Instantiating a node-based client is the simplest way to allow customers to run operations on Elasticsearch.

Import Static org . Elasticsearch . node . Nodebuilder .*;  //on Startupnode node = Nodebuilder (). node (); Client client = Node.client ();//on Shutdownnode.close ();

When you start a node, it adds a elasticsearch cluster. You can set different clusters with simple settings cluster.name, or explicitly use the ClusterName method.

You can define cluster.name in the/src/main/resources/elasticsearch.yml file in your project. Just to Elasticsearch.yml is present in the classpath, it will be used when you start the node.

Yourclustername

Or in Java:

Node=  nodebuilder(). clustername ("Yourclustername"). node ();  Client client = Node.client ();

The advantage of using the client is that the operation requires the operation to be routed to the node itself, without running double hop. For example, the index operation will be active on the Shard itself.

When you start a node, the most important thing is to decide whether the data should be saved or not. In other words, the indices and shards are assigned to it. Very often we will need the client but the client, no shards assigned to them. This is the simple setting is Node.data set to False or Node.client configuration true (the Nodebuilder respectivehelper methods on it):

Import Static org . Elasticsearch . node . Nodebuilder .*;  //on Startupnode node = Nodebuilder (). Client (True). Node (); Client client = Node.client ();//on Shutdownnode.close ();

There is also a common way to start a node and use the client unit/integrated test. In this case, we are going to start a "local" node (discovery and Transpor with "local"). Again, this is just a simple setup when starting the node. Note that "local" here refers to the local JVM (well, the actual class loader), which means that two local servers are in the same JVM, forming a cluster.

Import Static org . Elasticsearch . node . Nodebuilder .*;  //on Startupnode node = Nodebuilder (). local (true). Node (); Client client = Node.client ();//on Shutdownnode.close ();

Transport Client

Transportclient uses transport module to connect elasticsearch cluster remotely, it does not increase the cluster, but only has one or more initial transport addresses and communicates with the cluster on every action Robin Fashion (although most actions will likely be "one-hop" operations).

On startupClient client = new Transportclient (). addtransportaddress (New Inetsockettransportaddres S ("Host1", 9300)). Addtransportaddress (New inetsockettransportaddress ("Host2", 9300));//on Shutdownclient.close ();

Note that you must set the cluster name, assuming you are using a different "elasticsearch":

Settings=immutablesettings. Settingsbuilder (). Put ("Cluster.name", "Myclustername"). Build (); Client client = new Transportclient (settings),//add transport addresses and do something with the client ...

or use elasticsearch.yml  

The customer is able to sniff the rest of the cluster and add the machine to the list to use. In such a case, please note that the IP address used will be the other node ("Publish"). To use it, set the Client.transport.sniff to true:

Settings=immutablesettings. Settingsbuilder (). Put ("Client.transport.sniff", true). Build (); Transportclient client = new transportclient (settings);

Other transport client levelsettings include:

Client.transport.ignore_cluster_name

Set to True to ignore cluster name validation of connected nodes. (Since 0.19.4)

Client.transport.ping_timeout

The time to wait for a pings response from a node. Defaults to 5s.

Client.transport.nodes_sampler_interval

How often to sample/ping the nodes listed and connected. Defaults to 5s.

Original: Http://www.elasticsearch.org/guide/en/elasticsearch/client/java-api/current/client.html#node-client

I hope that the translation is not misleading.


Elasticsearch's JAVAAPI Client

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.