# Aggregation on childern but filters on parent

**URL:** <https://discuss.elastic.co/t/aggregation-on-childern-but-filters-on-parent/29946>\
**Category:** Elasticsearch\
**Created:** [September 24, 2015, 10:13pm UTC](https://discuss.elastic.co/t/aggregation-on-childern-but-filters-on-parent/29946 "2015-09-24T22:13:17Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![lokeshhctm](https://avatars.discourse-cdn.com/v4/letter/l/a4c791/32.png) [@lokeshhctm](https://discuss.elastic.co/u/lokeshhctm)\
**Post date:** [September 24, 2015, 10:13pm UTC](https://discuss.elastic.co/t/aggregation-on-childern-but-filters-on-parent/29946/1 "2015-09-24T22:13:17Z")

</div>

How can the query be optimized:

{  
"query": {  
"filtered": {  
"query": {  
"match\_all": []  
},  
"filter": {  
"bool": {  
"must": [],  
"must\_not": []  
}  
}  
}  
},  
"size": 0,  
"aggs": {  
"details-filter": {  
"filter": {  
"bool": {  
"must": [  
{  
"terms": {  
"Y": [  
"10810"  
]  
}  
},  
{  
"terms": {  
"P": [  
"121187"  
]  
}  
},  
{  
"terms": {  
"D": [  
"T"  
]  
}  
},  
{  
"terms": {  
"H": [  
"2"  
]  
}  
}  
]  
}  
},  
"aggs": {  
"uniques\_index": {  
"children": {  
"type": "u"  
},  
"aggs": {  
"uniques": {  
"cardinality": {  
"field": "u.userid.hash",  
"precision\_threshold": 100  
}  
}  
}  
}  
}  
}  
}  
}

The above query always takes the same time regardless of number of filters i put for parent.  
It seems like it always scans the whole data.

---

<div class="post-metadata">

**Author:** ![lokeshhctm](https://avatars.discourse-cdn.com/v4/letter/l/a4c791/32.png) [@lokeshhctm](https://discuss.elastic.co/u/lokeshhctm)\
**Post date:** [September 27, 2015, 2:52am UTC](https://discuss.elastic.co/t/aggregation-on-childern-but-filters-on-parent/29946/2 "2015-09-27T02:52:06Z")

</div>

Can somebody help me here please?

---

<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 5, 2017, 11:47pm UTC](https://discuss.elastic.co/t/aggregation-on-childern-but-filters-on-parent/29946/3 "2017-07-05T23:47:54Z")

</div>


