Geohash grid 집계

Geohash grid 집계

geohash_grid 집계는 문서를 geohash 값을 기반으로 그리드 셀로 그룹화해요. 각 셀은 geohash 문자열로 레이블이 지정되며, precision 파라미터가 셀 크기를 제어해요. 낮은 precision 값은 더 적고 더 큰 셀을, 높은 값은 더 많고 더 작은 셀을 생성해요. geo_point 또는 geo_shape 필드에 대해 문서를 집계할 수 있어요. geo_point는 정확히 하나의 셀에 배치되는 반면, geo_shape는 교차하는 모든 셀에 집계돼요.

precision 값의 범위는 1부터 12까지예요. 높은 precision 요청은 많은 버킷을 생성하므로 상당한 메모리를 소비하고 큰 응답을 만들 수 있어요. 높은 precision 값을 사용하기 전에 작은 지리적 영역으로 필터링하세요.

출처: 문서

본문

파라미터 (Parameters)

geohash_grid 집계는 다음 파라미터를 받아요.

파라미터 필수/선택 데이터 타입 설명
field 필수 String 집계할 필드. geo_point 또는 geo_shape로 매핑되어야 해요.
precision 선택 Integer 또는 String 셀 크기를 제어하는 geohash 길이. 유효한 정수 값은 1–12예요. 근사 거리(예: 1km 또는 10m)를 지정할 수도 있으며, OpenSearch는 셀이 그 크기를 초과하지 않는 precision 수준을 선택해요. 기본값은 5예요.
bounds 선택 Object 고려할 포인트를 제한하는 경계 상자. 상자 안의 포인트만 집계돼요. 모든 geo_point 형식을 받아요.
size 선택 Integer 반환할 버킷의 최대 수. 버킷이 size보다 많으면 문서 수가 가장 많은 버킷이 반환돼요. 기본값은 10000이에요.
shard_size 선택 Integer 각 샤드에서 반환되는 버킷의 최대 수. 기본값은 max(10, size × number of shards)예요.

예제: 저정밀도 그리드 (Low-precision grid)

다음 예제는 전자상거래 고객 위치를 precision 4(약 39 km × 19.5 km)의 geohash 셀로 그룹화해요:

GET /opensearch_dashboards_sample_data_ecommerce/_search
{
  "size": 0,
  "aggs": {
    "geo_hash": {
      "geohash_grid": {
        "field": "geoip.location",
        "precision": 4
      }
    }
  }
}

예제: 경계 상자 필터가 있는 고정밀도 그리드 (High-precision grid with bounding box filter)

특정 지역으로 확대할 때는 높은 precision을 요청하기 전에 먼저 문서를 해당 지역으로 필터링하세요. 다음 예제는 geo_bounding_box 쿼리를 사용해 뉴욕 시 지역으로 좁힌 다음 precision 7(약 153 m × 152 m)로 집계해요:

GET /opensearch_dashboards_sample_data_ecommerce/_search
{
  "size": 0,
  "aggs": {
    "zoomed_in": {
      "filter": {
        "geo_bounding_box": {
          "geoip.location": {
            "top_left": "41.0, -74.5",
            "bottom_right": "40.5, -73.5"
          }
        }
      },
      "aggs": {
        "detailed_grid": {
          "geohash_grid": {
            "field": "geoip.location",
            "precision": 7
          }
        }
      }
    }
  }
}

예제 응답 (Example response)

다음 응답은 고정밀도 예제에 해당해요:

{
  "took": 23,
  "timed_out": false,
  "terminated_early": true,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 4675,
      "relation": "eq"
    },
    "max_score": null,
    "hits": []
  },
  "aggregations": {
    "zoomed_in": {
      "doc_count": 896,
      "detailed_grid": {
        "buckets": [
          {
            "key": "dr72h56",
            "doc_count": 747
          },
          {
            "key": "dr5rs14",
            "doc_count": 149
          }
        ]
      }
    }
  }
}

응답 본문 필드 (Response body fields)

필드 데이터 타입 설명
buckets Array doc_count 내림차순으로 정렬된 그리드 셀 버킷.
buckets.key String 셀을 식별하는 geohash 문자열.
buckets.doc_count Integer 이 셀의 문서(또는 geoshape 교차) 수.

