# Converting a string value into a double in Elasticsearch 5.0.0

**URL:** <https://discuss.elastic.co/t/converting-a-string-value-into-a-double-in-elasticsearch-5-0-0/64430>\
**Category:** Elasticsearch\
**Created:** [October 31, 2016, 11:05am UTC](https://discuss.elastic.co/t/converting-a-string-value-into-a-double-in-elasticsearch-5-0-0/64430 "2016-10-31T11:05:59Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kulasangar\_Gowrisang](https://avatars.discourse-cdn.com/v4/letter/k/ce73a5/32.png) [@Kulasangar\_Gowrisang](https://discuss.elastic.co/u/Kulasangar_Gowrisang)\
**Post date:** [October 31, 2016, 11:05am UTC](https://discuss.elastic.co/t/converting-a-string-value-into-a-double-in-elasticsearch-5-0-0/64430/1 "2016-10-31T11:05:59Z")

</div>

I've done this in the earlier versions of `elasticsearch` by simply converting the identical field (`chargeamount`) within the `aggs` tag which looks like this:

```
{  
   "query":{  
      "query_string":{  
         "query":"api:\"payment\" AND transactionoperationstatus:\"charged\"AND year:2016 AND month:09 AND day:01 AND userid:\"codapayments*\" AND operatorid:\"XL\" AND responsecode:(200 201)"
      }
   },
   "aggs":{  
      "total":{  
         "terms":{  
            "field":"userid"
         },
         "aggs":{  
            "total":{  
               "sum":{  
                   "script":{"inline" : "Double.parseDouble(doc['chargeamount'].value)"}
               }
            }
         }
      }
   }
}

```

But then I'm struggling to do it in the `elasticsearch` version 5.0.0 where initially I had to do the mapping for the index as following:

```
{
  "mappings": {
    "type": {
      "properties": {
        "chargeamount": {
          "type": "text",
          "fielddata": true
        }
      }
    }
  }
}

```

Then I tried to send an http `POST` with the following body:

```
{  
   "query":{  
      "query_string":{  
         "query":"api:\"payment\" AND transactionoperationstatus:\"charged\"AND year:2016 AND month:09 AND day:01 AND userid:\"codapayments*\" AND operatorid:\"XL\" AND responsecode:(200 201)"
      }
   },
   "aggs":{  
      "total":{  
         "terms":{  
            "field":"userid"
         },
         "aggs":{  
            "total":{  
               "sum":{  
                   "script":{"inline" : "Double.parseDouble(doc['chargeamount'].value)"}
               }
            }
         }
      }
   }
}

```

But the result still shows as a `string`. Where am I going wrong? 🤔

Any help could be appreciated.

---

<div class="post-metadata">

**Author:** ![nik9000](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nik9000/32/44947_2.png) [@nik9000](https://discuss.elastic.co/u/nik9000)\
**Post date:** [October 31, 2016, 1:28pm UTC](https://discuss.elastic.co/t/converting-a-string-value-into-a-double-in-elasticsearch-5-0-0/64430/2 "2016-10-31T13:28:46Z")

</div>

> [@Kulasangar\_Gowrisang](#):
>
> "type": "text",

You might want `"type": "keyword"` here so we don't try an analyze the text. You may also want `"type": "double"` so you get numbers.

> [@Kulasangar\_Gowrisang](#):
>
> But the result still shows as a string. Where am I going wrong?

Can you post the result and point out the thing that is wrong? Could you try `"inline": "return Double.parseDouble(doc['chargeamount'].value)"`?

---

<div class="post-metadata">

**Author:** ![Kulasangar\_Gowrisang](https://avatars.discourse-cdn.com/v4/letter/k/ce73a5/32.png) [@Kulasangar\_Gowrisang](https://discuss.elastic.co/u/Kulasangar_Gowrisang)\
**Post date:** [October 31, 2016, 1:44pm UTC](https://discuss.elastic.co/t/converting-a-string-value-into-a-double-in-elasticsearch-5-0-0/64430/3 "2016-10-31T13:44:19Z")

</div>

@nik9000 Thanks for the reply. I'm having the mapping as :

```
{
  "mappings": {
    "my_type": {
      "properties": {
        "userid": {
          "type": "text",
          "fielddata": true
        },
        "responsecode": {
          "type": "integer"
        },
        "chargeamount": {
          "type": "double",
          "coerce": true
        }
      }
    }
  }
}  

```

And then i'm trying to query as such:

```
{  
   "query":{  
      "query_string":{  
         "query":"api:\"payment\" AND transactionoperationstatus:\"charged\"AND year:2016 AND month:09 AND day:01 AND userid:\"codapayments*\" AND operatorid:\"XL\" AND responsecode:(200 201)"
      }
   },
   "aggs":{  
      "total":{  
         "terms":{  
            "field":"userid"
         },
         "aggs":{  
            "total":{  
               "sum":{  
                   "script":{
                   	"inline": "return Double.parseDouble(doc['chargeamount'].value)"
                   }
               }
            }
         }
      }
   }
}

```

And it throws a [script exception](http://pastebin.com/H6meWTQE).

---

<div class="post-metadata">

**Author:** ![nik9000](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nik9000/32/44947_2.png) [@nik9000](https://discuss.elastic.co/u/nik9000)\
**Post date:** [October 31, 2016, 1:59pm UTC](https://discuss.elastic.co/t/converting-a-string-value-into-a-double-in-elasticsearch-5-0-0/64430/4 "2016-10-31T13:59:21Z")

</div>

Now that you've set the mapping to `double` you don't need the script at all. The script error is, in a rather strange way, telling you that "You can't call `Double.parseDouble` on `double`. It needs a `String`".

---

<div class="post-metadata">

**Author:** ![Kulasangar\_Gowrisang](https://avatars.discourse-cdn.com/v4/letter/k/ce73a5/32.png) [@Kulasangar\_Gowrisang](https://discuss.elastic.co/u/Kulasangar_Gowrisang)\
**Post date:** [October 31, 2016, 2:01pm UTC](https://discuss.elastic.co/t/converting-a-string-value-into-a-double-in-elasticsearch-5-0-0/64430/5 "2016-10-31T14:01:29Z")

</div>

@nik9000 So If i'm to get the `sum` of `chargeamount` as in the above query what should I be having within the `query` tag?

Something like this:

```
 "aggs":{  
            "total":{  
               "sum":{  
                   "script":{
                   	"inline" : "doc['chargeamount'].value"
                   }
               }
            }
         }

```

With the above I'm getting the `sum` as a `string` but not as a `double`. I mean the `sum` value isn't having any decimals. 😕

---

<div class="post-metadata">

**Author:** ![nik9000](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nik9000/32/44947_2.png) [@nik9000](https://discuss.elastic.co/u/nik9000)\
**Post date:** [October 31, 2016, 2:03pm UTC](https://discuss.elastic.co/t/converting-a-string-value-into-a-double-in-elasticsearch-5-0-0/64430/6 "2016-10-31T14:03:50Z")

</div>

It'd work but I think:

```auto
"sum": {
   "field": "chargeamount"
}

```

will be a bit lower overhead.

Ooh! Another thing. In 5.0 the default scripting language is `painless`, an embedded oriented language that we made for Elasticsearch. It is new so I expect it to be a bit rough around the edges. It is the default because it is properly secured, unlike groovy, so we can enable it by default.

---

<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, 10:08pm UTC](https://discuss.elastic.co/t/converting-a-string-value-into-a-double-in-elasticsearch-5-0-0/64430/7 "2017-07-05T22:08:16Z")

</div>


