# Use Elasticsearch FIlter JSON directly in JAVA Api

**URL:** <https://discuss.elastic.co/t/use-elasticsearch-filter-json-directly-in-java-api/11857>\
**Category:** Elasticsearch\
**Created:** [May 7, 2013, 2:52pm UTC](https://discuss.elastic.co/t/use-elasticsearch-filter-json-directly-in-java-api/11857 "2013-05-07T14:52:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ankit\_Jain](https://avatars.discourse-cdn.com/v4/letter/a/96bed5/32.png) [@Ankit\_Jain](https://discuss.elastic.co/u/Ankit_Jain)\
**Post date:** [May 7, 2013, 2:52pm UTC](https://discuss.elastic.co/t/use-elasticsearch-filter-json-directly-in-java-api/11857/1 "2013-05-07T14:52:03Z")

</div>

Hi All,

I am passing the JSON String to wrapperFilter method.

FilterBuilder filterBuilder = FilterBuilders.wrapperFilter("{"filter":  
{"and"=[{"term":{"c24":TEXT} }, {"term":{"C25":text} }]} }");

FilteredQueryBuilder qb = QueryBuilders.filteredQuery(queryBuilder,  
filterBuilder);

System.out.println("Query : " + qb);

SearchResponse searchResponse = null;  
// SortBuilder sort=SortBuilders.fieldSort("c0").order(SortOrder.DESC);  
searchResponse = client.prepareSearch("ipdr").addField("c0")  
.setQuery(qb).setSize(10000)  
.setSearchType(SearchType.QUERY\_THEN\_FETCH).execute()  
.actionGet();  
for (SearchHit hit : searchResponse.getHits()) {

try {  
System.out.println(hit.getFields().get("c0").getValue()  
.toString());  
} catch (Exception e) {  
e.printStackTrace();  
}  
}

But, ES will internally fire following query to fetch records:

{  
"filtered" : {  
"query" : {  
"match\_all" : { }  
},  
"filter" : {  
"wrapper" : {  
"filter" :  
"eyJmaWx0ZXIiOiB7ImFuZCI9W3sidGVybSI6eyJjMjQiOlRFWFR9IH0sIHsidGVybSI6eyJDMjUiOnRleHR9IH1dfSB9"  
}  
}  
}  
}

Can you guide me to sort out the issue.

Thanks,  
Ankit Jain

--  
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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![brian\_yoder](https://avatars.discourse-cdn.com/v4/letter/b/f1d935/32.png) [@brian\_yoder](https://discuss.elastic.co/u/brian_yoder)\
**Post date:** [May 7, 2013, 9:51pm UTC](https://discuss.elastic.co/t/use-elasticsearch-filter-json-directly-in-java-api/11857/2 "2013-05-07T21:51:11Z")

</div>

I find it's much better and easier to create searches in Java (you're doing  
that) and then use Java all the way and not try to create JSON (the hard  
way) or try to create Java-escaped JSON string (the really really hard way).

Start with the JavaDoc at [http://es-java-doc.medcl.net/](http://es-java-doc.medcl.net/)

Look at the static methods of the QueryBuilders and FilterBuilders classes.  
They'll create your query and filter terms as needed. This is much easier.

The create an option in your application to call the root query's (or  
filter's) toString method to create the JSON string for posting to this  
newsgroup.

But do the work in Java all the way, and you'll find it much easier.

Hope this helps!

On Tuesday, May 7, 2013 10:52:03 AM UTC-4, Ankit Jain wrote:

> Hi All,
> 
> I am passing the JSON String to wrapperFilter method.
> 
> FilterBuilder filterBuilder = FilterBuilders.wrapperFilter("{"filter":  
> {"and"=[{"term":{"c24":TEXT} }, {"term":{"C25":text} }]} }");
> 
> FilteredQueryBuilder qb = QueryBuilders.filteredQuery(queryBuilder,  
> filterBuilder);
> 
> System.out.println("Query : " + qb);
> 
> SearchResponse searchResponse = null;  
> // SortBuilder sort=SortBuilders.fieldSort("c0").order(SortOrder.DESC);  
> searchResponse = client.prepareSearch("ipdr").addField("c0")  
> .setQuery(qb).setSize(10000)  
> .setSearchType(SearchType.QUERY\_THEN\_FETCH).execute()  
> .actionGet();  
> for (SearchHit hit : searchResponse.getHits()) {
> 
> try {  
> System.out.println(hit.getFields().get("c0").getValue()  
> .toString());  
> } catch (Exception e) {  
> e.printStackTrace();  
> }  
> }
> 
> But, ES will internally fire following query to fetch records:
> 
> {  
> "filtered" : {  
> "query" : {  
> "match\_all" : { }  
> },  
> "filter" : {  
> "wrapper" : {  
> "filter" :  
> "eyJmaWx0ZXIiOiB7ImFuZCI9W3sidGVybSI6eyJjMjQiOlRFWFR9IH0sIHsidGVybSI6eyJDMjUiOnRleHR9IH1dfSB9"  
> }  
> }  
> }  
> }
> 
> Can you guide me to sort out the issue.
> 
> Thanks,  
> Ankit Jain

--  
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).  
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:** [May 8, 2013, 5:25am UTC](https://discuss.elastic.co/t/use-elasticsearch-filter-json-directly-in-java-api/11857/3 "2013-05-08T05:25:28Z")

</div>

Have also a look at [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/java-api/search/)

--  
David 😉  
Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs

Le 7 mai 2013 à 23:51, InquiringMind [brian.from.fl@gmail.com](mailto:brian.from.fl@gmail.com) a écrit :

I find it's much better and easier to create searches in Java (you're doing that) and then use Java all the way and not try to create JSON (the hard way) or try to create Java-escaped JSON string (the really really hard way).

Start with the JavaDoc at [http://es-java-doc.medcl.net/](http://es-java-doc.medcl.net/)

Look at the static methods of the QueryBuilders and FilterBuilders classes. They'll create your query and filter terms as needed. This is much easier.

The create an option in your application to call the root query's (or filter's) toString method to create the JSON string for posting to this newsgroup.

But do the work in Java all the way, and you'll find it much easier.

Hope this helps!

On Tuesday, May 7, 2013 10:52:03 AM UTC-4, Ankit Jain wrote:

> Hi All,
> 
> I am passing the JSON String to wrapperFilter method.
> 
> FilterBuilder filterBuilder = FilterBuilders.wrapperFilter("{"filter": {"and"=[{"term":{"c24":TEXT} }, {"term":{"C25":text} }]} }");
> 
> ```
> FilteredQueryBuilder qb = QueryBuilders.filteredQuery(queryBuilder, filterBuilder);
> 
>   
> System.out.println("Query : " + qb);
> 
> SearchResponse searchResponse = null;
> // SortBuilder sort=SortBuilders.fieldSort("c0").order(SortOrder.DESC);
> searchResponse = client.prepareSearch("ipdr").addField("c0")
> .setQuery(qb).setSize(10000)
> .setSearchType(SearchType.QUERY_THEN_FETCH).execute()
> .actionGet();
> for (SearchHit hit : searchResponse.getHits()) {
> 
> try {
> System.out.println(hit.getFields().get("c0").getValue()
> .toString());
> } catch (Exception e) {
> e.printStackTrace();
> }
> }
> 
> ```
> 
> But, ES will internally fire following query to fetch records:
> 
> {  
> "filtered" : {  
> "query" : {  
> "match\_all" : { }  
> },  
> "filter" : {  
> "wrapper" : {  
> "filter" : "eyJmaWx0ZXIiOiB7ImFuZCI9W3sidGVybSI6eyJjMjQiOlRFWFR9IH0sIHsidGVybSI6eyJDMjUiOnRleHR9IH1dfSB9"  
> }  
> }  
> }  
> }
> 
> Can you guide me to sort out the issue.
> 
> Thanks,  
> Ankit Jain

--  
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).  
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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<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, 2:37am UTC](https://discuss.elastic.co/t/use-elasticsearch-filter-json-directly-in-java-api/11857/4 "2017-07-06T02:37:46Z")

</div>


