# Searching root document and nested documents in facet query

**URL:** https://discuss.elastic.co/t/searching-root-document-and-nested-documents-in-facet-query/11741
**Category:** Elasticsearch
**Created:** [April 30, 2013, 6:05am UTC](https://discuss.elastic.co/t/searching-root-document-and-nested-documents-in-facet-query/11741 "2013-04-30T06:05:28Z")
**Posts on this page:** 3
**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 30, 2013, 6:05am UTC](https://discuss.elastic.co/t/searching-root-document-and-nested-documents-in-facet-query/11741/1 "2013-04-30T06:05:28Z")

</div>

Hi,  
I have user document which contains nested tag documents. These tag  
documents can be of varied types i.e it can be a tag of college, company,  
user\_type, course, etc. I want to implement a linked in type of people  
search ([http://www.linkedin.com/search/fpsearch?trk=tab\_pro](http://www.linkedin.com/search/fpsearch?trk=tab_pro)) in my  
application. What I am trying to do is, when user selects any tag from the  
search box. The facet count of various tags are updated and users are  
filtered according to that tag. But while getting the facet counts for,  
say, company, when I select company tag, the company facet count should not  
be updated (as implemented in the people search of linked in).  
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  
}  
}  
}  
}  
}  
}  
}  
}  
Here is the sample user document containing college and company tags  
{  
"tags": [  
{  
"type": "college",  
"value": "Velammal Institute of Technology",  
"id": "f96807dcac0f218f41ff67ecb5a4881ee4f93a3f"  
},  
{  
"type": "company",  
"value": "TCS-Tata consultancy services",  
"id": "d3f91402aa0a795bf29dcc2295a804060c0a00f1"  
},  
{  
"type": "company",  
"value": "Google"  
"id" : "d3f91402aa0a795bf29dcc2295a804060c0a00f1"  
"current" : true  
}  
]  
}  
I want to get the filtered users and the facets in a single query to  
implement the above. For this, I need to use query parts that are common  
for computing filtered users and fetching facets in the "query" section of  
ES query. The additional filter that will be used to filter out the users  
to compute facet counts will be included in the "facet\_filter" section of  
ES query. Also, "facet\_filter" will filter out the tag\_types - i.e if I  
want to get the facet of college tags only. I will used nested facet filter  
to filter out the tags in the users.  
Presently, I am able to filter out the tags using nested facet filter using  
this query:  
{  
"query": {  
"filtered": {  
"query": {  
"match\_all": {

```
    }
  },
}

```

},  
"facets": {  
"company": {  
"terms": {  
"field": "tags.value.value\_untouched",  
"size": 0  
},?  
"nested": "tags",  
"facet\_filter": {  
"term": {  
"tags.type": "company"  
}  
}  
}  
}  
}  
My question is I want to filter out the user documents based on tags in the  
facet filter like this:  
{"nested" : {"path": "tags", "query": {"bool":{"must": [{term: {"tags.type"  
=\> "company"}}, {"term":{"tags.value.value\_untouched" =\> "Google"}}]}}}}  
The above subquery will filter out the user documents. Then I can filter  
out the tags whose count I want to return, as given in the ES query above.

In general, Is there a way to filter out root document based on nested  
queries and then compute nested facets in the "facet\_filter" of ES query?

--  
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: ![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 30, 2013, 6:11am UTC](https://discuss.elastic.co/t/searching-root-document-and-nested-documents-in-facet-query/11741/2 "2013-04-30T06:11:58Z")

</div>

Or Is there a another way the filter out the parent document using nested  
queries to compute facets only i.e that query part will not be used in the  
filtering users using the actual query ?

On Tue, Apr 30, 2013 at 11:35 AM, aash dhariya [aash.discover@gmail.com](mailto:aash.discover@gmail.com)wrote:

> Hi,  
> I have user document which contains nested tag documents. These tag  
> documents can be of varied types i.e it can be a tag of college, company,  
> user\_type, course, etc. I want to implement a linked in type of people  
> search ([Sign Up | LinkedIn](http://www.linkedin.com/search/fpsearch?trk=tab_pro)) in my  
> application. What I am trying to do is, when user selects any tag from the  
> search box. The facet count of various tags are updated and users are  
> filtered according to that tag. But while getting the facet counts for,  
> say, company, when I select company tag, the company facet count should not  
> be updated (as implemented in the people search of linked in).  
> 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  
> }  
> }  
> }  
> }  
> }  
> }  
> }  
> }  
> Here is the sample user document containing college and company tags  
> {  
> "tags": [  
> {  
> "type": "college",  
> "value": "Velammal Institute of Technology",  
> "id": "f96807dcac0f218f41ff67ecb5a4881ee4f93a3f"  
> },  
> {  
> "type": "company",  
> "value": "TCS-Tata consultancy services",  
> "id": "d3f91402aa0a795bf29dcc2295a804060c0a00f1"  
> },  
> {  
> "type": "company",  
> "value": "Google"  
> "id" : "d3f91402aa0a795bf29dcc2295a804060c0a00f1"  
> "current" : true  
> }  
> ]  
> }  
> I want to get the filtered users and the facets in a single query to  
> implement the above. For this, I need to use query parts that are common  
> for computing filtered users and fetching facets in the "query" section of  
> ES query. The additional filter that will be used to filter out the users  
> to compute facet counts will be included in the "facet\_filter" section of  
> ES query. Also, "facet\_filter" will filter out the tag\_types - i.e if I  
> want to get the facet of college tags only. I will used nested facet filter  
> to filter out the tags in the users.  
> Presently, I am able to filter out the tags using nested facet filter  
> using this query:  
> {  
> "query": {  
> "filtered": {  
> "query": {  
> "match\_all": {
> 
> ```
> }
> },
> }
> 
> ```
> 
> },  
> "facets": {  
> "company": {  
> "terms": {  
> "field": "tags.value.value\_untouched",  
> "size": 0  
> },?  
> "nested": "tags",  
> "facet\_filter": {  
> "term": {  
> "tags.type": "company"  
> }  
> }  
> }  
> }  
> }  
> My question is I want to filter out the user documents based on tags in  
> the facet filter like this:  
> {"nested" : {"path": "tags", "query": {"bool":{"must": [{term:  
> {"tags.type" =\> "company"}}, {"term":{"tags.value.value\_untouched" =\>  
> "Google"}}]}}}}  
> The above subquery will filter out the user documents. Then I can filter  
> out the tags whose count I want to return, as given in the ES query above.
> 
> In general, Is there a way to filter out root document based on nested  
> queries and then compute nested facets in the "facet\_filter" of ES query?
> 
> --  
> Thanks,  
> Aash

--  
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/searching-root-document-and-nested-documents-in-facet-query/11741/3 "2017-07-06T02:39:08Z")

</div>


