# ElasticSearch Metric Aggregation
# ์ค์ต ํ๊ฒฝ
- ๐ก Elasticsearch 7.9.0
- ๐ก Windows 10
- ๐ก Git Bash
# Aggregation์ด๋?
๊ฐ๋จํ ์ค๋ช ํ๋ฉด ElasticSearch์ Document์ ์กฐํฉ์ ํตํด ๊ฐ์ ๋์ถํ ๋ ์ฐ์ด๋ ๋ฐฉ๋ฒ์ด๋ค.
๊ทธ ์ค Metric Aggregation์ ์ฐ์
์ ์ฌ์ฉ๋๋ค.
ํ๊ท , ์ต๋, ์ต์๊ฐ ๋ฑ์ ๊ตฌํ ๋ ์ ์ฉํ๋ค!
๋จผ์ sample ๋ฐ์ดํฐ๋ฅผ bulk
ํด๋ณด์.
sample ๋ฐ์ดํฐ ์์ค (opens new window)
{ "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) ์ผ๋ก ๋ฐ์ดํฐ ๋ถ์ (opens new window) ๊ฐ์๋ฅผ ์ฐธ๊ณ ํ์ฌ ์์ฑ๋์์ต๋๋ค.