首页 > 解决方案 > geojson 到 Elasticsearch:无法解析 [geo_shape] 类型的字段 [geometry.coordinates]

问题描述

我正在尝试使用 Python 将 geojson 文件索引到 elasticsearch(版本 7.6.2)中。

这是我在elasticsearch中定义的映射。

'mappings': {
  "properties": {
    "geometry": {
      "properties": {
        "coordinates": {
          "type": "geo_shape"
        },
        "type": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        }
      }
    },    
 }
}

geojson 文件如下所示:

{
"type": "FeatureCollection",
"name": "testting",
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },
"features": [
{ "type": "Feature", "properties": { "LEGEND": "x_1", "THRESHOLD": -109, "COLOR": "0 0 255", "Prediction": "Coverage" }, "geometry": { "type": "MultiPolygon", "coordinates": [ [ [ [ 151.20061069847705, -33.886918725260998 ], [ 151.200620164862698, -33.886467994010133 ].....

但是,当我将文件写入 Elasticsearch 时,受到此链接的启发:

如何在elasticsearch中索引geojson文件?

def geojson2es(gj):
    for feature in gj['features']:
        yield feature

with open(input_path+'/'+ data) as f:
    gj = json.load(f)

    es = Elasticsearch(hosts=[{'host': 'localhost', 'port': 9200}])

    k = [{
        "_index": "test",
        "_source": feature,
    } for feature in geojson2es(gj)]

    helpers.bulk(es, k)

我有这个错误:

{'type': 'mapper_parsing_exception', 'reason': '无法解析 [geo_shape] 类型的字段 [geometry.coordinates]', ' Cause_by': {'type': 'parse_exception', 'reason': 'shape must是一个由类型和坐标组成的对象'}}

有没有人遇到过类似的问题?我该如何解决?

标签: pythonelasticsearchgeojson

解决方案


您的映射不正确。该geo_shape类型已经暗示了typeand coordinates,因此您无需再次声明它们。

所以你的映射应该是这样的,即每个特征都有一个type(例如Feature,一个散列properties和一个geometry类型geo_shape):

{
  "mappings": {
    "properties": {
      "type": {
        "type": "keyword"
      },
      "properties": {
        "type": "object"
      },
      "geometry": {
        "type": "geo_shape"
      }
    }
  }
}

推荐阅读