将FeatureCollection转换为Elasticsearch中的geo_shape

3

如何正确将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元素。 一个后续问题,我是否可以进行查询,例如:给我所有几何图形在元素X内(其中X也在索引中)一次查询,而无需获取X,然后为每个多边形执行多个后续查询?

当我尝试按照您展示的方式进行映射时,我得到了“无法解析映射[_doc]:字段[features]未指定类型”的错误。我错过了什么?您可以告诉我如何正确地进行映射吗?谢谢。 - OhadR
1个回答

6

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
  • 使用geometry内部对象填充geometries数组

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

POST /example/_search
{
    "query":{
        "bool": {
            "filter": {
                "geo_shape": {
                    "location": {
                        "shape": {
                            "type": "envelope",
                            "coordinates" : [[13.0, 53.0], [14.0, 52.0]]
                        },
                        "relation": "within"
                    }
                }
            }
        }
    }
}
< p > within 关系返回所有其 geo_shape 字段在查询中给定的几何形状内的文档。


1
感谢您的回答。我知道可以将其转换为“geometrycollection”格式,但我的问题更多的是为什么其他方法也能起作用,并且在转换中有什么优势。对于查询,我的问题是是否可以查询索引中一个元素内的所有元素。 因此,基本上是获取一个已索引的元素并询问该元素内部有什么。 - rincewind
我认为其他方法不会按照您的期望工作,因为每个特征都将被索引为独立的形状或“不相关”的形状数组。 - Val
好的,但似乎索引元素仍然匹配,因为其中一个独立形状匹配,那么从搜索结果的角度来看有什么区别呢? - rincewind
1
所以我刚刚尝试了一下,在索引“geometrycollection”和索引几何列表(就像我的第一个示例中那样),我无法产生不同的搜索结果。 - rincewind
1
好的,我建议选择需要最少努力和对源结构更改的解决方案。 - Val
显示剩余5条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接