Creating extra field with geolocation from field with location names

So I have a field which contains names of locations (type string) but I want to add an extra field by converting the location names to lat and long. How is this possible?

You will want to do this before sending the docs to ES for indexing.

Do you have a list of the coordinates of all the locations?

  1. The docs had been indexed already.
  2. The data is over 20million rows
  3. There is an external RESTful api to fetch the lat and lon

How can this be done? Although I am already reading into creating an elasticsearch plugin for this
Thanks in advance

20 million documents isn't much.
It'd take a lot less effort to reindex with the structure you want that it would to build a plugin.

So, I decided to go with scripting using Javascript to call the external API. my code looks like this:
` var xhr = new XMLHttpRequest();
xhr.open("GET", external_api+region, false);

xhr.send()

var payload = JSON.parse(xhr.response)

var response = {}

var details = payload.data
for(var i=0; i < payload.length; ++i){
response = (payload[i].coordinates)
}

response`

where region is the param passed from the query.
My query:
{
"query": {"match_all": {}},
"script_fields": {
"lat_lon": {
"script_file" : "location_script",
"lang" : "javascript",
"params" : {
"region" : "doc['region'].value"
}
}
}
}

and the response I get is:
"shard": 0,
"index": "amr",
"node": "XXXXXX",
"reason": {
"type": "ecma_error",
"reason": "ReferenceError: "XMLHttpRequest" is not defined. (Script3.js#1)"
}