OpenSearch Service examples using SDK for Java 2.x - Amazon SDK for Java 2.x
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

OpenSearch Service examples using SDK for Java 2.x

The following code examples show you how to perform actions and implement common scenarios by using the Amazon SDK for Java 2.x with OpenSearch Service.

Actions are code excerpts from larger programs and must be run in context. While actions show you how to call individual service functions, you can see actions in context in their related scenarios and cross-service examples.

Scenarios are code examples that show you how to accomplish a specific task by calling multiple functions within the same service.

Each example includes a link to GitHub, where you can find instructions on how to set up and run the code in context.

Topics

Actions

The following code example shows how to use CreateDomain.

SDK for Java 2.x
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.opensearch.OpenSearchClient; import software.amazon.awssdk.services.opensearch.model.ClusterConfig; import software.amazon.awssdk.services.opensearch.model.EBSOptions; import software.amazon.awssdk.services.opensearch.model.VolumeType; import software.amazon.awssdk.services.opensearch.model.NodeToNodeEncryptionOptions; import software.amazon.awssdk.services.opensearch.model.CreateDomainRequest; import software.amazon.awssdk.services.opensearch.model.CreateDomainResponse; import software.amazon.awssdk.services.opensearch.model.OpenSearchException; /** * Before running this Java V2 code example, set up your development * environment, including your credentials. * * For more information, see the following documentation topic: * * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class CreateDomain { public static void main(String[] args) { final String usage = """ Usage: <domainName> Where: domainName - The name of the domain to create. """; if (args.length != 1) { System.out.println(usage); System.exit(1); } String domainName = args[0]; Region region = Region.US_EAST_1; OpenSearchClient searchClient = OpenSearchClient.builder() .region(region) .build(); createNewDomain(searchClient, domainName); System.out.println("Done"); } public static void createNewDomain(OpenSearchClient searchClient, String domainName) { try { ClusterConfig clusterConfig = ClusterConfig.builder() .dedicatedMasterEnabled(true) .dedicatedMasterCount(3) .dedicatedMasterType("t2.small.search") .instanceType("t2.small.search") .instanceCount(5) .build(); EBSOptions ebsOptions = EBSOptions.builder() .ebsEnabled(true) .volumeSize(10) .volumeType(VolumeType.GP2) .build(); NodeToNodeEncryptionOptions encryptionOptions = NodeToNodeEncryptionOptions.builder() .enabled(true) .build(); CreateDomainRequest domainRequest = CreateDomainRequest.builder() .domainName(domainName) .engineVersion("OpenSearch_1.0") .clusterConfig(clusterConfig) .ebsOptions(ebsOptions) .nodeToNodeEncryptionOptions(encryptionOptions) .build(); System.out.println("Sending domain creation request..."); CreateDomainResponse createResponse = searchClient.createDomain(domainRequest); System.out.println("Domain status is " + createResponse.domainStatus().toString()); System.out.println("Domain Id is " + createResponse.domainStatus().domainId()); } catch (OpenSearchException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } } }
  • For API details, see CreateDomain in Amazon SDK for Java 2.x API Reference.

The following code example shows how to use DeleteDomain.

SDK for Java 2.x
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.opensearch.OpenSearchClient; import software.amazon.awssdk.services.opensearch.model.OpenSearchException; import software.amazon.awssdk.services.opensearch.model.DeleteDomainRequest; /** * Before running this Java V2 code example, set up your development * environment, including your credentials. * * For more information, see the following documentation topic: * * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class DeleteDomain { public static void main(String[] args) { final String usage = """ Usage: <domainName> Where: domainName - The name of the domain to delete. """; if (args.length != 1) { System.out.println(usage); System.exit(1); } String domainName = args[0]; Region region = Region.US_EAST_1; OpenSearchClient searchClient = OpenSearchClient.builder() .region(region) .build(); deleteSpecificDomain(searchClient, domainName); System.out.println("Done"); } public static void deleteSpecificDomain(OpenSearchClient searchClient, String domainName) { try { DeleteDomainRequest domainRequest = DeleteDomainRequest.builder() .domainName(domainName) .build(); searchClient.deleteDomain(domainRequest); System.out.println(domainName + " was successfully deleted."); } catch (OpenSearchException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } } }
  • For API details, see DeleteDomain in Amazon SDK for Java 2.x API Reference.

The following code example shows how to use ListDomainNames.

SDK for Java 2.x
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

import software.amazon.awssdk.auth.credentials.ProfileCredentialsProvider; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.opensearch.OpenSearchClient; import software.amazon.awssdk.services.opensearch.model.DomainInfo; import software.amazon.awssdk.services.opensearch.model.ListDomainNamesRequest; import software.amazon.awssdk.services.opensearch.model.ListDomainNamesResponse; import software.amazon.awssdk.services.opensearch.model.OpenSearchException; import java.util.List; /** * Before running this Java V2 code example, set up your development * environment, including your credentials. * * For more information, see the following documentation topic: * * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class ListDomainNames { public static void main(String[] args) { Region region = Region.US_EAST_1; OpenSearchClient searchClient = OpenSearchClient.builder() .region(region) .credentialsProvider(ProfileCredentialsProvider.create()) .build(); listAllDomains(searchClient); System.out.println("Done"); } public static void listAllDomains(OpenSearchClient searchClient) { try { ListDomainNamesRequest namesRequest = ListDomainNamesRequest.builder() .engineType("OpenSearch") .build(); ListDomainNamesResponse response = searchClient.listDomainNames(namesRequest); List<DomainInfo> domainInfoList = response.domainNames(); for (DomainInfo domain : domainInfoList) System.out.println("Domain name is " + domain.domainName()); } catch (OpenSearchException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } } }
  • For API details, see ListDomainNames in Amazon SDK for Java 2.x API Reference.

The following code example shows how to use UpdateDomainConfig.

SDK for Java 2.x
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.opensearch.OpenSearchClient; import software.amazon.awssdk.services.opensearch.model.ClusterConfig; import software.amazon.awssdk.services.opensearch.model.OpenSearchException; import software.amazon.awssdk.services.opensearch.model.UpdateDomainConfigRequest; import software.amazon.awssdk.services.opensearch.model.UpdateDomainConfigResponse; /** * Before running this Java V2 code example, set up your development * environment, including your credentials. * * For more information, see the following documentation topic: * * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class UpdateDomain { public static void main(String[] args) { final String usage = """ Usage: <domainName> Where: domainName - The name of the domain to update. """; if (args.length != 1) { System.out.println(usage); System.exit(1); } String domainName = args[0]; Region region = Region.US_EAST_1; OpenSearchClient searchClient = OpenSearchClient.builder() .region(region) .build(); updateSpecificDomain(searchClient, domainName); System.out.println("Done"); } public static void updateSpecificDomain(OpenSearchClient searchClient, String domainName) { try { ClusterConfig clusterConfig = ClusterConfig.builder() .instanceCount(3) .build(); UpdateDomainConfigRequest updateDomainConfigRequest = UpdateDomainConfigRequest.builder() .domainName(domainName) .clusterConfig(clusterConfig) .build(); System.out.println("Sending domain update request..."); UpdateDomainConfigResponse updateResponse = searchClient.updateDomainConfig(updateDomainConfigRequest); System.out.println("Domain update response from Amazon OpenSearch Service:"); System.out.println(updateResponse.toString()); } catch (OpenSearchException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } } }