Elasticsearch: Bucket Aggregations (버킷 집계) 완벽 정리
Bucket Aggregations은 특정 기준으로 데이터를 그룹화하여 분석하는 강력한 기능을 제공합니다.
✔ 숫자, 날짜, 문자열 데이터를 기반으로 그룹을 나눌 수 있음
✔ 각 그룹(버킷)별로 포함된 문서 수(doc_count)를 계산
✔ 각 버킷에 추가적으로 Metrics Aggregation을 적용하여 통계 분석 가능
이번 글에서는 Bucket Aggregations 개념, 주요 기능, 실전 예제까지 상세히 알아보겠습니다.
1. Bucket Aggregations 개요
개념 | 데이터를 특정 기준에 따라 그룹화 | |
사용 목적 | 데이터 분석 및 요약 정보 제공 | |
주요 유형 | range, histogram, date_range, date_histogram, terms |
2. 데이터 예제 (지하철 승객 수 데이터)
Bucket Aggregations을 테스트할 데이터를 준비합니다.
📌 예제 1: my_stations 인덱스 생성 및 데이터 입력
PUT my_stations/_bulk
{"index": {"_id": "1"}}
{"date": "2019-06-01", "line": "1호선", "station": "종각", "passangers": 2314}
{"index": {"_id": "2"}}
{"date": "2019-06-01", "line": "2호선", "station": "강남", "passangers": 5412}
{"index": {"_id": "3"}}
{"date": "2019-07-10", "line": "2호선", "station": "강남", "passangers": 6221}
{"index": {"_id": "4"}}
{"date": "2019-07-15", "line": "2호선", "station": "강남", "passangers": 6478}
{"index": {"_id": "5"}}
{"date": "2019-08-07", "line": "2호선", "station": "강남", "passangers": 5821}
{"index": {"_id": "6"}}
{"date": "2019-08-18", "line": "2호선", "station": "강남", "passangers": 5724}
{"index": {"_id": "7"}}
{"date": "2019-09-02", "line": "2호선", "station": "신촌", "passangers": 3912}
{"index": {"_id": "8"}}
{"date": "2019-09-11", "line": "3호선", "station": "양재", "passangers": 4121}
{"index": {"_id": "9"}}
{"date": "2019-09-20", "line": "3호선", "station": "홍제", "passangers": 1021}
{"index": {"_id": "10"}}
{"date": "2019-10-01", "line": "3호선", "station": "불광", "passangers": 971}
✅ 지하철 승객 수 데이터를 포함한 샘플 데이터 생성
3. Range Aggregation (범위별 버킷)
✔ 숫자 필드 값을 기준으로 특정 범위를 지정하여 그룹화
✔ from(이상), to(미만)으로 범위를 정의
📌 예제 2: 승객 수(passangers)를 기준으로 그룹 나누기
GET my_stations/_search
{
"size": 0,
"aggs": {
"passangers_range": {
"range": {
"field": "passangers",
"ranges": [
{ "to": 1000 },
{ "from": 1000, "to": 4000 },
{ "from": 4000 }
]
}
}
}
}
✅ 1000명 미만, 1000~4000명, 4000명 이상으로 그룹화
4. Histogram Aggregation (구간별 버킷)
✔ 특정 간격(interval)으로 숫자 필드를 그룹화
✔ range와 달리 interval 값을 설정하여 자동 분할
📌 예제 3: 승객 수를 2000명 단위로 그룹화
GET my_stations/_search
{
"size": 0,
"aggs": {
"passangers_his": {
"histogram": {
"field": "passangers",
"interval": 2000
}
}
}
}
✅ 2000 단위로 그룹을 생성하여 데이터 분포 확인
5. Date Histogram Aggregation (날짜별 버킷)
✔ 날짜 필드를 일정한 기간 간격으로 그룹화
✔ interval 대신 fixed_interval 또는 calendar_interval 사용
📌 예제 4: 한 달 단위로 날짜 필드(date) 그룹화
GET my_stations/_search
{
"size": 0,
"aggs": {
"date_his": {
"date_histogram": {
"field": "date",
"calendar_interval": "month"
}
}
}
}
✅ 1개월 단위로 데이터 그룹화하여 시계열 분석 가능
6. Terms Aggregation (카테고리별 버킷)
✔ keyword 필드를 기준으로 데이터를 그룹화
✔ 가장 많이 등장하는 값 순으로 정렬됨
✔ 데이터 분석 시 특정 필드의 "빈도수"를 파악할 때 유용
📌 예제 5: 지하철 역(station) 기준으로 그룹화
GET my_stations/_search
{
"size": 0,
"aggs": {
"stations": {
"terms": {
"field": "station.keyword"
}
}
}
}
✅ 각 역(station)의 승객 수를 집계하여 가장 많이 등장하는 역을 파악 가능
📌 예제 6: 지하철 노선(line)별 그룹화 및 승객 수 평균 계산
GET my_stations/_search
{
"size": 0,
"aggs": {
"lines": {
"terms": {
"field": "line.keyword"
},
"aggs": {
"avg_passangers": {
"avg": { "field": "passangers" }
}
}
}
}
}
✅ 노선별 평균 승객 수를 분석하는 예제
7. 정리
숫자 범위 | range, histogram | 특정 범위 또는 간격(interval)으로 그룹화 |
날짜 범위 | date_range, date_histogram | 특정 기간 간격으로 그룹화 |
카테고리별 그룹화 | terms | keyword 필드 기준으로 그룹화 |
8. 마무리
✔ Bucket Aggregations을 사용하면 데이터를 원하는 기준으로 그룹화 가능
✔ 각 그룹별 개수(doc_count)와 추가 Metrics Aggregations을 조합하여 통계 분석 가능
✔ 시계열 데이터 분석, 숫자 범위 분석, 문자열(카테고리) 분석에 매우 유용
다음 학습에서는 고급 Aggregations 및 Nested Aggregations 활용법을 다루겠습니다! 🚀
'Elastic Search > 집계' 카테고리의 다른 글
[Elasticsearch] Pipeline Aggregations (파이프라인 집계) 완벽 정리 (0) | 2025.02.03 |
---|---|
[Elasticsearch] Sub-Aggregations (하위 집계) 완벽 정리 (0) | 2025.02.03 |
[Elasticsearch] Metrics Aggregations (메트릭 집계) 완벽 정리 (0) | 2025.02.03 |
[Elasticsearch] 집계(Aggregations) 완벽 정리 (0) | 2025.02.03 |