Search code examples
elasticsearchgeolocationmappingelasticsearch-painlessgeopoints

how can I generate lat long fields into geo point in elasticsearch


I've ingested CSV data via pgsync from postgres RDS to elasticsearch.my index contains "lat" and "lng"

"lat" : {
      "type" : "float"
    },
    "lng" : {
      "type" : "float"

How would I convert this into an acceptable geopoint format so that I can map it in Kibana?

I already add this mapping:

    ``` PUT /my-index/_mapping
{
  "properties": {
    "location": {
      "type": "geo_point"
    }
  }
}```

but when I'm trying to generate new coordinate field via:

``` POST your_index/_update_by_query
{
  "script": {
    "inline": "ctx._source.location = ctx._source.lat, ctx._source.lon",
    "lang": "painless"
  },
  "query": {
    "match_all": {}
  }
}```





I am unable and getting this error

'''
{
  "error" : {
    "root_cause" : [
      {
        "type" : "script_exception",
        "reason" : "compile error",
        "script_stack" : [
          "... ocation = ctx._source.lat, ctx._source.lng",
          "                             ^---- HERE"
        ],
        "script" : "ctx._source.location = ctx._source.lat, ctx._source.lng",
        "lang" : "painless",
        "position" : {
          "offset" : 38,
          "start" : 13,
          "end" : 55
        }
      }
'''

Is there any suggestion or correction for this?


Solution

  • Good start! Your script needs to look like this

    POST your_index/_update_by_query
    {
      "script": {
        "inline": "ctx._source.location = ['lat': ctx._source.lat, 'lon': ctx._source.lng]",
        "lang": "painless"
      },
      "query": {
        "bool": {
          "filter": [
            {
              "exists": { 
                "field": "lat"
              }
            },
            {
              "exists": { 
                "field": "lng"
              }
            }
          ]
        }
      }
    }