Java high-level REST client

The Elasticsearch OSS Java high-level REST client allows you to interact with your OpenSearch clusters and indices through Java methods and data structures rather than HTTP methods and JSON.

You submit requests to your cluster using request objects, which allows you to create indices, add data to documents, or complete other operations with your cluster. In return, you get back response objects that have all of the available information, such as the associated index or ID, from your cluster.

Setup

To start using the Elasticsearch OSS Java high-level REST client, ensure that you have the following dependency in your project’s pom.xml file:

  1. <dependency>
  2. <groupId>org.elasticsearch.client</groupId>
  3. <artifactId>elasticsearch-rest-high-level-client</artifactId>
  4. <version>7.10.2</version>
  5. </dependency>

You can now start your OpenSearch cluster. The 7.10.2 high-level REST client works with the 1.x versions of OpenSearch.

Sample code

  1. import org.apache.http.HttpHost;
  2. import org.apache.http.auth.AuthScope;
  3. import org.apache.http.auth.UsernamePasswordCredentials;
  4. import org.apache.http.client.CredentialsProvider;
  5. import org.apache.http.impl.client.BasicCredentialsProvider;
  6. import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
  7. import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
  8. import org.elasticsearch.action.delete.DeleteRequest;
  9. import org.elasticsearch.action.delete.DeleteResponse;
  10. import org.elasticsearch.action.get.GetRequest;
  11. import org.elasticsearch.action.get.GetResponse;
  12. import org.elasticsearch.action.index.IndexRequest;
  13. import org.elasticsearch.action.index.IndexResponse;
  14. import org.elasticsearch.action.support.master.AcknowledgedResponse;
  15. import org.elasticsearch.client.RequestOptions;
  16. import org.elasticsearch.client.RestClient;
  17. import org.elasticsearch.client.RestClientBuilder;
  18. import org.elasticsearch.client.RestHighLevelClient;
  19. import org.elasticsearch.client.indices.CreateIndexRequest;
  20. import org.elasticsearch.client.indices.CreateIndexResponse;
  21. import org.elasticsearch.common.settings.Settings;
  22. import org.elasticsearch.common.xcontent.XContentType;
  23. import java.io.IOException;
  24. import java.util.HashMap;
  25. public class RESTClientSample {
  26. public static void main(String[] args) throws IOException {
  27. //Point to keystore with appropriate certificates for security.
  28. System.setProperty("javax.net.ssl.trustStore", "/full/path/to/keystore");
  29. System.setProperty("javax.net.ssl.trustStorePassword", password-to-keystore);
  30. //Establish credentials to use basic authentication.
  31. //Only for demo purposes. Do not specify your credentials in code.
  32. final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
  33. credentialsProvider.setCredentials(AuthScope.ANY,
  34. new UsernamePasswordCredentials("admin", "admin"));
  35. //Create a client.
  36. RestClientBuilder builder = RestClient.builder(new HttpHost("localhost", 9200, "https"))
  37. .setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
  38. @Override
  39. public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
  40. return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
  41. }
  42. });
  43. RestHighLevelClient client = new RestHighLevelClient(builder);
  44. //Create a non-default index with custom settings and mappings.
  45. CreateIndexRequest createIndexRequest = new CreateIndexRequest("test-index");
  46. createIndexRequest.settings(Settings.builder() //Specify in the settings how many shards you want in the index.
  47. .put("index.number_of_shards", 4)
  48. .put("index.number_of_replicas", 3)
  49. );
  50. //Create a set of maps for the index's mappings.
  51. HashMap<String, String> typeMapping = new HashMap<String,String>();
  52. typeMapping.put("type", "integer");
  53. HashMap<String, Object> ageMapping = new HashMap<String, Object>();
  54. ageMapping.put("age", typeMapping);
  55. HashMap<String, Object> mapping = new HashMap<String, Object>();
  56. mapping.put("properties", ageMapping);
  57. createIndexRequest.mapping(mapping);
  58. CreateIndexResponse createIndexResponse = client.indices().create(createIndexRequest, RequestOptions.DEFAULT
  59. //Adding data to the index.
  60. IndexRequest request = new IndexRequest("custom-index"); //Add a document to the custom-index we created.
  61. request.id("1"); //Assign an ID to the document.
  62. HashMap<String, String> stringMapping = new HashMap<String, String>();
  63. stringMapping.put("message:", "Testing Java REST client");
  64. request.source(stringMapping); //Place your content into the index's source.
  65. IndexResponse indexResponse = client.index(request, RequestOptions.DEFAULT);
  66. //Getting back the document
  67. GetRequest getRequest = new GetRequest("custom-index", "1");
  68. GetResponse response = client.get(getRequest, RequestOptions.DEFAULT);
  69. System.out.println(response.getSourceAsString());
  70. //Delete the document
  71. DeleteRequest deleteDocumentRequest = new DeleteRequest("custom-index", "1"); //Index name followed by the ID.
  72. DeleteResponse deleteResponse = client.delete(deleteDocumentRequest, RequestOptions.DEFAULT);
  73. //Delete the index
  74. DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest("custom-index"); //Index name.
  75. AcknowledgedResponse deleteIndexResponse = client.indices().delete(deleteIndexRequest, RequestOptions.DEFAULT);
  76. client.close();
  77. }
  78. }