Search code examples
elasticsearchsearchgeopointselasticsearch-mapping

Index array of geopoints in Elasticsearch


Following is my (sample) Elastic search data which has a series of geo coordinates that I'm trying to index.

PUT geomap/_doc/1
{
  "geometry": {
    "coordinates": [
      [
        [
          -10.8544921875,
          49.82380908513249
        ],
        [
          -10.8544921875,
          59.478568831926395
        ],
        [
          2.021484375,
          59.478568831926395
        ],
        [
          2.021484375,
          49.82380908513249
        ],
        [
          -10.8544921875,
          49.82380908513249
        ]
      ]
    ]
  }
}

and this is the elasticsearch mapping I've created for it.

PUT geomap
{
  "mappings": {
    "properties": {
      "geometry": {
        "properties": {
          "coordinates": { "type": "geo_point" }
        }
      }
    }
  }
}

When I tried to insert the data it did not work. I suspect it is due to the fact that I've got arrays of array coordinates. When I updated the sample dataset to single array of coordinates it worked (below).

PUT geomap/_doc/1
{
  "geometry": {
    "coordinates": [
      [
        -10.8544921875,
        49.82380908513249
      ],
      [
        -10.8544921875,
        59.478568831926395
      ],
      [
        2.021484375,
        59.478568831926395
      ],
      [
        2.021484375,
        49.82380908513249
      ],
      [
        -10.8544921875,
        49.82380908513249
      ]
    ]
  }
}

I would be glad to know what mistake I've done in my mapping which doesn't let me to do so.


Solution

  • I suspect your doc is a polygon so you're gonna want to use geo_shape instead:

    PUT geomap
    {
      "mappings": {
        "properties": {
          "geometry": {
            "type": "geo_shape",
            "strategy": "recursive"
          }
        }
      }
    }
    

    Notice the "recursive" strategy too to support more spatial queries (at least in the newer ES releases).

    PUT geomap/_doc/1
    {
      "geometry": {
        "coordinates": [
          [
            [
              -10.8544921875,
              49.82380908513249
            ],
            [
              -10.8544921875,
              59.478568831926395
            ],
            [
              2.021484375,
              59.478568831926395
            ],
            [
              2.021484375,
              49.82380908513249
            ],
            [
              -10.8544921875,
              49.82380908513249
            ]
          ]
        ],
        "type": "polygon"
      }
    }
    

    Notice how the coords array was wrapped in one more array to comply w/ the geojson standard and that the type: polygon attribute was added.