반환된 geohash 키를 geo_bounding_box 쿼리의 top_left와 bottom_right 모두로 사용하면 더 높은 precision으로 해당 셀을 확대할 수 있어요. 클라이언트 측 geohash 라이브러리(예: JavaScript용 ngeohash)는 버킷 키를 lat/lon 경계 상자로 디코딩해 지도 렌더링에 사용할 수 있어요.

Geoshape 집계 (Aggregating geoshapes)

geoshape 필드에 대해 집계를 실행하려면 먼저 인덱스를 만들고 location 필드를 geo_shape로 매핑해요:

PUT /national_parks
{
  "mappings": {
    "properties": {
      "location": {
        "type": "geo_shape"
      }
    }
  }
}

다음으로, national_parks 인덱스에 몇몇 문서를 색인해요:

PUT /national_parks/_doc/1
{
  "name": "Yellowstone National Park",
  "location":
  {"type": "envelope","coordinates": [ [-111.15, 45.12], [-109.83, 44.12] ]}
}
PUT /national_parks/_doc/2
{
  "name": "Yosemite National Park",
  "location": 
  {"type": "envelope","coordinates": [ [-120.23, 38.16], [-119.05, 37.45] ]}
}
PUT /national_parks/_doc/3
{
  "name": "Death Valley National Park",
  "location": 
  {"type": "envelope","coordinates": [ [-117.34, 37.01], [-116.38, 36.25] ]}
}

location 필드에 대해 다음과 같이 집계를 실행할 수 있어요:

GET /national_parks/_search
{
  "aggregations": {
    "grouped": {
      "geohash_grid": {
        "field": "location",
        "precision": 1
      }
    }
  }
}

geoshape은 둘 이상의 그리드 셀에 걸쳐 있을 때 여러 버킷에 나타날 수 있어요:

{
  "took" : 24,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "national_parks",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "name" : "Yellowstone National Park",
          "location" : {
            "type" : "envelope",
            "coordinates" : [
              [
                -111.15,
                45.12
              ],
              [
                -109.83,
                44.12
              ]
            ]
          }
        }
      },
      {
        "_index" : "national_parks",
        "_id" : "2",
        "_score" : 1.0,
        "_source" : {
          "name" : "Yosemite National Park",
          "location" : {
            "type" : "envelope",
            "coordinates" : [
              [
                -120.23,
                38.16
              ],
              [
                -119.05,
                37.45
              ]
            ]
          }
        }
      },
      {
        "_index" : "national_parks",
        "_id" : "3",
        "_score" : 1.0,
        "_source" : {
          "name" : "Death Valley National Park",
          "location" : {
            "type" : "envelope",
            "coordinates" : [
              [
                -117.34,
                37.01
              ],
              [
                -116.38,
                36.25
              ]
            ]
          }
        }
      }
    ]
  },
  "aggregations" : {
    "grouped" : {
      "buckets" : [
        {
          "key" : "9",
          "doc_count" : 3
        },
        {
          "key" : "c",
          "doc_count" : 1
        }
      ]
    }
  }
}

OpenSearch는 API를 통한 geoshape 집계는 지원하지만 OpenSearch Dashboards 시각화에서는 지원하지 않아요.

Geohash 정밀도 (Geohash precision)

다음 표는 각 precision 수준에서의 근사 셀 크기를 나열해요. 셀 크기는 위도에 따라 달라지며, 표시된 값은 적도에서 가장 넓은 경우를 나타내요.

Precision / geohash 길이 위도 비트 경도 비트 위도 오차 경도 오차 셀 높이 셀 너비
1 2 3 ±23 ±23 4992.6 km 5009.4 km
2 5 5 ±2.8 ±5.6 624.1 km 1252.3 km
3 7 8 ±0.70 ±0.70 156 km 156.5 km
4 10 10 ±0.087 ±0.18 19.5 km 39.1 km
5 12 13 ±0.022 ±0.022 4.9 km 4.9 km
6 15 15 ±0.0027 ±0.0055 609.4 m 1.2 km
7 17 18 ±0.00068 ±0.00068 152.5 m 152.9 m
8 20 20 ±0.00086 ±0.000172 19 m 38.2 m
9 22 23 ±0.000021 ±0.000021 4.8 m 4.8 m
10 25 25 ±0.00000268 ±0.00000536 59.5 cm 1.2 m
11 27 28 ±0.00000067 ±0.00000067 14.9 cm 14.9 cm
12 30 30 ±0.00000008 ±0.00000017 1.9 cm 3.7 cm

더 알아보기 (Learn more)