# Convert elasticsearch query output of float fields( latitude & longtitude) to String

**URL:** https://discuss.elastic.co/t/convert-elasticsearch-query-output-of-float-fields-latitude-longtitude-to-string/282001
**Category:** Logstash
**Created:** [August 19, 2021, 6:59pm UTC](https://discuss.elastic.co/t/convert-elasticsearch-query-output-of-float-fields-latitude-longtitude-to-string/282001 "2021-08-19T18:59:11Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Sagar\_kadu](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sagar_kadu/32/76973_2.png) [@Sagar\_kadu](https://discuss.elastic.co/u/Sagar_kadu)
#### Post date: [August 19, 2021, 6:59pm UTC](https://discuss.elastic.co/t/convert-elasticsearch-query-output-of-float-fields-latitude-longtitude-to-string/282001/1 "2021-08-19T18:59:11Z")

</div>

I'm using elasticsearch input plugin to fetch data from elasticsearch using scroll api and it returns JSON document which consists of [customerAddresses] array containing latitude & logitude fields of float type...I would like to convert those fields to String..I have tried convert plugin but its not converting to String though.

below is my logstash configuration

> input {  
> elasticsearch {  
> hosts =\> "xxxx:9200"  
> index =\> "customer"  
> ssl =\> true  
> ca\_file =\> "/app/elasticsearch/config/ssl/xxx.pem"  
> user =\> "xxx"  
> password =\> "xxx"  
> query =\> '{"query":{"match":{"\_id":"NUK0001234691"}}}'  
> size =\> 500  
> scroll =\> "5m"  
> }  
> }  
> filter{  
> mutate {  
> remove\_field =\> ["@timestamp","@version","tags"]  
> }  
> if ![customerId] {  
> drop { }  
> }  
> if [customerAddresses][latitude] {  
> mutate {  
> convert =\> { "[customerAddresses][latitude]" =\> "string" }  
> }  
> }  
> if [customerAddresses][longitude] {  
> mutate {  
> convert =\> { "[customerAddresses][longitude]" =\> "string" }  
> }  
> }  
> }  
> output {  
> stdout { codec =\> rubydebug }  
> }

However I don't see float values being converted to String..  
Below is ruby debug output.

> "customerAddresses" =\> [  
> [0] {  
> "addressLine1" =\> "Muslim Council of Great Britain,London",  
> "status" =\> "Active",  
> "longitude" =\> -5814.0,  
> "addressLine3" =\> nil,  
> "height" =\> nil,  
> "microBrick" =\> nil,  
> "latitude" =\> 5151902.0,  
> }

---

<div class="post-metadata">

### Author: ![Badger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/badger/32/25190_2.png) [@Badger](https://discuss.elastic.co/u/Badger)
#### Post date: [August 19, 2021, 8:02pm UTC](https://discuss.elastic.co/t/convert-elasticsearch-query-output-of-float-fields-latitude-longtitude-to-string/282001/2 "2021-08-19T20:02:41Z")

</div>

> [@Sagar\_kadu](#):
>
> "[customerAddresses][latitude]"

The rubydebug output shows [customerAddresses] is any array, so use

```
mutate { convert => { "[customerAddresses][0][longitude]" => "string" } }

```

If there are multiple addresses then I would use a ruby filter to iterate over them, although if you know there are never more than two addresses you could unconditionally use

```
mutate { convert => { "[customerAddresses][1][longitude]" => "string" } }

```

which will be a no-op if there is only one address.

---

<div class="post-metadata">

### Author: ![Sagar\_kadu](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sagar_kadu/32/76973_2.png) [@Sagar\_kadu](https://discuss.elastic.co/u/Sagar_kadu)
#### Post date: [August 19, 2021, 8:47pm UTC](https://discuss.elastic.co/t/convert-elasticsearch-query-output-of-float-fields-latitude-longtitude-to-string/282001/3 "2021-08-19T20:47:55Z")

</div>

@Badger thanks...Could please give me example of ruby filter for iterating array & converting longitude & latitude field for each address to String..The thing is [customerAddresses] can be empty by default or it can contain any number of addresses .. appreciate your help

---

<div class="post-metadata">

### Author: ![Badger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/badger/32/25190_2.png) [@Badger](https://discuss.elastic.co/u/Badger)
#### Post date: [August 19, 2021, 8:59pm UTC](https://discuss.elastic.co/t/convert-elasticsearch-query-output-of-float-fields-latitude-longtitude-to-string/282001/4 "2021-08-19T20:59:18Z")

</div>

I have not tested it, but you could try

```
ruby {
    code => '
        oldAddresses = event.get("customerAddresses")
        if oldAddresses.is_a? Array
            newAddresses = []
            oldAddresses.each { |a|
                a["longitude"] = a["longitude"].to_s
                a["latitude"] = a["latitude"].to_s
                newAddresses << a
            }
            event.set("customerAddreses", newAddresses)
        end
    '
}
```

---

<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: [September 17, 2021, 4:32am UTC](https://discuss.elastic.co/t/convert-elasticsearch-query-output-of-float-fields-latitude-longtitude-to-string/282001/6 "2021-09-17T04:32:44Z")

</div>

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