elasticsearch Elastic:存储`FeatureCollection`类型的GeoJSON,查询

a1o7rhls  于 2023-03-29  发布在  ElasticSearch
关注(0)|答案(1)|浏览(174)

我使用Elastic 7.x来存储这样的文档:

{
    "id": "polygon_tests_01.ast-ORTHOMOSAIC",
    "name": "tmpl_integration_test_GeoTIFF",
    "region": {
        "type": "FeatureCollection",
        "features": [
            {
                "type": "Feature",
                "geometry": {
                    "coordinates": [[[-149.67474372431124,61.27942558978003],
                        [-149.65726554157862,60.993770332779064],
                        [-150.11544465434918,61.15680203118899],
                        [-149.87699170822603,61.28122531469481],
                        [-149.67474372431124,61.27942558978003]]],
                    "type": "Polygon"
                },
                "properties": {}
            }
        ],
        "type": "Feature",
        "properties": {}
    }
    ... more data...
    
}

尝试为它创建一个Map,我用途:

{
    "mappings": {
        "properties": {
            "region": {
                "properties": {
                    "features": {
                        "properties": {
                            "geometry": {
                              "type": "geo_shape"
                            }
                        }
                    }
                }
            }
        }
    }
}

首先-这是正确的方式来Map?
第二,假设我想创建一个查询,从弹性的所有文档中提取交叉的“区域”。我使用这个查询:

{
  "query": {
    "geo_shape": {
      "region.features.geometry": { 
        "relation": "intersects",
        "shape": {
          "type":  "polygon",
          "coordinates": [[[10.526270711323841,10.444489244321758],
                           [11.925063668547947,10.371171909552444],
                           [11.070002142972083,9.364612094349482],
                           [10.526270711323841,10.444489244321758]
            ]]
        }
      }
    }
  }
}

我得到一个错误:

failed to find geo_shape field [region.features.geometry]

那么,如何存储FeatureCollection类型的“规范化”GeoJSON并进行下降查询呢?

bkkx9g8r

bkkx9g8r1#

ES确实支持GeometryCollections,但需要一些预处理。
使用最小可重复索引设置:

PUT geoindex
{
  "mappings": {
    "properties": {
      "regions": {
        "type": "geo_shape"
      }
    }
  }
}

提取features以符合w/

POST geoindex/_doc
{
  "id": "polygon_tests_01.ast-ORTHOMOSAIC",
  "name": "tmpl_integration_test_GeoTIFF",
  
  "regions": {
    "type": "geometrycollection",
    "geometries": [
      {
        "type": "polygon",
        "coordinates": [[[-149.67474372431124,61.27942558978003],[-149.65726554157862,60.993770332779064],[-150.11544465434918,61.15680203118899],[-149.87699170822603,61.28122531469481],[-149.67474372431124,61.27942558978003]]]
      }
    ]
  }
}

请注意,regions.geometries可以包含多个geojson特征,而不仅仅是多边形。
之后,我们可以查询多边形的交集:

POST geoindex/_search
{
  "query": {
    "geo_shape": {
      "regions": { 
        "relation": "intersects",
        "shape": {
          "type":  "polygon",
          "coordinates": [[[-149.68734741210938,61.34276125480617],[-149.78347778320312,61.268912537559316],[-149.53628540039062,61.18628656437939],[-149.37286376953125,61.29662618671741],[-149.4085693359375,61.3671195097931],[-149.65164184570312,61.37962043716795],[-149.68734741210938,61.34276125480617]]]
        }
      }
    }
  }
}

注意:你的索引多边形在Anchorage,AK,而查询中的多边形在尼日利亚东部。两者不会重叠:)
所以我上面的查询测试了Eagle River周围的紫色多边形:

相关问题