PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, October 25, 2022

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

 October 25, 2022     elasticsearch, elasticsearch-painless, geolocation, geopoints, mapping     No comments   

Issue

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"
          }
        }
      ]
    }
  }
}


Answered By - Val
Answer Checked By - David Goodson (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing