# Help with grouping facet

**URL:** <https://discuss.elastic.co/t/help-with-grouping-facet/14433>\
**Category:** Elasticsearch\
**Created:** [November 15, 2013, 7:22pm UTC](https://discuss.elastic.co/t/help-with-grouping-facet/14433 "2013-11-15T19:22:02Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Matheus\_Salvia](https://avatars.discourse-cdn.com/v4/letter/m/c68b51/32.png) [@Matheus\_Salvia](https://discuss.elastic.co/u/Matheus_Salvia)\
**Post date:** [November 15, 2013, 7:22pm UTC](https://discuss.elastic.co/t/help-with-grouping-facet/14433/1 "2013-11-15T19:22:02Z")

</div>

Hello everyone,

I'm having a little trouble in building a query in my current elasticsearch  
structure, and I wandered if its possible without changing the modeling.  
My current structure is as follows:  
I have a parent document type namely 'user', which has many child types  
representing the many aspects of a users (the user documents themselves  
contain no descriptive information whatsoever).  
For example, I may have the child type 'computer' which contains the fields  
'screencolor', 'screenwidth' and 'screenheight'. then I may have the child  
type 'personal\_info' which contains the fields 'name', 'age' and 'address'.  
(By definition no different child types will have a field with the same  
name, if that matters).  
Another thing which is part of my design is the possibility of a single  
user containing more than one child of the same type (e.g. one user can  
have _N_ computers).  
The query I want to execute is as follows:  
For every user which match a certain criteria, group the values of the  
fields and return me the count of users grouped by the values of the fields.  
An example would be:  
Grouped by the values of 'screenwidth' and 'age', return me the count of  
users.  
It would be something like that:  
{  
"size": 0,  
"query": {  
"match\_all": {}  
},  
"facets": {  
"my\_facet": {  
"terms": {  
"script\_field": "_source.age + '_' +  
\_source.screenwidth",  
"all\_terms": True  
}  
}  
}  
}

This query would return me something like:  
"facets": {  
"my\_facet": {  
"_type": "terms",  
"missing": 0,  
"total": 9,  
"other": 0,  
"terms": [  
{  
"term": "[24]_[1280]",  
"count": 4  
},  
{  
"term": "[32]_[800]",  
"count": 2  
},  
{  
"term": "[24]_[1920]",  
"count": 1  
},  
{  
"term": "[24]_[1440]",  
"count": 1  
},  
{  
"term": "[24]_[1366]",  
"count": 1  
}  
]  
}  
}

The problem is, this only returns me the number of documents which match  
the query, which is not the number of users, as there maybe more than 1  
child document of the same type per user (by definition).  
Is there some way to get what I want in a single query?  
It should be clear that I cannot change this architecture, because other  
parts of my system which depend on it, but I could create another index  
just for this purpose.

Thanks a lot in advance,  
Matheus Salvia

--  
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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<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, 2:06am UTC](https://discuss.elastic.co/t/help-with-grouping-facet/14433/2 "2017-07-06T02:06:41Z")

</div>

Closed on July 6, 2017, 2:06am UTC


