# Not getting whole response when I search using java api client

**URL:** <https://discuss.elastic.co/t/not-getting-whole-response-when-i-search-using-java-api-client/359425>\
**Category:** Elasticsearch\
**Created:** [May 14, 2024, 5:24am UTC](https://discuss.elastic.co/t/not-getting-whole-response-when-i-search-using-java-api-client/359425 "2024-05-14T05:24:53Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rohini1](https://avatars.discourse-cdn.com/v4/letter/r/dbc845/32.png) [@Rohini1](https://discuss.elastic.co/u/Rohini1)\
**Post date:** [May 14, 2024, 5:24am UTC](https://discuss.elastic.co/t/not-getting-whole-response-when-i-search-using-java-api-client/359425/1 "2024-05-14T05:24:53Z")

</div>

I am using below code to search using java api client,  
SearchResponse searchResponse = esClient.search(searchRequest,Object.class);

When I test it using postman - I am getting below response  
{"took":4,"timed\_out":false,"\_shards":{"failed":0.0,"successful":6.0,"total":6.0,"skipped":0.0},"hits":{"total":{"relation":"gte","value":10000},"here many lines of response  
and at the end "....."  
It doesn't show whole response with all closing brackets.  
Please assist

---

<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:** [May 15, 2024, 7:21am UTC](https://discuss.elastic.co/t/not-getting-whole-response-when-i-search-using-java-api-client/359425/2 "2024-05-15T07:21:12Z")

</div>

What "whole response" are you expecting to see?  
What is the complete code?

---

<div class="post-metadata">

**Author:** ![Rohini1](https://avatars.discourse-cdn.com/v4/letter/r/dbc845/32.png) [@Rohini1](https://discuss.elastic.co/u/Rohini1)\
**Post date:** [May 15, 2024, 4:32pm UTC](https://discuss.elastic.co/t/not-getting-whole-response-when-i-search-using-java-api-client/359425/3 "2024-05-15T16:32:16Z")

</div>

I mean when I run below query in Kibana I am getting response  
{  
"aggregations": {  
"terms": {  
"aggregations": {  
"terms": {  
"terms": {  
"field": "",  
"order": [  
{  
"\_count": "desc"  
},  
{  
"\_key": "asc"  
}  
],  
"size": 5  
}  
}  
},  
"date\_histogram": {  
"extended\_bounds": {  
"max": "2024-04-03T18:50:00Z",  
"min": "2022-01-03T18:50:00Z"  
},  
"field": "createTime",  
"fixed\_interval": "7d",  
"min\_doc\_count": 0  
}  
}  
},  
"from": 0,  
"query": {  
"bool": {  
"must": [  
{  
"range": {  
"createTime": {  
"gte": "2022-01-03T18:50:00Z",  
"lte": "2024-04-03T18:50:00Z"  
}  
}  
}  
]  
}  
},  
"size": 0  
}

if same query is being executed using java code (java api client)....it gives response but its getting cut off and only get first 10000 characters.  
In my java code I am creating search request object with aggregations (on various conditions ) and then calling search method  
SearchResponse searchResponse = esClient.search(searchRequest,Object.class);  
When I print searchResponse using system out, I am getting first 10000 characters means partial response.

---

<div class="post-metadata">

**Author:** ![DavidTurner](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/davidturner/32/22453_2.png) [@DavidTurner](https://discuss.elastic.co/u/DavidTurner)\
**Post date:** [May 15, 2024, 5:15pm UTC](https://discuss.elastic.co/t/not-getting-whole-response-when-i-search-using-java-api-client/359425/4 "2024-05-15T17:15:04Z")

</div>

I think that's expected:

> <https://github.com/elastic/elasticsearch-java/blob/25e61a58356ec3b826a4b77272c4ce324df4c2d4/java-client/src/main/java/co/elastic/clients/json/JsonpUtils.java#L368>

You still got the full response, it's just that you can't see it all by calling `.toString()`.

---

<div class="post-metadata">

**Author:** ![Rohini1](https://avatars.discourse-cdn.com/v4/letter/r/dbc845/32.png) [@Rohini1](https://discuss.elastic.co/u/Rohini1)\
**Post date:** [May 15, 2024, 5:39pm UTC](https://discuss.elastic.co/t/not-getting-whole-response-when-i-search-using-java-api-client/359425/5 "2024-05-15T17:39:29Z")

</div>

if that is the case then why below code is throwing error

String data=data.replace("SearchResponse:", ""); //data is my search response from ES.I dont want word "SearchResponse:" which is being added in the response.  
JsonNode dataObj = mapper.readTree(data);

and error I am getting is at JsonNode dataObj = mapper.readTree(data);  
com.fasterxml.jackson.core.io.JsonEOFException: Unexpected end-of-input: was expecting closing quote for a string value.

---

<div class="post-metadata">

**Author:** ![DavidTurner](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/davidturner/32/22453_2.png) [@DavidTurner](https://discuss.elastic.co/u/DavidTurner)\
**Post date:** [May 15, 2024, 6:40pm UTC](https://discuss.elastic.co/t/not-getting-whole-response-when-i-search-using-java-api-client/359425/6 "2024-05-15T18:40:00Z")

</div>

Because you're converting the response to a string, truncating it in the process. So don't do that.

---

<div class="post-metadata">

**Author:** ![Rohini1](https://avatars.discourse-cdn.com/v4/letter/r/dbc845/32.png) [@Rohini1](https://discuss.elastic.co/u/Rohini1)\
**Post date:** [May 15, 2024, 8:15pm UTC](https://discuss.elastic.co/t/not-getting-whole-response-when-i-search-using-java-api-client/359425/7 "2024-05-15T20:15:20Z")

</div>

so you mean I should directly return SearchResponse object as a response of my REST api call?

---

<div class="post-metadata">

**Author:** ![DavidTurner](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/davidturner/32/22453_2.png) [@DavidTurner](https://discuss.elastic.co/u/DavidTurner)\
**Post date:** [May 15, 2024, 8:28pm UTC](https://discuss.elastic.co/t/not-getting-whole-response-when-i-search-using-java-api-client/359425/8 "2024-05-15T20:28:01Z")

</div>

I'm not sure what exactly you're trying to do here. You have a `SearchResponse` object, normally you would process it in some way, maybe iterating over its `hits()` or `aggregations()` properties.

---

<div class="post-metadata">

**Author:** ![Rohini1](https://avatars.discourse-cdn.com/v4/letter/r/dbc845/32.png) [@Rohini1](https://discuss.elastic.co/u/Rohini1)\
**Post date:** [May 15, 2024, 10:59pm UTC](https://discuss.elastic.co/t/not-getting-whole-response-when-i-search-using-java-api-client/359425/9 "2024-05-15T22:59:38Z")

</div>

Thanks for your info!  
I had to use serialize method on my response object to get complete response which is more than 10k chars. Actually I am interested in aggregations property and it was more than 10k chars.

---

<div class="post-metadata">

**Author:** ![DavidTurner](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/davidturner/32/22453_2.png) [@DavidTurner](https://discuss.elastic.co/u/DavidTurner)\
**Post date:** [May 16, 2024, 6:58am UTC](https://discuss.elastic.co/t/not-getting-whole-response-when-i-search-using-java-api-client/359425/10 "2024-05-16T06:58:25Z")

</div>

Ok, normally you wouldn't just serialize the response again (the client just put a bunch of effort into deserializing it for you) but it sounds like it's doing what you need now?

---

<div class="post-metadata">

**Author:** ![Rohini1](https://avatars.discourse-cdn.com/v4/letter/r/dbc845/32.png) [@Rohini1](https://discuss.elastic.co/u/Rohini1)\
**Post date:** [May 16, 2024, 2:14pm UTC](https://discuss.elastic.co/t/not-getting-whole-response-when-i-search-using-java-api-client/359425/11 "2024-05-16T14:14:03Z")

</div>

But I dont have any other option. I am interested in aggregations part of respons eobject but that is also more than 10k chars and I have to somehow put it in json so that my rest api (which in turn having logic to connect to ES , execute query and get response) will give me a response as a json

---

<div class="post-metadata">

**Author:** ![DavidTurner](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/davidturner/32/22453_2.png) [@DavidTurner](https://discuss.elastic.co/u/DavidTurner)\
**Post date:** [May 16, 2024, 3:08pm UTC](https://discuss.elastic.co/t/not-getting-whole-response-when-i-search-using-java-api-client/359425/12 "2024-05-16T15:08:16Z")

</div>

Are you saying that you want to just forward the data from Elasticsearch on exactly as ES sent it? You can do that by calling `searchResponse.serialize(...)`, although it'd be a lot more efficient if you didn't even deserialize the response first, using `org.elasticsearch.client.RestClient#performRequestAsync(org.elasticsearch.client.Request, org.elasticsearch.client.ResponseListener)` directly.

---

<div class="post-metadata">

**Author:** ![Rohini1](https://avatars.discourse-cdn.com/v4/letter/r/dbc845/32.png) [@Rohini1](https://discuss.elastic.co/u/Rohini1)\
**Post date:** [May 16, 2024, 5:32pm UTC](https://discuss.elastic.co/t/not-getting-whole-response-when-i-search-using-java-api-client/359425/13 "2024-05-16T17:32:24Z")

</div>

I am using java api client and I am doing like this.

import co.elastic.clients.elasticsearch.core.SearchResponse;  
SearchResponse searchResponse = esClient.search(searchRequest,void.class); //getting response

JsonpUtils.toJsonString(searchResponse , new JacksonJsonpMapper() //converting response to json string

---

<div class="post-metadata">

**Author:** ![DavidTurner](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/davidturner/32/22453_2.png) [@DavidTurner](https://discuss.elastic.co/u/DavidTurner)\
**Post date:** [May 17, 2024, 9:37am UTC](https://discuss.elastic.co/t/not-getting-whole-response-when-i-search-using-java-api-client/359425/14 "2024-05-17T09:37:10Z")

</div>

Right, so either use `org.elasticsearch.client.RestClient#performRequestAsync(org.elasticsearch.client.Request, org.elasticsearch.client.ResponseListener)` instead of `esClient.search`, or else use `searchResponse.serialize(...)` instead of `JsonpUtils.toJsonString(...)`.
