# 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/7109>\
**Category:** Elasticsearch\
**Created:** [March 23, 2012, 7:57pm UTC](https://discuss.elastic.co/t/how-can-i-pass-a-json-query-string-to-a-query-builder/7109 "2012-03-23T19:57:02Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![kraythe](https://avatars.discourse-cdn.com/v4/letter/k/dec6dc/32.png) [@kraythe](https://discuss.elastic.co/u/kraythe)\
**Post date:** [March 23, 2012, 7:57pm UTC](https://discuss.elastic.co/t/how-can-i-pass-a-json-query-string-to-a-query-builder/7109/1 "2012-03-23T19:57:02Z")

</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:** ![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:35am UTC](https://discuss.elastic.co/t/how-can-i-pass-a-json-query-string-to-a-query-builder/7109/2 "2017-07-06T03:35:04Z")

</div>


