# Multi field terms facet

**URL:** <https://discuss.elastic.co/t/multi-field-terms-facet/3798>\
**Category:** Elasticsearch\
**Created:** [January 17, 2011, 11:43pm UTC](https://discuss.elastic.co/t/multi-field-terms-facet/3798 "2011-01-17T23:43:04Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![nmosafi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nmosafi/32/3280_2.png) [@nmosafi](https://discuss.elastic.co/u/nmosafi)\
**Post date:** [January 17, 2011, 11:43pm UTC](https://discuss.elastic.co/t/multi-field-terms-facet/3798/1 "2011-01-17T23:43:04Z")

</div>

Hi there

I am trying to use the multi field terms facet. Say I have documents  
which look like this:

{  
"fruit": "apple",  
"colour": "green",  
"size": "large"  
},  
{  
"fruit": "banana",  
"colour": "yellow",  
"size": "large"  
},  
{  
"fruit": "grape",  
"colour": "green",  
"size": "small"  
}

and I run a multi terms facet:

{  
"query": { "match\_all": {} },  
"size": 0,  
"facets": { "myFacet": { "terms": { "fields" : ["colour",  
"size"] } } }  
}

Here I am trying to get three facets to be returned

{"terms": ["green", "large"], "count": 1 }  
{"terms": ["yellow", "large"], "count": 1 }  
{"terms": ["green", "small"], "count": 1 }

However I get back this

{"term": "green", "count": 2 }  
{"term": "yellow", "count": 1 }  
{"term": "large", "count": 2 }  
{"term": "small", "count": 1 }

Is there a way to achieve what I am trying?

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [January 18, 2011, 12:22am UTC](https://discuss.elastic.co/t/multi-field-terms-facet/3798/2 "2011-01-18T00:22:35Z")

</div>

there is no support for hierarchal facet (yet) in elasticsearch.  
On Tuesday, January 18, 2011 at 1:43 AM, Neil Mosafi wrote:

> Hi there
> 
> I am trying to use the multi field terms facet. Say I have documents  
> which look like this:
> 
> {  
> "fruit": "apple",  
> "colour": "green",  
> "size": "large"  
> },  
> {  
> "fruit": "banana",  
> "colour": "yellow",  
> "size": "large"  
> },  
> {  
> "fruit": "grape",  
> "colour": "green",  
> "size": "small"  
> }
> 
> and I run a multi terms facet:
> 
> {  
> "query": { "match\_all": {} },  
> "size": 0,  
> "facets": { "myFacet": { "terms": { "fields" : ["colour",  
> "size"] } } }  
> }
> 
> Here I am trying to get three facets to be returned
> 
> {"terms": ["green", "large"], "count": 1 }  
> {"terms": ["yellow", "large"], "count": 1 }  
> {"terms": ["green", "small"], "count": 1 }
> 
> However I get back this
> 
> {"term": "green", "count": 2 }  
> {"term": "yellow", "count": 1 }  
> {"term": "large", "count": 2 }  
> {"term": "small", "count": 1 }
> 
> Is there a way to achieve what I am trying?

---

<div class="post-metadata">

**Author:** ![nmosafi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nmosafi/32/3280_2.png) [@nmosafi](https://discuss.elastic.co/u/nmosafi)\
**Post date:** [January 18, 2011, 10:11am UTC](https://discuss.elastic.co/t/multi-field-terms-facet/3798/3 "2011-01-18T10:11:30Z")

</div>

Just come up with a solution

{  
"query": { "match\_all": {} },  
"size": 0,  
"facets": { "myFacet": { "terms": { "script\_field" : "\_source.colour +  
',' + \_source.size" } } }  
}

Seems to work, obviously assumes that there are no commas in the data.

Let me know if this is a bad idea? The docs say it's slow and has high  
load.

On Jan 18, 12:22 am, Shay Banon [shay.ba...@elasticsearch.com](mailto:shay.ba...@elasticsearch.com) wrote:

> there is no support for hierarchal facet (yet) in elasticsearch.
> 
> On Tuesday, January 18, 2011 at 1:43 AM, Neil Mosafi wrote:
> 
> > Hi there
> 
> > I am trying to use the multi field terms facet. Say I have documents  
> > which look like this:
> 
> > {  
> > "fruit": "apple",  
> > "colour": "green",  
> > "size": "large"  
> > },  
> > {  
> > "fruit": "banana",  
> > "colour": "yellow",  
> > "size": "large"  
> > },  
> > {  
> > "fruit": "grape",  
> > "colour": "green",  
> > "size": "small"  
> > }
> 
> > and I run a multi terms facet:
> 
> > {  
> > "query": { "match\_all": {} },  
> > "size": 0,  
> > "facets": { "myFacet": { "terms": { "fields" : ["colour",  
> > "size"] } } }  
> > }
> 
> > Here I am trying to get three facets to be returned
> 
> > {"terms": ["green", "large"], "count": 1 }  
> > {"terms": ["yellow", "large"], "count": 1 }  
> > {"terms": ["green", "small"], "count": 1 }
> 
> > However I get back this
> 
> > {"term": "green", "count": 2 }  
> > {"term": "yellow", "count": 1 }  
> > {"term": "large", "count": 2 }  
> > {"term": "small", "count": 1 }
> 
> > Is there a way to achieve what I am trying?

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [January 18, 2011, 10:14am UTC](https://discuss.elastic.co/t/multi-field-terms-facet/3798/4 "2011-01-18T10:14:18Z")

</div>

It will be slower compared to a native solution, but might be good for what you do. Make sure to use the doc notation, not the \_source (so the source will not be loaded and parsed, but values will be loaded and used from memory), for example:

doc['colour'].value + doc['size'].value

-shay.banon  
On Tuesday, January 18, 2011 at 12:11 PM, Neil Mosafi wrote:

> Just come up with a solution
> 
> {  
> "query": { "match\_all": {} },  
> "size": 0,  
> "facets": { "myFacet": { "terms": { "script\_field" : "\_source.colour +  
> ',' + \_source.size" } } }  
> }
> 
> Seems to work, obviously assumes that there are no commas in the data.
> 
> Let me know if this is a bad idea? The docs say it's slow and has high  
> load.
> 
> On Jan 18, 12:22 am, Shay Banon [shay.ba...@elasticsearch.com](mailto:shay.ba...@elasticsearch.com) wrote:
> 
> > there is no support for hierarchal facet (yet) in elasticsearch.
> > 
> > On Tuesday, January 18, 2011 at 1:43 AM, Neil Mosafi wrote:
> > 
> > > Hi there
> > 
> > > I am trying to use the multi field terms facet. Say I have documents  
> > > which look like this:
> > 
> > > {  
> > > "fruit": "apple",  
> > > "colour": "green",  
> > > "size": "large"  
> > > },  
> > > {  
> > > "fruit": "banana",  
> > > "colour": "yellow",  
> > > "size": "large"  
> > > },  
> > > {  
> > > "fruit": "grape",  
> > > "colour": "green",  
> > > "size": "small"  
> > > }
> > 
> > > and I run a multi terms facet:
> > 
> > > {  
> > > "query": { "match\_all": {} },  
> > > "size": 0,  
> > > "facets": { "myFacet": { "terms": { "fields" : ["colour",  
> > > "size"] } } }  
> > > }
> > 
> > > Here I am trying to get three facets to be returned
> > 
> > > {"terms": ["green", "large"], "count": 1 }  
> > > {"terms": ["yellow", "large"], "count": 1 }  
> > > {"terms": ["green", "small"], "count": 1 }
> > 
> > > However I get back this
> > 
> > > {"term": "green", "count": 2 }  
> > > {"term": "yellow", "count": 1 }  
> > > {"term": "large", "count": 2 }  
> > > {"term": "small", "count": 1 }
> > 
> > > Is there a way to achieve what I am trying?

---

<div class="post-metadata">

**Author:** ![nmosafi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nmosafi/32/3280_2.png) [@nmosafi](https://discuss.elastic.co/u/nmosafi)\
**Post date:** [January 18, 2011, 1:33pm UTC](https://discuss.elastic.co/t/multi-field-terms-facet/3798/5 "2011-01-18T13:33:55Z")

</div>

Great, it's working well.

Thanks for the help!  
Neil

On Jan 18, 10:14 am, Shay Banon [shay.ba...@elasticsearch.com](mailto:shay.ba...@elasticsearch.com) wrote:

> It will be slower compared to a native solution, but might be good for what you do. Make sure to use the doc notation, not the \_source (so the source will not be loaded and parsed, but values will be loaded and used from memory), for example:
> 
> doc['colour'].value + doc['size'].value
> 
> -shay.banon
> 
> On Tuesday, January 18, 2011 at 12:11 PM, Neil Mosafi wrote:
> 
> > Just come up with a solution
> 
> > {  
> > "query": { "match\_all": {} },  
> > "size": 0,  
> > "facets": { "myFacet": { "terms": { "script\_field" : "\_source.colour +  
> > ',' + \_source.size" } } }  
> > }
> 
> > Seems to work, obviously assumes that there are no commas in the data.
> 
> > Let me know if this is a bad idea? The docs say it's slow and has high  
> > load.
> 
> > On Jan 18, 12:22 am, Shay Banon [shay.ba...@elasticsearch.com](mailto:shay.ba...@elasticsearch.com) wrote:
> 
> > > there is no support for hierarchal facet (yet) in elasticsearch.
> 
> > > On Tuesday, January 18, 2011 at 1:43 AM, Neil Mosafi wrote:
> 
> > > > Hi there
> 
> > > > I am trying to use the multi field terms facet. Say I have documents  
> > > > which look like this:
> 
> > > > {  
> > > > "fruit": "apple",  
> > > > "colour": "green",  
> > > > "size": "large"  
> > > > },  
> > > > {  
> > > > "fruit": "banana",  
> > > > "colour": "yellow",  
> > > > "size": "large"  
> > > > },  
> > > > {  
> > > > "fruit": "grape",  
> > > > "colour": "green",  
> > > > "size": "small"  
> > > > }
> 
> > > > and I run a multi terms facet:
> 
> > > > {  
> > > > "query": { "match\_all": {} },  
> > > > "size": 0,  
> > > > "facets": { "myFacet": { "terms": { "fields" : ["colour",  
> > > > "size"] } } }  
> > > > }
> 
> > > > Here I am trying to get three facets to be returned
> 
> > > > {"terms": ["green", "large"], "count": 1 }  
> > > > {"terms": ["yellow", "large"], "count": 1 }  
> > > > {"terms": ["green", "small"], "count": 1 }
> 
> > > > However I get back this
> 
> > > > {"term": "green", "count": 2 }  
> > > > {"term": "yellow", "count": 1 }  
> > > > {"term": "large", "count": 2 }  
> > > > {"term": "small", "count": 1 }
> 
> > > > Is there a way to achieve what I am trying?

---

<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, 4:13am UTC](https://discuss.elastic.co/t/multi-field-terms-facet/3798/6 "2017-07-06T04:13:37Z")

</div>


