diff --git a/_clients/java-rest-high-level.md b/_clients/java-rest-high-level.md new file mode 100644 index 00000000..a6c2733f --- /dev/null +++ b/_clients/java-rest-high-level.md @@ -0,0 +1,123 @@ +--- +layout: default +title: Java high-level REST client +nav_order: 97 +--- + +# 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: + +``` + + org.elasticsearch.client + elasticsearch-rest-high-level-client + 7.10.2 + +``` + +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 + +```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; +import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest; +import org.elasticsearch.action.delete.DeleteRequest; +import org.elasticsearch.action.delete.DeleteResponse; +import org.elasticsearch.action.get.GetRequest; +import org.elasticsearch.action.get.GetResponse; +import org.elasticsearch.action.index.IndexRequest; +import org.elasticsearch.action.index.IndexResponse; +import org.elasticsearch.action.support.master.AcknowledgedResponse; +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.CreateIndexRequest; +import org.elasticsearch.client.indices.CreateIndexResponse; +import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.common.xcontent.XContentType; + +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"); + System.setProperty("javax.net.ssl.trustStorePassword", password-to-keystore); + + //Establish credentials to use basic authentication. + 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. + CreateIndexRequest createIndexRequest = new CreateIndexRequest("test-index"); + + 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 typeMapping = new HashMap(); + typeMapping.put("type", "integer"); + HashMap ageMapping = new HashMap(); + ageMapping.put("age", typeMapping); + HashMap mapping = new HashMap(); + mapping.put("properties", ageMapping); + createIndexRequest.mapping(mapping); + CreateIndexResponse createIndexResponse = client.indices().create(createIndexRequest, RequestOptions.DEFAULT + + //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 stringMapping = new HashMap(); + 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(); + } +} +``` diff --git a/_opensearch/java-rest-high-level.md b/_opensearch/java-rest-high-level.md deleted file mode 100644 index f8452a1d..00000000 --- a/_opensearch/java-rest-high-level.md +++ /dev/null @@ -1,111 +0,0 @@ ---- -layout: default -title: Java high-level REST client -nav_order: 97 ---- - -# Java high-level REST client - -The Elasticsearch OSS Java high-level REST client allows you to interact with your OpenSearch clusters and indices through request objects and response objects. To create an index, add data to a document, or complete some other operation, you submit request objects to your cluster, and you get back response objects that have all of the available information, such as the associated index or ID, returned 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: - -``` - - org.elasticsearch.client - elasticsearch-rest-high-level-client - 7.10.2 - -``` - -You can now start your OpenSearch cluster. Any version of OpenSearch works with the Elasticsearch OSS high-level REST client. - -## Create high-level client - -Before you begin submitting request objects, you need to first create a high-level REST client object. The following example demonstrates how to establish a client that uses basic authorization credentials to access a cluster. - -If you're using the Elasticsearch OSS high-level REST client over HTTPS, you also need to point your java project to a keystore that has the appropriate security certificates. - -```java -System.setProperty("javax.net.ssl.trustStore", "/full/path/to/keystore.jks"); -System.setProperty("javax.net.ssl.trustStorePassword", password-to-keystore); - -final CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); - -credentialsProvider.setCredentials(AuthScope.ANY, - new UsernamePasswordCredentials("admin", "admin")); - -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); -``` - -After creating your client, you're ready to start sending requests to your cluster. - -## Create index and index documents - -The following example demonstrates how to create an index with non-default settings. - -```java -CreateIndexRequest createIndexRequest = new CreateIndexRequest("custom-index"); - -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) - ); - -createIndexRequest.mapping( //Specify any mappings you want. - "{\n" + - " \"properties\": {\n" + - " \"age\": {\n" + - " \"type\": \"integer\"\n" + - " }\n" + - " }\n" + - "}", - XContentType.JSON); -CreateIndexResponse createIndexResponse = client.indices().create(createIndexRequest, RequestOptions.DEFAULT); //Send the request -``` -## Add data - -Now that the index is created, you can start adding data to it. - -```java -IndexRequest request = new IndexRequest("custom-index"); //Add the document to the custom-index we created. -request.id("1"); //Assigning an ID to the document. -String data = "{" + - "\"message\":\"Testing Java REST client\"" + - "}"; -request.source(data,XContentType.JSON); //Place your content into the index's source. -IndexResponse indexResponse = client.index(request, RequestOptions.DEFAULT); -``` - -## Read data - -After you add data to your cluster, you can get it back as a string. - -```java -GetRequest getRequest = new GetRequest("custom-index", "1"); //Index name followed by the document ID. -GetResponse response = client.get(getRequest, RequestOptions.DEFAULT); - -System.out.println(response.getSourceAsString()); -``` - -## Delete the document and index - -When you're done with the documents, you can delete them. If you want, you can also delete the index. - -```java -DeleteRequest deleteRequest = new DeleteRequest("custom-index", "1"); //Index name followed by the document ID. -DeleteResponse deleteResponse = client.delete(deleteRequest, RequestOptions.DEFAULT); - -DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest("custom-index"); - AcknowledgedResponse deleteIndexResponse = client.indices().delete(deleteIndexRequest, RequestOptions.DEFAULT); -```