# How to get more than 10 terms bucket with Java API?

**URL:** <https://discuss.elastic.co/t/how-to-get-more-than-10-terms-bucket-with-java-api/18934>\
**Category:** Elasticsearch\
**Created:** [July 28, 2014, 7:22pm UTC](https://discuss.elastic.co/t/how-to-get-more-than-10-terms-bucket-with-java-api/18934 "2014-07-28T19:22:20Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alain\_Desilets](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/alain_desilets/32/29314_2.png) [@Alain\_Desilets](https://discuss.elastic.co/u/Alain_Desilets)\
**Post date:** [July 28, 2014, 7:22pm UTC](https://discuss.elastic.co/t/how-to-get-more-than-10-terms-bucket-with-java-api/18934/1 "2014-07-28T19:22:20Z")

</div>

I have an ES where I have indexed a bunch of files. Each file is tagged  
with a category field. I want to write Java code to get a list of all the  
categories. I am trying to do this using the terms aggregation:

QueryBuilder qb = QueryBuilders.matchAllQuery();  
SearchResponse sr =  
esClient.prepareSearch()  
.setQuery(qb)  
.setSize(20)  
.addAggregation(AggregationBuilders.terms("category\_names").field("category")).setSize(20)  
.execute()  
.actionGet();

Terms terms = sr.getAggregations().get("category\_names");  
int numCategories = terms.getBuckets().size();  
System.out.println("Number of category buckets found: "+ numCategories);

But this code always prints out that it found 10 category buckets. Yet, if  
I execute the following query in Sense:

GET files-index/file\_with\_category/\_search  
{  
"query": {  
"match\_all": {}  
},  
"aggs": {  
"categories": {  
"terms": {  
"field": "category",  
"size": 100  
}  
}  
}  
}

I get 18 category buckets. What am I doing wrong in the Java code?

Thx.

Alain

--  
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/b6d0300c-524e-423b-bd5a-6ce9d9e7b168%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/b6d0300c-524e-423b-bd5a-6ce9d9e7b168%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![jpountz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jpountz/32/45836_2.png) [@jpountz](https://discuss.elastic.co/u/jpountz)\
**Post date:** [July 29, 2014, 7:56pm UTC](https://discuss.elastic.co/t/how-to-get-more-than-10-terms-bucket-with-java-api/18934/2 "2014-07-29T19:56:06Z")

</div>

Hi,

You need to set the size on the terms aggregation:

AggregationBuilders.terms("category\_names").field("category").size(20)

On Mon, Jul 28, 2014 at 9:22 PM, Alain Désilets [alaindesilets0@gmail.com](mailto:alaindesilets0@gmail.com)  
wrote:

> I have an ES where I have indexed a bunch of files. Each file is tagged  
> with a category field. I want to write Java code to get a list of all the  
> categories. I am trying to do this using the terms aggregation:
> 
> QueryBuilder qb = QueryBuilders.matchAllQuery();  
> SearchResponse sr =  
> esClient.prepareSearch()  
> .setQuery(qb)  
> .setSize(20)
> 
> .addAggregation(AggregationBuilders.terms("category\_names").field("category")).setSize(20)  
> .execute()  
> .actionGet();
> 
> Terms terms = sr.getAggregations().get("category\_names");  
> int numCategories = terms.getBuckets().size();  
> System.out.println("Number of category buckets found: "+ numCategories);
> 
> But this code always prints out that it found 10 category buckets. Yet, if  
> I execute the following query in Sense:
> 
> GET files-index/file\_with\_category/\_search  
> {  
> "query": {  
> "match\_all": {}  
> },  
> "aggs": {  
> "categories": {  
> "terms": {  
> "field": "category",  
> "size": 100  
> }  
> }  
> }  
> }
> 
> I get 18 category buckets. What am I doing wrong in the Java code?
> 
> Thx.
> 
> Alain
> 
> --  
> 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/b6d0300c-524e-423b-bd5a-6ce9d9e7b168%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/b6d0300c-524e-423b-bd5a-6ce9d9e7b168%40googlegroups.com)  
> [https://groups.google.com/d/msgid/elasticsearch/b6d0300c-524e-423b-bd5a-6ce9d9e7b168%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/b6d0300c-524e-423b-bd5a-6ce9d9e7b168%40googlegroups.com?utm_medium=email&utm_source=footer)  
> .  
> For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
Adrien Grand

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

---

<div class="post-metadata">

**Author:** ![Alain\_Desilets](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/alain_desilets/32/29314_2.png) [@Alain\_Desilets](https://discuss.elastic.co/u/Alain_Desilets)\
**Post date:** [July 30, 2014, 2:37pm UTC](https://discuss.elastic.co/t/how-to-get-more-than-10-terms-bucket-with-java-api/18934/3 "2014-07-30T14:37:16Z")

</div>

Thx. That did the trick. I now realize that I was setting the size on the  
return value of addAggregation(), when in fact, I needed to set it on the  
return value of the field() method.

On Tuesday, 29 July 2014 15:56:29 UTC-4, Adrien Grand wrote:

> Hi,
> 
> You need to set the size on the terms aggregation:
> 
> AggregationBuilders.terms("category\_names").field("category").size(20)
> 
> On Mon, Jul 28, 2014 at 9:22 PM, Alain Désilets \<[alainde...@gmail.com](mailto:alainde...@gmail.com)  
> \<javascript:\>\> wrote:
> 
> > I have an ES where I have indexed a bunch of files. Each file is tagged  
> > with a category field. I want to write Java code to get a list of all the  
> > categories. I am trying to do this using the terms aggregation:
> > 
> > QueryBuilder qb = QueryBuilders.matchAllQuery();  
> > SearchResponse sr =  
> > esClient.prepareSearch()  
> > .setQuery(qb)  
> > .setSize(20)
> > 
> > .addAggregation(AggregationBuilders.terms("category\_names").field("category")).setSize(20)  
> > .execute()  
> > .actionGet();
> > 
> > Terms terms = sr.getAggregations().get("category\_names");  
> > int numCategories = terms.getBuckets().size();  
> > System.out.println("Number of category buckets found: "+ numCategories);
> > 
> > But this code always prints out that it found 10 category buckets. Yet,  
> > if I execute the following query in Sense:
> > 
> > GET files-index/file\_with\_category/\_search  
> > {  
> > "query": {  
> > "match\_all": {}  
> > },  
> > "aggs": {  
> > "categories": {  
> > "terms": {  
> > "field": "category",  
> > "size": 100  
> > }  
> > }  
> > }  
> > }
> > 
> > I get 18 category buckets. What am I doing wrong in the Java code?
> > 
> > Thx.
> > 
> > Alain
> > 
> > --  
> > 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/b6d0300c-524e-423b-bd5a-6ce9d9e7b168%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/b6d0300c-524e-423b-bd5a-6ce9d9e7b168%40googlegroups.com)  
> > [https://groups.google.com/d/msgid/elasticsearch/b6d0300c-524e-423b-bd5a-6ce9d9e7b168%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/b6d0300c-524e-423b-bd5a-6ce9d9e7b168%40googlegroups.com?utm_medium=email&utm_source=footer)  
> > .  
> > For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).
> 
> --  
> Adrien Grand

--  
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/f4ff78a9-f042-43c6-ad61-d11ba5fb7ff1%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/f4ff78a9-f042-43c6-ad61-d11ba5fb7ff1%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:12am UTC](https://discuss.elastic.co/t/how-to-get-more-than-10-terms-bucket-with-java-api/18934/4 "2017-07-06T01:12:13Z")

</div>


