Elastic 가이드북
  • Elastic 가이드 북
  • 1. 서문
    • 1.1 Elastic Stack 소개
      • 1.1.1 Elasticsearch
      • 1.1.2 Logstash
      • 1.1.3 Kibana
      • 1.1.4 Beats
  • 2. Elasticsearch 시작하기
    • 2.1 데이터 색인
    • 2.2 설치 및 실행
      • 2.2.1 다운로드 설치 및 실행
      • 2.2.2 Unix RPM (yum) 설치 및 실행
      • 2.2.3 윈도우 운영체제에서 MSI 파일로 설치
    • 2.3 elasticsearch 환경 설정
      • 2.3.1 jvm.options
      • 2.3.2 elasticsearch.yml
      • 2.3.3 노드의 역할 : master, data, ingest, ml
      • 2.3.4 커맨드 라인 설정
  • 3. Elasticsearch 시스템 구조
    • 3.1 클러스터 구성
    • 3.2 인덱스와 샤드 - Index & Shards
    • 3.3 마스터 노드와 데이터 노드 - Master & Data Nodes
  • 4. Elasticsearch 데이터 처리
    • 4.1 REST API
    • 4.2 CRUD - 입력, 조회, 수정, 삭제
    • 4.3 벌크 API - _bulk API
    • 4.4 검색 API - _search API
  • 5. 검색과 쿼리 - Query DSL
    • 5.1 풀 텍스트 쿼리 - Full Text Query
    • 5.2 Bool 복합 쿼리 - Bool Query
    • 5.3 정확도 - Relevancy
    • 5.4 Bool : Should
    • 5.5 정확값 쿼리 - Exact Value Query
    • 5.6 범위 쿼리 - Range Query
  • 6. 데이터 색인과 텍스트 분석
    • 6.1 역 인덱스 - Inverted Index
    • 6.2 텍스트 분석 - Text Analysis
    • 6.3 애널라이저 - Analyzer
      • 6.3.1 _analyze API
      • 6.3.2 Term 쿼리
      • 6.3.3 사용자 정의 애널라이저 - Custom Analyzer
      • 6.3.4 텀 벡터 - _termvectors API
    • 6.4 캐릭터 필터 - Character Filter
      • 6.4.1 HTML Strip
      • 6.4.2 Mapping
      • 6.4.3 Pattern Replace
    • 6.5 토크나이저 - Tokenizer
      • 6.5.1 Standard, Letter, Whitespace
      • 6.5.2 UAX URL Email
      • 6.5.3 Pattern
      • 6.5.4 Path Hierarchy
    • 6.6 토큰 필터 - Token Filter
      • 6.6.1 Lowercase, Uppercase
      • 6.6.2 Stop
      • 6.6.3 Synonym
      • 6.6.4 NGram, Edge NGram, Shingle
      • 6.6.5 Unique
    • 6.7 형태소 분석 - Stemming
      • 6.7.1 Snowball
      • 6.7.2 노리 (nori) 한글 형태소 분석기
  • 7. 인덱스 설정과 매핑 - Settings & Mappings
    • 7.1 설정 - Settings
    • 7.2 매핑 - Mappings
      • 7.2.1 문자열 - text, keyword
      • 7.2.2 숫자 - long, double ...
      • 7.2.3 날짜 - date
      • 7.2.4 불리언 - boolean
      • 7.2.5 Object 와 Nested
      • 7.2.6 위치 정보 - Geo
      • 7.2.7 기타 필드 타입 - IP, Range, Binary
    • 7.3 멀티 (다중) 필드 - Multi Field
  • 8. 집계 - Aggregations
    • 8.1 메트릭 - Metrics Aggregations
    • 8.2 버킷 - Bucket Aggregations
    • 8.3 하위 - sub-aggregations
    • 8.4 파이프라인 - Pipeline Aggregations
Powered by GitBook
On this page

Was this helpful?

  1. 8. 집계 - Aggregations

8.3 하위 - sub-aggregations

이 문서의 허가되지 않은 무단 복제나 배포 및 출판을 금지합니다. 본 문서의 내용 및 도표 등을 인용하고자 하는 경우 출처를 명시하고 김종민(kimjmin@gmail.com)에게 사용 내용을 알려주시기 바랍니다.

