问题
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 geometry
s.
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
togeometrycollection
- change
features
togeometries
- populate the
geometries
array with thegeometry
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