# Can I use post\_filter with top\_hits?

**URL:** <https://discuss.elastic.co/t/can-i-use-post-filter-with-top-hits/203127>\
**Category:** Elasticsearch\
**Created:** [October 11, 2019, 12:06am UTC](https://discuss.elastic.co/t/can-i-use-post-filter-with-top-hits/203127 "2019-10-11T00:06:41Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Arnab\_Banerjee](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/arnab_banerjee/32/55738_2.png) [@Arnab\_Banerjee](https://discuss.elastic.co/u/Arnab_Banerjee)\
**Post date:** [October 11, 2019, 12:06am UTC](https://discuss.elastic.co/t/can-i-use-post-filter-with-top-hits/203127/1 "2019-10-11T00:06:41Z")

</div>

Hi,

I am using top\_hits aggregation with sort aggregation and want to filter out some of the top\_hits based on a term. Can I use post filter to do that? If now how to use filter aggregation with top hits?

So I've pasted my query below. It works on a index containing citizens of a state. I have aggregated them based on city as the top level bucket and want to show the information of the oldest citizen of each city using top\_hits and sort aggregation. If the oldest citizen is disabled (we can determine that with a field named isDisabled = true, isDisabled is true if someone is disabled, this field will not be present if someone is not disabled) we need to show null (it should not show the oldest non-disabled citizen instead). Either the oldest citizen who is not disabled or null.

query:

{  
"size": 0,  
"aggregations": {  
"search\_documents": {  
"terms": {  
"field": "city",  
"size": 10,  
"min\_doc\_count": 1,  
"shard\_min\_doc\_count": 0,  
"show\_term\_doc\_count\_error": true,  
"order": [  
{  
"\_count": "desc"  
},  
{  
"\_term": "asc"  
}  
],  
"include": {  
"partition": 0,  
"num\_partitions": 1  
}  
},  
"aggregations": {  
"citizen\_search\_result": {  
"top\_hits": {  
"from": 0,  
"size": 1,  
"version": false,  
"explain": false,  
"sort": [  
{  
"age": {  
"order": "desc"  
}  
}  
]  
}  
}  
}  
}  
}  
}

response

{  
"took" : 2,  
"timed\_out" : false,  
"\_shards" : {  
"total" : 5,  
"successful" : 5,  
"skipped" : 0,  
"failed" : 0  
},  
"hits" : {  
"total" : 3,  
"max\_score" : 0.0,  
"hits" :   
},  
"aggregations" : {  
"search\_documents" : {  
"doc\_count\_error\_upper\_bound" : 0,  
"sum\_other\_doc\_count" : 0,  
"buckets" : [  
{  
"key" : "Bothel",  
"doc\_count" : 3,  
"doc\_count\_error\_upper\_bound" : 0,  
"citizen\_search\_result" : {  
"hits" : {  
"total" : 3,  
"max\_score" : null,  
"hits" : [  
{  
"\_index" : "absentboolkeytestold",  
"\_type" : "citizen",  
"\_id" : "LTzMt20BrCTD-YYbAK\_c",  
"\_score" : null,  
"\_source" : {  
"name" : "Nancy",  
"city" : "Bothel",  
"age" : 40,  
"isDisabled" : true  
},  
"sort" : [  
"40"  
]  
}  
]  
}  
}  
}  
]  
}  
}  
}

I want to show null here as Nancy the oldest citizen of Bothell is disabled. Remember that I cant use a top level filter aggregation for isDisabled field as it will filter out all disabled citizens even before the aggregation.

---

<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:** [November 8, 2019, 12:06am UTC](https://discuss.elastic.co/t/can-i-use-post-filter-with-top-hits/203127/2 "2019-11-08T00:06:47Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
