Search code examples
pythonelasticsearchgeojson

geojson to Elasticsearch : Failed to parse field [geometry.coordinates] of type [geo_shape]


I am trying to indexing geojson file into elasticsearch (version 7.6.2) using Python.

Here is the mapping I defined in elasticsearch.

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

The geojson file looks like this:

{
"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 ].....

However, when I write the file to Elasticsearch, inspired from this link:

How to index geojson file in elasticsearch?

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)

I have got this error:

{'type': 'mapper_parsing_exception', 'reason': 'failed to parse field [geometry.coordinates] of type [geo_shape]', ' caused_by': {'type': 'parse_exception', 'reason': 'shape must be an object consisting of type and coordinates'}}

Did anyone encounter a similar issue? How can I fix it?


Solution

  • Your mapping is not correct. The geo_shape type already implies type and coordinates, so you don't need to declare them again.

    So your mapping should be like this instead, i.e. each feature has a type (e.g. Feature, a hash of properties and a geometry of type geo_shape):

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