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 performed using the client object.

All operations are essentially completely asynchronous (accept a listener, or return a future).

In addition, the operations on 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, perform standard get,delete and search operations

Perform management tasks on one run cluster

Run the Elasticsearch embedded in your own application, or when you want to perform unit and integration testing, start the node

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

Another way is to connect to the cluster by creating a transportclient.

Important:

Note that we recommend that you use the same version of clients and clusters. Because you may encounter some incompatible issues when mixing different versions.

Node Client:

Instantiating a node-based client is the simplest way to allow customers to perform 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 joins a elasticsearch cluster. You can set different clusters by simply setting cluster.name, or explicitly use the ClusterName method.

You can define cluster.name in the/src/main/resources/elasticsearch.yml file in your project. As long as 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 is automatically routed to the node and the operation needs to be performed without executing "double hop". For example, the index operation will be performed automatically on Shard.

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. Many times we will need the client to be just the client and 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 ();

Another common way to start a node and use client unit/integration testing. 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 join the cluster, but only has one or more initial transport addresses and communicates with the cluster on each action Robin fashion (although most actions will probably be " The operation).

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

Please note that you must set the cluster name if you use 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  

Customers can sniff the rest of the cluster and add them to the list of machines to use. In this case, be aware that the IP address used will be the start of 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.