Post_filter:
Post_filter применяется к поисковым запросам в самом конце поискового запроса, после того как агрегаты уже рассчитаны
Нельзя фильтровать документы по количеству вхождений поля в запросе или post_filter, а также нельзя использовать результат агрегирования для фильтрации документов в запросе.
Решение проблемы может быть достигнуто двумя способами
1. Получить термины, которые встречаются указанное количество раз, и выполнить поиск документов в этих терминах (2 вызова на поиск elasti c)
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"
}
}
]
}
}
}
]
}
}