Jose Ramirez
Jose Ramirez

Reputation: 401

Swapping coordinates of geopoints in elasticsearch index

Well, I have an index with documents that have geopoints as arrays of doubles. But it turns out that the longitude and latitude are in the wrong order, i.e., instead of [lon, lat] the arrays are like [lat, lon]. Is there any way to switch those values on all documents of the index without having to recreate them all again? Or change the format of the geopoints from an array to a string, so the data for them becomes "lat, lon"? So far, the solution I found was to recreate the whole dataset again, which in this case is very time consuming to be practical.

Upvotes: 0

Views: 567

Answers (1)

Val
Val

Reputation: 217254

You could use the update by query API in order to update all documents and swap the coordinates, like this:

POST your_index/_update_by_query
{
  "script": {
    "inline": "ctx._source.location = [ctx._source.location[1], ctx._source.location[0]]",
    "lang": "painless"
  },
  "query": {
    "match_all": {}
  }
}

Upvotes: 2

Related Questions