# See actual JSON from SearchRequestBuilder

**URL:** <https://discuss.elastic.co/t/see-actual-json-from-searchrequestbuilder/3909>\
**Category:** Elasticsearch\
**Created:** [February 10, 2011, 8:00pm UTC](https://discuss.elastic.co/t/see-actual-json-from-searchrequestbuilder/3909 "2011-02-10T20:00:58Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Enrique\_Medina\_Monte](https://avatars.discourse-cdn.com/v4/letter/e/4491bb/32.png) [@Enrique\_Medina\_Monte](https://discuss.elastic.co/u/Enrique_Medina_Monte)\
**Post date:** [February 10, 2011, 8:00pm UTC](https://discuss.elastic.co/t/see-actual-json-from-searchrequestbuilder/3909/1 "2011-02-10T20:00:58Z")

</div>

Hi,

I have sort of a complex logic to build a search request using the Java API.  
I'm finding a corner case which is not working as expected, and I'm  
struggling to figure out what's going on.

Having access or a way to see the actual JSON that gets sent to the ES  
server would be extremely helpful. Is there any way to achieve this? In  
other words, can I see the JSON being sent to ES from the Java API?

Thanks.

---

<div class="post-metadata">

**Author:** ![Karussell1](https://avatars.discourse-cdn.com/v4/letter/k/50afbb/32.png) [@Karussell1](https://discuss.elastic.co/u/Karussell1)\
**Post date:** [February 10, 2011, 8:03pm UTC](https://discuss.elastic.co/t/see-actual-json-from-searchrequestbuilder/3909/2 "2011-02-10T20:03:58Z")

</div>

is this what you want?

public static String toString(ToXContent tmp) {  
try {  
return  
tmp.toXContent(JsonXContent.unCachedContentBuilder(),  
ToXContent.EMPTY\_PARAMS).  
prettyPrint().  
string();  
} catch (Exception ex) {  
return "\<ERROR:" + ex.getMessage() + "\>";  
}  
}

On 10 Feb., 21:00, Enrique Medina Montenegro [e.medin...@gmail.com](mailto:e.medin...@gmail.com)  
wrote:

> Hi,
> 
> I have sort of a complex logic to build a search request using the Java API.  
> I'm finding a corner case which is not working as expected, and I'm  
> struggling to figure out what's going on.
> 
> Having access or a way to see the actual JSON that gets sent to the ES  
> server would be extremely helpful. Is there any way to achieve this? In  
> other words, can I see the JSON being sent to ES from the Java API?
> 
> Thanks.

---

<div class="post-metadata">

**Author:** ![Administrator\_2](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/administrator_2/32/3211_2.png) [@Administrator\_2](https://discuss.elastic.co/u/Administrator_2)\
**Post date:** [February 10, 2011, 8:05pm UTC](https://discuss.elastic.co/t/see-actual-json-from-searchrequestbuilder/3909/3 "2011-02-10T20:05:54Z")

</div>

Also, you should be able to use any HTTP sniffer (there is Fiddler on  
Windows, and I'm sure there are counterparts on other systems) which don't  
require you to change your code so significantly.

-----Original Message-----  
From: Karussell [[mailto:tableyourtime@googlemail.com](mailto:tableyourtime@googlemail.com)]  
Sent: Thursday, February 10, 2011 3:04 PM  
To: users  
Subject: Re: See actual JSON from SearchRequestBuilder

is this what you want?

public static String toString(ToXContent tmp) {  
try {  
return  
tmp.toXContent(JsonXContent.unCachedContentBuilder(),  
ToXContent.EMPTY\_PARAMS).  
prettyPrint().  
string();  
} catch (Exception ex) {  
return "\<ERROR:" + ex.getMessage() + "\>";  
}  
}

On 10 Feb., 21:00, Enrique Medina Montenegro [e.medin...@gmail.com](mailto:e.medin...@gmail.com)  
wrote:

> Hi,
> 
> I have sort of a complex logic to build a search request using the Java  
> API.  
> I'm finding a corner case which is not working as expected, and I'm  
> struggling to figure out what's going on.
> 
> Having access or a way to see the actual JSON that gets sent to the ES  
> server would be extremely helpful. Is there any way to achieve this? In  
> other words, can I see the JSON being sent to ES from the Java API?
> 
> Thanks.

---

<div class="post-metadata">

**Author:** ![Karussell1](https://avatars.discourse-cdn.com/v4/letter/k/50afbb/32.png) [@Karussell1](https://discuss.elastic.co/u/Karussell1)\
**Post date:** [February 10, 2011, 8:22pm UTC](https://discuss.elastic.co/t/see-actual-json-from-searchrequestbuilder/3909/4 "2011-02-10T20:22:59Z")

</div>

although it would be interesting if there is a logging settings for  
the node ...

On 10 Feb., 21:05, "Administrator" [ad...@sf4answers.com](mailto:ad...@sf4answers.com) wrote:

> Also, you should be able to use any HTTP sniffer (there is Fiddler on  
> Windows, and I'm sure there are counterparts on other systems) which don't  
> require you to change your code so significantly.
> 
> -----Original Message-----  
> From: Karussell [[mailto:tableyourt...@googlemail.com](mailto:tableyourt...@googlemail.com)]  
> Sent: Thursday, February 10, 2011 3:04 PM  
> To: users  
> Subject: Re: See actual JSON from SearchRequestBuilder
> 
> is this what you want?
> 
> public static String toString(ToXContent tmp) {  
> try {  
> return  
> tmp.toXContent(JsonXContent.unCachedContentBuilder(),  
> ToXContent.EMPTY\_PARAMS).  
> prettyPrint().  
> string();  
> } catch (Exception ex) {  
> return "\<ERROR:" + ex.getMessage() + "\>";  
> }  
> }
> 
> On 10 Feb., 21:00, Enrique Medina Montenegro [e.medin...@gmail.com](mailto:e.medin...@gmail.com)  
> wrote:
> 
> > Hi,
> 
> > I have sort of a complex logic to build a search request using the Java  
> > API.  
> > I'm finding a corner case which is not working as expected, and I'm  
> > struggling to figure out what's going on.
> 
> > Having access or a way to see the actual JSON that gets sent to the ES  
> > server would be extremely helpful. Is there any way to achieve this? In  
> > other words, can I see the JSON being sent to ES from the Java API?
> 
> > Thanks.

---

<div class="post-metadata">

**Author:** ![Enrique\_Medina\_Monte](https://avatars.discourse-cdn.com/v4/letter/e/4491bb/32.png) [@Enrique\_Medina\_Monte](https://discuss.elastic.co/u/Enrique_Medina_Monte)\
**Post date:** [February 10, 2011, 9:06pm UTC](https://discuss.elastic.co/t/see-actual-json-from-searchrequestbuilder/3909/5 "2011-02-10T21:06:01Z")

</div>

I forgot to mention that the object where I build the search using the Java  
API is a SearchRequestBuilder.

On Thu, Feb 10, 2011 at 9:22 PM, Karussell [tableyourtime@googlemail.com](mailto:tableyourtime@googlemail.com)wrote:

> although it would be interesting if there is a logging settings for  
> the node ...
> 
> On 10 Feb., 21:05, "Administrator" [ad...@sf4answers.com](mailto:ad...@sf4answers.com) wrote:
> 
> > Also, you should be able to use any HTTP sniffer (there is Fiddler on  
> > Windows, and I'm sure there are counterparts on other systems) which  
> > don't  
> > require you to change your code so significantly.
> > 
> > -----Original Message-----  
> > From: Karussell [[mailto:tableyourt...@googlemail.com](mailto:tableyourt...@googlemail.com)]  
> > Sent: Thursday, February 10, 2011 3:04 PM  
> > To: users  
> > Subject: Re: See actual JSON from SearchRequestBuilder
> > 
> > is this what you want?
> > 
> > public static String toString(ToXContent tmp) {  
> > try {  
> > return  
> > tmp.toXContent(JsonXContent.unCachedContentBuilder(),  
> > ToXContent.EMPTY\_PARAMS).  
> > prettyPrint().  
> > string();  
> > } catch (Exception ex) {  
> > return "\<ERROR:" + ex.getMessage() + "\>";  
> > }  
> > }
> > 
> > On 10 Feb., 21:00, Enrique Medina Montenegro [e.medin...@gmail.com](mailto:e.medin...@gmail.com)  
> > wrote:
> > 
> > > Hi,
> > 
> > > I have sort of a complex logic to build a search request using the Java  
> > > API.  
> > > I'm finding a corner case which is not working as expected, and I'm  
> > > struggling to figure out what's going on.
> > 
> > > Having access or a way to see the actual JSON that gets sent to the ES  
> > > server would be extremely helpful. Is there any way to achieve this? In  
> > > other words, can I see the JSON being sent to ES from the Java API?
> > 
> > > Thanks.

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [February 10, 2011, 10:02pm UTC](https://discuss.elastic.co/t/see-actual-json-from-searchrequestbuilder/3909/6 "2011-02-10T22:02:12Z")

</div>

You can get the internal builder that implements the ToXContent (which allows you to print it as json) using SearchRequestBuilder#internalBuilder.  
On Thursday, February 10, 2011 at 11:06 PM, Enrique Medina Montenegro wrote:

> I forgot to mention that the object where I build the search using the Java API is a SearchRequestBuilder.
> 
> On Thu, Feb 10, 2011 at 9:22 PM, Karussell [tableyourtime@googlemail.com](mailto:tableyourtime@googlemail.com) wrote:
> 
> > although it would be interesting if there is a logging settings for  
> > the node ...
> > 
> > On 10 Feb., 21:05, "Administrator" [ad...@sf4answers.com](mailto:ad...@sf4answers.com) wrote:
> > 
> > > Also, you should be able to use any HTTP sniffer (there is Fiddler on  
> > > Windows, and I'm sure there are counterparts on other systems) which don't  
> > > require you to change your code so significantly.
> > > 
> > > -----Original Message-----  
> > > From: Karussell [[mailto:tableyourt...@googlemail.com](mailto:tableyourt...@googlemail.com)]  
> > > Sent: Thursday, February 10, 2011 3:04 PM  
> > > To: users  
> > > Subject: Re: See actual JSON from SearchRequestBuilder
> > > 
> > > is this what you want?
> > > 
> > > public static String toString(ToXContent tmp) {  
> > > try {  
> > > return  
> > > tmp.toXContent(JsonXContent.unCachedContentBuilder(),  
> > > ToXContent.EMPTY\_PARAMS).  
> > > prettyPrint().  
> > > string();  
> > > } catch (Exception ex) {  
> > > return "\<ERROR:" + ex.getMessage() + "\>";  
> > > }  
> > > }
> > > 
> > > On 10 Feb., 21:00, Enrique Medina Montenegro [e.medin...@gmail.com](mailto:e.medin...@gmail.com)  
> > > wrote:
> > > 
> > > > Hi,
> > > 
> > > > I have sort of a complex logic to build a search request using the Java  
> > > > API.  
> > > > I'm finding a corner case which is not working as expected, and I'm  
> > > > struggling to figure out what's going on.
> > > 
> > > > Having access or a way to see the actual JSON that gets sent to the ES  
> > > > server would be extremely helpful. Is there any way to achieve this? In  
> > > > other words, can I see the JSON being sent to ES from the Java API?
> > > 
> > > > Thanks.

---

<div class="post-metadata">

**Author:** ![srrin](https://avatars.discourse-cdn.com/v4/letter/s/85f322/32.png) [@srrin](https://discuss.elastic.co/u/srrin)\
**Post date:** [February 11, 2011, 9:00am UTC](https://discuss.elastic.co/t/see-actual-json-from-searchrequestbuilder/3909/7 "2011-02-11T09:00:36Z")

</div>

Hope this code snippet may help you

try {  
XContentBuilder builderString1 = XContentFactory.jsonBuilder();

searchRequestBuilder.toXContent(builderString1, ToXContent.EMPTY\_PARAMS);  
String s1 = builderString1.string();

} catch (IOException e) {  
e.printStackTrace();  
}

---

<div class="post-metadata">

**Author:** ![Enrique\_Medina\_Monte](https://avatars.discourse-cdn.com/v4/letter/e/4491bb/32.png) [@Enrique\_Medina\_Monte](https://discuss.elastic.co/u/Enrique_Medina_Monte)\
**Post date:** [February 11, 2011, 11:59am UTC](https://discuss.elastic.co/t/see-actual-json-from-searchrequestbuilder/3909/8 "2011-02-11T11:59:31Z")

</div>

Eventually, this is what I used:

public static String toString(ToXContent tmp) {  
try {  
return  
tmp.toXContent(JsonXContent.unCachedContentBuilder(),  
ToXContent.EMPTY\_PARAMS).  
prettyPrint().  
string();  
} catch (Exception ex) {  
return "\<ERROR:" + ex.getMessage() + "\>";  
}  
}

SearchRequestBuilder search = new ....

log.debug(toString(search.internalBuilder())

@Shay: would it make sense to add this to the SearchRequestBuilder class as  
an aid to debugging? I can take over that if you want...

That way we could do simply:

search.toJSON()

On Fri, Feb 11, 2011 at 10:00 AM, srrIN [srrranga@gmail.com](mailto:srrranga@gmail.com) wrote:

> Hope this code snippet may help you
> 
> try {  
> XContentBuilder builderString1 = XContentFactory.jsonBuilder();
> 
> searchRequestBuilder.toXContent(builderString1, ToXContent.EMPTY\_PARAMS);  
> String s1 = builderString1.string();
> 
> ## } catch (IOException e) { e.printStackTrace(); }
> 
> View this message in context:  
> [http://elasticsearch-users.115913.n3.nabble.com/See-actual-JSON-from-SearchRequestBuilder-tp2468564p2472188.html](http://elasticsearch-users.115913.n3.nabble.com/See-actual-JSON-from-SearchRequestBuilder-tp2468564p2472188.html)  
> Sent from the Elasticsearch Users mailing list archive at [Nabble.com](http://Nabble.com).

---

<div class="post-metadata">

**Author:** ![Rich\_Kroll](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rich_kroll/32/3229_2.png) [@Rich\_Kroll](https://discuss.elastic.co/u/Rich_Kroll)\
**Post date:** [February 11, 2011, 1:05pm UTC](https://discuss.elastic.co/t/see-actual-json-from-searchrequestbuilder/3909/9 "2011-02-11T13:05:02Z")

</div>

+1i think this would help  
On Feb 11, 2011 7:00 AM, "Enrique Medina Montenegro" [e.medina.m@gmail.com](mailto:e.medina.m@gmail.com)  
wrote:

> Eventually, this is what I used:
> 
> public static String toString(ToXContent tmp) {  
> try {  
> return  
> tmp.toXContent(JsonXContent.unCachedContentBuilder(),  
> ToXContent.EMPTY\_PARAMS).  
> prettyPrint().  
> string();  
> } catch (Exception ex) {  
> return "\<ERROR:" + ex.getMessage() + "\>";  
> }  
> }
> 
> SearchRequestBuilder search = new ....
> 
> log.debug(toString(search.internalBuilder())
> 
> @Shay: would it make sense to add this to the SearchRequestBuilder class  
> as  
> an aid to debugging? I can take over that if you want...
> 
> That way we could do simply:
> 
> search.toJSON()
> 
> On Fri, Feb 11, 2011 at 10:00 AM, srrIN [srrranga@gmail.com](mailto:srrranga@gmail.com) wrote:
> 
> > Hope this code snippet may help you
> > 
> > try {  
> > XContentBuilder builderString1 = XContentFactory.jsonBuilder();
> > 
> > searchRequestBuilder.toXContent(builderString1, ToXContent.EMPTY\_PARAMS);  
> > String s1 = builderString1.string();
> > 
> > ## } catch (IOException e) { e.printStackTrace(); }
> > 
> > View this message in context:

[http://elasticsearch-users.115913.n3.nabble.com/See-actual-JSON-from-SearchRequestBuilder-tp2468564p2472188.html](http://elasticsearch-users.115913.n3.nabble.com/See-actual-JSON-from-SearchRequestBuilder-tp2468564p2472188.html)

> > Sent from the Elasticsearch Users mailing list archive at [Nabble.com](http://Nabble.com).

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [February 11, 2011, 1:57pm UTC](https://discuss.elastic.co/t/see-actual-json-from-searchrequestbuilder/3909/10 "2011-02-11T13:57:33Z")

</div>

Right, a simpel String toJson can help in debugging. Actually, it can help on any class that implements ToXContent.  
On Friday, February 11, 2011 at 3:05 PM, Rich Kroll wrote:

> +1i think this would help  
> On Feb 11, 2011 7:00 AM, "Enrique Medina Montenegro" [e.medina.m@gmail.com](mailto:e.medina.m@gmail.com) wrote:
> 
> > Eventually, this is what I used:
> > 
> > public static String toString(ToXContent tmp) {  
> > try {  
> > return  
> > tmp.toXContent(JsonXContent.unCachedContentBuilder(),  
> > ToXContent.EMPTY\_PARAMS).  
> > prettyPrint().  
> > string();  
> > } catch (Exception ex) {  
> > return "\<ERROR:" + ex.getMessage() + "\>";  
> > }  
> > }
> > 
> > SearchRequestBuilder search = new ....
> > 
> > log.debug(toString(search.internalBuilder())
> > 
> > @Shay: would it make sense to add this to the SearchRequestBuilder class as  
> > an aid to debugging? I can take over that if you want...
> > 
> > That way we could do simply:
> > 
> > search.toJSON()
> > 
> > On Fri, Feb 11, 2011 at 10:00 AM, srrIN [srrranga@gmail.com](mailto:srrranga@gmail.com) wrote:
> > 
> > > Hope this code snippet may help you
> > > 
> > > try {  
> > > XContentBuilder builderString1 = XContentFactory.jsonBuilder();
> > > 
> > > searchRequestBuilder.toXContent(builderString1, ToXContent.EMPTY\_PARAMS);  
> > > String s1 = builderString1.string();
> > > 
> > > ## } catch (IOException e) { e.printStackTrace(); }
> > > 
> > > View this message in context:  
> > > [http://elasticsearch-users.115913.n3.nabble.com/See-actual-JSON-from-SearchRequestBuilder-tp2468564p2472188.html](http://elasticsearch-users.115913.n3.nabble.com/See-actual-JSON-from-SearchRequestBuilder-tp2468564p2472188.html)  
> > > Sent from the Elasticsearch Users mailing list archive at [Nabble.com](http://Nabble.com).

---

<div class="post-metadata">

**Author:** ![Enrique\_Medina\_Monte](https://avatars.discourse-cdn.com/v4/letter/e/4491bb/32.png) [@Enrique\_Medina\_Monte](https://discuss.elastic.co/u/Enrique_Medina_Monte)\
**Post date:** [February 11, 2011, 2:08pm UTC](https://discuss.elastic.co/t/see-actual-json-from-searchrequestbuilder/3909/11 "2011-02-11T14:08:26Z")

</div>

Or even a log.debug() statement when executing it, for instance 😉

On Fri, Feb 11, 2011 at 2:57 PM, Shay Banon [shay.banon@elasticsearch.com](mailto:shay.banon@elasticsearch.com)wrote:

> Right, a simpel String toJson can help in debugging. Actually, it can  
> help on any class that implements ToXContent.
> 
> On Friday, February 11, 2011 at 3:05 PM, Rich Kroll wrote:
> 
> +1i think this would help  
> On Feb 11, 2011 7:00 AM, "Enrique Medina Montenegro" [e.medina.m@gmail.com](mailto:e.medina.m@gmail.com)  
> wrote:
> 
> > Eventually, this is what I used:
> > 
> > public static String toString(ToXContent tmp) {  
> > try {{  
> > return  
> > tmp.toXContent(JsonXContent.unCachedContentBuilder(),  
> > ToXContent.EMPTY\_PARAMS).  
> > prettyPrint().  
> > string();  
> > } catch (Exception ex) {  
> > return "\<ERROR:" + ex.getMessage() + "\>";  
> > }  
> > }
> > 
> > SearchRequestBuilder search = new ....
> > 
> > log.debug(toString(search.internalBuilder())
> > 
> > @Shay: would it make sense to add this to the SearchRequestBuilder class  
> > as  
> > an aid to debugging? I can take over that if you want...
> > 
> > That way we could do simply:
> > 
> > search.toJSON()
> > 
> > On Fri, Feb 11, 2011 at 10:00 AM, srrIN [srrranga@gmail.com](mailto:srrranga@gmail.com) wrote:
> > 
> > > Hope this code snippet may help you
> > > 
> > > try {  
> > > XContentBuilder builderString1 = XContentFactory.jsonBuilder();
> > > 
> > > searchRequestBuilder.toXContent(builderString1,  
> > > ToXContent.EMPTY\_PARAMS);  
> > > String s1 = builderString1.string();
> > > 
> > > ## } catch (IOException e) { e.printStackTrace(); }
> > > 
> > > View this message in context:
> 
> [http://elasticsearch-users.115913.n3.nabble.com/See-actual-JSON-from-SearchRequestBuilder-tp2468564p2472188.html](http://elasticsearch-users.115913.n3.nabble.com/See-actual-JSON-from-SearchRequestBuilder-tp2468564p2472188.html)
> 
> > > Sent from the Elasticsearch Users mailing list archive at [Nabble.com](http://Nabble.com).

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 6, 2017, 4:12am UTC](https://discuss.elastic.co/t/see-actual-json-from-searchrequestbuilder/3909/12 "2017-07-06T04:12:12Z")

</div>


