FeatureCollection to geo_shape in Elasticsearch

微笑、不失礼 提交于 2021-02-07 04:00:49

问题


Whats the right way to translate a geojson FeatureCollection to a es geo_shape?

I have a FeatureCollection looking like this:

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

How can I translate this into the es geo_shape. Currently I just index it like that (dropping type: Feature and type: FeatureCollection fields) and add a mapping saying:

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

This seems to work fine, but feels wrong, as I give an array of geometrys. Is this okay or would the right way be to translate the FeatureCollection to type geometrycollection? Which clearly wants multiple geometry elements.

One Followup question, can I do a query a la: Give me all elements geometrically inside Element X(where X is also in the index) in one query, without fetching X and than doing multiple follow up queries for each polygon?


回答1:


The GeometryCollection is probably what you're looking for.

So if you have this:

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

You can index it in ES like this:

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": [...]
            }
        ]
    }
}

So basically, you simply need to:

  • change FeatureCollection to geometrycollection
  • change features to geometries
  • populate the geometries array with the geometry inner-objects

Regarding your query, you can do it like this:

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

The within relationship returns all documents whose geo_shape field is within the geometry given in the query.



来源:https://stackoverflow.com/questions/51761480/featurecollection-to-geo-shape-in-elasticsearch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!