# Creating extra field with geolocation from field with location names

**URL:** <https://discuss.elastic.co/t/creating-extra-field-with-geolocation-from-field-with-location-names/46869>\
**Category:** Elasticsearch\
**Created:** [April 9, 2016, 12:02pm UTC](https://discuss.elastic.co/t/creating-extra-field-with-geolocation-from-field-with-location-names/46869 "2016-04-09T12:02:41Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Olalekan\_Elesin](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/olalekan_elesin/32/827_2.png) [@Olalekan\_Elesin](https://discuss.elastic.co/u/Olalekan_Elesin)\
**Post date:** [April 9, 2016, 12:02pm UTC](https://discuss.elastic.co/t/creating-extra-field-with-geolocation-from-field-with-location-names/46869/1 "2016-04-09T12:02:41Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)\
**Post date:** [April 10, 2016, 5:41am UTC](https://discuss.elastic.co/t/creating-extra-field-with-geolocation-from-field-with-location-names/46869/2 "2016-04-10T05:41:35Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Olalekan\_Elesin](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/olalekan_elesin/32/827_2.png) [@Olalekan\_Elesin](https://discuss.elastic.co/u/Olalekan_Elesin)\
**Post date:** [April 10, 2016, 4:30pm UTC](https://discuss.elastic.co/t/creating-extra-field-with-geolocation-from-field-with-location-names/46869/3 "2016-04-10T16:30:12Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)\
**Post date:** [April 10, 2016, 7:50pm UTC](https://discuss.elastic.co/t/creating-extra-field-with-geolocation-from-field-with-location-names/46869/4 "2016-04-10T19:50:27Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Olalekan\_Elesin](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/olalekan_elesin/32/827_2.png) [@Olalekan\_Elesin](https://discuss.elastic.co/u/Olalekan_Elesin)\
**Post date:** [April 11, 2016, 12:06pm UTC](https://discuss.elastic.co/t/creating-extra-field-with-geolocation-from-field-with-location-names/46869/5 "2016-04-11T12:06:17Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 5, 2017, 11:00pm UTC](https://discuss.elastic.co/t/creating-extra-field-with-geolocation-from-field-with-location-names/46869/6 "2017-07-05T23:00:44Z")

</div>