Bucket Aggregation 으로 만든 버킷들 내부에 다시 "aggs" : { } 를 선언해서 또다른 버킷을 만들거나 Metrics Aggregation 을 만들어 사용이 가능합니다. 다음은 terms aggregation을 이용해서 생성한 stations 버킷 별로 avg aggregation을 이용해서 passangers 필드의 평균값을 계산하는 avg_psg_per_st 을 생성하는 예제입니다.

terms aggs 아래에 avg aggs 사용
GET my_stations/_search
{
  "size": 0,
  "aggs": {
    "stations": {
      "terms": {
        "field": "station.keyword"
      },
      "aggs": {
        "avg_psg_per_st": {
          "avg": {
            "field": "passangers"
          }
        }
      }
    }
  }
}
terms aggs 아래에 avg aggs 사용 결과
{
  "took" : 3,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 10,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "aggregations" : {
    "stations" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : "강남",
          "doc_count" : 5,
          "avg_psg_per_st" : {
            "value" : 5931.2
          }
        },
        {
          "key" : "불광",
          "doc_count" : 1,
          "avg_psg_per_st" : {
            "value" : 971.0
          }
        },
        {
          "key" : "신촌",
          "doc_count" : 1,
          "avg_psg_per_st" : {
            "value" : 3912.0
          }
        },
        {
          "key" : "양재",
          "doc_count" : 1,
          "avg_psg_per_st" : {
            "value" : 4121.0
          }
        },
        {
          "key" : "종각",
          "doc_count" : 1,
          "avg_psg_per_st" : {
            "value" : 2314.0
          }
        },
        {
          "key" : "홍제",
          "doc_count" : 1,
          "avg_psg_per_st" : {
            "value" : 1021.0
          }
        }
      ]
    }
  }
}

stations 버킷들 별로 avg_psg_per_st 라는 aggregation이 실행된 것을 확인할 수 있습니다. 버킷 안에 또 다른 하위 버킷을 만드는 것도 가능합니다. 다음은 terms aggregation 을 이용해서 line.keyword 별로 lines 버킷을 만들고 그 안에 또다시 terms aggregation을 이용한 stations_per_lines 버킷을 만든 예제입니다.

terms aggs 아래에 하위 terms aggs 사용
GET my_stations/_search
{
  "size": 0,
  "aggs": {
    "lines": {
      "terms": {
        "field": "line.keyword"
      },
      "aggs": {
        "stations_per_lines": {
          "terms": {
            "field": "station.keyword"
          }
        }
      }
    }
  }
}
terms aggs 아래에 하위 terms aggs 사용 결과
{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 10,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "aggregations" : {
    "lines" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : "2호선",
          "doc_count" : 6,
          "stations_per_lines" : {
            "doc_count_error_upper_bound" : 0,
            "sum_other_doc_count" : 0,
            "buckets" : [
              {
                "key" : "강남",
                "doc_count" : 5
              },
              {
                "key" : "신촌",
                "doc_count" : 1
              }
            ]
          }
        },
        {
          "key" : "3호선",
          "doc_count" : 3,
          "stations_per_lines" : {
            "doc_count_error_upper_bound" : 0,
            "sum_other_doc_count" : 0,
            "buckets" : [
              {
                "key" : "불광",
                "doc_count" : 1
              },
              {
                "key" : "양재",
                "doc_count" : 1
              },
              {
                "key" : "홍제",
                "doc_count" : 1
              }
            ]
          }
        },
        {
          "key" : "1호선",
          "doc_count" : 1,
          "stations_per_lines" : {
            "doc_count_error_upper_bound" : 0,
            "sum_other_doc_count" : 0,
            "buckets" : [
              {
                "key" : "종각",
                "doc_count" : 1
              }
            ]
          }
        }
      ]
    }
  }
}

각 호선별로 역 버킷이 생성된 것을 확인할 수 있습니다. 두 번째 하위 aggregations 안에 또 다른 metrics aggregations를 입력하는 것도 가능합니다.

하위 버킷이 깊어질수록 elasticsearch 가 하는 작업량과 메모리 소모량이 기하급수적으로 늘어나기 때문에 예상치 못한 오류를 발생 시킬수도 있습니다. 보통은 2레벨의 깊이 이상의 버킷은 생성하지 않는 것이 좋습니다.

Previous8.2 버킷 - Bucket AggregationsNext8.4 파이프라인 - Pipeline Aggregations

Last updated 5 years ago

Was this helpful?