Makes requests to Amazon OpenSearch using the OpenSearch Java Client with native AWS SDK 2.0 transport support added in opensarch-java 2.1.0. Supports OpenSearch Serverless since opensearch-java 2.2.0.
For support with older versions and for using OpenSearch High Level REST Client directly, see opensearch-1.x or opensearch-2.x.
Create an OpenSearch domain in (AWS) which support IAM based AuthN/AuthZ.
export AWS_ACCESS_KEY_ID=
export AWS_SECRET_ACCESS_KEY=
export AWS_SESSION_TOKEN=
export AWS_REGION=us-west-2
export SERVICE=es # use "aoss" for OpenSearch Serverless
export ENDPOINT=https://....us-west-2.es.amazonaws.com
mvn install
mvn compile exec:java \
-Dexec.mainClass="Example" \
-Dlog4j.configurationFile=target/log4j2.xml \
-Dorg.apache.commons.logging.Log=org.apache.commons.logging.impl.SimpleLog \
-Dorg.apache.commons.logging.simplelog.log.org.apache.http.wire=INFO
The code will show the server version, create an index, add a document, search for it, output the result, then cleanup.
2022-12-26 15:55:02 [Example.main()] INFO - opensearch: 2.3.0
2022-12-26 15:55:04 [Example.main()] INFO - Movie{Director='Bennett Miller', Title='Moneyball', Year=2011}
A sample that performs a vector (k-nn) search is also available in src/main/java/Knn.java.
mvn compile exec:java \
-Dexec.mainClass="Knn" \
-Dlog4j.configurationFile=target/log4j2.xml \
-Dorg.apache.commons.logging.Log=org.apache.commons.logging.impl.SimpleLog \
-Dorg.apache.commons.logging.simplelog.log.org.apache.http.wire=INFO
A sample that uses a generic client (raw JSON) is also available in src/main/java/Generic.java.
mvn compile exec:java \
-Dexec.mainClass="Generic" \
-Dlog4j.configurationFile=target/log4j2.xml \
-Dorg.apache.commons.logging.Log=org.apache.commons.logging.impl.SimpleLog \
-Dorg.apache.commons.logging.simplelog.log.org.apache.http.wire=INFO
A sample that uses dynamic proxy to intercept calls to OpenSearchClient
so you can write cross-cutting concerns (such as logging, error handling or custom retry policies, etc.) once is also available in src/main/java/LoggingClientExample.java.
mvn compile exec:java \
-Dexec.mainClass="LoggingClientExample" \
-Dlog4j.configurationFile=target/log4j2.xml \
-Dorg.apache.commons.logging.Log=org.apache.commons.logging.impl.SimpleLog \
-Dorg.apache.commons.logging.simplelog.log.org.apache.http.wire=INFO
This project is licensed under the Apache v2.0 License.
Copyright OpenSearch Contributors. See NOTICE for details.