Elasticsearch에서의 집계 기능과 활용 방법

집계 기능 개요

ES에서 데이터를 분석하기 위해 제공하는 집계 기능은 두 가지 주요 카테고리로 구분됩니다:

유형설명
메트릭(Metric)필터링된 데이터셋에 대해 평균, 최대값 등 단일 값 계산
버킷(Bucket)조건에 따라 데이터를 여러 그룹으로 나누어 각 그룹에 메트릭 적용

메트릭은 SQL의 AVG, MAX, MIN과 유사하며, 버킷은 GROUP BY와 비슷한 역할을 합니다.

데이터 준비

자동차 판매 데이터를 삽입합니다:

curl -XPOST http://localhost:9200/vehicles/sales/_bulk -d '
{ "index": {} }
{ "cost" : 10000, "color" : "red", "brand" : "honda", "transactionDate" : "2014-10-28" }
{ "index": {} }
{ "cost" : 20000, "color" : "red", "brand" : "honda", "transactionDate" : "2014-11-05" }
{ "index": {} }
{ "cost" : 30000, "color" : "green", "brand" : "ford", "transactionDate" : "2014-05-18" }
{ "index": {} }
{ "cost" : 15000, "color" : "blue", "brand" : "toyota", "transactionDate" : "2014-07-02" }
{ "index": {} }
{ "cost" : 12000, "color" : "green", "brand" : "toyota", "transactionDate" : "2014-08-19" }
{ "index": {} }
{ "cost" : 20000, "color" : "red", "brand" : "honda", "transactionDate" : "2014-11-05" }
{ "index": {} }
{ "cost" : 80000, "color" : "red", "brand" : "bmw", "transactionDate" : "2014-01-01" }
{ "index": {} }
{ "cost" : 25000, "color" : "blue", "brand" : "ford", "transactionDate" : "2014-02-12" }
'

버킷 활용 사례

1.1 시간 기반 집계(date_histogram)

거래 일자 필드를 월별로 분류:

curl -XGET 'http://localhost:9200/vehicles/sales/_search?pretty' -d '
{
  "aggs" : {
    "timeAggregation" : {
      "date_histogram" : {
        "field" : "transactionDate",
        "interval": "month"
      }
    }
  }
}
'

1.2 가격 구간 통계(histogram)

가격을 5000 단위로 구간화:

curl -XGET 'http://localhost:9200/vehicles/sales/_search?pretty' -d '
{
  "aggs" : {
    "priceDistribution" : {
      "histogram" : {
        "field" : "cost",
        "interval" : 5000
      }
    }
  }
}
'

1.3 색상별 판매량

색상 필드를 텍스트 타입으로 설정 후 집계:

curl -XPUT 'http://localhost:9200/vehicles/_mapping/sales' -d '
{
  "properties": {
    "color": {
      "type": "text",
      "fielddata": true
    }
  }
}
'

fielddata 옵션은 메모리에 로드하여 집계 가능하게 함

GET /vehicles/sales/_search?pretty
{
  "aggs" : {
    "colorAggregation" : {
      "terms" : { "field" : "color" }
    }
  }
}
'

메트릭 활용 사례

2.1 단일 값 집계

2.1.1 총 판매액 계산

curl -XGET 'http://localhost:9200/vehicles/sales/_search?pretty' -d '
{
  "aggs" : {
    "totalSales" : {
      "sum" : { "field" : "cost" }
    }
  }
}
'

2.1.2 최소 가격

curl -XGET 'http://localhost:9200/vehicles/sales/_search?pretty' -d '
{
  "aggs" : {
    "minPrice" : {
      "min" : { "field" : "cost" }
    }
  }
}
'

2.1.3 최대 가격

curl -XGET 'http://localhost:9200/vehicles/sales/_search?pretty' -d '
{
  "aggs" : {
    "maxPrice" : {
      "max" : { "field" : "cost" }
    }
  }
}
'

2.1.4 평균 가격

curl -XGET 'http://localhost:9200/vehicles/sales/_search?pretty' -d '
{
  "aggs" : {
    "avgPrice" : {
      "avg" : { "field" : "cost" }
    }
  }
}
'

다중 값 집계

3.1 통계 정보 수집(stats)

curl -XPOST "http://localhost:9200/students/class/_search?pretty" -d '
{
  "aggs": {
    "ageStatistics" : {
      "stats": { "field": "age" }
    }
  }
}
'

3.2 상위 결과 추출(top_hits)

나이 순으로 상위 3명 조회:

curl -XPOST "http://localhost:9200/students/class/_search?pretty" -d '
{
  "aggs": {
    "topAge" : {
      "top_hits": {
        "sort": [ { "age": { "order": "desc" } }],
        "_source": { "include": [ "name", "age" ] },
        "size": 3
      }
    }
  }
}
'

3.3 중첩 집계

클래스별 최대 나이 계산:

curl -XPOST "http://localhost:9200/students/class/_search?pretty" -d '
{
  "aggs": {
    "classAggregation" : {
      "terms": { "field": "classID" },
      "aggs": {
        "maxAge" : {
          "max": { "field": "age" }
        }
      }
    }
  }
}
'

태그: elasticsearch 집계 메트릭 버킷 통계

6월 29일 21:37에 게시됨