Elasticsearch 地理搜索聚合

发布时间 2023-12-18 22:57:33作者: 粒子先生

 

数据类型 查询功能 聚合功能

geo_point

geo_shape

[geo shape]查询文档的geo-
shape是否跟查询指定geo-shap
e相交、内嵌、不相交。


[geo bounding box] 查询文
档的point是否在指定的矩形范围
内。


[geo distance]:查询中心点指
定半径范围内的文档
聚合功能

[geo polygon]查询文档的po
int是否在指定的多边形范围内。

[geo bounds]给指定条下的
坐标点画一个框


[geo distance]按距离中心点
的范围聚台,比如:1km以内,
1~10km,10km以外


[ geohash grid]按 geohash
聚合,可指定geohash精度。

 

 

一、地理数据类型

geo_point 地理点类型

geojson、wkt、geohash、对象

geo_shape 地理形状类型

Point 、LineString 、Polygon 、MultiPoint 、MultiLineString 、MultiPolygon 、GeometryCollection 、envelope 、circle 

二、地理查询

 

geo_bounding_box 矩形过滤

 

# 范围查询 支持geo_point 和 geo_shape
GET qgpoi/_search
{
  "query": {
    "bool": {
      "must": {
        "match_all": {}
      },
      "filter": {
        "geo_bounding_box": {
          "point": {
            "top_left": {
              "lat": 29.4674833601239,
              "lon": 106.478643978476
            },
            "bottom_right": {
              "lat": 28.4674833601239,
              "lon": 107.478643978476
            }
          }
        }
      }
    }
  }
}

geo_distance 距离查询(圆形过滤)

# 距离查询
GET qgpoi/_search
{
  "query": {
    "bool": {
      "must": {
        "match_all": {}
      },
      "filter": {
        "geo_distance": {
          "distance": "200m",
          "point": {
            "lat": 29.4674833601239,
            "lon": 106.478643978476
          }
        }
      }
    }
  }
}

geo_polygon 多边形查询(已过时)

# 多边形查询(7.12版本废弃)
GET qgpoi/_search
{
  "query": {
    "bool": {
      "must": {
        "match_all": {}
      },
      "filter": {
        "geo_polygon": {
          "point": {
            "points": [
              { "lat": 29, "lon": 104 },
              { "lat": 29, "lon": 105 },
              { "lat": 29, "lon": 106 }
            ]
          }
        }
      }
    }
  }
}

geo_shape 地理形状查询

# geo_shape查询 预置查询范围
PUT /shapes
{
  "mappings": {
    "properties": {
      "location": {
        "type": "geo_shape"
      }
    }
  }
}

PUT /shapes/_doc/deu
{
  "location": {
    "type": "envelope",
    "coordinates" : [[107,29], [106,28]]
  }
}

GET qgpoi/_search
{
  "query": {
    "bool": {
      "filter": {
        "geo_shape": {
          "point": {
            "indexed_shape": {
              "index": "shapes",
              "id": "deu",
              "path": "location"
            }
          }
        }
      }
    }
  }
}
# 按矩形bbox范围查询
GET qgpoi/_search { "query": { "bool": { "must": { "match_all": {} }, "filter": { "geo_shape": { "point": { "shape": { "type": "envelope", "coordinates": [ [ 107, 29 ], [ 106, 28 ] ] }, "relation": "within" } } } } } }

二、地理聚合

Geo distance 聚合

Elasticsearch 允许您针对 GeoPoint 执行聚合:地理距离聚合。使用文档中可用的 location 字段,我们将聚合以下五个范围内的其他文档:

不到 10 公里
从 10 公里到 20 公里
从 20 公里到 50 公里
从 50 公里到 100 公里
100 公里以上

POST qgpoi/_search?filter_path=aggregations
{
  "size": 0,
  "aggs": {
    "NAME": {
      "geo_distance": {
        "field": "point",
        "origin": {
          "lat": 39.970718,
          "lon": 116.325747
        },
        "ranges": [
          {
            "to": 10
          },
          {
            "from": 10,
            "to": 20
          },
          {
            "from": 20,
            "to": 50
          },
          {
            "from": 50,
            "to": 100
          },
          {
            "from": 100
          }
        ]
      }
    }
  }
}

Geo bounds 聚合

计算边界范围

POST qgpoi/_search?filter_path=aggregations
{
  "size": 0,
  "aggs": {
    "box": {
      "geo_bounds": {
        "field": "point",
        "wrap_longitude": true
      }
    }
  }
}
    • field:这是包含文档地理点的字段。
    • wrap_longitude:这是一个可选参数,指定是否应允许边界框与国际日期变更线重叠(默认为 true)。

Geo centroid 聚合

计算中心点

POST qgpoi/_search?filter_path=aggregations
{
  "size": 0,
  "aggs": {
    "centroid": {
      "geo_centroid": {
        "field": "point"
      }
    }
  }
}
    • precision(默认 6):即从 0 到 29 的缩放级别。29 值是一个高精度值,覆盖大约 10 cm x 10 cm 的土地。 通常,更常用的值在 6 到 12 之间。

# Geo tile 聚合

使用 Elasticsearch 在地图上显示数据是 Elasticsearch 用户之间非常常见的模式。 最常用的地图格式之一是瓦片格式,其中地图被分成几个小的正方形部分,当需要渲染位置时,服务器会获取该位置附近的瓦片。

POST qgpoi/_search?filter_path=aggregations
{
  "size": 0,
  "aggs": {
    "tiles": {
      "geotile_grid": {
        "field": "point",
        "precision": 5
      }
    }
  }
}
# 按条件过滤Geo tile聚合
GET qgpoi/_search
{
  "size": 0,
  "track_total_hits": false,
  "aggs": {
    "gridSplit": {
      "geotile_grid": {
        "bounds": {
          "top_left": [
            -180,
            85.05113
          ],
          "bottom_right": [
            180,
            -66.51326
          ]
        },
        "field": "point",
        "precision": 8,
        "size": 65535,
        "shard_size": 65535
      },
      "aggs": {
        "gridCentroid": {
          "geo_centroid": {
            "field": "point"
          }
        }
      }
    }
  },
  "fields": [],
  "script_fields": {},
  "stored_fields": [
    "*"
  ],
  "runtime_mappings": {},
  "_source": {
    "excludes": []
  },
  "query": {
    "bool": {
      "must": [],
      "filter": [
        {
          "bool": {
            "must": [
              {
                "exists": {
                  "field": "point"
                }
              },
              {
                "geo_bounding_box": {
                  "point": {
                    "top_left": [
                      -180,
                      86.65723
                    ],
                    "bottom_right": [
                      180,
                      -66.51326
                    ]
                  }
                }
              }
            ]
          }
        }
      ],
      "should": [],
      "must_not": []
    }
  }
}

 

geohash_grid 聚合

 使用 geohash_grid 聚合,可以根据文档的 geohash 值对文档进行分组

GET qgpoi/_search
{
  "size" : 0,
  "aggs" : {
     "grouped" : {
        "geohash_grid" : {
           "field" : "point",
           "precision" : 2
        }
     }
  }
}

geohex grid聚合

对于 geo_hex 网格,它只能用于 geo_point 字段,付费

GET qgpoi/_search?filter_path=aggregations
{
  "size" : 0,
  "aggs" : {
     "grouped" : {
        "geohex_grid" : {
           "field" : "point",
           "precision" : 1
        }
     }
  }
}

 

 官网地址

https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-geo-shape-query.html