# How can I pass a JSON query string to a Query builder

**URL:** <https://discuss.elastic.co/t/how-can-i-pass-a-json-query-string-to-a-query-builder/7110>\
**Category:** Elasticsearch\
**Created:** [March 23, 2012, 8:06pm UTC](https://discuss.elastic.co/t/how-can-i-pass-a-json-query-string-to-a-query-builder/7110 "2012-03-23T20:06:17Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Robert\_Simmons](https://avatars.discourse-cdn.com/v4/letter/r/a87d85/32.png) [@Robert\_Simmons](https://discuss.elastic.co/u/Robert_Simmons)\
**Post date:** [March 23, 2012, 8:06pm UTC](https://discuss.elastic.co/t/how-can-i-pass-a-json-query-string-to-a-query-builder/7110/1 "2012-03-23T20:06:17Z")

</div>

I have been trying for hours to get this working. Basically I have a class  
that performs a filtered query and I want the user to be able to pass a  
JSON query string to my constructor and then use that on the filtered query  
to further reduce the results. I have tried all manner of QueryBuilders  
methods and SearchRequestBuilder methods.

Here is the code I settled on but it doesn't allow custom query:

final SearchRequestBuilder srb =  
client.prepareSearch(indices).setSearchType(  
SearchType.QUERY\_AND\_FETCH).setTypes(indexTypes);  
final GeoDistanceFilterBuilder filter =  
FilterBuilders.geoDistanceFilter("point").distance(maxDistance, units)

.lat(address.coordinate.latitude).lon(address.coordinate.longitude)  
.geoDistance(GeoDistance.ARC);  
final GeoDistanceSortBuilder sort =  
SortBuilders.geoDistanceSort("location.point").geoDistance(  
GeoDistance.PLANE).point(address.coordinate.latitude,  
address.coordinate.longitude);  
srb.setQuery(QueryBuilders.filteredQuery(QueryBuilders.matchAllQuery(),  
filter));  
srb.addSort(sort);

What I would like is something like:

final SearchRequestBuilder srb =  
client.prepareSearch(indices).setSearchType(  
SearchType.QUERY\_AND\_FETCH).setTypes(indexTypes);  
final GeoDistanceFilterBuilder filter =  
FilterBuilders.geoDistanceFilter("point").distance(maxDistance, units)

.lat(address.coordinate.latitude).lon(address.coordinate.longitude)  
.geoDistance(GeoDistance.ARC);  
final GeoDistanceSortBuilder sort =  
SortBuilders.geoDistanceSort("location.point").geoDistance(  
GeoDistance.PLANE).point(address.coordinate.latitude,  
address.coordinate.longitude);  
final QueryBuilder query;  
if (this.queryJSON == null) {  
query = QueryBuilders.fromJSON(this.queryJSON); \<\<=== doesn't exist.  
} else {  
query = QueryBuilders.matchAllQuery()  
}  
srb.setQuery(QueryBuilders.filteredQuery(query, filter));  
srb.addSort(sort);

I have tried using srb.setSource() but it completely mangles the JSON  
putting a comma just inside the first { and not after the query clause. I  
have tried other methods in search request builder but with no success.  
Using QueryBuilders.wrapperQuerry(this.queryJSON) doesn't work either.

I have spent hours working on this and would appreciate any pointers.

---

<div class="post-metadata">

**Author:** ![Lar\_Mader](https://avatars.discourse-cdn.com/v4/letter/l/a6a055/32.png) [@Lar\_Mader](https://discuss.elastic.co/u/Lar_Mader)\
**Post date:** [March 23, 2012, 10:51pm UTC](https://discuss.elastic.co/t/how-can-i-pass-a-json-query-string-to-a-query-builder/7110/2 "2012-03-23T22:51:15Z")

</div>

I don't know if this is the best answer (or even helps), but you could  
construct your entire query as JSON, and then use a SearchRequest to  
execute the query passing in the full JSON query:

client.search(Requests.searchRequest(index).source(jsonStr)).actionGet()

On Fri, Mar 23, 2012 at 1:06 PM, Robert Simmons [kraythe@gmail.com](mailto:kraythe@gmail.com) wrote:

> I have been trying for hours to get this working. Basically I have a class  
> that performs a filtered query and I want the user to be able to pass a  
> JSON query string to my constructor and then use that on the filtered query  
> to further reduce the results. I have tried all manner of QueryBuilders  
> methods and SearchRequestBuilder methods.
> 
> Here is the code I settled on but it doesn't allow custom query:
> 
> final SearchRequestBuilder srb =  
> client.prepareSearch(indices).setSearchType(  
> SearchType.QUERY\_AND\_FETCH).setTypes(indexTypes);  
> final GeoDistanceFilterBuilder filter =  
> FilterBuilders.geoDistanceFilter("point").distance(maxDistance, units)
> 
> .lat(address.coordinate.latitude).lon(address.coordinate.longitude)  
> .geoDistance(GeoDistance.ARC);  
> final GeoDistanceSortBuilder sort =  
> SortBuilders.geoDistanceSort("location.point").geoDistance(  
> GeoDistance.PLANE).point(address.coordinate.latitude,  
> address.coordinate.longitude);  
> srb.setQuery(QueryBuilders.filteredQuery(QueryBuilders.matchAllQuery(),  
> filter));  
> srb.addSort(sort);
> 
> What I would like is something like:
> 
> final SearchRequestBuilder srb =  
> client.prepareSearch(indices).setSearchType(  
> SearchType.QUERY\_AND\_FETCH).setTypes(indexTypes);  
> final GeoDistanceFilterBuilder filter =  
> FilterBuilders.geoDistanceFilter("point").distance(maxDistance, units)
> 
> .lat(address.coordinate.latitude).lon(address.coordinate.longitude)  
> .geoDistance(GeoDistance.ARC);  
> final GeoDistanceSortBuilder sort =  
> SortBuilders.geoDistanceSort("location.point").geoDistance(  
> GeoDistance.PLANE).point(address.coordinate.latitude,  
> address.coordinate.longitude);  
> final QueryBuilder query;  
> if (this.queryJSON == null) {  
> query = QueryBuilders.fromJSON(this.queryJSON); \<\<=== doesn't exist.  
> } else {  
> query = QueryBuilders.matchAllQuery()  
> }  
> srb.setQuery(QueryBuilders.filteredQuery(query, filter));  
> srb.addSort(sort);
> 
> I have tried using srb.setSource() but it completely mangles the JSON  
> putting a comma just inside the first { and not after the query clause. I  
> have tried other methods in search request builder but with no success.  
> Using QueryBuilders.wrapperQuerry(this.queryJSON) doesn't work either.
> 
> I have spent hours working on this and would appreciate any pointers.

---

<div class="post-metadata">

**Author:** ![Robert\_Simmons](https://avatars.discourse-cdn.com/v4/letter/r/a87d85/32.png) [@Robert\_Simmons](https://discuss.elastic.co/u/Robert_Simmons)\
**Post date:** [March 23, 2012, 10:59pm UTC](https://discuss.elastic.co/t/how-can-i-pass-a-json-query-string-to-a-query-builder/7110/3 "2012-03-23T22:59:21Z")

</div>

Alas no because I need to combine the passed in string of json with the search filter and sort. Your method would require me to define the entire query. This is analogous to asking a user to pass you a where clause in an RDBMS app.

---

<div class="post-metadata">

**Author:** ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)\
**Post date:** [March 24, 2012, 1:00am UTC](https://discuss.elastic.co/t/how-can-i-pass-a-json-query-string-to-a-query-builder/7110/4 "2012-03-24T01:00:55Z")

</div>

I think QueryBuilders.wrapperQuery(jsonStr) is exactly what you want. Can  
you post some details why it doesn't work for you? Posting test jsonStr and  
the error that you are getting might be helpful.

On Friday, March 23, 2012 6:59:21 PM UTC-4, kraythe wrote:

> Alas no because I need to combine the passed in string of json with the  
> search filter and sort. Your method would require me to define the entire  
> query. This is analogous to asking a user to pass you a where clause in an  
> RDBMS app.

---

<div class="post-metadata">

**Author:** ![Robert\_Simmons](https://avatars.discourse-cdn.com/v4/letter/r/a87d85/32.png) [@Robert\_Simmons](https://discuss.elastic.co/u/Robert_Simmons)\
**Post date:** [March 24, 2012, 2:08am UTC](https://discuss.elastic.co/t/how-can-i-pass-a-json-query-string-to-a-query-builder/7110/5 "2012-03-24T02:08:57Z")

</div>

here is the relevant changes for the wrapper query:

```
    srb.setQuery(QueryBuilders.filteredQuery(*QueryBuilders.wrapperQuery(" 

```

"match\_all": {}")\*, filter));  
srb.addSort(sort);  
// -- Now execute the search  
final ActionFuture results = srb.execute();

The produced JSON from SRB is:

{  
"query" : {  
"filtered" : {  
"query" : {

- 

```
   "wrapper" : {*

```

- 

```
     "query" : "ICJtYXRjaF9hbGwiOiB7fQ=="*

```

- 

```
   }*
},
"filter" : {
  "geo_distance" : {
    "point" : [-81.480298, 41.107519],
    "distance" : "200.0mi",
    "distance_type" : "arc"
  }
}

```

}  
},  
"sort" : [ {  
"\_geo\_distance" : {  
"location.point" : [-81.480298, 41.107519],  
"distance\_type" : "plane"  
}  
} ]  
}

note the garbage inside the query. That is obviously a problem.

So that is why wrapper query doesn't work.

On Friday, March 23, 2012 7:00:55 PM UTC-6, Igor Motov wrote:

> I think QueryBuilders.​wrapperQuery(jsonStr) is exactly what you want. Can  
> you post some details why it doesn't work for you? Posting test jsonStr and  
> the error that you are getting might be helpful.
> 
> On Friday, March 23, 2012 6:59:21 PM UTC-4, kraythe wrote:
> 
> > Alas no because I need to combine the passed in string of json with the  
> > search filter and sort. Your method would require me to define the entire  
> > query. This is analogous to asking a user to pass you a where clause in an  
> > RDBMS app.

---

<div class="post-metadata">

**Author:** ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)\
**Post date:** [March 24, 2012, 2:23am UTC](https://discuss.elastic.co/t/how-can-i-pass-a-json-query-string-to-a-query-builder/7110/6 "2012-03-24T02:23:19Z")

</div>

ICJtYXRjaF9hbGwiOiB7fQ== is base64-encoded string " "match\_all": {}".  
However, it needs additional { ... } to work. Try this:

srb.setQuery(QueryBuilders.​filteredQuery(QueryBuilders.​wrapperQuery("{"match\_all":  
{}}"), filter));

On Friday, March 23, 2012 10:08:57 PM UTC-4, kraythe wrote:

> here is the relevant changes for the wrapper query:
> 
> ```
> srb.setQuery(QueryBuilders.​filteredQuery(*QueryBuilders.​wrapperQuery(" 
> 
> ```
> 
> "match\_all": {}")\*, filter));  
> srb.addSort(sort);  
> // -- Now execute the search  
> final ActionFuture results = srb.execute();
> 
> The produced JSON from SRB is:
> 
> {  
> "query" : {  
> "filtered" : {  
> "query" : {
> 
> - 
> 
> ```
> "wrapper" : {*
> 
> ```
> 
> - 
> 
> ```
> "query" : "ICJtYXRjaF9hbGwiOiB7fQ=="*
> 
> ```
> 
> - 
> 
> ```
> }*
> },
> "filter" : {
> "geo_distance" : {
> "point" : [-81.480298, 41.107519],
> "distance" : "200.0mi",
> "distance_type" : "arc"
> }
> }
> 
> ```
> 
> }  
> },  
> "sort" : [ {  
> "\_geo\_distance" : {  
> "location.point" : [-81.480298, 41.107519],  
> "distance\_type" : "plane"  
> }  
> } ]  
> }
> 
> note the garbage inside the query. That is obviously a problem.
> 
> So that is why wrapper query doesn't work.
> 
> On Friday, March 23, 2012 7:00:55 PM UTC-6, Igor Motov wrote:
> 
> > I think QueryBuilders.​​wrapperQuery(jsonStr) is exactly what you want.  
> > Can you post some details why it doesn't work for you? Posting test jsonStr  
> > and the error that you are getting might be helpful.
> > 
> > On Friday, March 23, 2012 6:59:21 PM UTC-4, kraythe wrote:
> > 
> > > Alas no because I need to combine the passed in string of json with the  
> > > search filter and sort. Your method would require me to define the entire  
> > > query. This is analogous to asking a user to pass you a where clause in an  
> > > RDBMS app.

---

<div class="post-metadata">

**Author:** ![Robert\_Simmons](https://avatars.discourse-cdn.com/v4/letter/r/a87d85/32.png) [@Robert\_Simmons](https://discuss.elastic.co/u/Robert_Simmons)\
**Post date:** [March 24, 2012, 5:12am UTC](https://discuss.elastic.co/t/how-can-i-pass-a-json-query-string-to-a-query-builder/7110/7 "2012-03-24T05:12:21Z")

</div>

Ok that actually did work, thanks. However the JSON rendering of the query  
leaves much to be desired. It is nearly impossible to read if you try to  
copy out the query and paste it into curl or something. Is there a way to  
get the output unencoded ?

On Friday, March 23, 2012 8:23:19 PM UTC-6, Igor Motov wrote:

> ICJtYXRjaF9hbGwiOiB7fQ== is base64-encoded string " "match\_all": {}".  
> However, it needs additional { ... } to work. Try this:
> 
> srb.setQuery(QueryBuilders.​​filteredQuery(QueryBuilders.​​wrapperQuery("{"match\_all":  
> {}}"), filter));
> 
> On Friday, March 23, 2012 10:08:57 PM UTC-4, kraythe wrote:
> 
> > here is the relevant changes for the wrapper query:
> > 
> > ```
> > srb.setQuery(QueryBuilders.​​filteredQuery(*QueryBuilders.​​wrapperQuery(" 
> > 
> > ```
> > 
> > "match\_all": {}")\*, filter));  
> > srb.addSort(sort);  
> > // -- Now execute the search  
> > final ActionFuture results = srb.execute();
> > 
> > The produced JSON from SRB is:
> > 
> > {  
> > "query" : {  
> > "filtered" : {  
> > "query" : {
> > 
> > - 
> > 
> > ```
> > "wrapper" : {*
> > 
> > ```
> > 
> > - 
> > 
> > ```
> > "query" : "ICJtYXRjaF9hbGwiOiB7fQ=="*
> > 
> > ```
> > 
> > - 
> > 
> > ```
> > }*
> > },
> > "filter" : {
> > "geo_distance" : {
> > "point" : [-81.480298, 41.107519],
> > "distance" : "200.0mi",
> > "distance_type" : "arc"
> > }
> > }
> > 
> > ```
> > 
> > }  
> > },  
> > "sort" : [ {  
> > "\_geo\_distance" : {  
> > "location.point" : [-81.480298, 41.107519],  
> > "distance\_type" : "plane"  
> > }  
> > } ]  
> > }
> > 
> > note the garbage inside the query. That is obviously a problem.
> > 
> > So that is why wrapper query doesn't work.
> > 
> > On Friday, March 23, 2012 7:00:55 PM UTC-6, Igor Motov wrote:
> > 
> > > I think QueryBuilders.​​​wrapperQuery(jsonStr) is exactly what you want.  
> > > Can you post some details why it doesn't work for you? Posting test jsonStr  
> > > and the error that you are getting might be helpful.
> > > 
> > > On Friday, March 23, 2012 6:59:21 PM UTC-4, kraythe wrote:
> > > 
> > > > Alas no because I need to combine the passed in string of json with the  
> > > > search filter and sort. Your method would require me to define the entire  
> > > > query. This is analogous to asking a user to pass you a where clause in an  
> > > > RDBMS app.

---

<div class="post-metadata">

**Author:** ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)\
**Post date:** [March 24, 2012, 1:42pm UTC](https://discuss.elastic.co/t/how-can-i-pass-a-json-query-string-to-a-query-builder/7110/8 "2012-03-24T13:42:40Z")

</div>

You could try parsing the query and then creating your own QueryBuilder  
that would regenerate this query on the client side, I guess.

```
public static QueryBuilder fromSource(String source) {
    byte[] data = source.getBytes();
    return fromSource(data, 0, data.length);
}

public static QueryBuilder fromSource(byte[] data, int offset, int 

```

length) {  
final Map\<String, Object\> source =  
XContentHelper.convertToMap(data, offset, length, true).v2();  
return new BaseQueryBuilder() {  
@Override  
protected void doXContent(XContentBuilder builder, Params  
params) throws IOException {  
for (Map.Entry\<String, Object\> entry : source.entrySet()) {  
builder.field(entry.getKey());  
Object value = entry.getValue();  
if (value == null) {  
builder.nullValue();  
} else {  
builder.value(value);  
}  
}  
}  
};  
}

On Saturday, March 24, 2012 1:12:21 AM UTC-4, kraythe wrote:

> Ok that actually did work, thanks. However the JSON rendering of the query  
> leaves much to be desired. It is nearly impossible to read if you try to  
> copy out the query and paste it into curl or something. Is there a way to  
> get the output unencoded ?
> 
> On Friday, March 23, 2012 8:23:19 PM UTC-6, Igor Motov wrote:
> 
> > ICJtYXRjaF9hbGwiOiB7fQ== is base64-encoded string " "match\_all": {}".  
> > However, it needs additional { ... } to work. Try this:
> > 
> > srb.setQuery(QueryBuilders.​​​filteredQuery(QueryBuilders.​​​wrapperQuery("{"match\_all":  
> > {}}"), filter));
> > 
> > On Friday, March 23, 2012 10:08:57 PM UTC-4, kraythe wrote:
> > 
> > > here is the relevant changes for the wrapper query:
> > > 
> > > ```
> > > srb.setQuery(QueryBuilders.​​​filteredQuery(*QueryBuilders.​​​wrapperQuery(" 
> > > 
> > > ```
> > > 
> > > "match\_all": {}")\*, filter));  
> > > srb.addSort(sort);  
> > > // -- Now execute the search  
> > > final ActionFuture results = srb.execute();
> > > 
> > > The produced JSON from SRB is:
> > > 
> > > {  
> > > "query" : {  
> > > "filtered" : {  
> > > "query" : {
> > > 
> > > - 
> > > 
> > > ```
> > > "wrapper" : {*
> > > 
> > > ```
> > > 
> > > - 
> > > 
> > > ```
> > > "query" : "ICJtYXRjaF9hbGwiOiB7fQ=="*
> > > 
> > > ```
> > > 
> > > - 
> > > 
> > > ```
> > > }*
> > > },
> > > "filter" : {
> > > "geo_distance" : {
> > > "point" : [-81.480298, 41.107519],
> > > "distance" : "200.0mi",
> > > "distance_type" : "arc"
> > > }
> > > }
> > > 
> > > ```
> > > 
> > > }  
> > > },  
> > > "sort" : [ {  
> > > "\_geo\_distance" : {  
> > > "location.point" : [-81.480298, 41.107519],  
> > > "distance\_type" : "plane"  
> > > }  
> > > } ]  
> > > }
> > > 
> > > note the garbage inside the query. That is obviously a problem.
> > > 
> > > So that is why wrapper query doesn't work.
> > > 
> > > On Friday, March 23, 2012 7:00:55 PM UTC-6, Igor Motov wrote:
> > > 
> > > > I think QueryBuilders.​​​​wrapperQuery(jsonStr) is exactly what you  
> > > > want. Can you post some details why it doesn't work for you? Posting test  
> > > > jsonStr and the error that you are getting might be helpful.
> > > > 
> > > > On Friday, March 23, 2012 6:59:21 PM UTC-4, kraythe wrote:
> > > > 
> > > > > Alas no because I need to combine the passed in string of json with  
> > > > > the search filter and sort. Your method would require me to define the  
> > > > > entire query. This is analogous to asking a user to pass you a where clause  
> > > > > in an RDBMS app.

---

<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:** [March 25, 2012, 11:56am UTC](https://discuss.elastic.co/t/how-can-i-pass-a-json-query-string-to-a-query-builder/7110/9 "2012-03-25T11:56:10Z")

</div>

The wrapper query just wraps another binary form of json and is parsed on  
the shard level when executing the search. The reason why it works like  
that is basically performance, instead of parsing the passed json on the  
client side in order to recreate it back into the full enclosing query  
structure, it simply passed the binary value as is and then the parsing is  
done once, when the search is executed on the shard.

We could potentially add a flag to the wrapper query to do the parsing the  
generation on the client side as well, then you will get a nicer "printing"  
of it, at the price of performance.

On Sat, Mar 24, 2012 at 7:12 AM, Robert Simmons [kraythe@gmail.com](mailto:kraythe@gmail.com) wrote:

> Ok that actually did work, thanks. However the JSON rendering of the query  
> leaves much to be desired. It is nearly impossible to read if you try to  
> copy out the query and paste it into curl or something. Is there a way to  
> get the output unencoded ?
> 
> On Friday, March 23, 2012 8:23:19 PM UTC-6, Igor Motov wrote:
> 
> > ICJtYXRjaF9hbGwiOiB7fQ== is base64-encoded string " "match\_all": {}".  
> > However, it needs additional { ... } to work. Try this:
> > 
> > srb.setQuery(QueryBuilders.​​filteredQuery(QueryBuilders.​​wrapperQuery("{"match\_all":  
> > {}}"), filter));
> > 
> > On Friday, March 23, 2012 10:08:57 PM UTC-4, kraythe wrote:
> > 
> > > here is the relevant changes for the wrapper query:
> > > 
> > > ```
> > > srb.setQuery(QueryBuilders.​​filteredQuery(*QueryBuilders.​​wrapperQuery("
> > > 
> > > ```
> > > 
> > > "match\_all": {}")\*, filter));  
> > > srb.addSort(sort);  
> > > // -- Now execute the search  
> > > final ActionFuture results = srb.execute();
> > > 
> > > The produced JSON from SRB is:
> > > 
> > > {  
> > > "query" : {  
> > > "filtered" : {  
> > > "query" : {
> > > 
> > > - 
> > > 
> > > ```
> > > "wrapper" : {*
> > > 
> > > ```
> > > 
> > > - 
> > > 
> > > ```
> > > "query" : "ICJtYXRjaF9hbGwiOiB7fQ=="*
> > > 
> > > ```
> > > 
> > > - 
> > > 
> > > ```
> > > }*
> > > },
> > > "filter" : {
> > > "geo_distance" : {
> > > "point" : [-81.480298, 41.107519],
> > > "distance" : "200.0mi",
> > > "distance_type" : "arc"
> > > }
> > > }
> > > 
> > > ```
> > > 
> > > }  
> > > },  
> > > "sort" : [ {  
> > > "\_geo\_distance" : {  
> > > "location.point" : [-81.480298, 41.107519],  
> > > "distance\_type" : "plane"  
> > > }  
> > > } ]  
> > > }
> > > 
> > > note the garbage inside the query. That is obviously a problem.
> > > 
> > > So that is why wrapper query doesn't work.
> > > 
> > > On Friday, March 23, 2012 7:00:55 PM UTC-6, Igor Motov wrote:
> > > 
> > > > I think QueryBuilders.​​​wrapperQuery(jsonStr) is exactly what you  
> > > > want. Can you post some details why it doesn't work for you? Posting test  
> > > > jsonStr and the error that you are getting might be helpful.
> > > > 
> > > > On Friday, March 23, 2012 6:59:21 PM UTC-4, kraythe wrote:
> > > > 
> > > > > Alas no because I need to combine the passed in string of json with  
> > > > > the search filter and sort. Your method would require me to define the  
> > > > > entire query. This is analogous to asking a user to pass you a where clause  
> > > > > in an RDBMS app.

---

<div class="post-metadata">

**Author:** ![Robert\_Simmons](https://avatars.discourse-cdn.com/v4/letter/r/a87d85/32.png) [@Robert\_Simmons](https://discuss.elastic.co/u/Robert_Simmons)\
**Post date:** [March 25, 2012, 4:26pm UTC](https://discuss.elastic.co/t/how-can-i-pass-a-json-query-string-to-a-query-builder/7110/10 "2012-03-25T16:26:07Z")

</div>

I think that would be a great addition shay. It would help out in situations of debugging and tracking. However, what would be better is if the querybuilders were serializable to begin with. Then the user could just pass me a querybuilder and I could send it over the wire to another process.

For context sake I will tell you what we are doing. We have a task using JPPF that is being serialized out to nodes to do a bunch of things including searching in ES. The tasks going out to the grid must be serializable and so this is where I hit the snag. I initially planned that they would send me a querybuilder which I would combine with the filter in the search phase of the task but since the querybuilder wasn't serializable I had to break it into a byte stream of JSON and use the wrapped query. Since I had potentially dozens of queries the Base64 encoding made it impossible to figure out which querry was being run where.

Thanks for the help all.

Oh and Shay, the javadoc needs a lot of help. I would be happy to help out there if you need it and are willing to answer a few thousand questions in the meantime. 🙂

---

<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:** [March 25, 2012, 6:20pm UTC](https://discuss.elastic.co/t/how-can-i-pass-a-json-query-string-to-a-query-builder/7110/11 "2012-03-25T18:20:37Z")

</div>

The query builders could be serialized, but you can just "build them as  
bytes" and then use the wrapper option if you want to construct more  
complex queries around the serialized versions. The only problem you have  
is the "toString" here, but its considerably better when it comes to  
performance. Note also, that the toString will use json to serialize the  
data, but by default, smile is used for the Java API, so its actually a  
binary value without the base64 conversion.

On Sun, Mar 25, 2012 at 6:26 PM, Robert Simmons [kraythe@gmail.com](mailto:kraythe@gmail.com) wrote:

> I think that would be a great addition shay. It would help out in  
> situations of debugging and tracking. However, what would be better is if  
> the querybuilders were serializable to begin with. Then the user could just  
> pass me a querybuilder and I could send it over the wire to another process.
> 
> For context sake I will tell you what we are doing. We have a task using  
> JPPF that is being serialized out to nodes to do a bunch of things  
> including searching in ES. The tasks going out to the grid must be  
> serializable and so this is where I hit the snag. I initially planned that  
> they would send me a querybuilder which I would combine with the filter in  
> the search phase of the task but since the querybuilder wasn't serializable  
> I had to break it into a byte stream of JSON and use the wrapped query.  
> Since I had potentially dozens of queries the Base64 encoding made it  
> impossible to figure out which querry was being run where.
> 
> Thanks for the help all.
> 
> Oh and Shay, the javadoc needs a lot of help. I would be happy to help out  
> there if you need it and are willing to answer a few thousand questions in  
> the meantime. 🙂

---

<div class="post-metadata">

**Author:** ![Robert\_Simmons](https://avatars.discourse-cdn.com/v4/letter/r/a87d85/32.png) [@Robert\_Simmons](https://discuss.elastic.co/u/Robert_Simmons)\
**Post date:** [March 26, 2012, 3:19pm UTC](https://discuss.elastic.co/t/how-can-i-pass-a-json-query-string-to-a-query-builder/7110/12 "2012-03-26T15:19:33Z")

</div>

I could see instances where making the query builders serializable would  
pay off pretty well. You may have to add more match terms to a passed in  
query for example and it would make it much easier to use in parallel  
processing frameworks and so on. I guess there are workarounds but I would  
say that if it isn't that hard to make them serializable it would be a big  
win.

But at any rate the docs on those query builders need to be updated and a  
lot more examples given.

-- Robert

---

<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, 3:34am UTC](https://discuss.elastic.co/t/how-can-i-pass-a-json-query-string-to-a-query-builder/7110/13 "2017-07-06T03:34:46Z")

</div>


