Java high-level REST client

Although the OpenSearch Java high-level REST client is still usable, we recommend that you use the OpenSearch Java client, which replaces the existing Java high-level REST client.

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

Setup

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

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

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

The following example uses credentials that come with the default OpenSearch configuration. If you’re using the high-level REST client with your own OpenSearch cluster, be sure to change the code to use your own credentials.

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

Elasticsearch OSS Java high-level REST client

We recommend using the OpenSearch client to connect to OpenSearch clusters, but if you must use the Elasticsearch OSS Java high-level REST client, version 7.10.2 of the Elasticsearch OSS client also works with the 1.x versions of OpenSearch.

Migrating to the OpenSearch Java high-level REST client

Migrating from the Elasticsearch OSS client to the OpenSearch high-level REST client is as simple as changing your Maven dependency to one that references OpenSearch’s dependency.

Afterward, change all references of org.elasticsearch to org.opensearch, and you’re ready to start submitting requests to your OpenSearch cluster.