Elasticsearch 살펴보기 4탄 - 집계

bradley·2022년 6월 14일
1

ELK

목록 보기
6/8

Elasticsearch에서 집계하는 포맷은 다음과 같다.

"aggregations" : {
   "" : {
      "" : {

      }
 
      [,"meta" : { [] } ]?
      [,"aggregations" : { []+ } ]?
   }
   [,"" : { ... } ]*
}

Metrics 집계

Metrics Aggregations는 집계된 documents의 field 값으로부터 metric을 계산한다. 때때로 값은 script에 의해 생성되기도 한다.
Numeric Metrics는 평균같은 단일값이거나 통계같은 다중값이다.

평균(Avg) 집계

집계된 document에서 Numeric field의 평균을 구한다.

Command Example
school index에서 각 document들의 fees field에 대한 평균 값을 구한다.

POST /school/_search
{
   "aggs":{
      "avg_fees":{"avg":{"field":"fees"}}
   }
}

정상 출력
집계된 2개의 document의 fees field를 보면 값이 5000, 2500이 있고, 따라서 평균값 3750.0이 출력됐다.

{
  "took" : 3,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "school",
        "_type" : "_doc",
        "_id" : "10",
        "_score" : 1.0,
        "_source" : {
          "name" : "Saint Paul School",
          "description" : "ICSE Afiliation",
          "street" : "Dawarka",
          "city" : "Delhi",
          "state" : "Delhi",
          "zip" : "110075",
          "location" : [
            28.5733056,
            77.0122136
          ],
          "fees" : 5000,
          "tags" : [
            "Good Faculty",
            "Great Sports"
          ],
          "rating" : "4.5"
        }
      },
      {
        "_index" : "school",
        "_type" : "_doc",
        "_id" : "16",
        "_score" : 1.0,
        "_source" : {
          "name" : "Crescent School",
          "description" : "State Board Affiliation",
          "street" : "Tonk Road",
          "city" : "Jaipur",
          "state" : "RJ",
          "zip" : "176114",
          "location" : [
            26.8535922,
            75.7923988
          ],
          "fees" : 2500,
          "tags" : [
            "Well equipped labs"
          ],
          "rating" : "4.5"
        }
      }
    ]
  },
  "aggregations" : {
    "avg_fees" : {
      "value" : 3750.0
    }
  }
}

Cardinality 집계

특정 field의 distinct한 count를 구한다.

Command Example

POST /school/_search?size=0
{
  "aggs": {
    "distinct_name_count": {
      "cardinality": {
        "field": "fees"
      }
    }
  }
}

정상 출력

{
  "took" : 7,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "aggregations" : {
    "distinct_name_count" : {
      "value" : 2
    }
  }
}

Sum 집계

집계된 document에서 Numeric field의 합계를 계산한다.

Command Example

POST /school/_search?size=0
{
  "aggs": {
    "total_fees": {
      "sum": {
        "field": "fees"
      }
    }
  }
}

정상 출력

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "aggregations" : {
    "total_fees" : {
      "value" : 7500.0
    }
  }
}

Min 집계

집계된 document에서 Numeric field의 최소값을 구한다.

Command Example

POST /school/_search?size=0
{
  "aggs": {
    "min_fees": {
      "min": {
        "field": "fees"
      }
    }
  }
}

정상 출력

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "aggregations" : {
    "min_fees" : {
      "value" : 2500.0
    }
  }
}

Max 집계

집계된 document의 Numeric field의 최대값을 구한다.

Command Example

POST /school/_search?size=0
{
  "aggs": {
    "max_fees": {
      "max": {
        "field": "fees"
      }
    }
  }
}

정상 출력

{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "aggregations" : {
    "max_fees" : {
      "value" : 5000.0
    }
  }
}

통계(Stats) 집계

집계된 document에서 추출된 Numeric 값의 통계치를 계산하여 여러 개의 집계로 표시한다.

Command Example

POST /school/_search?size=0
{
  "aggs": {
    "grades_fees": {
      "stats": {
        "field": "fees"
      }
    }
  }
}

정상 출력

{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "aggregations" : {
    "grades_fees" : {
      "count" : 2,
      "min" : 2500.0,
      "max" : 5000.0,
      "avg" : 3750.0,
      "sum" : 7500.0
    }
  }
}

확장된 통계(Extended Stats) 집계

집계된 document에서 Numerical field에 대한 모든 통계값을 생성한다.

Command Example

POST /school/_search?size=0
{
 "aggs": {
   "fees_stats": {
     "extended_stats": {
       "field": "fees"
     }
   }
 }
}

정상 출력

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "aggregations" : {
    "fees_stats" : {
      "count" : 2,
      "min" : 2500.0,
      "max" : 5000.0,
      "avg" : 3750.0,
      "sum" : 7500.0,
      "sum_of_squares" : 3.125E7,
      "variance" : 1562500.0,
      "std_deviation" : 1250.0,
      "std_deviation_bounds" : {
        "upper" : 6250.0,
        "lower" : 1250.0
      }
    }
  }
}

집계 Metadata

meta tag를 이용해 request 시 집계에 대한 추가 데이터를 추가할 수 있다. 그리고 response 시 이 데이터를 얻을 수 있다.

Command Example

POST /school/_search?size=0
{
  "aggs": {
    "avg_fees": {
      "avg": {
        "field": "fees"
      },
      "meta": {
        "dsc" : "Lowest Fees This Year"
      }
    }
  }
}

정상 출력

{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "aggregations" : {
    "avg_fees" : {
      "meta" : {
        "dsc" : "Lowest Fees This Year"
      },
      "value" : 3750.0
    }
  }
}
profile
데이터 엔지니어링에 관심이 많은 홀로 삽질하는 느림보

0개의 댓글