# Term aggregations and filtering values with the Java client

**URL:** <https://discuss.elastic.co/t/term-aggregations-and-filtering-values-with-the-java-client/22520>\
**Category:** Elasticsearch\
**Created:** [March 4, 2015, 6:20pm UTC](https://discuss.elastic.co/t/term-aggregations-and-filtering-values-with-the-java-client/22520 "2015-03-04T18:20:37Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![JZ1](https://avatars.discourse-cdn.com/v4/letter/j/958977/32.png) [@JZ1](https://discuss.elastic.co/u/JZ1)\
**Post date:** [March 4, 2015, 6:20pm UTC](https://discuss.elastic.co/t/term-aggregations-and-filtering-values-with-the-java-client/22520/1 "2015-03-04T18:20:37Z")

</div>

Hi all,

According to the documentation online and I quote:

"It is possible to filter the values for which buckets will be created.  
This can be done using the include and exclude parameters which are based  
on regular expressions."

{  
"aggs" : {  
"tags" : {  
"terms" : {  
"field" : "tags",  
"include" : "._sport._",  
"exclude" : "water\_.\*"  
}  
}  
}  
}

See: [http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html)

I wonder how the include and exclude parameters can be used with the Java  
client.

Currently, I use the AggregationBuilder class to add a terms aggregation  
to my query like this.

AggregationBuilder aggregation = AggregationBuilders  
.terms(queryTerm)  
.field(queryField)  
.size(size.or(1))  
.order(Terms.Order.term(true));

However, I wonder how to use the "include" as described above using the  
Java client.  
Any help is greatly appreciated! Thanks

/JZ

--  
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/0aca7958-98a5-48e2-9437-13f01a8a2854%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/0aca7958-98a5-48e2-9437-13f01a8a2854%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![colings86](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/colings86/32/44960_2.png) [@colings86](https://discuss.elastic.co/u/colings86)\
**Post date:** [March 5, 2015, 9:45am UTC](https://discuss.elastic.co/t/term-aggregations-and-filtering-values-with-the-java-client/22520/2 "2015-03-05T09:45:47Z")

</div>

How about this:

AggregationBuilder aggregation = AggregationBuilders

.terms(queryTerm)  
.field(queryField)  
.size(size)  
.order(Terms.Order.term(true))  
.include(includeRegex)  
.exclude(excludeRegex);

Hope that helps,

Colin

On Wednesday, 4 March 2015 18:20:37 UTC, [zhang...@gmail.com](mailto:zhang...@gmail.com) wrote:

> Hi all,
> 
> According to the documentation online and I quote:
> 
> "It is possible to filter the values for which buckets will be created.  
> This can be done using the include and exclude parameters which are based  
> on regular expressions."
> 
> {  
> "aggs" : {  
> "tags" : {  
> "terms" : {  
> "field" : "tags",  
> "include" : "._sport._",  
> "exclude" : "water\_.\*"  
> }  
> }  
> }  
> }
> 
> See:  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html)
> 
> I wonder how the include and exclude parameters can be used with the Java  
> client.
> 
> Currently, I use the AggregationBuilder class to add a terms aggregation  
> to my query like this.
> 
> AggregationBuilder aggregation = AggregationBuilders  
> .terms(queryTerm)  
> .field(queryField)  
> .size(size.or(1))  
> .order(Terms.Order.term(true));
> 
> However, I wonder how to use the "include" as described above using the  
> Java client.  
> Any help is greatly appreciated! Thanks
> 
> /JZ

--  
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/67fa4d7a-e803-45cb-86af-63cb342700ad%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/67fa4d7a-e803-45cb-86af-63cb342700ad%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, 12:28am UTC](https://discuss.elastic.co/t/term-aggregations-and-filtering-values-with-the-java-client/22520/3 "2017-07-06T00:28:21Z")

</div>


