[ElasticSearch] Metric Aggregation
by youngjun._.
실습 환경
- 💡 Elasticsearch 7.9.0
- 💡 Windows 10
- 💡 Git Bash
Aggregation이란?
간단히 설명하면 ElasticSearch안 Document의 조합을 통해 값을 도출할 때 쓰이는 방법이다.
그 중 Metric Aggregation은 산술
에 사용된다.
평균, 최대, 최소값 등을 구할 때 유용하다!
먼저 sample 데이터를 bulk
해보자.
{ "index" : { "_index" : "basketball", "_type" : "record", "_id" : "1" } } {"team" : "Chicago Bulls","name" : "Michael Jordan", "points" : 30,"rebounds" : 3,"assists" : 4, "submit_date" : "1996-10-11"} { "index" : { "_index" : "basketball", "_type" : "record", "_id" : "2" } } {"team" : "Chicago Bulls","name" : "Michael Jordan","points" : 20,"rebounds" : 5,"assists" : 8, "submit_date" : "1996-10-11"}
20점의 point를 기록한 것과 30점의 point를 기록한 것만 기억하면 된다.
1. Index에 데이터 Bulk하기
위에서 확인한 sample 데이터를 삽입하면
curl -XPOST "/ES주소/"_bulk --data-binary @"file명".json
$ curl -XPOST 'http://localhost:9200/_bulk?pretty' --data-binary @simple_basketball.json -H 'Content-Type: application/json'
결과
{ "took" : 423, "errors" : false, "items" : [ { "index" : { "_index" : "basketball", "_type" : "record", "_id" : "1", "_version" : 1, "result" : "created", "_shards" : { "total" : 2, "successful" : 1, "failed" : 0 }, "_seq_no" : 0, "_primary_term" : 1, "status" : 201 } }, { "index" : { "_index" : "basketball", "_type" : "record", "_id" : "2", "_version" : 1, "result" : "created", "_shards" : { "total" : 2, "successful" : 1, "failed" : 0 }, "_seq_no" : 1, "_primary_term" : 1, "status" : 201 } } ] }
잘 bulk 됐다!
2. ✔ AVG Aggregation
평균을 구하는 Aggregation을 먼저 실습해보자.
avg_points_aggs.json
파일이다.
{ "size" : 0, "aggs" : { "avg_score" : { "avg" : { "field" : "points" } } } }
size 0
= 결과 값에 보고싶은 값만 출력
aggs
= aggregation의 Keyword
avg
= 평균 값을 구하는 aggregation을 사용
field 중 points의 평균 값을 구하는 코드이다.
curl
커멘드로 돌려서 확인해보자.
$ curl -XGET 'http://localhost:9200//_search?pretty' --data-binary @avg_points_aggs.json -H 'Content-Type: application/json'
결과
{ "took" : 12, "timed_out" : false, "_shards" : { "total" : 2, "successful" : 2, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 26, "relation" : "eq" }, "max_score" : null, "hits" : [ ] }, "aggregations" : { "avg_score" : { "value" : 25.0 } } }
point 값인 20과 30의 평균인 value
가 25.0으로 잘 출력된 것을 확인할 수 있다.
3. ✔ Max/Min Aggregation
최대값을 구하는 Aggregation도 실습해보자.
max_points_aggs.json
파일이다.
{ "size" : 0, "aggs" : { "max_score" : { "max" : { "field" : "points" } } } }
위에서 실습한 avg
와 같은 형식으로 작성되었고
avg
가 max
로 바뀌었다.
curl
커멘드로 돌려서 확인해보자.
$ curl -XGET 'http://localhost:9200//_search?pretty' --data-binary @max_points_aggs.json -H 'Content-Type: application/json'
결과
{ "took" : 1, "timed_out" : false, "_shards" : { "total" : 2, "successful" : 2, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 26, "relation" : "eq" }, "max_score" : null, "hits" : [ ] }, "aggregations" : { "max_score" : { "value" : 30.0 } } }
최대값인 30이 출력되는 것을 확인할 수 있다.
최소값도 마찬가지이다.
min_points_aggs.json
파일을 보면
{ "size" : 0, "aggs" : { "min_score" : { "min" : { "field" : "points" } } } }
max
에서 min
으로 변경해주고 똑같은 실습을 진행하면
최소값인 20이 출력된다.
4. ✔ Sum Aggregation
합을 구하는 Aggregation도 실습해보자.
sum_points_aggs.json
파일이다.
{ "size" : 0, "aggs" : { "sum_score" : { "sum" : { "field" : "points" } } } }
똑같은 형식이며 sum
keyword만 입력해줬다.
curl
커멘드로 돌려서 확인해보자.
$ curl -XGET 'http://localhost:9200//_search?pretty' --data-binary @sum_points_aggs.json -H 'Content-Type: application/json'
결과
{ "took" : 1, "timed_out" : false, "_shards" : { "total" : 2, "successful" : 2, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 26, "relation" : "eq" }, "max_score" : null, "hits" : [ ] }, "aggregations" : { "sum_score" : { "value" : 50.0 } } }
20과 30의 합인 50
이 출력된다.
5. ✔ STATS Aggregation 한번에 확인하기
위에서 실습한 내용을 한번에 볼 수 있는 명령도 있다.
stats_points_aggs.json
파일이다.
{ "size" : 0, "aggs" : { "stats_score" : { "stats" : { "field" : "points" } } } }
curl
커멘드로 돌려서 확인해보자.
$ curl -XGET 'http://localhost:9200//_search?pretty' --data-binary @stats_points_aggs.json -H 'Content-Type: application/json'
결과
# ... 앞 생략 "aggregations" : { "stats_score" : { "count" : 2, "min" : 20.0, "max" : 30.0, "avg" : 25.0, "sum" : 50.0 } } # ... 뒤 생략
위 결과와 같이 min
, max
, avg
, sum
모두 출력되는 것을 확인할 수 있다.
본 포스팅은
Inflearn
의 ELK 스택 (ElasticSearch, Logstash, Kibana) 으로 데이터 분석 강의를 참고하여 작성되었습니다.
블로그의 정보
개발하는만두
youngjun._.