# ElasticSearch Metric Aggregation

img


# ์‹ค์Šต ํ™˜๊ฒฝ

  • ๐Ÿ’ก 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) ๊ฐ•์˜๋ฅผ ์ฐธ๊ณ ํ•˜์—ฌ ์ž‘์„ฑ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.


Last Updated: 6/18/2023, 2:13:15 PM