Working directly with Elasticsearch's diverse APIs can be complex and inefficient. CSS simplifies this by providing a High Level REST Client, offering a more efficient way for querying and managing your data. The High Level REST Client encapsulates Elasticsearch APIs. You only need to construct the required request structures to access an Elasticsearch cluster. This simplifies the process of working with Elasticsearch clusters, as well as the development process. For details about how to use the REST Client, see Java High Level REST Client.
If your High Level REST Client version is later than the Elasticsearch cluster version and there are incompatibility issues with some requests, you can use RestHighLevelClient.getLowLevelClient() to obtain the Low Level REST Client and customize Elasticsearch requests. For details, see Connecting to a Cluster Using the Low Level REST Client.
Import Java dependencies on the server that runs the Java code using either of the following ways:
Replace 7.10.2 with the actual Java client version.
<dependency><groupId>org.elasticsearch.client</groupId><artifactId>elasticsearch-rest-high-level-client</artifactId><version>7.10.2</version></dependency><dependency><groupId>org.elasticsearch</groupId><artifactId>elasticsearch</artifactId><version>7.10.2</version></dependency>
Replace 7.10.2 with the actual Java client version.
compile group: 'org.elasticsearch.client', name: 'elasticsearch-rest-high-level-client', version: '7.10.2'
The sample code varies depending on the security mode settings of the target Elasticsearch cluster. Select the right reference document based on your service scenario.
Elasticsearch Cluster Security-Mode Settings | Whether to Load a Security Certificate | Details |
|---|---|---|
Non-security mode | - | Connecting to a Non-Security Mode Cluster Using the High Level REST Client |
Security mode + HTTP Security mode + HTTPS | No | Connecting to a Security-Mode Cluster Using the High Level REST Client (Without a Certificate) |
Security mode + HTTPS | Yes | Connecting to a Security-Mode Cluster Using the High Level REST Client (With a Certificate) |
Use the High Level REST Client to connect to an Elasticsearch cluster for which the security mode is disabled, and query whether the test index exists. The sample code is as follows:
1234567891011121314151617181920212223242526272829303132import org.apache.http.HttpHost;import org.elasticsearch.client.RequestOptions;import org.elasticsearch.client.RestClient;import org.elasticsearch.client.RestClientBuilder;import org.elasticsearch.client.RestHighLevelClient;import org.elasticsearch.client.indices.GetIndexRequest;import java.io.IOException;import java.util.Arrays;import java.util.List;/*** Use the High Level REST Client to connect to a non-security mode cluster.*/public class Main {public static void main(String[] args) throws IOException {List<String> host = Arrays.asList("{Cluster access address}");RestClientBuilder builder = RestClient.builder(constructHttpHosts(host, 9200, "http"));final RestHighLevelClient client = new RestHighLevelClient(builder);GetIndexRequest indexRequest = new GetIndexRequest("test");boolean exists = client.indices().exists(indexRequest, RequestOptions.DEFAULT);System.out.println(exists);client.close();}/*** Use the constructHttpHosts function to convert the node IP address list of the host cluster.*/public static HttpHost[] constructHttpHosts(List<String> host, int port, String protocol) {return host.stream().map(p -> new HttpHost(p, port, protocol)).toArray(HttpHost[]::new);}}
This piece of code checks whether the test index exists in the cluster. If true (the index exists) or false (the index does not exist) is returned, it indicates that the cluster is connected.
Use the High Level REST Client to connect to a security-mode Elasticsearch cluster (HTTP or HTTPS) without loading a security certificate, and query whether the test index exists. The sample code is as follows:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115import org.apache.http.HttpHost;import org.apache.http.auth.AuthScope;import org.apache.http.auth.UsernamePasswordCredentials;import org.apache.http.client.CredentialsProvider;import org.apache.http.conn.ssl.NoopHostnameVerifier;import org.apache.http.impl.client.BasicCredentialsProvider;import org.apache.http.nio.conn.ssl.SSLIOSessionStrategy;import org.apache.logging.log4j.LogManager;import org.apache.logging.log4j.Logger;import org.elasticsearch.action.admin.cluster.health.ClusterHealthRequest;import org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse;import org.elasticsearch.client.RequestOptions;import org.elasticsearch.client.RestClient;import org.elasticsearch.client.RestClientBuilder;import org.elasticsearch.client.RestHighLevelClient;import org.elasticsearch.client.indices.GetIndexRequest;import java.io.IOException;import java.security.KeyManagementException;import java.security.NoSuchAlgorithmException;import java.security.SecureRandom;import java.security.cert.CertificateException;import java.security.cert.X509Certificate;import java.util.Arrays;import java.util.List;import java.util.concurrent.TimeUnit;import javax.net.ssl.SSLContext;import javax.net.ssl.TrustManager;import javax.net.ssl.X509TrustManager;/*** Use the High Level REST Client to connect to a security-mode cluster (without a certificate).*/public class Main {private static final Logger logger = LogManager.getLogger(Main.class);/*** Create a class for the client. Define the create function.*/public static RestHighLevelClient create(List<String> host, int port, String protocol, int connectTimeout,int connectionRequestTimeout, int socketTimeout, String username, String password) throws IOException {RestClientBuilder builder = RestClient.builder(constructHttpHosts(host, port, protocol)).setRequestConfigCallback(requestConfig -> requestConfig.setConnectTimeout(connectTimeout).setConnectionRequestTimeout(connectionRequestTimeout).setSocketTimeout(socketTimeout)).setHttpClientConfigCallback(httpClientBuilder -> {// enable user authenticationfinal CredentialsProvider credentialsProvider = new BasicCredentialsProvider();credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);// set keepalivehttpClientBuilder.setKeepAliveStrategy(((httpResponse, httpContext) -> TimeUnit.MINUTES.toMinutes(10)));// enable SSL / TLSSSLContext sc = null;try {sc = SSLContext.getInstance("SSL");sc.init(null, trustAllCerts, new SecureRandom());} catch (KeyManagementException | NoSuchAlgorithmException e) {e.printStackTrace();}SSLIOSessionStrategy sslStrategy = new SSLIOSessionStrategy(sc, new NoopHostnameVerifier());httpClientBuilder.setSSLStrategy(sslStrategy);return httpClientBuilder;});final RestHighLevelClient client = new RestHighLevelClient(builder);logger.info("es rest client build success {} ", client);ClusterHealthRequest request = new ClusterHealthRequest();ClusterHealthResponse response = client.cluster().health(request, RequestOptions.DEFAULT);System.out.println("es rest client health response {} " + response);return client;}/*** Use the constructHttpHosts function to convert the node IP address list of the host cluster.*/public static HttpHost[] constructHttpHosts(List<String> host, int port, String protocol) {return host.stream().map(p -> new HttpHost(p, port, protocol)).toArray(HttpHost[]::new);}/*** Configure trustAllCerts to ignore the certificate configuration.*/public static TrustManager[] trustAllCerts = new TrustManager[] {new X509TrustManager() {@Overridepublic void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {}@Overridepublic void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {}@Overridepublic X509Certificate[] getAcceptedIssuers() {return null;}}};/*** The following is an example of the main function. Call the create function to create a client and check whether the test index exists.*/public static void main(String[] args) throws IOException {RestHighLevelClient client = create(Arrays.asList("{host}"), 9200, "https", 1000, 1000, 1000, "username", "password");GetIndexRequest indexRequest = new GetIndexRequest("test");boolean exists = client.indices().exists(indexRequest, RequestOptions.DEFAULT);System.out.println(exists);client.close();}}
Parameter | Description |
|---|---|
host | IP address for accessing the cluster. If there are multiple IP addresses, separate them using a comma (,). |
port | Access port of the cluster. The default value is 9200. |
protocol | Connection protocol, which can be http or https. |
connectTimeout | Socket connection timeout (in ms). |
connectionRequestTimeout | Socket connection request timeout (in ms). |
socketTimeout | Socket request timeout (in ms). |
username | Username for accessing the cluster. |
password | Password of the user. |
This piece of code checks whether the test index exists in the cluster. If true (the index exists) or false (the index does not exist) is returned, it indicates that the cluster is connected.
Use the High Level REST Client to connect to a security-mode Elasticsearch cluster that uses HTTPS with a security certificate loaded, and query whether the test index exists. The sample code is as follows:
For how to obtain and upload a security certificate, see Obtaining and Uploading a Security Certificate.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137import org.apache.http.HttpHost;import org.apache.http.auth.AuthScope;import org.apache.http.auth.UsernamePasswordCredentials;import org.apache.http.client.CredentialsProvider;import org.apache.http.conn.ssl.NoopHostnameVerifier;import org.apache.http.impl.client.BasicCredentialsProvider;import org.apache.http.nio.conn.ssl.SSLIOSessionStrategy;import org.apache.logging.log4j.LogManager;import org.apache.logging.log4j.Logger;import org.elasticsearch.action.admin.cluster.health.ClusterHealthRequest;import org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse;import org.elasticsearch.client.RequestOptions;import org.elasticsearch.client.RestClient;import org.elasticsearch.client.RestClientBuilder;import org.elasticsearch.client.RestHighLevelClient;import org.elasticsearch.client.indices.GetIndexRequest;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStream;import java.security.KeyStore;import java.security.SecureRandom;import java.security.cert.CertificateException;import java.security.cert.X509Certificate;import java.util.Arrays;import java.util.List;import java.util.concurrent.TimeUnit;import javax.net.ssl.SSLContext;import javax.net.ssl.TrustManager;import javax.net.ssl.TrustManagerFactory;import javax.net.ssl.X509TrustManager;/*** Use the Hive Level REST Client to connect to a security-mode cluster (with an HTTPS certificate).*/public class Main {public static RestHighLevelClient create(List<String> host, int port, String protocol, int connectTimeout,int connectionRequestTimeout, int socketTimeout, String username, String password, String certFilePath,String certPassword) throws IOException {RestClientBuilder builder = RestClient.builder(constructHttpHosts(host, port, protocol)).setRequestConfigCallback(requestConfig -> requestConfig.setConnectTimeout(connectTimeout).setConnectionRequestTimeout(connectionRequestTimeout).setSocketTimeout(socketTimeout)).setHttpClientConfigCallback(httpClientBuilder -> {// enable user authenticationfinal CredentialsProvider credentialsProvider = new BasicCredentialsProvider();credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);// set keepalivehttpClientBuilder.setKeepAliveStrategy(((httpResponse, httpContext) -> TimeUnit.MINUTES.toMinutes(10)));// enable SSL / TLSSSLContext sc = null;try {TrustManager[] tm = {new MyX509TrustManager(certFilePath, certPassword)};sc = SSLContext.getInstance("SSL", "SunJSSE");//You can also use SSLContext sslContext = SSLContext.getInstance("TLSv1.2");sc.init(null, tm, new SecureRandom());} catch (Exception e) {e.printStackTrace();}SSLIOSessionStrategy sslStrategy = new SSLIOSessionStrategy(sc, new NoopHostnameVerifier());httpClientBuilder.setSSLStrategy(sslStrategy);return httpClientBuilder;});final RestHighLevelClient client = new RestHighLevelClient(builder);logger.info("es rest client build success {} ", client);ClusterHealthRequest request = new ClusterHealthRequest();ClusterHealthResponse response = client.cluster().health(request, RequestOptions.DEFAULT);logger.info("es rest client health response {} ", response);return client;}/*** Use the constructHttpHosts function to convert the node IP address list of the host cluster.*/public static HttpHost[] constructHttpHosts(List<String> host, int port, String protocol) {return host.stream().map(p -> new HttpHost(p, port, protocol)).toArray(HttpHost[]::new);}private static final Logger logger = LogManager.getLogger(Main.class);public static class MyX509TrustManager implements X509TrustManager {X509TrustManager sunJSSEX509TrustManager;MyX509TrustManager(String certFilePath, String certPassword) throws Exception {File file = new File(certFilePath);if (!file.isFile()) {throw new Exception("Wrong Certification Path");}System.out.println("Loading KeyStore " + file + "...");InputStream in = new FileInputStream(file);KeyStore ks = KeyStore.getInstance("JKS");ks.load(in, certPassword.toCharArray());TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509", "SunJSSE");tmf.init(ks);TrustManager[] tms = tmf.getTrustManagers();for (TrustManager tm : tms) {if (tm instanceof X509TrustManager) {sunJSSEX509TrustManager = (X509TrustManager) tm;return;}}throw new Exception("Couldn't initialize");}@Overridepublic void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {}@Overridepublic void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {}@Overridepublic X509Certificate[] getAcceptedIssuers() {return new X509Certificate[0];}}/*** The following is an example of the main function. Call the create function to create a client and check whether the test index exists.*/public static void main(String[] args) throws IOException {RestHighLevelClient client = create(Arrays.asList({host}), 9200, "https", 1000, 1000, 1000, "username", "password", "certFilePath", "certPassword");GetIndexRequest indexRequest = new GetIndexRequest("test");boolean exists = client.indices().exists(indexRequest, RequestOptions.DEFAULT);System.out.println(exists);client.close();}}
Parameter | Description |
|---|---|
host | IP address for accessing the cluster. If there are multiple IP addresses, separate them using a comma (,). |
port | Access port of the cluster. The default value is 9200. |
protocol | Connection protocol. Set this parameter to https. |
connectTimeout | Socket connection timeout (in ms). |
connectionRequestTimeout | Socket connection request timeout (in ms). |
socketTimeout | Socket request timeout (in ms). |
username | Username for accessing the cluster. |
password | Password of the user. |
certFilePath | Path for storing the security certificate. |
certPassword | Password of the security certificate. |
This piece of code checks whether the test index exists in the cluster. If true (the index exists) or false (the index does not exist) is returned, it indicates that the cluster is connected.
To access a security-mode Elasticsearch cluster that uses HTTPS, perform the following steps to obtain the security certificate if it is required, and upload it to the client.
keytool -import -alias newname -keystore ./truststore.jks -file ./CloudSearchService.cer
keytool -import -alias newname -keystore .\truststore.jks -file .\CloudSearchService.cer
In the preceding command, newname indicates the user-defined certificate name.
After this command is executed, you will be prompted to set the certificate password and confirm the password. Securely store the password. It will be used for accessing the cluster.