# Problem in facet

**URL:** <https://discuss.elastic.co/t/problem-in-facet/16222>\
**Category:** Elasticsearch\
**Created:** [March 7, 2014, 8:42am UTC](https://discuss.elastic.co/t/problem-in-facet/16222 "2014-03-07T08:42:56Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dapeng\_liu](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dapeng_liu/32/1744_2.png) [@dapeng\_liu](https://discuss.elastic.co/u/dapeng_liu)\
**Post date:** [March 7, 2014, 8:42am UTC](https://discuss.elastic.co/t/problem-in-facet/16222/1 "2014-03-07T08:42:56Z")

</div>

Hello:  
I got a problem in facet, for example I have person documents defined as  
following

person = {  
name:'personName1',  
attributes:[  
{  
name:'attributeName1',  
value:'attributeName1Value1'  
},  
{  
name:'attributeName2',  
value:'attributeName2Value1'  
},  
{  
name:'attributeName3',  
value:'attributeName3Value1'  
}  
]  
}

each person contains one name and a list of attributes, and each attribute  
contains name and value.  
each attribute name e.g 'attributeName1' might have several possible values  
'attrbuteName1Value1','attributeName1Value2',attributeName1Value3....  
I have a lot of person documents combined with different attribute names  
with different possible attribute values

I want to define a facet to query how many person in specific attribute name  
e.g. how many persons with attribute name 'attributeName1'  
the result would be:  
facet = {  
person\_attributes\_attribute\_name\_1:{  
terms:[  
{  
"term":"attributeName1Value1",  
"count":1000  
},  
{  
"term":"attributeName1Value2",  
"count":1300  
},  
{  
"term":"attributeName1Value3",  
"count":700  
}  
]  
}  
}

thanks

--  
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/2a10f9b0-c8ad-42ae-80a2-914cfa32cfa5%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/2a10f9b0-c8ad-42ae-80a2-914cfa32cfa5%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![jpountz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jpountz/32/45836_2.png) [@jpountz](https://discuss.elastic.co/u/jpountz)\
**Post date:** [March 7, 2014, 1:39pm UTC](https://discuss.elastic.co/t/problem-in-facet/16222/2 "2014-03-07T13:39:19Z")

</div>

Hi,

For this you need to use nested documents and the nested aggregation[1], I  
just wrote a query that should give the expected results:

> <https://gist.github.com/jpountz/9411624>

[1]

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

On Fri, Mar 7, 2014 at 9:42 AM, [dapeng.liu@ysura.com](mailto:dapeng.liu@ysura.com) wrote:

> Hello:  
> I got a problem in facet, for example I have person documents defined as  
> following
> 
> person = {  
> name:'personName1',  
> attributes:[  
> {  
> name:'attributeName1',  
> value:'attributeName1Value1'  
> },  
> {  
> name:'attributeName2',  
> value:'attributeName2Value1'  
> },  
> {  
> name:'attributeName3',  
> value:'attributeName3Value1'  
> }  
> ]  
> }
> 
> each person contains one name and a list of attributes, and each attribute  
> contains name and value.  
> each attribute name e.g 'attributeName1' might have several possible  
> values 'attrbuteName1Value1','attributeName1Value2',attributeName1Value3....  
> I have a lot of person documents combined with different attribute names  
> with different possible attribute values
> 
> I want to define a facet to query how many person in specific attribute  
> name  
> e.g. how many persons with attribute name 'attributeName1'  
> the result would be:  
> facet = {  
> person\_attributes\_attribute\_name\_1:{  
> terms:[  
> {  
> "term":"attributeName1Value1",  
> "count":1000  
> },  
> {  
> "term":"attributeName1Value2",  
> "count":1300  
> },  
> {  
> "term":"attributeName1Value3",  
> "count":700  
> }  
> ]  
> }  
> }
> 
> thanks
> 
> --  
> 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/2a10f9b0-c8ad-42ae-80a2-914cfa32cfa5%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/2a10f9b0-c8ad-42ae-80a2-914cfa32cfa5%40googlegroups.com)[https://groups.google.com/d/msgid/elasticsearch/2a10f9b0-c8ad-42ae-80a2-914cfa32cfa5%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/2a10f9b0-c8ad-42ae-80a2-914cfa32cfa5%40googlegroups.com?utm_medium=email&utm_source=footer)  
> .  
> For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
Adrien Grand

--  
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/CAL6Z4j68Cp5xdvtWVwT4yUqDgbNVDCH%3DCcUEvf0wo\_6oNNe0nw%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAL6Z4j68Cp5xdvtWVwT4yUqDgbNVDCH%3DCcUEvf0wo_6oNNe0nw%40mail.gmail.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, 1:44am UTC](https://discuss.elastic.co/t/problem-in-facet/16222/3 "2017-07-06T01:44:56Z")

</div>

Closed on July 6, 2017, 1:44am UTC


