Can I use post_filter with top_hits?

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.

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