# New feature request: Nested terms facets

**URL:** <https://discuss.elastic.co/t/new-feature-request-nested-terms-facets/3504>\
**Category:** Elasticsearch\
**Created:** [November 2, 2010, 10:34pm UTC](https://discuss.elastic.co/t/new-feature-request-nested-terms-facets/3504 "2010-11-02T22:34:44Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lukas\_Vlcek1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/lukas_vlcek1/32/819_2.png) [@Lukas\_Vlcek1](https://discuss.elastic.co/u/Lukas_Vlcek1)\
**Post date:** [November 2, 2010, 10:34pm UTC](https://discuss.elastic.co/t/new-feature-request-nested-terms-facets/3504/1 "2010-11-02T22:34:44Z")

</div>

Hi,

I am looking for a feature that would allow me to get nested terms facets  
without prior knowledge of the values of the "parent" term.

Here is example:

Document has a two fields: category and subcategory. Providing that the  
category has two values: 01 and 02 I can issue the following query:

{ "query":{"match\_all":{}},  
"facets":{  
"category":{  
"terms":{"field":"category","order":"term"}  
},  
"subcategory-for-category01":{  
"terms":{"fields":["subcategory"],"order":"term"},  
"facet\_filter":{  
"term":{"category":"01"}  
}  
},  
"subcategory-for-categor02":{  
"terms":{"fields":["subcategory"],"order":"term"},  
"facet\_filter":{  
"term":{"category":"02"}  
}  
}  
}  
}

This returns the following facets:

{ "category": { "\_type": "terms", "\_field": "category", "terms": [ { "term":  
"01", "count": 3 }, { "term": "02", "count": 4 } ] },  
"subcategory-for-category01": { "\_type": "terms", "\_field": "subcategory",  
"terms": [{ "term": "01", "count": 1 }, { "term": "02", "count": 2 }] },  
"subcategory-for-categor02": { "\_type": "terms", "\_field": "subcategory",  
"terms": [{ "term": "01", "count": 2 }, { "term": "02", "count": 2 }] } }

The problem is that this requires prior knowledge of category values. Would  
it be possible to have this automated? Doing it on the client side (by  
issuing an extra query to get category values in advance) is not very handy  
and may not be data consistent.

Regards,  
Lukas

---

<div class="post-metadata">

**Author:** ![Lukas\_Vlcek1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/lukas_vlcek1/32/819_2.png) [@Lukas\_Vlcek1](https://discuss.elastic.co/u/Lukas_Vlcek1)\
**Post date:** [November 2, 2010, 10:37pm UTC](https://discuss.elastic.co/t/new-feature-request-nested-terms-facets/3504/2 "2010-11-02T22:37:30Z")

</div>

An of course the query does not have to use "fields" terms facet for  
subcategory, simple "filed" facet works fine as well 🙂

{ "query":{"match\_all":{}},  
"facets":{  
"category":{  
"terms":{"field":"category","order":"term"}  
},  
"subcategory-for-category01":{  
"terms":{"field":"subcategory","order":"term"},  
"facet\_filter":{  
"term":{"category":"01"}  
}  
},  
"subcategory-for-categor02":{  
"terms":{"field":"subcategory","order":"term"},  
"facet\_filter":{  
"term":{"category":"02"}  
}  
}  
}  
}

On Tue, Nov 2, 2010 at 11:34 PM, Lukáš Vlček [lukas.vlcek@gmail.com](mailto:lukas.vlcek@gmail.com) wrote:

> Hi,
> 
> I am looking for a feature that would allow me to get nested terms facets  
> without prior knowledge of the values of the "parent" term.
> 
> Here is example:
> 
> Document has a two fields: category and subcategory. Providing that the  
> category has two values: 01 and 02 I can issue the following query:
> 
> { "query":{"match\_all":{}},  
> "facets":{  
> "category":{  
> "terms":{"field":"category","order":"term"}  
> },  
> "subcategory-for-category01":{  
> "terms":{"fields":["subcategory"],"order":"term"},  
> "facet\_filter":{  
> "term":{"category":"01"}  
> }  
> },  
> "subcategory-for-categor02":{  
> "terms":{"fields":["subcategory"],"order":"term"},  
> "facet\_filter":{  
> "term":{"category":"02"}  
> }  
> }  
> }  
> }
> 
> This returns the following facets:
> 
> { "category": { "\_type": "terms", "\_field": "category", "terms": [ {  
> "term": "01", "count": 3 }, { "term": "02", "count": 4 } ] },  
> "subcategory-for-category01": { "\_type": "terms", "\_field": "subcategory",  
> "terms": [{ "term": "01", "count": 1 }, { "term": "02", "count": 2 }] },  
> "subcategory-for-categor02": { "\_type": "terms", "\_field": "subcategory",  
> "terms": [{ "term": "01", "count": 2 }, { "term": "02", "count": 2 }] } }
> 
> The problem is that this requires prior knowledge of category values. Would  
> it be possible to have this automated? Doing it on the client side (by  
> issuing an extra query to get category values in advance) is not very handy  
> and may not be data consistent.
> 
> Regards,  
> Lukas

---

<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:17am UTC](https://discuss.elastic.co/t/new-feature-request-nested-terms-facets/3504/3 "2017-07-06T04:17:16Z")

</div>

Closed on July 6, 2017, 4:17am UTC


