# Double to long conversion

**URL:** <https://discuss.elastic.co/t/double-to-long-conversion/297179>\
**Category:** Kibana\
**Tags:** painless, kql-kibana-query-language\
**Created:** [February 14, 2022, 6:28pm UTC](https://discuss.elastic.co/t/double-to-long-conversion/297179 "2022-02-14T18:28:47Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![makudu](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/makudu/32/101455_2.png) [@makudu](https://discuss.elastic.co/u/makudu)\
**Post date:** [February 14, 2022, 6:28pm UTC](https://discuss.elastic.co/t/double-to-long-conversion/297179/1 "2022-02-14T18:28:47Z")

</div>

Hi,

How to convert (default) double type value returned by aggregation( Sum).  
I am receiving this exception while trying to explicitly convert to long type. Appreciate any advice. thank you.

# ===================================== type" : "illegal\_argument\_exception", "reason" : "unexpected token ['long'] was expecting one of [ID]."

GET logstash-2022.01.18/\_search?size=0  
{  
"query": {  
"exists": {  
"field":"message.keyword"  
}  
},

"aggs": {  
"msgsize": {  
"sum": {  
"script": {  
"lang": "painless",  
"source": """  
double msglen = "doc['message.keyword'].value.length()",  
long msgcast = msglen  
return msgcast  
"""  
}  
}  
}  
}  
}

---

<div class="post-metadata">

**Author:** ![stu](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stu/32/75063_2.png) [@stu](https://discuss.elastic.co/u/stu)\
**Post date:** [February 14, 2022, 8:09pm UTC](https://discuss.elastic.co/t/double-to-long-conversion/297179/2 "2022-02-14T20:09:18Z")

</div>

> [@makudu](#):
>
> double msglen = "doc['message.keyword'].value.length()",  
> long msgcast = msglen  
> return msgcast

Hi @makudu, try adding a semicolon at the end of these statements.

The first statement wasn't terminated (there's a comma at the end of the line), so the next token when parsing the script, `long` is unexpected.

```auto
double msglen = doc['message.keyword'].value.length();
long msgcast = msglen;
return msgcast;

```

---

<div class="post-metadata">

**Author:** ![makudu](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/makudu/32/101455_2.png) [@makudu](https://discuss.elastic.co/u/makudu)\
**Post date:** [February 15, 2022, 6:48am UTC](https://discuss.elastic.co/t/double-to-long-conversion/297179/3 "2022-02-15T06:48:26Z")

</div>

Tried, but this error.

"class\_cast\_exception",  
"reason" : "Cannot cast from [double] to [long]."

---

<div class="post-metadata">

**Author:** ![stu](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stu/32/75063_2.png) [@stu](https://discuss.elastic.co/u/stu)\
**Post date:** [February 15, 2022, 3:18pm UTC](https://discuss.elastic.co/t/double-to-long-conversion/297179/4 "2022-02-15T15:18:00Z")

</div>

Because of the potential for loss of precision, painless does not implicitly cast from double to long, the script will have to explicitly cast it.

```auto
long msgcast = (long) msglen;

```

or

```auto
return (long) msglen;

```

---

<div class="post-metadata">

**Author:** ![makudu](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/makudu/32/101455_2.png) [@makudu](https://discuss.elastic.co/u/makudu)\
**Post date:** [February 15, 2022, 6:28pm UTC](https://discuss.elastic.co/t/double-to-long-conversion/297179/5 "2022-02-15T18:28:24Z")

</div>

Tried providing explicit cast, this is the exception  
"type" : "class\_cast\_exception",  
"reason" : "Cannot cast from [java.lang.String] to [long].

"source": """  
long msglen = "doc['message.keyword'].value.length()";  
return (long) msglen;  
"""

"source": """  
double msglen = "doc['message.keyword'].value.length()";  
return (long) msglen;  
"""

---

<div class="post-metadata">

**Author:** ![stu](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stu/32/75063_2.png) [@stu](https://discuss.elastic.co/u/stu)\
**Post date:** [February 15, 2022, 6:43pm UTC](https://discuss.elastic.co/t/double-to-long-conversion/297179/6 "2022-02-15T18:43:39Z")

</div>

> [@makudu](#):
>
> "doc['message.keyword'].value.length()"

That's a string literal due to the quotes, you'll want:

> doc['message.keyword'].value.length()

Without the quotes.

---

<div class="post-metadata">

**Author:** ![makudu](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/makudu/32/101455_2.png) [@makudu](https://discuss.elastic.co/u/makudu)\
**Post date:** [February 16, 2022, 8:50am UTC](https://discuss.elastic.co/t/double-to-long-conversion/297179/7 "2022-02-16T08:50:32Z")

</div>

"type" : "class\_cast\_exception",  
"reason" : "Cannot cast from [double] to [long]."

"source": """  
double msglen = doc['message.keyword'].value.length();  
long msgcast = (double) msglen;  
return (long) msgcast;  
"""

---

<div class="post-metadata">

**Author:** ![stu](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stu/32/75063_2.png) [@stu](https://discuss.elastic.co/u/stu)\
**Post date:** [February 16, 2022, 1:38pm UTC](https://discuss.elastic.co/t/double-to-long-conversion/297179/8 "2022-02-16T13:38:51Z")

</div>

What is the goal of the script? The type of `doc['message.keyword'].value.length()` is `int`, it can be returned directly.

Check out the painless docs on [casting](https://www.elastic.co/guide/en/elasticsearch/painless/current/painless-casting.html) for more details.

---

<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:** [March 16, 2022, 1:38pm UTC](https://discuss.elastic.co/t/double-to-long-conversion/297179/9 "2022-03-16T13:38:58Z")

</div>

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