Post_filter:
The post_filter is applied to the search hits at the very end of a search request, after aggregations have already been calculated
You cannot filter documents based on number of occurrence of a field in query or post_filter and neither can you use an aggregation result to filter documents in query.
Solution to the problem can be achieved in two ways
1. Get the terms which occur given number of times and search the documents for these terms (2 calls to the elastic search)
2. Get documents in aggregation itself using top_hits
Mapping:
{
"index48" : {
"mappings" : {
"properties" : {
"name" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
}
}
}
}
Query:
{
"size": 0,
"aggs": {
"NAME": {
"terms": {
"field": "name.keyword",
"size": 10
},
"aggs": {
"documents": {
"top_hits": {
"size": 10
}
},
"bucket_count": {
"bucket_selector": {
"buckets_path": {
"path": "_count"
},
"script": "if(params.path>=1 && params.path<=3) return true"
}
}
}
}
}
}
Result:
"aggregations" : {
"NAME" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : "John",
"doc_count" : 3,
"documents" : {
"hits" : {
"total" : {
"value" : 3,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "index48",
"_type" : "_doc",
"_id" : "ZRPC3HABF9RqmGpImxj_",
"_score" : 1.0,
"_source" : {
"name" : "John"
}
},
{
"_index" : "index48",
"_type" : "_doc",
"_id" : "ZhPC3HABF9RqmGpIpBh4",
"_score" : 1.0,
"_source" : {
"name" : "John"
}
},
{
"_index" : "index48",
"_type" : "_doc",
"_id" : "ZxPC3HABF9RqmGpIqRhj",
"_score" : 1.0,
"_source" : {
"name" : "John"
}
}
]
}
}
},
{
"key" : "Doe",
"doc_count" : 2,
"documents" : {
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "index48",
"_type" : "_doc",
"_id" : "aBPC3HABF9RqmGpIyhhU",
"_score" : 1.0,
"_source" : {
"name" : "Doe"
}
},
{
"_index" : "index48",
"_type" : "_doc",
"_id" : "aRPC3HABF9RqmGpIzhh7",
"_score" : 1.0,
"_source" : {
"name" : "Doe"
}
}
]
}
}
}
]
}
}