OpenSearch Java Client

repository·main·Indexed 41 days ago

https://github.com/opensearch-project/opensearch-java

A high-performance, open-source Java library designed to interact with OpenSearch clusters, serving as the modern replacement for the legacy High Level REST Client. It provides tools for bulk indexing via BulkIngester, AWS authentication using AwsSdk2Transport, and support for JDK 8, 11, 17, and 21.

Tokens
28K
Snippets
77
Records
104
Agent score
51%

What's inside OpenSearch Java Client

  1. Use OpenSearchGenericClient for low-level HTTP communication

    main

    When the standard typed OpenSearch APIs are too restrictive, use OpenSearchGenericClient to communicate with OpenSearch clusters or nodes via generic HTTP requests and responses.

    By default, the generic client returns responses exactly as received from the server without interpreting status codes. You can configure it to throw an OpenSearchClientException for non-successful HTTP status codes using ClientOptions.throwOnHttpErrors().

    Important: Always use a try-with-resources block when handling Response objects to ensure that underlying resources like input streams and buffers are properly closed.

    // Get the generic client
    final OpenSearchGenericClient generic = javaClient().generic();
    
    // Get a generic client that throws exceptions on HTTP errors
    final OpenSearchGenericClient genericWithErrorHandling = javaClient().generic().witClientOptions(ClientOptions.throwOnHttpErrors());
  2. Understand the Automated Release process

    main

    The release process is automated using several workflows:

    1. Drafting: The release-drafter.yml workflow automatically drafts a new pre-release on GitHub containing release artifacts.
    2. Approval: Before a release is finalized, the workflow creates a GitHub issue. Maintainers must manually approve this issue to continue the workflow.
    3. Signing and Publishing: Once approved, the opensearch-java-maven-sign-and-release Jenkins workflow is triggered. This workflow automatically signs artifacts and publishes them to Maven.
    4. GitHub Release: After the Maven publication is successful, a GitHub issue is created requesting maintainers to manually publish the pre-release on GitHub.
  3. Java Language Formatting Guidelines

    main

    To maintain consistency, follow these formatting rules:

    • Indentation: 4 spaces.
    • Line Width: 140 characters.
    • Documentation Tags: Code regions wrapped in // tag::NAME and // end::NAME are used for documentation. These must be kept to a maximum of 76 characters wide (excluding indentation). These regions are not automatically formatted.
    • Imports: Wildcard imports (import foo.bar.baz.*) are strictly forbidden and will cause build failures.
    • Comments: Line comments (// ...) are formatted; JavaDoc and block comments (/* ... */) are not.
    • Boolean Expressions: For readability, use foo == false instead of !foo where possible (though not strictly enforced).
  4. Install the OpenSearch Java Client

    main

    To use the OpenSearch Java client, add the opensearch-java dependency to your project. The client requires a transport layer, and the default ApacheHttpClient5Transport is included in the dependency.

    ### Maven
    ```xml
    <dependency>
      <groupId>org.opensearch.client</groupId>
      <artifactId>opensearch-java</artifactId>
      <version>2.6.0</version>
    </dependency>

    Gradle

    dependencies {
      implementation 'org.opensearch.client:opensearch-java:2.6.0'
    }
  5. Handle duplicate document conflicts

    main

    When using the create API, a duplicate ID returns a 409 conflict. You should catch ResponseException or OpenSearchException and check the status code to distinguish between a conflict and other errors.

    try {
        client.create(c -> c.index(index).id("1").document(new Movie("Beauty and the Beast", 1991)));
    } catch (ResponseException e) {
        if (e.status() != 409) {
            throw e;
        }
    } catch (OpenSearchException e) {
        if (e.status() != 409) {
            throw e;
        }
    }
  6. Upgrade from 2.x to 3.0: Request Parameter Type Changes

    main

    Several request objects have changed their parameter types for metric or other fields during the 3.0 upgrade:

    • IndicesStatsRequest: metric is now List<IndicesStatsMetric> (previously List<String>).
    • RerouteRequest: metric is now List<ClusterRerouteMetric> (previously List<String>).
    • StateRequest: metric is now List<ClusterStateMetric> (previously List<String>).
    • NodesInfoRequest: metric is now List<NodesInfoMetric> (previously List<String>).
    • NodesStatsRequest:
      • groups is now List<String> (previously Boolean).
      • indexMetric is now List<NodesStatsIndexMetric> (previously List<String>).
      • metric is now List<NodesStatsMetric> (previously List<String>).
    • NodesUsageRequest: metric is now List<NodesUsageMetric> (previously List<String>).
    • HealthRequest:
      • level is now ClusterHealthLevel (previously Level).
      • waitForNodes is now WaitForNodes (previously String).
  7. Deserialize objects using withJson

    main

    For classes whose Builders implement the PlainDeserializable interface, you can use the withJson(InputStream) method. This method returns the Builder, allowing you to chain additional configuration methods before calling .build().

    Example of deserializing an IndexTemplateMapping from a JSON string:

    String stringTemplate = "{\"mappings\":{\"properties\":{\"age\":{\"type\":\"integer\"}}},\"settings\":{\"number_of_shards\":\"2\",\"number_of_replicas\":\"1\"}}";
    
    InputStream inputStream = new ByteArrayInputStream(stringTemplate.getBytes(StandardCharsets.UTF_8));
    IndexTemplateMapping indexTemplateMapping = new IndexTemplateMapping.Builder().withJson(inputStream).build();
  8. Migrate IndexTemplate and IndexTemplateSummary (v3.0)

    main

    In version 3.0, IndexTemplate and IndexTemplateSummary have been moved from the org.opensearch.client.opensearch.indices.get_index_template package to the org.opensearch.client.opensearch.indices package.

    Additionally, several property types have changed:

    • IndexTemplate.dataStream is now of type IndexTemplateDataStreamConfiguration (previously Map<String, JsonData>).
    • IndexTemplateSummary.settings is now of type IndexSettings (previously Map<String, JsonData>).
    • PutIndexTemplateRequest.dataStream is now of type IndexTemplateDataStreamConfiguration.
    • SimulateIndexTemplateRequest.dataStream is now of type IndexTemplateDataStreamConfiguration.
  9. Run AWS Transport Integration Tests

    main

    To run integration tests for the AWS transport client, you must have working AWS credentials in ~/.aws/credentials. You must specify the domain host, region, and service name.

    For standard OpenSearch domains, use serviceName=es. For OpenSearch Serverless, use serviceName=aoss.

    # Standard OpenSearch Domain
    ./gradlew integrationTest --tests "*AwsSdk2*" -Dtests.awsSdk2support.domainHost=search-...us-west-2.es.amazonaws.com -Dtests.awsSdk2support.domainRegion=us-west-2 -Dtests.awsSdk2support.serviceName=es
    
    # OpenSearch Serverless
    ./gradlew integrationTest --tests "*AwsSdk2*" -Dtests.awsSdk2support.domainHost=....us-west-2.aoss.amazonaws.com -Dtests.awsSdk2support.domainRegion=us-west-2 -Dtests.awsSdk2support.serviceName=aoss