2021-07-09 12:56:54 -07:00
---
layout : default
2021-10-21 15:57:33 -07:00
title : OpenSearch Java high-level REST client
nav_order : 60
2021-07-09 12:56:54 -07:00
---
2021-10-21 15:57:33 -07:00
# OpenSearch Java high-level REST client
2021-07-09 12:56:54 -07:00
2021-10-21 16:34:14 -07:00
Although the OpenSearch Java high-level REST client is still usable, we recommend that you use the [OpenSearch Java client ]({{site.url}}{{site.baseurl}}/clients/java/ ), which replaces the existing Java high-level REST client.
{: .note}
2021-07-09 12:56:54 -07:00
2021-10-21 16:34:14 -07:00
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.
2021-07-09 12:56:54 -07:00
## Setup
2021-10-21 15:57:33 -07:00
To start using the OpenSearch Java high-level REST client, ensure that you have the following dependency in your project's `pom.xml` file:
2021-07-09 12:56:54 -07:00
```
<dependency>
2021-10-21 15:57:33 -07:00
<groupId>org.opensearch.client</groupId>
<artifactId>opensearch-rest-high-level-client</artifactId>
2021-10-21 17:37:52 -07:00
<version>{{site.opensearch_version}}</version>
2021-07-09 12:56:54 -07:00
</dependency>
```
2021-10-21 15:57:33 -07:00
You can now start your OpenSearch cluster. The OpenSearch 1.x high-level REST client works with the 1.x versions of OpenSearch.
2021-07-09 12:56:54 -07:00
## Sample code
```java
import 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.impl.client.BasicCredentialsProvider ;
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder ;
2021-10-21 15:57:33 -07:00
import org.opensearch.action.admin.indices.delete.DeleteIndexRequest ;
import org.opensearch.action.delete.DeleteRequest ;
import org.opensearch.action.delete.DeleteResponse ;
import org.opensearch.action.get.GetRequest ;
import org.opensearch.action.get.GetResponse ;
import org.opensearch.action.index.IndexRequest ;
import org.opensearch.action.index.IndexResponse ;
import org.opensearch.action.support.master.AcknowledgedResponse ;
import org.opensearch.client.RequestOptions ;
import org.opensearch.client.RestClient ;
import org.opensearch.client.RestClientBuilder ;
import org.opensearch.client.RestHighLevelClient ;
import org.opensearch.client.indices.CreateIndexRequest ;
import org.opensearch.client.indices.CreateIndexResponse ;
import org.opensearch.common.settings.Settings ;
2021-07-09 12:56:54 -07:00
import java.io.IOException ;
import java.util.HashMap ;
public class RESTClientSample {
public static void main ( String [] args ) throws IOException {
//Point to keystore with appropriate certificates for security.
System . setProperty ( "javax.net.ssl.trustStore" , "/full/path/to/keystore" );
2021-10-21 15:57:33 -07:00
System . setProperty ( "javax.net.ssl.trustStorePassword" , "password-to-keystore" );
2021-07-09 12:56:54 -07:00
//Establish credentials to use basic authentication.
2021-07-12 09:02:11 -07:00
//Only for demo purposes. Do not specify your credentials in code.
2021-07-09 12:56:54 -07:00
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider ();
credentialsProvider . setCredentials ( AuthScope . ANY ,
new UsernamePasswordCredentials ( "admin" , "admin" ));
//Create a client.
RestClientBuilder builder = RestClient . builder ( new HttpHost ( "localhost" , 9200 , "https" ))
. setHttpClientConfigCallback ( new RestClientBuilder . HttpClientConfigCallback () {
@Override
public HttpAsyncClientBuilder customizeHttpClient ( HttpAsyncClientBuilder httpClientBuilder ) {
return httpClientBuilder . setDefaultCredentialsProvider ( credentialsProvider );
}
});
RestHighLevelClient client = new RestHighLevelClient ( builder );
//Create a non-default index with custom settings and mappings.
2021-08-19 13:51:01 -07:00
CreateIndexRequest createIndexRequest = new CreateIndexRequest ( "custom-index" );
2021-07-09 12:56:54 -07:00
createIndexRequest . settings ( Settings . builder () //Specify in the settings how many shards you want in the index.
. put ( "index.number_of_shards" , 4 )
. put ( "index.number_of_replicas" , 3 )
);
//Create a set of maps for the index's mappings.
HashMap < String , String > typeMapping = new HashMap < String , String > ();
typeMapping . put ( "type" , "integer" );
HashMap < String , Object > ageMapping = new HashMap < String , Object > ();
ageMapping . put ( "age" , typeMapping );
HashMap < String , Object > mapping = new HashMap < String , Object > ();
mapping . put ( "properties" , ageMapping );
createIndexRequest . mapping ( mapping );
2021-10-21 15:57:33 -07:00
CreateIndexResponse createIndexResponse = client . indices (). create ( createIndexRequest , RequestOptions . DEFAULT );
2021-07-09 12:56:54 -07:00
//Adding data to the index.
IndexRequest request = new IndexRequest ( "custom-index" ); //Add a document to the custom-index we created.
request . id ( "1" ); //Assign an ID to the document.
HashMap < String , String > stringMapping = new HashMap < String , String > ();
stringMapping . put ( "message:" , "Testing Java REST client" );
request . source ( stringMapping ); //Place your content into the index's source.
IndexResponse indexResponse = client . index ( request , RequestOptions . DEFAULT );
//Getting back the document
GetRequest getRequest = new GetRequest ( "custom-index" , "1" );
GetResponse response = client . get ( getRequest , RequestOptions . DEFAULT );
System . out . println ( response . getSourceAsString ());
//Delete the document
DeleteRequest deleteDocumentRequest = new DeleteRequest ( "custom-index" , "1" ); //Index name followed by the ID.
DeleteResponse deleteResponse = client . delete ( deleteDocumentRequest , RequestOptions . DEFAULT );
//Delete the index
DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest ( "custom-index" ); //Index name.
AcknowledgedResponse deleteIndexResponse = client . indices (). delete ( deleteIndexRequest , RequestOptions . DEFAULT );
client . close ();
}
}
```
2021-10-21 15:57:33 -07:00
## 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 ](#setup ).
Afterward, change all references of `org.elasticsearch` to `org.opensearch` , and you're ready to start submitting requests to your OpenSearch cluster.