# Convert lat and long to geohash

**URL:** https://discuss.elastic.co/t/convert-lat-and-long-to-geohash/197631
**Category:** Kibana
**Created:** [September 1, 2019, 8:11pm UTC](https://discuss.elastic.co/t/convert-lat-and-long-to-geohash/197631 "2019-09-01T20:11:39Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Brian\_Seel](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/brian_seel/32/53421_2.png) [@Brian\_Seel](https://discuss.elastic.co/u/Brian_Seel)
#### Post date: [September 1, 2019, 8:11pm UTC](https://discuss.elastic.co/t/convert-lat-and-long-to-geohash/197631/1 "2019-09-01T20:11:39Z")

</div>

I have done quite a bit of searching, but either don't know what I am doing wrong, or don't know the right question to ask.

I am ingesting data, including latitude and longitude, and converting them to floats like this:

```
filter {
ruby
{
    path => "/opt/bitnami/logstash/chop.rb"
}
mutate
{
    rename =>
    {
        "stop_lon" => "[location][lon]"
        "stop_lat" => "[location][lat]"
    }
}

```

However, I need to convert the location to a geohash so I can do mapping, but I am struggling to figure out how to do that. 'Location' doesn't seem to be a field in my index, but is just two floats that I generate in chop.rb.

How do I generate this?

---

<div class="post-metadata">

### Author: ![stephenb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stephenb/32/40856_2.png) [@stephenb](https://discuss.elastic.co/u/stephenb)
#### Post date: [September 2, 2019, 12:24am UTC](https://discuss.elastic.co/t/convert-lat-and-long-to-geohash/197631/2 "2019-09-02T00:24:52Z")

</div>

Perhaps see this thread. In short you need a index template with a geo\_point data type mapping typically accomplished via an index\_template

> [@Geo\_point problems with multiple indexes and Logstash](https://discuss.elastic.co/t/geo-point-problems-with-multiple-indexes-and-logstash/197121):
>
> Hi, We have a problem loading geo\_point data, the index template, after some tests, now is configured as: location..........conflict location.lat.....number location.lon....number So, when I try to create a map visualization there is no data. We load the data from Logstash: mutate { add\_field =\> ["[geolocation][lat]", "%{lat}" ] add\_field =\> ["[geolocation][lon]", "%{lng}" ] convert =\> { "[geolocation][lat]" =\> "float" "[geolocation][lon]" =\> "float" } remove\_field =\> [ "lat", "…

and perhaps this post

> [@Geo\_point problems with multiple indexes and Logstash](https://discuss.elastic.co/t/geo-point-problems-with-multiple-indexes-and-logstash/197121/7):
>
> Yes use an index template see [here](https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-templates.html) it is exactly for this case. It will be applied to all new indices that fit the pattern apologies I should have showed you this at first I just wasn't sure what you were trying to accomplish. it would look something like this and every new index created with the pattern will apply this mapping PUT \_template/stats { "index\_patterns": ["stats\*"], "settings": { "number\_of\_shards": 1 }, "mappings": { "properties": { "location": { "t…

---

<div class="post-metadata">

### Author: ![Brian\_Seel](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/brian_seel/32/53421_2.png) [@Brian\_Seel](https://discuss.elastic.co/u/Brian_Seel)
#### Post date: [September 2, 2019, 3:43pm UTC](https://discuss.elastic.co/t/convert-lat-and-long-to-geohash/197631/3 "2019-09-02T15:43:35Z")

</div>

Maybe I am going about this wrong. I am trying to visualize data in a coordinate map, which requires a geohash type. I have the mutation I included above, and I can see that the location.lat and location.lon are numbers in my index (through the GUI).

You were suggesting that I needed to apply a template, but this is the log during startup.

```
[2019-09-02T15:28:53,962][INFO][logstash.outputs.elasticsearch] Attempting to install template {:manage_template=>{"template"=>"logstash-*", "version"=>60001, "settings"=>{"index.refresh_interval"=>"5s"}, "mappings"=>{"_default_"=>{"dynamic_templates"=>[{"message_field"=>{"path_match"=>"message", "match_mapping_type"=>"string", "mapping"=>{"type"=>"text", "norms"=>false}}}, {"string_fields"=>{"match"=>"*", "match_mapping_type"=>"string", "mapping"=>{"type"=>"text", "norms"=>false, "fields"=>{"keyword"=>{"type"=>"keyword", "ignore_above"=>256}}}}}], "properties"=>{"@timestamp"=>{"type"=>"date"}, "@version"=>{"type"=>"keyword"}, "geoip"=>{"dynamic"=>true, "properties"=>{"ip"=>{"type"=>"ip"}, "location"=>{"type"=>"geo_point"}, "latitude"=>{"type"=>"half_float"}, "longitude"=>{"type"=>"half_float"}}}}}}}}

```

Does this mean that its already being applied? "location"=\>{"type"=\>"geo\_point"}

---

<div class="post-metadata">

### Author: ![stephenb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stephenb/32/40856_2.png) [@stephenb](https://discuss.elastic.co/u/stephenb)
#### Post date: [September 2, 2019, 4:17pm UTC](https://discuss.elastic.co/t/convert-lat-and-long-to-geohash/197631/4 "2019-09-02T16:17:53Z")

</div>

hi @Brian_Seel

First What Version are you on?

2nd my understanding is that there is no data type geohash, geohash is an aggregation used to aggregate `geo_point` data.

[https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-geohashgrid-aggregation.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-geohashgrid-aggregation.html)

Apologies but from the one line log line I can not tell, not enough context.

Where did that log come from Logstash?  
Did you create an index\_template manually like in the thread referenced, are you using a beat module?  
Are you trying to manage the template from Logstash?  
Both?

The typically flow if you are creating custom data is ..

1. Create and index template with the desired data types
2. Create your logstash to read, transform and load your data
3. Iterate 1 & 2 till you get it right, may include delete the index template until you get it right
4. Discover and visualize the data. If the data has a geo\_point data type it should show up automatically in the drop downs when you try to create a visualization.

But yes based on that context if "looks" like it is there is a geo\_point data type...

you can check by running these commands in the Dev tools

`GET /name-of-index/_mapping`

And look at the mappings you should see something like this....

```
{
  "city_center" : {
    "mappings" : {
      "properties" : {
        "location" : {
          "type" : "geo_point"
        },
        "name" : {
          "type" : "text"
        }
      }
    }
  }
}

```

and if you run a search it should look something like

```
GET /city/_search

{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "city_center",
        "_type" : "_doc",
        "_id" : "oAzB8mwBheMK44zyIkBj",
        "_score" : 1.0,
        "_source" : {
          "name" : "stamford",
          "location" : [
            -73.554212,
            41.053923
          ]
        }
      }
    ]
  }
}

```

You should also be able to see the index\_templates using this

`GET /_template/name-of-template`

---

<div class="post-metadata">

### Author: ![Brian\_Seel](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/brian_seel/32/53421_2.png) [@Brian\_Seel](https://discuss.elastic.co/u/Brian_Seel)
#### Post date: [September 7, 2019, 3:06pm UTC](https://discuss.elastic.co/t/convert-lat-and-long-to-geohash/197631/5 "2019-09-07T15:06:47Z")

</div>

Thank you for the response. To anyone that has this issue in the future, the thing I was doing wrong was to try and create the template, and then try and apply it to the same index. You can't do that. You have to reindex into a new index.

That means making the template, like this post says.

```
PUT my_index 
{
  "mappings": {
    "doc": { 
      "properties": { 
        "location": { "type": "geo_point" }
      }
    }
  }
}

```

But then reindexing:

```
POST _reindex
{
  "source": {
    "index": "gtfs-data"
  },
  "dest": {
    "index": "my_index1"
  }
}

```

Thanks for the help, Stephen!

---

<div class="post-metadata">

### Author: ![stephenb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stephenb/32/40856_2.png) [@stephenb](https://discuss.elastic.co/u/stephenb)
#### Post date: [September 7, 2019, 5:51pm UTC](https://discuss.elastic.co/t/convert-lat-and-long-to-geohash/197631/6 "2019-09-07T17:51:30Z")

</div>

Cool

That's correct and to be a little bit more clear / directional.... Since In general, you can't change the field type once the index is created

either you  
a) create the template with the specified field type before you index data or

B) like you showed create a new index with the correct template and re-index into it

---

<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: [October 5, 2019, 5:51pm UTC](https://discuss.elastic.co/t/convert-lat-and-long-to-geohash/197631/7 "2019-10-05T17:51:34Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
