在Elasticsearch中将FeatureCollection转换为geo_shape

时间:2018-08-09 07:47:17

标签: elasticsearch geojson

将geojson FeatureCollection转换为es geo_shape的正确方法是什么?

我有一个FeatureCollection看起来像这样:

{
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "geometry": {
        "type": "Polygon",
        "coordinates": [[[1.96, 42.455],[1.985,42.445]]]
      }
    },
    {
      "type": "Feature",
      "geometry": {
        "type": "Polygon",
        "coordinates": [...]
      }
    }
  ]
}

如何将其转换为es geo_shape。 目前,我只是像这样对其进行索引(删除type: Featuretype: FeatureCollection字段)并添加一个映射,内容为:

"features": {
    "geometry": {
      "type": "geo_shape"
    }
}

这似乎很好用,但由于我给出了一组geometry,所以感觉很不对。 这样可以吗,还是将FeatureCollection转换为geometrycollection类型的正确方法?显然需要多个geometry元素。

一个跟进问题,我可以在一个查询中查询la:Give me all elements geometrically inside Element X(其中X也在索引中),而无需获取X并且可以对每个多边形进行多个跟进查询?

1 个答案:

答案 0 :(得分:3)

GeometryCollection可能正是您想要的。

所以,如果您有这个:

{
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "geometry": {
        "type": "Polygon",
        "coordinates": [[[1.96, 42.455],[1.985,42.445]]]
      }
    },
    {
      "type": "Feature",
      "geometry": {
        "type": "Polygon",
        "coordinates": [...]
      }
    }
  ]
}

您可以像这样在ES中对其进行索引:

PUT example
{
  "mappings": {
    "doc": {
      "properties": {
        "location": {
          "type": "geo_shape"
        }
      }
    }
  }
}

POST /example/doc
{
    "location" : {
        "type": "geometrycollection",
        "geometries": [
            {
                "type": "Polygon",
                "coordinates": [[[1.96, 42.455],[1.985,42.445]]]
            },
            {
                "type": "Polygon",
                "coordinates": [...]
            }
        ]
    }
}

因此,基本上,您只需要:

  • FeatureCollection更改为geometrycollection
  • features更改为geometries
  • geometries内部对象填充geometry数组

关于您的查询,您可以这样做:

POST /example/_search
{
    "query":{
        "bool": {
            "filter": {
                "geo_shape": {
                    "location": {
                        "shape": {
                            "type": "envelope",
                            "coordinates" : [[13.0, 53.0], [14.0, 52.0]]
                        },
                        "relation": "within"
                    }
                }
            }
        }
    }
}

within关系返回其geo_shape字段在查询给定的几何范围内的所有文档。