# To search in nested facets and returning the count of root documents

**URL:** https://discuss.elastic.co/t/to-search-in-nested-facets-and-returning-the-count-of-root-documents/11725
**Category:** Elasticsearch
**Created:** [April 29, 2013, 7:17am UTC](https://discuss.elastic.co/t/to-search-in-nested-facets-and-returning-the-count-of-root-documents/11725 "2013-04-29T07:17:01Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![aash\_dhariya](https://avatars.discourse-cdn.com/v4/letter/a/c37758/32.png) [@aash\_dhariya](https://discuss.elastic.co/u/aash_dhariya)
#### Post date: [April 29, 2013, 7:17am UTC](https://discuss.elastic.co/t/to-search-in-nested-facets-and-returning-the-count-of-root-documents/11725/1 "2013-04-29T07:17:01Z")

</div>

Hi,  
I have a user document which has nested tags stored in it. Tags can be of  
varied types - college, company, location, etc. I want to construct a query  
that is composed of multiple facet queries each for different types of tags  
which I mentioned above.  
My individual facet queries are different in each case. So, I included  
those differences in facet query.  
But when I query nested facets, it returns the count of the nested  
documents. I want the count of root documents.  
Here is my user mapping  
{  
"user" : {  
"properties" : {  
"tags" : {  
"type" : "nested",  
"properties" : {  
"id" : {  
"type" : "string",  
"index" : "not\_analyzed",  
"store" : "yes"  
},  
"current" : {  
"type" : "boolean"  
},  
"type" : {  
"type" : "string"  
},  
"value" : {  
"type" : "multi\_field",  
"fields" : {  
"value" : {  
"type" : "string",  
"analyzer" : "name\_analyzer"  
},  
"value\_untouched" : {  
"type" : "string",  
"index" : "not\_analyzed",  
"include\_in\_all" : false  
}  
}  
}  
}  
}  
}  
}  
}  
This is my sample document:  
{  
"tags": [  
{  
"type": "college",  
"value": "Velammal Institute of Technology",  
"id": "f96807dcac0f218f41ff67ecb5a4881ee4f93a3f"  
},  
{  
"type": "company",  
"value": "TCS-Tata consultancy services",  
"id": "d3f91402aa0a795bf29dcc2295a804060c0a00f1"  
},  
{  
"type": "company",  
"value": "TCS-Tata consultancy services"  
"id" : "d3f91402aa0a795bf29dcc2295a804060c0a00f1"  
"current" : true  
}  
]  
}  
tag\_ids are generated manually through a combination of its type and value.  
The current field in company determines whether it is a current company or  
not.  
I want to get the company facets of all the documents. So, this is my query:  
{  
"query": {  
"filtered": {  
"query": {  
"match\_all": {

```
    }
  },
}

```

## }, "facets": { "company": { "terms": { "field": "tags.value.value\_untouched", "size": 0 }, "nested": "tags", "facet\_filter": { "term": { "tags.type": "company" } } } } } This query returns me the count of nested facets but I want the counts of matching root documents. How do I do that?

Thanks,  
Aash

--  
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:39am UTC](https://discuss.elastic.co/t/to-search-in-nested-facets-and-returning-the-count-of-root-documents/11725/2 "2017-07-06T02:39:16Z")

</div>


