Range Aggregations (범위 집계)

범위 집계는 사용자가 지정한 범위 내에서 집계를 수행하는 다중 버킷 집계다. 각 문서에서 추출된 값이 각 버킷 범위에 해당하는지 확인하고 범위에 해당되는 문서들은 버킷에 집계된다.

from, to 필드로 범위를 지정하며 from은 포함되고 to는 제외된다. 즉, from <= x < to 의 관계다.

GET /apache-web-log/_search?size=0
{
  "aggs": {
    "byte_ranges": {
      "range": {
        "field": "bytes",
        "ranges": [
          {
            "from": 1000,
            "to": 2000
          },
          {
            "from": 2000,
            "to": 3000
          }
        ]
      }
    }
  }
}

bytes 필드의 값이 1000 ~ 2000의 문서의 개수가 754개, 2000 ~ 3000의 문서의 개수가 81개다.

{
  ...
  "aggregations" : {
    "byte_ranges" : {
      "buckets" : [
        {
          "key" : "1000.0-2000.0",
          "from" : 1000.0,
          "to" : 2000.0,
          "doc_count" : 754
        },
        {
          "key" : "2000.0-3000.0",
          "from" : 2000.0,
          "to" : 3000.0,
          "doc_count" : 81
        }
      ]
    }
  }
}

key 필드를 이용하여 별칭을 줄수도 있다.

GET /apache-web-log/_search?size=0
{
  "aggs": {
    "byte_ranges": {
      "range": {
        "field": "bytes",
        "ranges": [
          {
            "key": "small",
            "from": 1000,
            "to": 2000
          },
          {
            "key": "medium",
            "from": 2000,
            "to": 3000
          },
          {
            "key": "large",
            "from": 2000,
            "to": 3000
          }
        ]
      }
    }
  }
}
# 결과
{
  ...
  "aggregations" : {
    "byte_ranges" : {
      "buckets" : [
        {
          "key" : "small",
          "from" : 1000.0,
          "to" : 2000.0,
          "doc_count" : 754
        },
        {
          "key" : "medium",
          "from" : 2000.0,
          "to" : 3000.0,
          "doc_count" : 81
        },
        {
          "key" : "large",
          "from" : 2000.0,
          "to" : 3000.0,
          "doc_count" : 81
        }
      ]
    }
  }
}

Date Range Aggregations (날짜 범위 집계)

날짜 범위 집계는 범위 집계의 날짜 버전이다.

GET /apache-web-log/_search?size=0
{
  "aggs": {
    "request count group by date": {
      "date_range": {
        "field": "timestamp",
        "ranges": [
          {
            "from": "2015-05-17T10:00:00",
            "to": "2015-05-17T12:00:00"
          }
        ]
      }
    }
  }
}
# 결과
{
  // ...
  "aggregations" : {
    "request count group by date" : {
      "buckets" : [
        {
          "key" : "2015-05-17T10:00:00.000Z-2015-05-17T12:00:00.000Z",
          "from" : 1.4318568E12,
          "from_as_string" : "2015-05-17T10:00:00.000Z",
          "to" : 1.431864E12,
          "to_as_string" : "2015-05-17T12:00:00.000Z",
          "doc_count" : 185
        }
      ]
    }
  }
}