elasticsearchgeopoints

How to convert existing coordinates in ElasticSearch to geopoints


I am trying to convert latitude and longitude to geo_points in ElasticSearch. The problem is, I already have the data uploaded latitude and longitude values in elasticsearch but am having trouble converting them. I am getting the feeling that there is a solution using painless, but haven't quite pinpointed it.

This is what the mapping looks like

{
  "temporary_index" : {
    "mappings" : {
      "handy" : {
        "properties" : {
          "CurrentLocationObj" : {
            "properties" : {
              "lat" : {
                "type" : "float"
              },
              "lon" : {
                "type" : "float"
              }
            }
          },
          "current_latitude" : {
            "type" : "text",
            "fields" : {
              "keyword" : {
                "type" : "keyword",
                "ignore_above" : 256
              }
            }
          },
          "current_longitude" : {
            "type" : "text",
            "fields" : {
              "keyword" : {
                "type" : "keyword",
                "ignore_above" : 256
              }
            }
          },
          "location" : {
            "type" : "geo_point"
          },
        }
      }
    }
  }
}

And this is what a sample doc looks like

"hits" : [
      {
        "_index" : "temporary_index",
        "_type" : "handy",
        "_id" : "9Q8ijmsBaU9mgS87_blD",
        "_score" : 1.0,
        "_source" : {
          "current_longitude" : "139.7243101",
          "current_latitude" : "35.6256271",
          "CurrentLocationObj" : {
            "lat" : 35.6256271,
            "lon" : 139.7243101
          }

There are obviously more fields, but I have removed them for the sake of clarity.

This is what I have tried.

POST temporary_index/_update_by_query
{
  "query": {
    "match_all": {}
  },
  "script": {
    "inline": "ctx._source.location = [ctx._source.current_latitude, ctx._source.current_longitude]",
    "lang": "painless"
  }
}

However I get the following error:

"reason": "failed to parse field [location] of type [geo_point]",
        "caused_by": {
          "type": "parse_exception",
          "reason": "unsupported symbol [.] in geohash [35.4428348]",
          "caused_by": {
            "type": "illegal_argument_exception",
            "reason": "unsupported symbol [.] in geohash [35.4428348]"
          }
        }

I have used the following stackoverflow as a basis for my solution, but I am clearly doing something wrong. Any advice is helpful! Thanks.

Swapping coordinates of geopoints in elasticsearch index


Solution

  • Great start! You're almost there.

    Just note that when specifying a geo_point as an array, the longitude must be the first element and the latitude comes next. However, I suggest you do it like this instead and it will work:

    POST temporary_index/_update_by_query
    {
      "query": {
        "match_all": {}
      },
      "script": {
        "inline": "ctx._source.location = ['lat': ctx._source.current_latitude, 'lon': ctx._source.current_longitude]",
        "lang": "painless"
      }
    }