# Any way to see json representation of request constructed with SearchRequestBuilder?

**URL:** <https://discuss.elastic.co/t/any-way-to-see-json-representation-of-request-constructed-with-searchrequestbuilder/4402>\
**Category:** Elasticsearch\
**Created:** [May 13, 2011, 8:28pm UTC](https://discuss.elastic.co/t/any-way-to-see-json-representation-of-request-constructed-with-searchrequestbuilder/4402 "2011-05-13T20:28:57Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ellery\_Crane\_2](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ellery_crane_2/32/3369_2.png) [@Ellery\_Crane\_2](https://discuss.elastic.co/u/Ellery_Crane_2)\
**Post date:** [May 13, 2011, 8:28pm UTC](https://discuss.elastic.co/t/any-way-to-see-json-representation-of-request-constructed-with-searchrequestbuilder/4402/1 "2011-05-13T20:28:57Z")

</div>

I'm trying to use the java api to construct a search request, and I'm  
encountering problems. Unfortunately, I have no idea what's causing  
them, because I can't find a way to see what query is actually getting  
constructed. Is there some way I can see the actual json that the  
SearchRequestBuilder gets converted into, so I can debug my query?

---

<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:** [May 14, 2011, 11:48am UTC](https://discuss.elastic.co/t/any-way-to-see-json-representation-of-request-constructed-with-searchrequestbuilder/4402/2 "2011-05-14T11:48:58Z")

</div>

There isn't a simple toString method (I'll add it). You can call SearchRequestBuilder#internalBuilder().buildAsBytes(XContentType.JSON).  
On Friday, May 13, 2011 at 11:28 PM, Ellery Crane wrote:

> I'm trying to use the java api to construct a search request, and I'm  
> encountering problems. Unfortunately, I have no idea what's causing  
> them, because I can't find a way to see what query is actually getting  
> constructed. Is there some way I can see the actual json that the  
> SearchRequestBuilder gets converted into, so I can debug my query?

---

<div class="post-metadata">

**Author:** ![js1](https://avatars.discourse-cdn.com/v4/letter/j/51bf81/32.png) [@js1](https://discuss.elastic.co/u/js1)\
**Post date:** [May 16, 2011, 1:54pm UTC](https://discuss.elastic.co/t/any-way-to-see-json-representation-of-request-constructed-with-searchrequestbuilder/4402/3 "2011-05-16T13:54:23Z")

</div>

Hi,  
maybe you wanna try this:

XContentBuilder jsonBuilder = XContentFactory.jsonBuilder();  
XContentBuilder toXContent = queryBuilder.toXContent(jsonBuilder, ToXContent.EMPTY\_PARAMS);  
String query = toXContent.string();

where as queryBuilder is type of BaseQueryBuilder.

---

<div class="post-metadata">

**Author:** ![Ellery\_Crane\_2](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ellery_crane_2/32/3369_2.png) [@Ellery\_Crane\_2](https://discuss.elastic.co/u/Ellery_Crane_2)\
**Post date:** [May 16, 2011, 3:01pm UTC](https://discuss.elastic.co/t/any-way-to-see-json-representation-of-request-constructed-with-searchrequestbuilder/4402/4 "2011-05-16T15:01:35Z")

</div>

Thanks Shay! Though as a note to anyone else trying to do this, you'll  
need to wrap the byte in a String:

String json = new  
String(builder.internalBuilder().buildAsBytes(XContentType.JSON));

On May 14, 7:48 am, Shay Banon [shay.ba...@elasticsearch.com](mailto:shay.ba...@elasticsearch.com) wrote:

> There isn't a simple toString method (I'll add it). You can call SearchRequestBuilder#internalBuilder().buildAsBytes(XContentType.JSON).
> 
> On Friday, May 13, 2011 at 11:28 PM, Ellery Crane wrote:
> 
> > I'm trying to use the java api to construct a search request, and I'm  
> > encountering problems. Unfortunately, I have no idea what's causing  
> > them, because I can't find a way to see what query is actually getting  
> > constructed. Is there some way I can see the actual json that the  
> > SearchRequestBuilder gets converted into, so I can debug my query?

---

<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:** [May 16, 2011, 7:49pm UTC](https://discuss.elastic.co/t/any-way-to-see-json-representation-of-request-constructed-with-searchrequestbuilder/4402/5 "2011-05-16T19:49:41Z")

</div>

Yes, though make sure to use UTF8 charset. There is a toString method now in master.  
On Monday, May 16, 2011 at 6:01 PM, Ellery Crane wrote:

> Thanks Shay! Though as a note to anyone else trying to do this, you'll  
> need to wrap the byte in a String:
> 
> String json = new  
> String(builder.internalBuilder().buildAsBytes(XContentType.JSON));
> 
> On May 14, 7:48 am, Shay Banon [shay.ba...@elasticsearch.com](mailto:shay.ba...@elasticsearch.com) wrote:
> 
> > There isn't a simple toString method (I'll add it). You can call SearchRequestBuilder#internalBuilder().buildAsBytes(XContentType.JSON).
> > 
> > On Friday, May 13, 2011 at 11:28 PM, Ellery Crane wrote:
> > 
> > > I'm trying to use the java api to construct a search request, and I'm  
> > > encountering problems. Unfortunately, I have no idea what's causing  
> > > them, because I can't find a way to see what query is actually getting  
> > > constructed. Is there some way I can see the actual json that the  
> > > SearchRequestBuilder gets converted into, so I can debug my query?

---

<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:06am UTC](https://discuss.elastic.co/t/any-way-to-see-json-representation-of-request-constructed-with-searchrequestbuilder/4402/6 "2017-07-06T04:06:04Z")

</div>


