# Aggregation framework, Java API

**URL:** <https://discuss.elastic.co/t/aggregation-framework-java-api/19658>\
**Category:** Elasticsearch\
**Created:** [September 8, 2014, 9:26am UTC](https://discuss.elastic.co/t/aggregation-framework-java-api/19658 "2014-09-08T09:26:12Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Emanuel\_Buzek](https://avatars.discourse-cdn.com/v4/letter/e/df705f/32.png) [@Emanuel\_Buzek](https://discuss.elastic.co/u/Emanuel_Buzek)\
**Post date:** [September 8, 2014, 9:26am UTC](https://discuss.elastic.co/t/aggregation-framework-java-api/19658/1 "2014-09-08T09:26:12Z")

</div>

Hi there,  
I just used the elasticsearch aggregations through the Java API for the  
first time.

All I wanted was a simple min/max/sum/avg, so I used the Stats aggregation.  
However, I was very surprised that the filter in the SearchRequestBuilder  
is ignored, so I had to wrap the Stats Aggregation into FilterAggregation.

Getting the aggregation result seems a bit tedious:

InternalStats stats =  
(InternalStats)((InternalFilter)a).getAggregations().asList().get(0);

Maybe I am using the Java API wrong (I hope I am, otherwise it's imho  
poorly designed.) Can anyone point me to an example how to access the  
aggregation results from Java better?

Also, I think that the aggregation should be filtered by default. If I  
specify the filter with a query or post filter:

queryBuilder = QueryBuilders.filteredQuery(queryBuilder, filterBuilder);

searchRequestBuilder.setQuery(queryBuilder);

and then add an aggregation GET to the same searchRequestBuilder, I think  
it's very unintuitive if the aggregation is computed globally. Anyone has  
this feeling as well?

thanks, emanuel

--  
Emanuel Buzek  
Software Engineer, ROKE.cz [http://www.roke.cz](http://www.roke.cz)  
tel: +420 776 54 26 26

--  
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/CAL1sZ7kghAa2Pvgba05yeraBbwjQqHG9hKG43M6FqjpRCay30w%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAL1sZ7kghAa2Pvgba05yeraBbwjQqHG9hKG43M6FqjpRCay30w%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![mooky](https://avatars.discourse-cdn.com/v4/letter/m/43a26b/32.png) [@mooky](https://discuss.elastic.co/u/mooky)\
**Post date:** [September 8, 2014, 12:41pm UTC](https://discuss.elastic.co/t/aggregation-framework-java-api/19658/2 "2014-09-08T12:41:05Z")

</div>

The aggregation takes into account a query - but not a post-filter. I'm not  
sure of the rationale behind the difference.

The java api for traversing results is quite painful - but I think a good  
part of that is due to Java & the fact that there is very little  
polymorphic behaviour between aggregation results (some have single  
results, others have buckets, some have sub-aggregations, some dont).  
The only alternative that I can think of is a completely type-less  
navigation of the data - which does little more than navigate the JSON  
document.

Hope that helps a bit.

On Monday, 8 September 2014 10:26:44 UTC+1, Emanuel Buzek wrote:

> Hi there,  
> I just used the elasticsearch aggregations through the Java API for the  
> first time.
> 
> All I wanted was a simple min/max/sum/avg, so I used the Stats  
> aggregation. However, I was very surprised that the filter in the  
> SearchRequestBuilder is ignored, so I had to wrap the Stats Aggregation  
> into FilterAggregation.
> 
> Getting the aggregation result seems a bit tedious:
> 
> InternalStats stats =  
> (InternalStats)((InternalFilter)a).getAggregations().asList().get(0);
> 
> Maybe I am using the Java API wrong (I hope I am, otherwise it's imho  
> poorly designed.) Can anyone point me to an example how to access the  
> aggregation results from Java better?
> 
> Also, I think that the aggregation should be filtered by default. If I  
> specify the filter with a query or post filter:
> 
> queryBuilder = QueryBuilders.filteredQuery(queryBuilder, filterBuilder);
> 
> searchRequestBuilder.setQuery(queryBuilder);
> 
> and then add an aggregation GET to the same searchRequestBuilder, I think  
> it's very unintuitive if the aggregation is computed globally. Anyone has  
> this feeling as well?
> 
> thanks, emanuel
> 
> --  
> Emanuel Buzek  
> Software Engineer, ROKE.cz [http://www.roke.cz](http://www.roke.cz)  
> tel: +420 776 54 26 26

--  
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/a2e3a1e1-912a-4257-b938-6036d0c9d3ff%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/a2e3a1e1-912a-4257-b938-6036d0c9d3ff%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Ivan](https://avatars.discourse-cdn.com/v4/letter/i/df788c/32.png) [@Ivan](https://discuss.elastic.co/u/Ivan)\
**Post date:** [September 8, 2014, 3:21pm UTC](https://discuss.elastic.co/t/aggregation-framework-java-api/19658/3 "2014-09-08T15:21:04Z")

</div>

Which filter was ignored? I am assuming you meant the post filter (which  
might be still called filter at the Java API), which in this case the  
filter is bypassed by design. Post filters allow you to filter the  
documents returned, but leave the aggregations as is. Sounds like you are  
looking for filtered queries. The method name is ambiguous, which is why it  
has been renamed (and should actually be deprecated in the API).

Best way to learn the Java API is via the unit tests, but I do agree, there  
is no clean way to write elegant code due to explicit casting.

[https://github.com/elasticsearch/elasticsearch/tree/master/src/test/java/org/elasticsearch/search/aggregations](https://github.com/elasticsearch/elasticsearch/tree/master/src/test/java/org/elasticsearch/search/aggregations)

Cheers,

Ivan

On Mon, Sep 8, 2014 at 5:41 AM, mooky [nick.minutello@gmail.com](mailto:nick.minutello@gmail.com) wrote:

> The aggregation takes into account a query - but not a post-filter. I'm  
> not sure of the rationale behind the difference.
> 
> The java api for traversing results is quite painful - but I think a good  
> part of that is due to Java & the fact that there is very little  
> polymorphic behaviour between aggregation results (some have single  
> results, others have buckets, some have sub-aggregations, some dont).  
> The only alternative that I can think of is a completely type-less  
> navigation of the data - which does little more than navigate the JSON  
> document.
> 
> Hope that helps a bit.
> 
> On Monday, 8 September 2014 10:26:44 UTC+1, Emanuel Buzek wrote:
> 
> > Hi there,  
> > I just used the elasticsearch aggregations through the Java API for the  
> > first time.
> > 
> > All I wanted was a simple min/max/sum/avg, so I used the Stats  
> > aggregation. However, I was very surprised that the filter in the  
> > SearchRequestBuilder is ignored, so I had to wrap the Stats Aggregation  
> > into FilterAggregation.
> > 
> > Getting the aggregation result seems a bit tedious:
> > 
> > InternalStats stats = (InternalStats)((InternalFilter)a).  
> > getAggregations().asList().get(0);
> > 
> > Maybe I am using the Java API wrong (I hope I am, otherwise it's imho  
> > poorly designed.) Can anyone point me to an example how to access the  
> > aggregation results from Java better?
> > 
> > Also, I think that the aggregation should be filtered by default. If I  
> > specify the filter with a query or post filter:
> > 
> > queryBuilder = QueryBuilders.filteredQuery(queryBuilder, filterBuilder);
> > 
> > searchRequestBuilder.setQuery(queryBuilder);
> > 
> > and then add an aggregation GET to the same searchRequestBuilder, I think  
> > it's very unintuitive if the aggregation is computed globally. Anyone has  
> > this feeling as well?
> > 
> > thanks, emanuel
> > 
> > --  
> > Emanuel Buzek  
> > Software Engineer, ROKE.cz [http://www.roke.cz](http://www.roke.cz)  
> > tel: +420 776 54 26 26
> 
> --  
> 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/a2e3a1e1-912a-4257-b938-6036d0c9d3ff%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/a2e3a1e1-912a-4257-b938-6036d0c9d3ff%40googlegroups.com)  
> [https://groups.google.com/d/msgid/elasticsearch/a2e3a1e1-912a-4257-b938-6036d0c9d3ff%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/a2e3a1e1-912a-4257-b938-6036d0c9d3ff%40googlegroups.com?utm_medium=email&utm_source=footer)  
> .
> 
> For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
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/CALY%3DcQA\_Ca%2B-dZOOcAxX97pDEmgoS53wLaoBtEiHb2xFHqMxnA%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CALY%3DcQA_Ca%2B-dZOOcAxX97pDEmgoS53wLaoBtEiHb2xFHqMxnA%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Emanuel\_Buzek](https://avatars.discourse-cdn.com/v4/letter/e/df705f/32.png) [@Emanuel\_Buzek](https://discuss.elastic.co/u/Emanuel_Buzek)\
**Post date:** [September 9, 2014, 9:26am UTC](https://discuss.elastic.co/t/aggregation-framework-java-api/19658/4 "2014-09-09T09:26:16Z")

</div>

Thanks Ivan.

Yes, it was the post filter which was ignored. We use filtered query only  
when the user sends a query string, otherwise (when only exact filters for  
specific columns are specified) we use the post filter. It seems strange to  
me to use the FilteredQuery when the query string is empty, but perhaps  
that would be the most straight forward way of doing this.

thank you,  
emanuel

Dne pondělí, 8. září 2014 17:21:21 UTC+2 Ivan Brusic napsal(a):

> Which filter was ignored? I am assuming you meant the post filter (which  
> might be still called filter at the Java API), which in this case the  
> filter is bypassed by design. Post filters allow you to filter the  
> documents returned, but leave the aggregations as is. Sounds like you are  
> looking for filtered queries. The method name is ambiguous, which is why it  
> has been renamed (and should actually be deprecated in the API).
> 
> Best way to learn the Java API is via the unit tests, but I do agree,  
> there is no clean way to write elegant code due to explicit casting.
> 
> [https://github.com/elasticsearch/elasticsearch/tree/master/src/test/java/org/elasticsearch/search/aggregations](https://github.com/elasticsearch/elasticsearch/tree/master/src/test/java/org/elasticsearch/search/aggregations)
> 
> Cheers,
> 
> Ivan
> 
> On Mon, Sep 8, 2014 at 5:41 AM, mooky \<[nick.mi...@gmail.com](mailto:nick.mi...@gmail.com) \<javascript:\>\>  
> wrote:
> 
> > The aggregation takes into account a query - but not a post-filter. I'm  
> > not sure of the rationale behind the difference.
> > 
> > The java api for traversing results is quite painful - but I think a good  
> > part of that is due to Java & the fact that there is very little  
> > polymorphic behaviour between aggregation results (some have single  
> > results, others have buckets, some have sub-aggregations, some dont).  
> > The only alternative that I can think of is a completely type-less  
> > navigation of the data - which does little more than navigate the JSON  
> > document.
> > 
> > Hope that helps a bit.
> > 
> > On Monday, 8 September 2014 10:26:44 UTC+1, Emanuel Buzek wrote:
> > 
> > > Hi there,  
> > > I just used the elasticsearch aggregations through the Java API for the  
> > > first time.
> > > 
> > > All I wanted was a simple min/max/sum/avg, so I used the Stats  
> > > aggregation. However, I was very surprised that the filter in the  
> > > SearchRequestBuilder is ignored, so I had to wrap the Stats Aggregation  
> > > into FilterAggregation.
> > > 
> > > Getting the aggregation result seems a bit tedious:
> > > 
> > > InternalStats stats = (InternalStats)((InternalFilter)a).  
> > > getAggregations().asList().get(0);
> > > 
> > > Maybe I am using the Java API wrong (I hope I am, otherwise it's imho  
> > > poorly designed.) Can anyone point me to an example how to access the  
> > > aggregation results from Java better?
> > > 
> > > Also, I think that the aggregation should be filtered by default. If I  
> > > specify the filter with a query or post filter:
> > > 
> > > queryBuilder = QueryBuilders.filteredQuery(queryBuilder, filterBuilder);
> > > 
> > > searchRequestBuilder.setQuery(queryBuilder);
> > > 
> > > and then add an aggregation GET to the same searchRequestBuilder, I  
> > > think it's very unintuitive if the aggregation is computed globally. Anyone  
> > > has this feeling as well?
> > > 
> > > thanks, emanuel
> > > 
> > > --  
> > > Emanuel Buzek  
> > > Software Engineer, ROKE.cz [http://www.roke.cz](http://www.roke.cz)  
> > > tel: +420 776 54 26 26
> > 
> > --  
> > 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/a2e3a1e1-912a-4257-b938-6036d0c9d3ff%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/a2e3a1e1-912a-4257-b938-6036d0c9d3ff%40googlegroups.com)  
> > [https://groups.google.com/d/msgid/elasticsearch/a2e3a1e1-912a-4257-b938-6036d0c9d3ff%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/a2e3a1e1-912a-4257-b938-6036d0c9d3ff%40googlegroups.com?utm_medium=email&utm_source=footer)  
> > .
> > 
> > For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
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/1eb0e2c2-91e9-43b0-b076-decd33fa6440%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/1eb0e2c2-91e9-43b0-b076-decd33fa6440%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Ivan](https://avatars.discourse-cdn.com/v4/letter/i/df788c/32.png) [@Ivan](https://discuss.elastic.co/u/Ivan)\
**Post date:** [September 9, 2014, 4:46pm UTC](https://discuss.elastic.co/t/aggregation-framework-java-api/19658/5 "2014-09-09T16:46:16Z")

</div>

A filtered query with no explicit query will ultimately be translated into  
a match-all/constant-score query at the Lucene level. I prefer to  
explicitly define all my match all queries and use the specific post filter  
name, and not the old filter name, which was deprecated due to its  
ambiguity.

Besides, even if you did not have aggregations, you want to do as much  
pre-filtered as you can. Post filters work on documents that have been  
scored. No need to score documents that will eventually be filtered. Post  
filters have some benefits, but it seems like they do not apply in this  
case.

--  
Ivan

On Tue, Sep 9, 2014 at 2:26 AM, Emanuel Buzek [emanuel.buzek@roke.cz](mailto:emanuel.buzek@roke.cz) wrote:

> Thanks Ivan.
> 
> Yes, it was the post filter which was ignored. We use filtered query only  
> when the user sends a query string, otherwise (when only exact filters for  
> specific columns are specified) we use the post filter. It seems strange to  
> me to use the FilteredQuery when the query string is empty, but perhaps  
> that would be the most straight forward way of doing this.
> 
> thank you,  
> emanuel
> 
> Dne pondělí, 8. září 2014 17:21:21 UTC+2 Ivan Brusic napsal(a):
> 
> > Which filter was ignored? I am assuming you meant the post filter (which  
> > might be still called filter at the Java API), which in this case the  
> > filter is bypassed by design. Post filters allow you to filter the  
> > documents returned, but leave the aggregations as is. Sounds like you are  
> > looking for filtered queries. The method name is ambiguous, which is why it  
> > has been renamed (and should actually be deprecated in the API).
> > 
> > Best way to learn the Java API is via the unit tests, but I do agree,  
> > there is no clean way to write elegant code due to explicit casting.
> > 
> > [GitHub - elastic/elasticsearch: Free and Open, Distributed, RESTful Search Engine](https://github.com/elasticsearch/elasticsearch/)  
> > tree/master/src/test/java/org/elasticsearch/search/aggregations
> > 
> > Cheers,
> > 
> > Ivan
> > 
> > On Mon, Sep 8, 2014 at 5:41 AM, mooky [nick.mi...@gmail.com](mailto:nick.mi...@gmail.com) wrote:
> > 
> > > The aggregation takes into account a query - but not a post-filter. I'm  
> > > not sure of the rationale behind the difference.
> > > 
> > > The java api for traversing results is quite painful - but I think a  
> > > good part of that is due to Java & the fact that there is very little  
> > > polymorphic behaviour between aggregation results (some have single  
> > > results, others have buckets, some have sub-aggregations, some dont).  
> > > The only alternative that I can think of is a completely type-less  
> > > navigation of the data - which does little more than navigate the JSON  
> > > document.
> > > 
> > > Hope that helps a bit.
> > > 
> > > On Monday, 8 September 2014 10:26:44 UTC+1, Emanuel Buzek wrote:
> > > 
> > > > Hi there,  
> > > > I just used the elasticsearch aggregations through the Java API for the  
> > > > first time.
> > > > 
> > > > All I wanted was a simple min/max/sum/avg, so I used the Stats  
> > > > aggregation. However, I was very surprised that the filter in the  
> > > > SearchRequestBuilder is ignored, so I had to wrap the Stats Aggregation  
> > > > into FilterAggregation.
> > > > 
> > > > Getting the aggregation result seems a bit tedious:
> > > > 
> > > > InternalStats stats = (InternalStats)((InternalFilter)a).  
> > > > getAggregations().asList().get(0);
> > > > 
> > > > Maybe I am using the Java API wrong (I hope I am, otherwise it's imho  
> > > > poorly designed.) Can anyone point me to an example how to access the  
> > > > aggregation results from Java better?
> > > > 
> > > > Also, I think that the aggregation should be filtered by default. If I  
> > > > specify the filter with a query or post filter:
> > > > 
> > > > queryBuilder = QueryBuilders.filteredQuery(queryBuilder,  
> > > > filterBuilder);
> > > > 
> > > > searchRequestBuilder.setQuery(queryBuilder);
> > > > 
> > > > and then add an aggregation GET to the same searchRequestBuilder, I  
> > > > think it's very unintuitive if the aggregation is computed globally. Anyone  
> > > > has this feeling as well?
> > > > 
> > > > thanks, emanuel
> > > > 
> > > > --  
> > > > Emanuel Buzek  
> > > > Software Engineer, ROKE.cz [http://www.roke.cz](http://www.roke.cz)  
> > > > tel: +420 776 54 26 26
> > > 
> > > --  
> > > 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/](https://groups.google.com/d/)  
> > > msgid/elasticsearch/a2e3a1e1-912a-4257-b938-6036d0c9d3ff%  
> > > [40googlegroups.com](http://40googlegroups.com)  
> > > [https://groups.google.com/d/msgid/elasticsearch/a2e3a1e1-912a-4257-b938-6036d0c9d3ff%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/a2e3a1e1-912a-4257-b938-6036d0c9d3ff%40googlegroups.com?utm_medium=email&utm_source=footer)  
> > > .
> > > 
> > > For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).
> > 
> > --  
> > 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/1eb0e2c2-91e9-43b0-b076-decd33fa6440%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/1eb0e2c2-91e9-43b0-b076-decd33fa6440%40googlegroups.com)  
> > [https://groups.google.com/d/msgid/elasticsearch/1eb0e2c2-91e9-43b0-b076-decd33fa6440%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/1eb0e2c2-91e9-43b0-b076-decd33fa6440%40googlegroups.com?utm_medium=email&utm_source=footer)  
> > .
> 
> For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
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/CALY%3DcQCwrV%3DS2-%2BS0GLr95LoSHCxsxCfFWqc88D6bffDr1WMFw%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CALY%3DcQCwrV%3DS2-%2BS0GLr95LoSHCxsxCfFWqc88D6bffDr1WMFw%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Emanuel\_Buzek](https://avatars.discourse-cdn.com/v4/letter/e/df705f/32.png) [@Emanuel\_Buzek](https://discuss.elastic.co/u/Emanuel_Buzek)\
**Post date:** [September 15, 2014, 12:55pm UTC](https://discuss.elastic.co/t/aggregation-framework-java-api/19658/6 "2014-09-15T12:55:19Z")

</div>

Ivan, thanks a lot for the reply, I switched to FilteredQuery (using  
matchAll when no query is submitted) and that simplified my code a lot. It  
also makes more sense than using post filters and filtered aggregations...  
best, emanuel

Dne úterý, 9. září 2014 18:46:25 UTC+2 Ivan Brusic napsal(a):

> A filtered query with no explicit query will ultimately be translated into  
> a match-all/constant-score query at the Lucene level. I prefer to  
> explicitly define all my match all queries and use the specific post filter  
> name, and not the old filter name, which was deprecated due to its  
> ambiguity.
> 
> Besides, even if you did not have aggregations, you want to do as much  
> pre-filtered as you can. Post filters work on documents that have been  
> scored. No need to score documents that will eventually be filtered. Post  
> filters have some benefits, but it seems like they do not apply in this  
> case.
> 
> --  
> Ivan
> 
> On Tue, Sep 9, 2014 at 2:26 AM, Emanuel Buzek \<emanue...@roke.cz  
> \<javascript:\>\> wrote:
> 
> > Thanks Ivan.
> > 
> > Yes, it was the post filter which was ignored. We use filtered query only  
> > when the user sends a query string, otherwise (when only exact filters for  
> > specific columns are specified) we use the post filter. It seems strange to  
> > me to use the FilteredQuery when the query string is empty, but perhaps  
> > that would be the most straight forward way of doing this.
> > 
> > thank you,  
> > emanuel
> > 
> > Dne pondělí, 8. září 2014 17:21:21 UTC+2 Ivan Brusic napsal(a):
> > 
> > > Which filter was ignored? I am assuming you meant the post filter (which  
> > > might be still called filter at the Java API), which in this case the  
> > > filter is bypassed by design. Post filters allow you to filter the  
> > > documents returned, but leave the aggregations as is. Sounds like you are  
> > > looking for filtered queries. The method name is ambiguous, which is why it  
> > > has been renamed (and should actually be deprecated in the API).
> > > 
> > > Best way to learn the Java API is via the unit tests, but I do agree,  
> > > there is no clean way to write elegant code due to explicit casting.
> > > 
> > > [GitHub - elastic/elasticsearch: Free and Open, Distributed, RESTful Search Engine](https://github.com/elasticsearch/elasticsearch/)  
> > > tree/master/src/test/java/org/elasticsearch/search/aggregations
> > > 
> > > Cheers,
> > > 
> > > Ivan
> > > 
> > > On Mon, Sep 8, 2014 at 5:41 AM, mooky [nick.mi...@gmail.com](mailto:nick.mi...@gmail.com) wrote:
> > > 
> > > > The aggregation takes into account a query - but not a post-filter. I'm  
> > > > not sure of the rationale behind the difference.
> > > > 
> > > > The java api for traversing results is quite painful - but I think a  
> > > > good part of that is due to Java & the fact that there is very little  
> > > > polymorphic behaviour between aggregation results (some have single  
> > > > results, others have buckets, some have sub-aggregations, some dont).  
> > > > The only alternative that I can think of is a completely type-less  
> > > > navigation of the data - which does little more than navigate the JSON  
> > > > document.
> > > > 
> > > > Hope that helps a bit.
> > > > 
> > > > On Monday, 8 September 2014 10:26:44 UTC+1, Emanuel Buzek wrote:
> > > > 
> > > > > Hi there,  
> > > > > I just used the elasticsearch aggregations through the Java API for  
> > > > > the first time.
> > > > > 
> > > > > All I wanted was a simple min/max/sum/avg, so I used the Stats  
> > > > > aggregation. However, I was very surprised that the filter in the  
> > > > > SearchRequestBuilder is ignored, so I had to wrap the Stats Aggregation  
> > > > > into FilterAggregation.
> > > > > 
> > > > > Getting the aggregation result seems a bit tedious:
> > > > > 
> > > > > InternalStats stats = (InternalStats)((InternalFilter)a).  
> > > > > getAggregations().asList().get(0);
> > > > > 
> > > > > Maybe I am using the Java API wrong (I hope I am, otherwise it's imho  
> > > > > poorly designed.) Can anyone point me to an example how to access the  
> > > > > aggregation results from Java better?
> > > > > 
> > > > > Also, I think that the aggregation should be filtered by default. If I  
> > > > > specify the filter with a query or post filter:
> > > > > 
> > > > > queryBuilder = QueryBuilders.filteredQuery(queryBuilder,  
> > > > > filterBuilder);
> > > > > 
> > > > > searchRequestBuilder.setQuery(queryBuilder);
> > > > > 
> > > > > and then add an aggregation GET to the same searchRequestBuilder, I  
> > > > > think it's very unintuitive if the aggregation is computed globally. Anyone  
> > > > > has this feeling as well?
> > > > > 
> > > > > thanks, emanuel
> > > > > 
> > > > > --  
> > > > > Emanuel Buzek  
> > > > > Software Engineer, ROKE.cz [http://www.roke.cz](http://www.roke.cz)  
> > > > > tel: +420 776 54 26 26
> > > > 
> > > > --  
> > > > 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/](https://groups.google.com/d/)  
> > > > msgid/elasticsearch/a2e3a1e1-912a-4257-b938-6036d0c9d3ff%  
> > > > [40googlegroups.com](http://40googlegroups.com)  
> > > > [https://groups.google.com/d/msgid/elasticsearch/a2e3a1e1-912a-4257-b938-6036d0c9d3ff%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/a2e3a1e1-912a-4257-b938-6036d0c9d3ff%40googlegroups.com?utm_medium=email&utm_source=footer)  
> > > > .
> > > > 
> > > > For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).
> > > 
> > > --  
> > > 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/1eb0e2c2-91e9-43b0-b076-decd33fa6440%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/1eb0e2c2-91e9-43b0-b076-decd33fa6440%40googlegroups.com)  
> > > [https://groups.google.com/d/msgid/elasticsearch/1eb0e2c2-91e9-43b0-b076-decd33fa6440%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/1eb0e2c2-91e9-43b0-b076-decd33fa6440%40googlegroups.com?utm_medium=email&utm_source=footer)  
> > > .
> > 
> > For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
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/bbfa2569-9bc8-46b0-b2d9-edbc62a5c3ca%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/bbfa2569-9bc8-46b0-b2d9-edbc62a5c3ca%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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:02am UTC](https://discuss.elastic.co/t/aggregation-framework-java-api/19658/7 "2017-07-06T01:02:20Z")

</div>


