Using the Java Client API to perform a query using JSON?

Hi guys,

I'm slowly learning the new Java Client API and I got good results. I'm now implementing some code where the use of the API is passing the JSON for the search request.

So far I cannot figure out how to pass this JSON to the java API.

I've tried:

        SearchResponse<Ping> search = client.search(s -> s
            .index(ElasticsearchClientManager.INDEX)
            .query(b -> b.queryString(b1 -> b1.query("\"query\" : { \"term\": { \"java.vendor\" : \""
                + System.getProperty("java.vendor") + "\" } }")))
            , Ping.class);

But I get an exception: co.elastic.clients.elasticsearch._types.ElasticsearchException: [es/search] failed: [search_phase_execution_exception] all shards failed (without more details).

I've also tried the following with the same result:

        SearchResponse<Ping> search = client.search(s -> s
            .index(ElasticsearchClientManager.INDEX)
            .query(b -> b.queryString(b1 -> b1.query("\"term\": { \"java.vendor\" : \""
                + System.getProperty("java.vendor") + "\" }")))
            , Ping.class);

Any hint for me?

Thanks!

You should use a b.term(...) and not provide a full json like this.

Thanks @dadoonet yes, yes I know that the goal of the Java Client API is to provide a typed API! :slight_smile:

And this is what I use already.

However, I also have the need to execute a search query in Java where the definition of this query is provided as a string (JSON content), dynamically and from a non-java language.

Is it possible to use the typed Java Client API to perform such a search by passing it a JSON string? Or do I need to use another Java API for that?

Thanks again

This cannot work for sure since it's not about the query string but about the query definition itself... Can't find any API though to pass a JSON value.

@swallez Is it possible today?

I'm under the impression that for this, you would need to use the LowLevel client instead...

lowLevelClient.performRequest(...)

You can use the low level Java REST client to send a JSON string. You'll be dealing with a low level response type though.

Ok. I was under the impression that it was supposed to be replaced by the newer Java Client API. At Java Low Level REST Client | Java REST Client [7.15] | Elastic it says " The Java REST Client is deprecated in favor of the Java API Client."

Is that low-level client supposed to continue being supported in the future or is it meant that the Java Client API will support this need in the future and I'll need to move my code to it when it becomes available and when the low-level client is removed?

I was really hoping to use the new java client API. Let's hope @swallez can provide some good news :slight_smile:

Thx!

Maybe there's a parser that takes a JSON String and converts it to a Query? Then I could use .query(Query value).

@vmassol there are two ways to achieve this:

Hope this helps!

Thanks a lot @swallez !

The following worked:

        String json =
            "{ \"term\": { \"java.vendor\" : \"" + System.getProperty("java.vendor") + "\" } }";
        String encodedJSON = Base64.getEncoder().encodeToString(json.getBytes());
        SearchResponse<Ping> search = client.search(s -> s
            .index(ElasticsearchClientManager.INDEX)
            .query(b0 -> b0.wrapper(b1 -> b1
                .query(encodedJSON)))
            , Ping.class);

Perfect!

1 Like