Elasticsearch. Агрегация - rodinandrey/dev_notes GitHub Wiki
Агрегация в общем виде на примере товаров магазина
{
"size": 0,
"aggs": {
"category": {
"terms": {
"field": "category"
}
}
}
}
в ответе видим что получили данные по каждой категории. Категория - количество товаров в категории
"aggregations": {
"category": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 45,
"buckets": [
{
"key": 3,
"doc_count": 10195
},
{
"key": 4,
"doc_count": 154
},
...
]
}
}
Добавляем статус в агрегации
{
"size": 0,
"aggs": {
"category": {
"terms": {
"field": "category"
}
},
"status": {
"terms": {
"field": "status"
}
}
}
}
к категориям добавился статус
...
"aggregations": {
...
"status": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "not_available",
"doc_count": 24001
},
{
"key": "available",
"doc_count": 2847
}
]
}
}
далее интереснее. что если нам необходимо получить данные по статусам для каждой категории. Это вложенные агрегации
{
"size": 0,
"aggs": {
"category": {
"terms": {
"field": "category"
},
"aggs": {
"status": {
"terms": {
"field": "status"
}
}
}
}
}
}
"aggregations": {
"category": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 45,
"buckets": [
{
"key": 3,
"doc_count": 10195,
"status": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "not_available",
"doc_count": 9718
},
{
"key": "available",
"doc_count": 477
}
]
}
},
...
]
}
}