# Elasticsearch dynamic faceting

**URL:** https://discuss.elastic.co/t/elasticsearch-dynamic-faceting/19508
**Category:** Elasticsearch
**Created:** [August 28, 2014, 9:21am UTC](https://discuss.elastic.co/t/elasticsearch-dynamic-faceting/19508 "2014-08-28T09:21:20Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Kresimir\_Slugan](https://avatars.discourse-cdn.com/v4/letter/k/dec6dc/32.png) [@Kresimir\_Slugan](https://discuss.elastic.co/u/Kresimir_Slugan)
#### Post date: [August 28, 2014, 9:21am UTC](https://discuss.elastic.co/t/elasticsearch-dynamic-faceting/19508/1 "2014-08-28T09:21:20Z")

</div>

I have Elasticsearch cluster with e-shop data. There are many product in  
there and they have attributes which belong to attribute categories. When I  
search I don't know which attribute category are going to be returned and I  
need facet statistic for every one of them.

E.g. documents look like:

{

"doc": "doc1",

categories: { "f1": "a", "f2": 0, "f3": "b", }

},

{

"doc": "doc2",

categories: { "f2": 3, "f3": "c", "f4": "d", }

}

I need to get facets statistic for every key in categories part of document  
e.g. statistic for f1, f2, f3 etc. and I need to get it in one query (not  
to get all categories first and then issue second query for actual  
data)...It is ok if I have to change data structure. Is it possible to  
do this using facets or aggregations in Elasticsearch?

--  
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/a9ac8ce6-14d0-44dd-9721-a3ba75477aaa%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/a9ac8ce6-14d0-44dd-9721-a3ba75477aaa%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

### Author: ![vineeth\_mohan\_2](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vineeth_mohan_2/32/747_2.png) [@vineeth\_mohan\_2](https://discuss.elastic.co/u/vineeth_mohan_2)
#### Post date: [August 28, 2014, 11:05am UTC](https://discuss.elastic.co/t/elasticsearch-dynamic-faceting/19508/2 "2014-08-28T11:05:03Z")

</div>

Hello ,

Aggregation is exactly what you are looking for.

First you need to structure your data in this manner -

categories : [

```
                  { "key" : "f2",

                      "value" : 3

                 },

                 { "key" : "f3",

                    "value" : "c"

                  }

                 ]

```

Then simply apply a 2 level aggregation -

> **[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

```
       Vineeth

```

On Thu, Aug 28, 2014 at 2:51 PM, Krešimir Slugan [kresimir.slugan@gmail.com](mailto:kresimir.slugan@gmail.com)  
wrote:

> I have Elasticsearch cluster with e-shop data. There are many product in  
> there and they have attributes which belong to attribute categories. When I  
> search I don't know which attribute category are going to be returned and I  
> need facet statistic for every one of them.
> 
> E.g. documents look like:
> 
> {
> 
> "doc": "doc1",
> 
> categories: { "f1": "a", "f2": 0, "f3": "b", }
> 
> },
> 
> {
> 
> "doc": "doc2",
> 
> categories: { "f2": 3, "f3": "c", "f4": "d", }
> 
> }
> 
> I need to get facets statistic for every key in categories part of  
> document e.g. statistic for f1, f2, f3 etc. and I need to get it in one  
> query (not to get all categories first and then issue second query for  
> actual data)...It is ok if I have to change data structure. Is it possible  
> to do this using facets or aggregations in Elasticsearch?
> 
> --  
> 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/a9ac8ce6-14d0-44dd-9721-a3ba75477aaa%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/a9ac8ce6-14d0-44dd-9721-a3ba75477aaa%40googlegroups.com)  
> [https://groups.google.com/d/msgid/elasticsearch/a9ac8ce6-14d0-44dd-9721-a3ba75477aaa%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/a9ac8ce6-14d0-44dd-9721-a3ba75477aaa%40googlegroups.com?utm_medium=email&utm_source=footer)  
> .  
> For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
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/CAGdPd5mN3JgSNayG58L3wQZBgt9Sx-sMxYM%3Dny9OfNpteDZGNg%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAGdPd5mN3JgSNayG58L3wQZBgt9Sx-sMxYM%3Dny9OfNpteDZGNg%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

### Author: ![Kresimir\_Slugan](https://avatars.discourse-cdn.com/v4/letter/k/dec6dc/32.png) [@Kresimir\_Slugan](https://discuss.elastic.co/u/Kresimir_Slugan)
#### Post date: [August 28, 2014, 3:25pm UTC](https://discuss.elastic.co/t/elasticsearch-dynamic-faceting/19508/3 "2014-08-28T15:25:26Z")

</div>

Thanks! I'm going to try this.

On Thursday, August 28, 2014 1:05:11 PM UTC+2, vineeth mohan wrote:

> Hello ,
> 
> Aggregation is exactly what you are looking for.
> 
> First you need to structure your data in this manner -
> 
> categories : [
> 
> ```
> { "key" : "f2",
> 
> "value" : 3
> 
> },
> 
> { "key" : "f3",
> 
> "value" : "c"
> 
> }
> 
> ]
> 
> ```
> 
> Then simply apply a 2 level aggregation -  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-aggregations.html)
> 
> Thanks
> 
> ```
> Vineeth
> 
> ```
> 
> On Thu, Aug 28, 2014 at 2:51 PM, Krešimir Slugan \<[kresimi...@gmail.com](mailto:kresimi...@gmail.com)  
> \<javascript:\>\> wrote:
> 
> > I have Elasticsearch cluster with e-shop data. There are many product in  
> > there and they have attributes which belong to attribute categories. When I  
> > search I don't know which attribute category are going to be returned and I  
> > need facet statistic for every one of them.
> > 
> > E.g. documents look like:
> > 
> > {
> > 
> > "doc": "doc1",
> > 
> > categories: { "f1": "a", "f2": 0, "f3": "b", }
> > 
> > },
> > 
> > {
> > 
> > "doc": "doc2",
> > 
> > categories: { "f2": 3, "f3": "c", "f4": "d", }
> > 
> > }
> > 
> > I need to get facets statistic for every key in categories part of  
> > document e.g. statistic for f1, f2, f3 etc. and I need to get it in one  
> > query (not to get all categories first and then issue second query for  
> > actual data)...It is ok if I have to change data structure. Is it possible  
> > to do this using facets or aggregations in Elasticsearch?
> > 
> > --  
> > 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 [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com) \<javascript:\>.  
> > To view this discussion on the web visit  
> > [https://groups.google.com/d/msgid/elasticsearch/a9ac8ce6-14d0-44dd-9721-a3ba75477aaa%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/a9ac8ce6-14d0-44dd-9721-a3ba75477aaa%40googlegroups.com)  
> > [https://groups.google.com/d/msgid/elasticsearch/a9ac8ce6-14d0-44dd-9721-a3ba75477aaa%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/a9ac8ce6-14d0-44dd-9721-a3ba75477aaa%40googlegroups.com?utm_medium=email&utm_source=footer)  
> > .  
> > For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
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/c04d9e13-c0e8-4233-b7e7-61f6663ab134%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/c04d9e13-c0e8-4233-b7e7-61f6663ab134%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

### Author: ![Kresimir\_Slugan](https://avatars.discourse-cdn.com/v4/letter/k/dec6dc/32.png) [@Kresimir\_Slugan](https://discuss.elastic.co/u/Kresimir_Slugan)
#### Post date: [October 16, 2014, 3:57pm UTC](https://discuss.elastic.co/t/elasticsearch-dynamic-faceting/19508/4 "2014-10-16T15:57:24Z")

</div>

This worked. Query look something like this:

{  
"query":{  
"match\_all":{}  
},  
"aggs": {  
"categories": {  
"nested": {  
"path": "categories"  
},  
"aggs": {  
"key": {  
"terms": {  
"field": "categories.key",  
"include" : "f.\*"  
},  
"aggs": {  
"value": {  
"terms": {  
"field": "categories.value"  
}  
}  
}  
}  
}  
}  
}  
}

It is nested query with bucketing by categories.key and then aggregating  
by categories.value. For this to work, categories field has to have "type":  
"nested" in mapping.json:

"categories": {  
"type": "nested",  
"properties": {  
"key": {  
"type": "string"  
},  
"value": {  
"type": "string"  
}  
}  
}

--  
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/db6acc38-10dd-4ae1-9d9e-2a312d77f742%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/db6acc38-10dd-4ae1-9d9e-2a312d77f742%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:55am UTC](https://discuss.elastic.co/t/elasticsearch-dynamic-faceting/19508/5 "2017-07-06T00:55:44Z")

</div>


