# Unable to execute query+aggs in same query string via SearchRequestBuilder

**URL:** <https://discuss.elastic.co/t/unable-to-execute-query-aggs-in-same-query-string-via-searchrequestbuilder/61436>\
**Category:** Elasticsearch\
**Created:** [September 24, 2016, 9:32am UTC](https://discuss.elastic.co/t/unable-to-execute-query-aggs-in-same-query-string-via-searchrequestbuilder/61436 "2016-09-24T09:32:32Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![cforce](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cforce/32/12132_2.png) [@cforce](https://discuss.elastic.co/u/cforce)\
**Post date:** [September 24, 2016, 9:32am UTC](https://discuss.elastic.co/t/unable-to-execute-query-aggs-in-same-query-string-via-searchrequestbuilder/61436/1 "2016-09-24T09:32:32Z")

</div>

hi  
i am trying to execite and ealstic search query with aggrgates .. but seems like the "query" api don't allows to have "aggs" inside

My Java Code:  
SearchRequestBuilder searchRequestBuilder = elasticsearchTemplate.getClient()  
.prepareSearch(ElasticSearchConfig.AUDIENCE\_INDEX\_NAME);  
searchRequestBuilder.setSearchType(SearchType.DFS\_QUERY\_THEN\_FETCH);  
searchRequestBuilder.setTypes(ElasticSearchConfig.AUDIENCE\_DOC\_TYPES);  
searchRequestBuilder.setQuery(esQuery);  
searchRequestBuilder.setQuery(esQuery);

```
    // searchRequestBuilder.execute().actionGet().getHits().getTotalHits();
    Filters agg = searchRequestBuilder.execute().actionGet().getAggregations()
            .get(AUDIENCE_AGGREGATE_IDENT);
    // sort services with most hits first
    Bucket maxBucket = agg.getBuckets().stream()
            .max((serviceA, serviceB) -> serviceA.getDocCount() < serviceB.getDocCount() ? -1
                    : serviceA.getDocCount() > serviceB.getDocCount() ? 1 : 0)
            .get();

```

My Query:  
{  
"query": {  
"filtered": {  
"query": {  
"match\_all": {  
}  
},  
"filter": {  
"ids": {  
"values": ["1","2"]  
}  
}  
}  
},  
"aggs": {  
"audience": {  
"filters": {  
"filters": {  
"deviceonline": { "term": {  
"gender": "m"  
} }  
}  
}  
}  
}  
}

The Error it throws is "nested: QueryParsingException[[audience] No query registered for [query]]".

From stackoverflow comments i guess the SearchRequestBuilder is adding another "query":{...} around the complete search string and than complains to have two of them. Does that mean only "query" content ist allowed gere but no "aggs" - searchRequestBuilder.setQuery( ...ONLY "query" string here)  
I can't remove the "query" because its needed on the same level as the "aggs" bot below. Btw i test the complete query against an es instance via the webfrontend [https://www.found.no/play/#search](https://www.found.no/play/#search) - that worked (being converted to yml before)  
Does that mean i have to find stgh like searchRequestBuilder.setAggregations(String) and call that wih only the "aggs" part of my query?  
However i only find "strange" method signatures for setAggregations() . i don't know how to get my "aggs" filter expression converted into one of the required param types.

---

<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 5, 2017, 10:17pm UTC](https://discuss.elastic.co/t/unable-to-execute-query-aggs-in-same-query-string-via-searchrequestbuilder/61436/2 "2017-07-05T22:17:31Z")

</div>


