Aggregation
Metric Aggregation
- min: 지정된 필드의 최소값을 계산합니다.
- max: 지정된 필드의 최대값을 계산합니다.
- sum: 지정된 필드의 합계를 계산합니다.
- avg: 지정된 필드의 평균값을 계산합니다.
- stats: min, max, sum, avg, count를 한 번에 제공합니다.
- cardinality: 필드의 고유한(unique) 값 개수를 계산합니다. 정확한 값이 아닌 근사치(approximate count)를 제공합니다.
- percentiles: 백분위수를 계산합니다.
- geo-centroid: 위치 정보 필드의 중심점을 계산합니다.
GET /index_name/_search
{
"size": 0,
"aggs": {
"agg_name": {
"avg": {
"field": "field_name"
}
}
}
}
- size: 0으로 설정하면 집계에 사용된 문서를 결과에 포함시키지 않습니다.
- agg_name: 집계의 이름으로, 원하는 형태로 지정 가능합니다.
- field: 집계를 수행할 필드명을 지정합니다.
# 평균값 계산
GET /my_index/_search
{
"size": 0,
"aggs": {
"avg_price": {
"avg": {
"field": "price"
}
}
}
}
# 통계 정보
GET /my_index/_search
{
"size": 0,
"aggs": {
"stats_price": {
"stats": {
"field": "price"
}
}
}
}
# 쿼리와 함께 사용
GET /my_index/_search
{
"query": {
"match": {
"category": "electronics"
}
},
"size": 0,
"aggs": {
"avg_price": {
"avg": {
"field": "price"
}
}
}
}
Bucket Aggregation
문서들을 특정 조건에 따라 그룹화하는 기능입니다. SQL의 GROUP BY와 유사한 역할을 합니다. 각 버킷별로 포함되는 문서의 개수는 doc_count 값에 기본적으로 표시되며, 각 버킷 내부에 다시 "aggs"를 선언하여 중첩된 집계를 수행할 수 있습니다.
Index & Documents
PUT /products
{
"mappings": {
"properties": {
"name": {
"type": "text"
},
"price": {
"type": "float"
},
"category": {
"type": "keyword"
},
"sales": {
"type": "integer"
},
"publish_date": {
"type": "date"
},
"comments": {
"type": "nested",
"properties": {
"user": {
"type": "text"
},
"comment": {
"type": "text"
},
"number_of_comments": {
"type": "integer"
}
}
}
}
}
}
POST /products/_doc/1
{
"name": "Laptop",
"price": 1000.00,
"category": "electronics",
"sales": 50,
"publish_date": "2023-01-10",
"comments": [
{
"user": "Alice",
"comment": "Great product!",
"number_of_comments": 5
},
{
"user": "Bob",
"comment": "Very useful for work.",
"number_of_comments": 3
}
]
}
POST /products/_doc/2
{
"name": "Smartphone",
"price": 500.00,
"category": "electronics",
"sales": 200,
"publish_date": "2023-02-15",
"comments": [
{
"user": "Charlie",
"comment": "I love the camera!",
"number_of_comments": 8
}
]
}
POST /products/_doc/3
{
"name": "Desk",
"price": 150.00,
"category": "furniture",
"sales": 20,
"publish_date": "2023-01-20",
"comments": [
{
"user": "Dave",
"comment": "Solid and sturdy.",
"number_of_comments": 2
},
{
"user": "Eve",
"comment": "Perfect for my home office.",
"number_of_comments": 1
}
]
}
POST /products/_doc/4
{
"name": "Chair",
"price": 100.00,
"category": "furniture",
"sales": 100,
"publish_date": "2023-03-05",
"comments": [
{
"user": "Frank",
"comment": "Very comfortable!",
"number_of_comments": 4
}
]
}
POST /products/_doc/5
{
"name": "Table",
"price": 200.00,
"sales": 200,
"publish_date": "2023-04-05",
"comments": [
{
"user": "Frank",
"comment": "Very big!",
"number_of_comments": 2
}
]
}
Terms Aggregation
지정된 필드의 고유값을 기준으로 문서를 그룹화합니다. (i. g GROUP BY)
GET /products/_search
{
"size": 0,
"aggs": {
"products_by_category": {
"terms": {
"field": "category"
}
}
}
}
Range Aggregation
숫자 필드 값으로 범위를 지정하고 각 범위에 해당하는 버킷을 만드는 집계입니다. field 옵션에 해당 필드의 이름을 지정하고 ranges 옵션에 배열로 from, to 값을 가진 오브젝트 값을 나열해서 범위를 지정합니다.
GET /products/_search
{
"size": 0,
"aggs": {
"products_by_price_range": {
"range": {
"field": "price",
"ranges": [
{ "to": 200 },
{ "from": 200, "to": 600 },
{ "from": 600 }
]
}
}
}
}
Histogram Aggregation
interval 옵션을 이용해 주어진 간격 크기대로 버킷을 구분합니다. 숫자 타입 필드를 일정 간격으로 분류하는데 사용됩니다.
GET /products/_search
{
"size": 0,
"aggs": {
"price_histogram": {
"histogram": {
"field": "price",
"interval": 100
}
}
}
}
Date Histogram Aggregation
날짜/시간 타입 필드를 일정 날짜/시간 간격으로 분류합니다. 시계열 데이터를 분석할 때 유용합니다.
GET /products/_search
{
"size": 0,
"aggs": {
"sales_over_time": {
"date_histogram": {
"field": "publish_date",
"calendar_interval": "month"
}
}
}
}
Filter and Filters Aggregation
각 그룹에 포함시킬 문서의 조건을 직접 지정합니다. 검색에 사용되는 query와 동일하게 지정할 수 있으며, Filters Aggregation은 여러 필터를 한 번에 적용할 수 있습니다. 다수의 Filter Aggregation을 사용하는 것보다 Filters Aggregation을 사용하는 것이 성능상 더 효율적입니다.
GET /products/_search
{
"size": 0,
"aggs": {
"expensive_products": {
"filter": {
"range": {
"price": {
"gt": 200
}
}
}
}
}
}
GET /products/_search
{
"size": 0,
"aggs": {
"product_filters": {
"filters": {
"filters": {
"expensive": { "range": { "price": { "gt": 500 } } },
"affordable": { "range": { "price": { "lte": 500 } } }
}
}
}
}
}
Missing Aggregation
지정된 필드에 값이 없는(null 또는 undefined) 문서들을 그룹화합니다.
GET /products/_search
{
"size": 0,
"aggs": {
"missing_category": {
"missing": {
"field": "category"
}
}
}
}
Nested Aggregation
중첩된 객체 배열 내의 필드를 기준으로 집계를 수행합니다. 중첩된 객체 구조에서 데이터를 분석할 때 사용됩니다.
GET /products/_search
{
"size": 0,
"aggs": {
"comments": {
"nested": {
"path": "comments"
},
"aggs": {
"average_comments": {
"avg": {
"field": "comments.number_of_comments"
}
}
}
}
}
}
Global Aggregation
쿼리 결과와 상관없이 인덱스의 모든 문서를 대상으로 집계를 수행합니다. 필터링된 결과와 전체 데이터를 비교할 때 유용합니다.
GET /products/_search
{
"query": {
"term": { "category": "electronics" }
},
"aggs": {
"all_products": {
"global": {},
"aggs": {
"average_price": {
"avg": {
"field": "price"
}
}
}
},
"electronics_avg_price": {
"avg": {
"field": "price"
}
}
}
}
[참고자료]