# How to get the json for a searchrequest?

**URL:** <https://discuss.elastic.co/t/how-to-get-the-json-for-a-searchrequest/16126>\
**Category:** Elasticsearch\
**Created:** [March 4, 2014, 12:30am UTC](https://discuss.elastic.co/t/how-to-get-the-json-for-a-searchrequest/16126 "2014-03-04T00:30:21Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tebring\_Daly](https://avatars.discourse-cdn.com/v4/letter/t/e274bd/32.png) [@Tebring\_Daly](https://discuss.elastic.co/u/Tebring_Daly)\
**Post date:** [March 4, 2014, 12:30am UTC](https://discuss.elastic.co/t/how-to-get-the-json-for-a-searchrequest/16126/1 "2014-03-04T00:30:21Z")

</div>

Is there a way to get the json string from a SearchRequest object?

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/30e07de8-ed0c-49c9-a163-aaa11f20c9a4%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/30e07de8-ed0c-49c9-a163-aaa11f20c9a4%40googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Amit\_Soni](https://avatars.discourse-cdn.com/v4/letter/a/c0e974/32.png) [@Amit\_Soni](https://discuss.elastic.co/u/Amit_Soni)\
**Post date:** [March 4, 2014, 2:46am UTC](https://discuss.elastic.co/t/how-to-get-the-json-for-a-searchrequest/16126/2 "2014-03-04T02:46:25Z")

</div>

If you mean in Java, toString() gives me the json representation.

-Amit.

On Mon, Mar 3, 2014 at 4:30 PM, Tebring Daly [tdalytx@gmail.com](mailto:tdalytx@gmail.com) wrote:

> Is there a way to get the json string from a SearchRequest object?
> 
> --  
> You received this message because you are subscribed to the Google Groups  
> "elasticsearch" group.  
> To unsubscribe from this group and stop receiving emails from it, send an  
> email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> To view this discussion on the web visit  
> [https://groups.google.com/d/msgid/elasticsearch/30e07de8-ed0c-49c9-a163-aaa11f20c9a4%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/30e07de8-ed0c-49c9-a163-aaa11f20c9a4%40googlegroups.com)  
> .  
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/CAAOGaQ%2BDVPw7dq2r%3DU1rNJzmLWaXHQZ1KsMRPbjBWqvj7\_nQNQ%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAAOGaQ%2BDVPw7dq2r%3DU1rNJzmLWaXHQZ1KsMRPbjBWqvj7_nQNQ%40mail.gmail.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Tebring\_Daly](https://avatars.discourse-cdn.com/v4/letter/t/e274bd/32.png) [@Tebring\_Daly](https://discuss.elastic.co/u/Tebring_Daly)\
**Post date:** [March 4, 2014, 4:45pm UTC](https://discuss.elastic.co/t/how-to-get-the-json-for-a-searchrequest/16126/3 "2014-03-04T16:45:52Z")

</div>

On a toString() I get the typical unimplemented toString class reference :  
"org.elasticsearch.action.search.SearchRequest@142daa2e"

I am using elasticsearch 1.0.0, wonder if something has changed from 0.90  
or 1.0.1?

Thanks for the response

On Monday, March 3, 2014 8:46:25 PM UTC-6, amit.soni wrote:

> If you mean in Java, toString() gives me the json representation.
> 
> -Amit.
> 
> On Mon, Mar 3, 2014 at 4:30 PM, Tebring Daly \<[tda...@gmail.com](mailto:tda...@gmail.com)\<javascript:\>
> 
> > wrote:
> 
> > Is there a way to get the json string from a SearchRequest object?
> > 
> > --  
> > You received this message because you are subscribed to the Google Groups  
> > "elasticsearch" group.  
> > To unsubscribe from this group and stop receiving emails from it, send an  
> > email to [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com) \<javascript:\>.  
> > To view this discussion on the web visit  
> > [https://groups.google.com/d/msgid/elasticsearch/30e07de8-ed0c-49c9-a163-aaa11f20c9a4%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/30e07de8-ed0c-49c9-a163-aaa11f20c9a4%40googlegroups.com)  
> > .  
> > For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/61c3c936-08bd-423d-8a44-f26d08477dd1%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/61c3c936-08bd-423d-8a44-f26d08477dd1%40googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<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:** [March 4, 2014, 4:53pm UTC](https://discuss.elastic.co/t/how-to-get-the-json-for-a-searchrequest/16126/4 "2014-03-04T16:53:24Z")

</div>

I think you can print a QueryBuilder or a SearchRequestBuilder but not a SearchRequest.

```
	QueryBuilder qb = QueryBuilders.queryString("my text");
	logger.info("Your query is : {}", qb);

	// this works as well
	node.client().prepareSearch().setQuery(qb).toString();

```

--  
David Pilato | Technical Advocate | [Elasticsearch.com](http://Elasticsearch.com)  
@dadoonet | @elasticsearchfr

Le 4 mars 2014 à 17:45:56, Tebring Daly ([tdalytx@gmail.com](mailto:tdalytx@gmail.com)) a écrit:

On a toString() I get the typical unimplemented toString class reference : "org.elasticsearch.action.search.SearchRequest@142daa2e"

I am using elasticsearch 1.0.0, wonder if something has changed from 0.90 or 1.0.1?

Thanks for the response

On Monday, March 3, 2014 8:46:25 PM UTC-6, amit.soni wrote:  
If you mean in Java, toString() gives me the json representation.

-Amit.

On Mon, Mar 3, 2014 at 4:30 PM, Tebring Daly [tda...@gmail.com](mailto:tda...@gmail.com) wrote:  
Is there a way to get the json string from a SearchRequest object?

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/30e07de8-ed0c-49c9-a163-aaa11f20c9a4%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/30e07de8-ed0c-49c9-a163-aaa11f20c9a4%40googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/61c3c936-08bd-423d-8a44-f26d08477dd1%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/61c3c936-08bd-423d-8a44-f26d08477dd1%40googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/etPan.53160504.2ae8944a.7701%40MacBook-Air-de-David.local](https://groups.google.com/d/msgid/elasticsearch/etPan.53160504.2ae8944a.7701%40MacBook-Air-de-David.local).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Tebring\_Daly](https://avatars.discourse-cdn.com/v4/letter/t/e274bd/32.png) [@Tebring\_Daly](https://discuss.elastic.co/u/Tebring_Daly)\
**Post date:** [March 4, 2014, 5:34pm UTC](https://discuss.elastic.co/t/how-to-get-the-json-for-a-searchrequest/16126/5 "2014-03-04T17:34:35Z")

</div>

Great! Thanks David!

On Tuesday, March 4, 2014 10:53:24 AM UTC-6, David Pilato wrote:

> I think you can print a QueryBuilder or a SearchRequestBuilder but not a  
> SearchRequest.
> 
> QueryBuilder qb = QueryBuilders.queryString("my text");  
> logger.info("Your query is : {}", qb);
> 
> // this works as well  
> node.client().prepareSearch().setQuery(qb).toString();
> 
> --  
> _David Pilato_ | _Technical Advocate_ | _[Elasticsearch.com](http://Elasticsearch.com)_  
> @dadoonet [https://twitter.com/dadoonet](https://twitter.com/dadoonet) | @elasticsearchfr[https://twitter.com/elasticsearchfr](https://twitter.com/elasticsearchfr)
> 
> Le 4 mars 2014 à 17:45:56, Tebring Daly ([tda...@gmail.com](mailto:tda...@gmail.com) \<javascript:\>)  
> a écrit:
> 
> On a toString() I get the typical unimplemented toString class reference :  
> "org.elasticsearch.action.search.SearchRequest@142daa2e"
> 
> I am using elasticsearch 1.0.0, wonder if something has changed from 0.90  
> or 1.0.1?
> 
> Thanks for the response
> 
> On Monday, March 3, 2014 8:46:25 PM UTC-6, amit.soni wrote:
> 
> > If you mean in Java, toString() gives me the json representation.
> > 
> > -Amit.
> > 
> > On Mon, Mar 3, 2014 at 4:30 PM, Tebring Daly [tda...@gmail.com](mailto:tda...@gmail.com) wrote:
> > 
> > > Is there a way to get the json string from a SearchRequest object?
> > > 
> > > --  
> > > You received this message because you are subscribed to the Google  
> > > Groups "elasticsearch" group.  
> > > To unsubscribe from this group and stop receiving emails from it, send  
> > > an email to [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com).  
> > > To view this discussion on the web visit  
> > > [https://groups.google.com/d/msgid/elasticsearch/30e07de8-ed0c-49c9-a163-aaa11f20c9a4%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/30e07de8-ed0c-49c9-a163-aaa11f20c9a4%40googlegroups.com)  
> > > .  
> > > For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).
> > 
> > --  
> > You received this message because you are subscribed to the Google Groups  
> > "elasticsearch" group.  
> > To unsubscribe from this group and stop receiving emails from it, send an  
> > email to [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com) \<javascript:\>.  
> > To view this discussion on the web visit  
> > [https://groups.google.com/d/msgid/elasticsearch/61c3c936-08bd-423d-8a44-f26d08477dd1%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/61c3c936-08bd-423d-8a44-f26d08477dd1%40googlegroups.com)  
> > .  
> > For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/091cff7c-d200-474f-becb-f6bb340af94e%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/091cff7c-d200-474f-becb-f6bb340af94e%40googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![android](https://avatars.discourse-cdn.com/v4/letter/a/cdc98d/32.png) [@android](https://discuss.elastic.co/u/android)\
**Post date:** [May 7, 2014, 1:15am UTC](https://discuss.elastic.co/t/how-to-get-the-json-for-a-searchrequest/16126/6 "2014-05-07T01:15:08Z")

</div>

Is there really no way to print a search request?

Because QueryBuilder does not know about indices, or the fields we want the search request to return. And I'd like to have those fields in my JSon representation.

Basically I'd like to create a JSon representation that I can use as a source of a search request later on, like an editable template.

---

<div class="post-metadata">

**Author:** ![waterguo](https://avatars.discourse-cdn.com/v4/letter/w/839c29/32.png) [@waterguo](https://discuss.elastic.co/u/waterguo)\
**Post date:** [September 19, 2014, 5:58pm UTC](https://discuss.elastic.co/t/how-to-get-the-json-for-a-searchrequest/16126/7 "2014-09-19T17:58:06Z")

</div>

this is how to do it

```
            String json = XContentHelper.convertToJson(request.source(), true);
            LOG.info(json);
            json = XContentHelper.convertToJson(request.extraSource(), true);
            LOG.info(json);
```

---

<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, 1:01am UTC](https://discuss.elastic.co/t/how-to-get-the-json-for-a-searchrequest/16126/8 "2017-07-06T01:01:01Z")

</div>


