# How to use facet filtering with nested documents on ElasticSearch

**URL:** <https://discuss.elastic.co/t/how-to-use-facet-filtering-with-nested-documents-on-elasticsearch/15263>\
**Category:** Elasticsearch\
**Created:** [January 16, 2014, 8:35am UTC](https://discuss.elastic.co/t/how-to-use-facet-filtering-with-nested-documents-on-elasticsearch/15263 "2014-01-16T08:35:21Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tobi\_M](https://avatars.discourse-cdn.com/v4/letter/t/848f3c/32.png) [@Tobi\_M](https://discuss.elastic.co/u/Tobi_M)\
**Post date:** [January 16, 2014, 8:35am UTC](https://discuss.elastic.co/t/how-to-use-facet-filtering-with-nested-documents-on-elasticsearch/15263/1 "2014-01-16T08:35:21Z")

</div>

Hi!

I have the following mapping:

curl -XPUT '[http://localhost:9200/bookstore/user/\_mapping](http://localhost:9200/bookstore/user/_mapping)' -d '  
{  
"user": {  
"properties": {  
"user\_id": { "type": "integer" },  
"gender": { "type": "string", "index" : "not\_analyzed" },  
"age": { "type": "integer" },  
"age\_bracket": { "type": "string", "index" : "not\_analyzed" },  
"current\_city": { "type": "string", "index" : "not\_analyzed" },  
"relationship\_status": { "type": "string", "index" : "not\_analyzed" },  
"books" : {  
"type": "nested",  
"properties" : {  
"b\_oid": { "type": "string", "index" : "not\_analyzed" },  
"b\_name": { "type": "string", "index" : "not\_analyzed" },  
"bc\_id": { "type": "integer" },  
"bc\_name": { "type": "string", "index" : "not\_analyzed" },  
"bcl\_name": { "type": "string", "index" : "not\_analyzed" },  
"b\_id": { "type": "integer" }  
}  
}  
}  
}  
}'

Now, I'd like to run a query where I count the books.b\_names for a certain  
books.bcl\_name (the books category name) and, for example, where the gender  
is "Male". I'm a little bit confused of how to get this to work. My query  
is the following:

curl -XGET '[http://localhost:9200/bookstore/user/\_search?pretty=1](http://localhost:9200/bookstore/user/_search?pretty=1)' -d '{  
"size": 0,  
"from": 0,  
"query": {  
"match\_all": {}  
},  
"facets": {  
"CategoryFacet": {  
"terms": {  
"field": "books.b\_name",  
"size": 5,  
"shard\_size": 1000,  
"order": "count"  
},  
"nested": "books",  
"facet\_filter": {  
"and": [  
{  
"nested" : {  
"path" : "books",  
"query" : {  
"match" : {  
"books.bcl\_name" : "Trivia"  
}  
},  
"join" : false  
}  
},  
{  
"term": {  
"gender": "Male"  
}  
}  
]  
}  
}  
}  
}'

So, I'd like to facet\_filter on both the nested document as well as the  
parent. How can I achieve this, because apparantly it doesnt's work the way  
I imagined. The result of my query is the following:

{  
"took" : 320,  
"timed\_out" : false,  
"\_shards" : {  
"total" : 5,  
"successful" : 5,  
"failed" : 0  
},  
"hits" : {  
"total" : 500000,  
"max\_score" : 1.0,  
"hits" : []  
},  
"facets" : {  
"CategoryFacet" : {  
"\_type" : "terms",  
"missing" : 0,  
"total" : 0,  
"other" : 0,  
"terms" : []  
}  
}  
}

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/59a95b77-a4d5-47dc-8095-d7124cb0d1ef%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/59a95b77-a4d5-47dc-8095-d7124cb0d1ef%40googlegroups.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, 1:56am UTC](https://discuss.elastic.co/t/how-to-use-facet-filtering-with-nested-documents-on-elasticsearch/15263/2 "2017-07-06T01:56:28Z")

</div>

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


