opensearch-java icon indicating copy to clipboard operation
opensearch-java copied to clipboard

[BUG] AwsSdk2Transport is incompatible with RestClientTransport: throws http_exception 404 instead of returning GetResponse.found=false

Open luontola opened this issue 2 years ago • 5 comments

Describe the bug When calling org.opensearch.client.opensearch.OpenSearchClient#get and the document doesn't exist, if the client uses org.opensearch.client.transport.rest_client.RestClientTransport then the method returns a response object where org.opensearch.client.opensearch.core.get.GetResult#found returns false.

But if the client uses org.opensearch.client.transport.aws.AwsSdk2Transport then the method instead throws an exception org.opensearch.client.opensearch._types.OpenSearchException: Request failed: [http_exception] server returned 404.

To Reproduce Run the following code once with AWS = false and another time with AWS = true, and compare the results.

import org.apache.http.HttpHost;
import org.opensearch.client.RestClient;
import org.opensearch.client.json.jackson.JacksonJsonpMapper;
import org.opensearch.client.opensearch.OpenSearchClient;
import org.opensearch.client.opensearch.core.GetResponse;
import org.opensearch.client.transport.OpenSearchTransport;
import org.opensearch.client.transport.aws.AwsSdk2Transport;
import org.opensearch.client.transport.aws.AwsSdk2TransportOptions;
import org.opensearch.client.transport.rest_client.RestClientTransport;
import software.amazon.awssdk.http.apache.ApacheHttpClient;
import software.amazon.awssdk.regions.Region;

import java.io.IOException;
import java.util.HashMap;

public class OpenSearchExample {
    private static final boolean AWS = true;

    public static void main(String[] args) throws IOException {
        try (OpenSearchTransport transport = AWS ? awsTransport() : localTransport()) {
            OpenSearchClient client = new OpenSearchClient(transport);
            GetResponse<HashMap> response = client.get(req -> req.index("some-index").id("no-such-id"),
                    HashMap.class);
            System.out.println("response.found() = " + response.found());
        }
    }

    private static OpenSearchTransport localTransport() {
        return new RestClientTransport(
                RestClient.builder(HttpHost.create("http://127.0.0.1:9200")).build(),
                new JacksonJsonpMapper());
    }

    private static OpenSearchTransport awsTransport() {
        return new AwsSdk2Transport(ApacheHttpClient.builder().build(),
                "xxx.eu-west-1.es.amazonaws.com",
                Region.EU_WEST_1,
                AwsSdk2TransportOptions.builder().build());
    }
}

When the code is run with AWS = true, it produces the wrong result of throwing an exception:

Exception in thread "main" org.opensearch.client.opensearch._types.OpenSearchException: Request failed: [http_exception] server returned 404
	at org.opensearch.client.transport.aws.AwsSdk2Transport.parseResponse(AwsSdk2Transport.java:496)
	at org.opensearch.client.transport.aws.AwsSdk2Transport.executeSync(AwsSdk2Transport.java:393)
	at org.opensearch.client.transport.aws.AwsSdk2Transport.performRequest(AwsSdk2Transport.java:191)
	at org.opensearch.client.opensearch.OpenSearchClient.get(OpenSearchClient.java:645)
	at org.opensearch.client.opensearch.OpenSearchClient.get(OpenSearchClient.java:659)
	at OpenSearchExample.main(OpenSearchExample.java:22)

Expected behavior When the code is run with AWS = false, it produces the expected result of printing:

response.found() = false

Environment:

  • org.opensearch.client/opensearch-java 2.3.0
  • org.opensearch.client/opensearch-rest-client 2.6.0

luontola avatar Mar 29 '23 10:03 luontola

do you think fixing this would break someone's existing code that relied on the flawed behavior?

Either way it needs fixing, and feel free to submit a PR.

wbeckler avatar Apr 07 '23 06:04 wbeckler

Hi, are you planning to fix this? I'm getting the same error! Is there a version that works fine?

zandieh avatar Feb 07 '24 18:02 zandieh

@zandieh AFAIK nobody is working on this, care to help?

dblock avatar Feb 07 '24 21:02 dblock

I'm wondering if there is any other java libraries I can use instead!

zandieh avatar Feb 07 '24 23:02 zandieh

I think we just need to align this behavior regardless of which transport is used. What's the correct one?

dblock avatar Feb 08 '24 16:02 dblock