# Term stat facets on multiple fields

**URL:** <https://discuss.elastic.co/t/term-stat-facets-on-multiple-fields/14668>\
**Category:** Elasticsearch\
**Created:** [December 3, 2013, 5:30am UTC](https://discuss.elastic.co/t/term-stat-facets-on-multiple-fields/14668 "2013-12-03T05:30:25Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hanish\_Bansal](https://avatars.discourse-cdn.com/v4/letter/h/e36b37/32.png) [@Hanish\_Bansal](https://discuss.elastic.co/u/Hanish_Bansal)\
**Post date:** [December 3, 2013, 5:30am UTC](https://discuss.elastic.co/t/term-stat-facets-on-multiple-fields/14668/1 "2013-12-03T05:30:25Z")

</div>

Hi All,

Using Terms Stats Facet we can create sums, counts and averages etc. of a  
value given in one field, aggregated by all values of another field. It's a  
way of creating statistical cross-tabs.

Does this feature work on more than one aggregation field? In other words,  
if I had a data set of: Building type, year built, area. I want to apply  
term stat facets on Year and area.

If my data looks like (this is just sample data):

\*BuildingType Year area \*  
Residential 2004 3500  
Residential 2005 3600  
Residential 2006 3700  
Residential 2007 3800  
Residential 2008 3700  
Residential 2009 3650  
Commerical 2004 14,000  
Commercial 2005 15,000

Then i want output as:

Residential min(Year)=2004, max(year)= 2009, min(area)=3500,  
max(area)=3800  
Commerical min(Year)=2004, max(year)= 2004, min(area)=14000,  
max(area)=15000

To apply term stat facet on single column (lets say "area") my query is:

TermsFacetBuilder termsFacetBuilder = FacetBuilders.termsFacet("facet1");  
termsFacetBuilder.keyField(_"BuildingType_").valueField("area");

SearchRequestBuilder srb  
=client.prepareSearch("TweetIndex").setQuery(qb).setSearchType(SearchType.QUERY\_THEN\_FETCH).addFacet(termsFacetBuilder);

searchResponse = srb.execute().actionGet();

In search response i am able to get my result if apply facet on single  
column.

Could I somehow apply term stat facets on two fields?

--  
_Thanks & Regards_  
_Hanish Bansal_

--  
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/CAB0WE8Zntuh3k-1eOX9tckHO4eaCyDnbc0H9%3DjqCRFSLbW94ew%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAB0WE8Zntuh3k-1eOX9tckHO4eaCyDnbc0H9%3DjqCRFSLbW94ew%40mail.gmail.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![mattweber](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mattweber/32/44940_2.png) [@mattweber](https://discuss.elastic.co/u/mattweber)\
**Post date:** [December 3, 2013, 6:18am UTC](https://discuss.elastic.co/t/term-stat-facets-on-multiple-fields/14668/2 "2013-12-03T06:18:00Z")

</div>

You can do this with1.0beta2 that was just released using the new  
aggregations functionality. You would create a terms aggregator on  
building type and then nest a min and max aggregator for year and area.

> **[Elasticsearch Platform — Find real-time answers at scale](https://www.elastic.co)**
>
> Power insights and outcomes with the Elasticsearch Platform and AI. See into your data and find answers that matter with enterprise solutions designed to help you build, observe, and protect. Try Elasticsearch free today.

> **[Elasticsearch Platform — Find real-time answers at scale](https://www.elastic.co)**
>
> Power insights and outcomes with the Elasticsearch Platform and AI. See into your data and find answers that matter with enterprise solutions designed to help you build, observe, and protect. Try Elasticsearch free today.

Thanks,  
Matt Weber  
On Dec 2, 2013 9:30 PM, "Hanish Bansal" [hanish.bansal.agarwal@gmail.com](mailto:hanish.bansal.agarwal@gmail.com)  
wrote:

> Hi All,
> 
> Using Terms Stats Facet we can create sums, counts and averages etc. of a  
> value given in one field, aggregated by all values of another field. It's a  
> way of creating statistical cross-tabs.
> 
> Does this feature work on more than one aggregation field? In other words,  
> if I had a data set of: Building type, year built, area. I want to apply  
> term stat facets on Year and area.
> 
> If my data looks like (this is just sample data):
> 
> \*BuildingType Year area \*  
> Residential 2004 3500  
> Residential 2005 3600  
> Residential 2006 3700  
> Residential 2007 3800  
> Residential 2008 3700  
> Residential 2009 3650  
> Commerical 2004 14,000  
> Commercial 2005 15,000
> 
> Then i want output as:
> 
> Residential min(Year)=2004, max(year)= 2009, min(area)=3500,  
> max(area)=3800  
> Commerical min(Year)=2004, max(year)= 2004, min(area)=14000,  
> max(area)=15000
> 
> To apply term stat facet on single column (lets say "area") my query is:
> 
> TermsFacetBuilder termsFacetBuilder = FacetBuilders.termsFacet("facet1");  
> termsFacetBuilder.keyField(_"BuildingType_").valueField("area");
> 
> SearchRequestBuilder srb  
> =client.prepareSearch("TweetIndex").setQuery(qb).setSearchType(SearchType.QUERY\_THEN\_FETCH).addFacet(termsFacetBuilder);
> 
> searchResponse = srb.execute().actionGet();
> 
> In search response i am able to get my result if apply facet on single  
> column.
> 
> Could I somehow apply term stat facets on two fields?
> 
> --  
> _Thanks & Regards_  
> _Hanish Bansal_
> 
> --  
> 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/CAB0WE8Zntuh3k-1eOX9tckHO4eaCyDnbc0H9%3DjqCRFSLbW94ew%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAB0WE8Zntuh3k-1eOX9tckHO4eaCyDnbc0H9%3DjqCRFSLbW94ew%40mail.gmail.com)  
> .  
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
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/CAJ3KEoDGJKpMkrYZ8oBWGrUnz\_Hv18E5\_LQZoaJqXNZWjG2RtQ%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAJ3KEoDGJKpMkrYZ8oBWGrUnz_Hv18E5_LQZoaJqXNZWjG2RtQ%40mail.gmail.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Hanish\_Bansal](https://avatars.discourse-cdn.com/v4/letter/h/e36b37/32.png) [@Hanish\_Bansal](https://discuss.elastic.co/u/Hanish_Bansal)\
**Post date:** [December 3, 2013, 9:24am UTC](https://discuss.elastic.co/t/term-stat-facets-on-multiple-fields/14668/3 "2013-12-03T09:24:17Z")

</div>

Hi Matt,

Thanks for response !!

I am using elasticsearch-0.90.5.

One way is that apply 2 facets separately and merge the result in-memory  
and then i can return the result. But its costly operation.

Is there any other way possible to do this or i'll have to upgrade  
Elasticsearch to get this functionality?

On Tue, Dec 3, 2013 at 6:18 AM, Matt Weber [matt@mattweber.org](mailto:matt@mattweber.org) wrote:

> You can do this with1.0beta2 that was just released using the new  
> aggregations functionality. You would create a terms aggregator on  
> building type and then nest a min and max aggregator for year and area.
> 
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/blog/1-0-0-beta2-released/)
> 
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/en/elasticsearch/reference/master/search-aggregations.html)
> 
> Thanks,  
> Matt Weber  
> On Dec 2, 2013 9:30 PM, "Hanish Bansal" [hanish.bansal.agarwal@gmail.com](mailto:hanish.bansal.agarwal@gmail.com)  
> wrote:
> 
> > Hi All,
> > 
> > Using Terms Stats Facet we can create sums, counts and averages etc. of a  
> > value given in one field, aggregated by all values of another field. It's a  
> > way of creating statistical cross-tabs.
> > 
> > Does this feature work on more than one aggregation field? In other  
> > words, if I had a data set of: Building type, year built, area. I want to  
> > apply term stat facets on Year and area.
> > 
> > If my data looks like (this is just sample data):
> > 
> > \*BuildingType Year area \*  
> > Residential 2004 3500  
> > Residential 2005 3600  
> > Residential 2006 3700  
> > Residential 2007 3800  
> > Residential 2008 3700  
> > Residential 2009 3650  
> > Commerical 2004 14,000  
> > Commercial 2005 15,000
> > 
> > Then i want output as:
> > 
> > Residential min(Year)=2004, max(year)= 2009, min(area)=3500,  
> > max(area)=3800  
> > Commerical min(Year)=2004, max(year)= 2004, min(area)=14000,  
> > max(area)=15000
> > 
> > To apply term stat facet on single column (lets say "area") my query is:
> > 
> > TermsFacetBuilder termsFacetBuilder = FacetBuilders.termsFacet("facet1");  
> > termsFacetBuilder.keyField(_"BuildingType_").valueField("area");
> > 
> > SearchRequestBuilder srb  
> > =client.prepareSearch("TweetIndex").setQuery(qb).setSearchType(SearchType.QUERY\_THEN\_FETCH).addFacet(termsFacetBuilder);
> > 
> > searchResponse = srb.execute().actionGet();
> > 
> > In search response i am able to get my result if apply facet on single  
> > column.
> > 
> > Could I somehow apply term stat facets on two fields?
> > 
> > --  
> > _Thanks & Regards_  
> > _Hanish Bansal_
> > 
> > --  
> > 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/CAB0WE8Zntuh3k-1eOX9tckHO4eaCyDnbc0H9%3DjqCRFSLbW94ew%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAB0WE8Zntuh3k-1eOX9tckHO4eaCyDnbc0H9%3DjqCRFSLbW94ew%40mail.gmail.com)  
> > .  
> > For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).
> 
> --  
> 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/CAJ3KEoDGJKpMkrYZ8oBWGrUnz\_Hv18E5\_LQZoaJqXNZWjG2RtQ%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAJ3KEoDGJKpMkrYZ8oBWGrUnz_Hv18E5_LQZoaJqXNZWjG2RtQ%40mail.gmail.com)  
> .  
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
_Thanks & Regards_  
_Hanish Bansal_

--  
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/CAB0WE8a\_E61o8nOySssGeD%2BhiofTfavJjxhyYzKGi3x-ToTZVQ%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAB0WE8a_E61o8nOySssGeD%2BhiofTfavJjxhyYzKGi3x-ToTZVQ%40mail.gmail.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<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, 2:03am UTC](https://discuss.elastic.co/t/term-stat-facets-on-multiple-fields/14668/4 "2017-07-06T02:03:43Z")

</div>


