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


(Alain Désilets) #1

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.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/b6d0300c-524e-423b-bd5a-6ce9d9e7b168%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


(Adrien Grand) #2

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
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.
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?utm_medium=email&utm_source=footer
.
For more options, visit 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.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/CAL6Z4j5msVrWa1TGkAFs1J3fudTs3mpKEA%3DCQevSJPFcbObgRQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


(Alain Désilets) #3

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
<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 <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?utm_medium=email&utm_source=footer
.
For more options, visit 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.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/f4ff78a9-f042-43c6-ad61-d11ba5fb7ff1%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


(system) #4