Java client

The OpenSearch Java client allows you to interact with your OpenSearch clusters through Java methods and data structures rather than HTTP methods and raw JSON.

For example, you can submit requests to your cluster using objects to create indices, add data to documents, or complete some other operation using the client’s built-in methods.

Setup

To start using the OpenSearch Java client, ensure that you have the following dependencies in your project’s pom.xml file:

  1. <dependency>
  2. <groupId>org.opensearch.client</groupId>
  3. <artifactId>opensearch-rest-client</artifactId>
  4. <version>1.2.0</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.opensearch.client</groupId>
  8. <artifactId>opensearch-java</artifactId>
  9. <version>0.1.0</version>
  10. </dependency>

If you’re using Gradle, add the following dependencies to your project.

  1. dependencies {
  2. implementation 'org.opensearch.client:opensearch-rest-client: 1.2.0'
  3. implementation 'org.opensearch.client:opensearch-java:0.1.0'
  4. }

You can now start your OpenSearch cluster.

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

Sample code

This section uses a class called IndexData, which is a simple Java class that stores basic data and methods. For your own OpenSearch cluster, you might find that you need a more robust class to store your data.

IndexData class

  1. static class IndexData {
  2. private String firstName;
  3. private String lastName;
  4. public IndexData(String firstName, String lastName) {
  5. this.firstName = firstName;
  6. this.lastName = lastName;
  7. }
  8. public String getFirstName() {
  9. return firstName;
  10. }
  11. public void setFirstName(String firstName) {
  12. this.firstName = firstName;
  13. }
  14. public String getLastName() {
  15. return lastName;
  16. }
  17. public void setLastName(String lastName) {
  18. this.lastName = lastName;
  19. }
  20. @Override
  21. public String toString() {
  22. return String.format("IndexData{first name='%s', last name='%s'}", firstName, lastName);
  23. }
  24. }

OpenSearch client example

  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.client.RestClient;
  8. import org.opensearch.client.RestClientBuilder;
  9. import org.opensearch.clients.base.RestClientTransport;
  10. import org.opensearch.clients.base.Transport;
  11. import org.opensearch.clients.json.jackson.JacksonJsonpMapper;
  12. import org.opensearch.clients.opensearch.OpenSearchClient;
  13. import org.opensearch.clients.opensearch._global.IndexRequest;
  14. import org.opensearch.clients.opensearch._global.IndexResponse;
  15. import org.opensearch.clients.opensearch._global.SearchResponse;
  16. import org.opensearch.clients.opensearch.indices.*;
  17. import org.opensearch.clients.opensearch.indices.put_settings.IndexSettingsBody;
  18. import java.io.IOException;
  19. public class OpenSearchClientExample {
  20. public static void main(String[] args) {
  21. try{
  22. System.setProperty("javax.net.ssl.trustStore", "/full/path/to/keystore");
  23. System.setProperty("javax.net.ssl.trustStorePassword", "password-to-keystore");
  24. //Only for demo purposes. Don't specify your credentials in code.
  25. final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
  26. credentialsProvider.setCredentials(AuthScope.ANY,
  27. new UsernamePasswordCredentials("admin", "admin"));
  28. //Initialize the client with SSL and TLS enabled
  29. RestClient restClient = RestClient.builder(new HttpHost("localhost", 9200, "https")).
  30. setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
  31. @Override
  32. public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
  33. return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
  34. }
  35. }).build();
  36. Transport transport = new RestClientTransport(restClient, new JacksonJsonpMapper());
  37. OpenSearchClient client = new OpenSearchClient(transport);
  38. //Create the index
  39. String index = "sample-index";
  40. CreateRequest createIndexRequest = new CreateRequest.Builder().index(index).build();
  41. client.indices().create(createIndexRequest);
  42. //Add some settings to the index
  43. IndexSettings indexSettings = new IndexSettings.Builder().autoExpandReplicas("0-all").build();
  44. IndexSettingsBody settingsBody = new IndexSettingsBody.Builder().settings(indexSettings).build();
  45. PutSettingsRequest putSettingsRequest = new PutSettingsRequest.Builder().index(index).value(settingsBody).build();
  46. client.indices().putSettings(putSettingsRequest);
  47. //Index some data
  48. IndexData indexData = new IndexData("first_name", "Bruce");
  49. IndexRequest<IndexData> indexRequest = new IndexRequest.Builder<IndexData>().index(index).id("1").value(indexData).build();
  50. client.index(indexRequest);
  51. //Search for the document
  52. SearchResponse<IndexData> searchResponse = client.search(s -> s.index(index), IndexData.class);
  53. for (int i = 0; i< searchResponse.hits().hits().size(); i++) {
  54. System.out.println(searchResponse.hits().hits().get(i).source());
  55. }
  56. //Delete the document
  57. client.delete(b -> b.index(index).id("1"));
  58. // Delete the index
  59. DeleteRequest deleteRequest = new DeleteRequest.Builder().index(index).build();
  60. DeleteResponse deleteResponse = client.indices().delete(deleteRequest);
  61. restClient.close();
  62. } catch (IOException e){
  63. System.out.println(e.toString());
  64. } finally {
  65. try {
  66. if (client != null) {
  67. client.close();
  68. }
  69. } catch (IOException e) {
  70. System.out.println(e.toString());
  71. }
  72. }
  73. }
  74. }