# RangeFilter on Nested/Child Facet Counts

**URL:** <https://discuss.elastic.co/t/rangefilter-on-nested-child-facet-counts/16293>\
**Category:** Elasticsearch\
**Created:** [March 11, 2014, 2:29pm UTC](https://discuss.elastic.co/t/rangefilter-on-nested-child-facet-counts/16293 "2014-03-11T14:29:10Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Benji\_Smith](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/benji_smith/32/1748_2.png) [@Benji\_Smith](https://discuss.elastic.co/u/Benji_Smith)\
**Post date:** [March 11, 2014, 2:29pm UTC](https://discuss.elastic.co/t/rangefilter-on-nested-child-facet-counts/16293/1 "2014-03-11T14:29:10Z")

</div>

Imagine I have three different types of data stored in my ElasticSearch  
index: users, posts, and replies. Each post belongs to a particular user.  
Each reply belongs to a particular user and post.

I'd like to write a query to find all users with a particular number of  
posts (e.g., find all users with exactly one post, or all users with  
between ten and twenty posts).

Along those same lines, I'd like to find all users with posts having a  
particular number of replies. Basically, I'd like to filter documents based  
on facet counts, which seems like a pretty reasonable thing to do, but I  
can't figure out any way to do it.

I've tried to model this problem with embedded objects, nested objects, and  
parent/child objects, but the query DSL doesn't seem to support these kinds  
of queries, no matter how I model the data.

Any advice?

Benji Smith

--  
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/055da68c-8641-4592-a0f3-a8f82ded29f1%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/055da68c-8641-4592-a0f3-a8f82ded29f1%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Binh\_Ly\_2](https://avatars.discourse-cdn.com/v4/letter/b/d07c76/32.png) [@Binh\_Ly\_2](https://discuss.elastic.co/u/Binh_Ly_2)\
**Post date:** [March 11, 2014, 7:31pm UTC](https://discuss.elastic.co/t/rangefilter-on-nested-child-facet-counts/16293/2 "2014-03-11T19:31:26Z")

</div>

If you can denormalize, you should be able to do script filters (query or  
aggregations, whichever you need). It's not gonna be the fastest thing in  
the world but consider this example:

POST [http://localhost:9200/user/doc](http://localhost:9200/user/doc)  
{ "name": "user1", "posts": ["1"] }

POST [http://localhost:9200/user/doc](http://localhost:9200/user/doc)  
{ "name": "user2", "posts": ["1", "2"] }

POST [http://localhost:9200/user/doc](http://localhost:9200/user/doc)  
{ "name": "user3", "posts": ["1", "2", "3"] }

Then this aggregation:

POST [http://localhost:9200/user/\_search](http://localhost:9200/user/_search)  
{  
"size": 0,  
"aggs": {  
"exactly\_one\_post": {  
"filter": {  
"script": {  
"script": "doc['posts'].values.size() == 1"  
}  
}  
},  
"one\_or\_more\_posts": {  
"filter": {  
"script": {  
"script": "doc['posts'].values.size() \>= 1"  
}  
}  
},  
"between\_one\_and\_two\_posts": {  
"filter": {  
"script": {  
"script": "doc['posts'].values.size() \>= 1 &&  
doc['posts'].values.size() \<= 2"  
}  
}  
}  
}  
}

--  
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/7619b100-f19e-41af-aa66-df46aa9785b0%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/7619b100-f19e-41af-aa66-df46aa9785b0%40googlegroups.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/rangefilter-on-nested-child-facet-counts/16293/3 "2017-07-06T01:44:08Z")

</div>


