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

**URL:** <https://discuss.elastic.co/t/using-the-java-client-api-to-perform-a-query-using-json/297872>\
**Category:** Elasticsearch\
**Created:** [February 22, 2022, 10:39am UTC](https://discuss.elastic.co/t/using-the-java-client-api-to-perform-a-query-using-json/297872 "2022-02-22T10:39:30Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![vmassol](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vmassol/32/17398_2.png) [@vmassol](https://discuss.elastic.co/u/vmassol)\
**Post date:** [February 22, 2022, 10:39am UTC](https://discuss.elastic.co/t/using-the-java-client-api-to-perform-a-query-using-json/297872/1 "2022-02-22T10:39:30Z")

</div>

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:

```auto
        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:

```auto
        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!

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [February 22, 2022, 11:14am UTC](https://discuss.elastic.co/t/using-the-java-client-api-to-perform-a-query-using-json/297872/2 "2022-02-22T11:14:47Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![vmassol](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vmassol/32/17398_2.png) [@vmassol](https://discuss.elastic.co/u/vmassol)\
**Post date:** [February 22, 2022, 12:21pm UTC](https://discuss.elastic.co/t/using-the-java-client-api-to-perform-a-query-using-json/297872/3 "2022-02-22T12:21:14Z")

</div>

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

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

---

<div class="post-metadata">

**Author:** ![vmassol](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vmassol/32/17398_2.png) [@vmassol](https://discuss.elastic.co/u/vmassol)\
**Post date:** [February 22, 2022, 12:57pm UTC](https://discuss.elastic.co/t/using-the-java-client-api-to-perform-a-query-using-json/297872/4 "2022-02-22T12:57:09Z")

</div>

> [@vmassol](#):
>
> `.query(b -> b.queryString`

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.

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [February 22, 2022, 1:06pm UTC](https://discuss.elastic.co/t/using-the-java-client-api-to-perform-a-query-using-json/297872/5 "2022-02-22T13:06:27Z")

</div>

> [@vmassol](#):
>
> 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?

@swallez Is it possible today?

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

```auto
lowLevelClient.performRequest(...)

```

---

<div class="post-metadata">

**Author:** ![forloop](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/forloop/32/9021_2.png) [@forloop](https://discuss.elastic.co/u/forloop)\
**Post date:** [February 22, 2022, 1:10pm UTC](https://discuss.elastic.co/t/using-the-java-client-api-to-perform-a-query-using-json/297872/6 "2022-02-22T13:10:19Z")

</div>

You can use the [low level Java REST client](https://www.elastic.co/guide/en/elasticsearch/client/java-api-client/current/java-rest-low-usage-requests.html) to send a JSON string. You'll be dealing with a low level response type though.

---

<div class="post-metadata">

**Author:** ![vmassol](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vmassol/32/17398_2.png) [@vmassol](https://discuss.elastic.co/u/vmassol)\
**Post date:** [February 22, 2022, 1:12pm UTC](https://discuss.elastic.co/t/using-the-java-client-api-to-perform-a-query-using-json/297872/7 "2022-02-22T13:12:54Z")

</div>

> [@dadoonet](#):
>
> I'm under the impression that for this, you would need to use the LowLevel client instead...

> [@forloop](#):
>
> You can use the [low level Java REST client](https://www.elastic.co/guide/en/elasticsearch/client/java-api-client/current/java-rest-low-usage-requests.html) 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 [https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-low.html](https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-low.html) it says " The Java REST Client is deprecated in favor of the [Java API Client](https://www.elastic.co/guide/en/elasticsearch/client/java-api-client/current/index.html)."

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 🙂

Thx!

---

<div class="post-metadata">

**Author:** ![vmassol](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vmassol/32/17398_2.png) [@vmassol](https://discuss.elastic.co/u/vmassol)\
**Post date:** [February 22, 2022, 1:16pm UTC](https://discuss.elastic.co/t/using-the-java-client-api-to-perform-a-query-using-json/297872/8 "2022-02-22T13:16:15Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![swallez](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/swallez/32/11972_2.png) [@swallez](https://discuss.elastic.co/u/swallez)\
**Post date:** [February 24, 2022, 9:01am UTC](https://discuss.elastic.co/t/using-the-java-client-api-to-perform-a-query-using-json/297872/9 "2022-02-24T09:01:43Z")

</div>

@vmassol there are two ways to achieve this:

- use the [wrapper query](https://www.elastic.co/guide/en/elasticsearch/reference/7.17/query-dsl-wrapper-query.html), which is a query kind that accepts a base64-encoded JSON query.
- deserialize the JSON text into the Java client object model. This actually applies to any API class and not only to queries. This has recently been discussed in [indices client and xcontent/json support · Issue #143 · elastic/elasticsearch-java · GitHub](https://github.com/elastic/elasticsearch-java/issues/143#issuecomment-1043186181)

Hope this helps!

---

<div class="post-metadata">

**Author:** ![vmassol](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vmassol/32/17398_2.png) [@vmassol](https://discuss.elastic.co/u/vmassol)\
**Post date:** [February 24, 2022, 10:11am UTC](https://discuss.elastic.co/t/using-the-java-client-api-to-perform-a-query-using-json/297872/10 "2022-02-24T10:11:36Z")

</div>

Thanks a lot @swallez !

The following worked:

```auto
        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!

---

<div class="post-metadata">

**Author:** ![swallez](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/swallez/32/11972_2.png) [@swallez](https://discuss.elastic.co/u/swallez)\
**Post date:** [February 28, 2022, 2:25pm UTC](https://discuss.elastic.co/t/using-the-java-client-api-to-perform-a-query-using-json/297872/11 "2022-02-28T14:25:22Z")

</div>


