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

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,
}

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.

@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

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
    '
}

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