后过滤器:
在计算了聚合之后,post_过滤器将应用于搜索请求末尾的搜索结果
不能根据查询或后筛选中字段的出现次数筛选文档,也不能使用聚合结果筛选查询中的文档。
一。获取给定次数出现的术语并在文档中搜索这些术语(2次调用弹性搜索)
2。使用
top_hits
映射:
{
"index48" : {
"mappings" : {
"properties" : {
"name" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
}
}
}
}
查询:
{
"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"
}
}
}
}
}
}
"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"
}
}
]
}
}
}
]
}
}