# Dynamic Faceting

**URL:** <https://discuss.elastic.co/t/dynamic-faceting/86850>\
**Category:** Elasticsearch\
**Created:** [May 23, 2017, 6:30pm UTC](https://discuss.elastic.co/t/dynamic-faceting/86850 "2017-05-23T18:30:56Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mymoona\_S\_M](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mymoona_s_m/32/17770_2.png) [@Mymoona\_S\_M](https://discuss.elastic.co/u/Mymoona_S_M)\
**Post date:** [May 23, 2017, 6:30pm UTC](https://discuss.elastic.co/t/dynamic-faceting/86850/1 "2017-05-23T18:30:56Z")

</div>

I was able to aggregate over dynamic facets that I have in my feed as mentioned in the comment:

> <https://stackoverflow.com/questions/24800545/how-to-aggregate-over-dynamic-fields-in-elasticsearch/32065070#32065070>

My response would look something like:  
"buckets": [{ "key": "color\_family", "doc\_count": 2, "value": { "doc\_count\_error\_upper\_bound": 0, "sum\_other\_doc\_count": 0, "buckets": [ { "key": "white", "doc\_count": 1 }, { "key": "black", "doc\_count": 1 }] } { "key": "paper color", "doc\_count": 2, "value": { "doc\_count\_error\_upper\_bound": 0, "sum\_other\_doc\_count": 0, "buckets": [{ "key": "white", "doc\_count": 3 }, { "key": "green", "doc\_count": 1 }] } },

When I do something like below: I get all color\_family and all white, where as it should be just white from color\_family.

{  
"filter": {  
"bool": {  
"must": [  
{  
"match": {  
"attributes.key": "color\_family"  
}  
},  
{  
"match": {  
"attributes.value": "white"  
}  
}  
]  
}  
}  
}

I am looking for it to work more like a normal facet, please let me know if any has solved this before.

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [May 24, 2017, 10:42am UTC](https://discuss.elastic.co/t/dynamic-faceting/86850/2 "2017-05-24T10:42:29Z")

</div>

I think you'll need to provide more detailed materials namely the JSON for :

1. The mapping
2. An example doc
3. The query

From the query snippet you provided it looks like there's no "nested" clause so your query will match root-level docs not the nested attributes. Once we have a cut-down reproducible example of the scenario we can iterate further

---

<div class="post-metadata">

**Author:** ![Mymoona\_S\_M](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mymoona_s_m/32/17770_2.png) [@Mymoona\_S\_M](https://discuss.elastic.co/u/Mymoona_S_M)\
**Post date:** [May 24, 2017, 6:59pm UTC](https://discuss.elastic.co/t/dynamic-faceting/86850/3 "2017-05-24T18:59:18Z")

</div>

First query for getting facets  
{  
"query": {  
"multi\_match" : {  
"query": "tops",  
"fields": ["name^12.1", "brand^20.0"]  
}  
},  
"aggs": {  
"attributes": {  
"nested": {  
"path": "attributes"  
},  
"aggs": {  
"key": {  
"terms": {  
"field": "attributes.key"  
},  
"aggs": {  
"value": {  
"terms": {  
"field": "attributes.value"  
}  
}  
}  
}  
}  
}  
}  
}

response:

{  
"took": 826,  
"hits": {  
"total": 29513,  
"max\_score": 102.30364,  
"hits": [  
....  
]  
},  
"aggregations": {  
"attributes": {  
"doc\_count": 82662,  
"key": {  
"doc\_count\_error\_upper\_bound": 766,  
"sum\_other\_doc\_count": 42982,  
"buckets": [  
{  
"key": "color\_family",  
"doc\_count": 10875,  
"value": {  
"doc\_count\_error\_upper\_bound": 0,  
"sum\_other\_doc\_count": 2520,  
"buckets": [  
{  
"key": "white",  
"doc\_count": 1996  
},  
{  
"key": "blue",  
"doc\_count": 1226  
},  
{  
"key": "brown",  
"doc\_count": 900  
},  
{  
"key": "green",  
"doc\_count": 862  
},  
{  
"key": "multicolor",  
"doc\_count": 752  
},  
{  
"key": "red",  
"doc\_count": 747  
},  
{  
"key": "gray",  
"doc\_count": 726  
},  
{  
"key": "silver",  
"doc\_count": 722  
},  
{  
"key": "pink",  
"doc\_count": 653  
},  
{  
"key": "yellow",  
"doc\_count": 527  
}  
]  
}  
},  
{  
"key": "paper\_color",  
"doc\_count": 2158,  
"value": {  
"doc\_count\_error\_upper\_bound": 9,  
"sum\_other\_doc\_count": 642,  
"buckets": [  
{  
"key": "white",  
"doc\_count": 802  
},  
{  
"key": "blue",  
"doc\_count": 208  
},  
{  
"key": "beige",  
"doc\_count": 116  
},  
{  
"key": "pink",  
"doc\_count": 112  
},  
{  
"key": "red",  
"doc\_count": 112  
},  
{  
"key": "brown",  
"doc\_count": 107  
},  
{  
"key": "gray",  
"doc\_count": 102  
},  
{  
"key": "green",  
"doc\_count": 101  
},  
{  
"key": "bright",  
"doc\_count": 82  
},  
{  
"key": "silver",  
"doc\_count": 76  
}  
]  
}  
},  
]  
}  
}  
}  
}

query to select a filter  
{  
"query": {  
"bool": {  
"must": {  
"multi\_match" : {  
"query": "tops",  
"fields": ["name^12.1", "brand^20.0"]  
}  
},  
"filter": {  
"bool":{  
"must":  
[  
{"match":{"attributes.key": "color\_family"}},  
{"match":{"attributes.value": "white"}}  
]  
}  
}  
}  
},  
"aggs": {  
"attributes": {  
"nested": {  
"path": "attributes"  
},  
"aggs": {  
"key": {  
"terms": {  
"field": "attributes.key"  
},  
"aggs": {  
"value": {  
"terms": {  
"field": "attributes.value"  
}  
}  
}  
}  
}  
}  
}  
}

second one gets all the color family and white ones and not the white in color\_family

{  
"took": 139,  
"hits": {  
"total": 2002,  
"max\_score": 102.30364,  
"hits": [  
......  
]  
},  
"aggregations": {  
"attributes": {  
"doc\_count": 9146,  
"key": {  
"doc\_count\_error\_upper\_bound": 72,  
"sum\_other\_doc\_count": 3495,  
"buckets": [  
{  
"key": "color\_family",  
"doc\_count": 2002,  
"value": {  
"doc\_count\_error\_upper\_bound": 0,  
"sum\_other\_doc\_count": 0,  
"buckets": [  
{  
"key": "white",  
"doc\_count": 1998  
},  
{  
"key": "black",  
"doc\_count": 1  
},  
{  
"key": "blue",  
"doc\_count": 1  
},  
{  
"key": "green",  
"doc\_count": 1  
},  
{  
"key": "multicolor",  
"doc\_count": 1  
},  
{  
"key": "red",  
"doc\_count": 1  
}  
]  
}  
}  
]  
}  
}  
}  
}

---

<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:** [June 21, 2017, 7:00pm UTC](https://discuss.elastic.co/t/dynamic-faceting/86850/4 "2017-06-21T19:00:10Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
