Get Document APIs throw incorrect method error

Versions (relevant - OpenSearch/Dashboard/Server OS/Browser):

opensearchproject/opensearch:2.13.0
org.opensearch.client:opensearch-java:2.6.0
org.opensearch.client:opensearch-rest-client:2.13.0

Describe the issue:

OpenSearchClient#get(GetRequest) throws:

Request failed: [string_error] Incorrect HTTP method for uri [/my_index/_doc/] and method [GET], allowed: [POST]

on usage:

val request = GetRequest.Builder()
            .index("my_index")
            .id("1001")
            .build()

val response = client.get(request, MyDocument:class.java)

making http request works just fine:

GET localhost:9200/my_index/_doc/1001
Content-Type: application/json

I think the error makes sense because the uri supposed to be /my_index/_doc/1001 instead of /my_index/_doc/? as per get document API.

to not mistaken with index document API which has an option for POST <index>/_doc.

Relevant Logs or Screenshots:

In RestClientTransport:

    public <RequestT, ResponseT, ErrorT> ResponseT performRequest(RequestT request, Endpoint<RequestT, ResponseT, ErrorT> endpoint, @Nullable TransportOptions options) throws IOException {
        Request clientReq = this.prepareLowLevelRequest(request, endpoint, options);
        Response clientResp = this.restClient.performRequest(clientReq);
        return this.getHighLevelResponse(clientResp, endpoint);
    }

Hey @danngyn

You using cURL or Dev tools to make those request?

The http request cURL-like syntax is IntelliJ built-in HTTP Client.
For that, I just follow the docs and type in <host>/<index>/_doc/<doc-id>, which works as expected.

As for the java client (I use kotlin), the output request seems to miss <doc-id> part?

Hey,

understood, as for kotlin I haven’t used that for a java client so I’m unsure.

@danngyn I believe in your use case, the _id of the document is not passed into GetRequest (it is not set).

I did have it, also posted in the snippet above:

val request = GetRequest.Builder()
            .index("my_index")
            .id("1001")
            .build()

Oh sorry, I was looking into the screenshots of Idea (which didn’t much this snippet). I am quite confused by this call val response = client.get(request), I suppose the client is OpenSearchClient instance?

yes it’s an instance of org.opensearch.client.opensearch.OpenSearchClient

sorry i missed the 2nd arg, edited, and should be val response = client.get(request, MyDocument:class.java)

Ah that explains it, thank you. I doubt this is Kotlin specific, but I cannot reproduce the issue with the Java snippet, may I ask you please to try out with the latest OpenSearch Java Client 2.10.2 ?

org.opensearch.client:opensearch-java:2.10.2

Yes the 2.10.2 works. I guess it was a bug on older version.

The latest documentation is also pointing at 2.6.0 Installing the client using RestClient Transport that was where I was onboarded with.

Thank you for all the help.

1 Like

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.