Aggregation은 사전적인 의미로 "집계"라는 말을 뜻하지만 Elasticsearch에서는 Elasticsearch안의 데이터 중에서 어떠한 특정한 값을 도출해 낼 때 사용한다. Aggregation은 사용 형태에 따라 Metric, Bucket Aggregation으로 나뉩니다.
1. Metric Aggregation
- 산술적인 연산을 통한 특수한 값을 도출할 때 사용(ex. 최대값, 최소값) - 데이터 집계를 위한 Json 파일 생성
ex) stat 데이터 출력 # vim score_agg_test.json . . . { # 결과 값만 출력 "size" : 0, # aggregation 선언 "aggs" : { # 이름 "stats_score" : { # aggregation 연산 값 # stats: 전체 출력 / max, min, avg 등... 사용 가능 "stats" : { # 필드 선택 "field" : "points" } } } } . . . : wq
sum, avg 결과 값
stats 결과 값 - 데이터 조회 #curl -H 'Content-Type:application/json' -X GET localhost:9200/_search?pretty --data-binary @score_agg_test .json
2. Bucket Aggregation
- RDB Group by와 비슷한 기능 ex) 팀 별데이터 분석 # vim stats_by_team.json . . . { "size" : 0, "aggs" : { "team_stats" : { "terms" : { "field" : "team" }, "aggs" : { "stats_score" : { "stats" : { "field" : "points" } } } } } } . . . :wq
- 결과값 확인 # curl -H 'Content-Type: application/json' -X GET 'localhost:9200/basketball/_search?pretty' --data-bianry @stats_by_team.json
** 오류 발생 ** {"error":{"root_cause":[{"type":"illegal_argument_exception","reason":"The bulk request must be terminated by a newline [\\n]"}]