makudu
(mahesh reddy)
February 14, 2022, 6:28pm
1
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
"""
}
}
}
}
}
stu
(Stuart Tettemer)
February 14, 2022, 8:09pm
2
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.
double msglen = doc['message.keyword'].value.length();
long msgcast = msglen;
return msgcast;
makudu
(mahesh reddy)
February 15, 2022, 6:48am
3
Tried, but this error.
"class_cast_exception",
"reason" : "Cannot cast from [double] to [long]."
stu
(Stuart Tettemer)
February 15, 2022, 3:18pm
4
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.
long msgcast = (long) msglen;
or
return (long) msglen;
makudu
(mahesh reddy)
February 15, 2022, 6:28pm
5
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;
"""
stu
(Stuart Tettemer)
February 15, 2022, 6:43pm
6
That's a string literal due to the quotes, you'll want:
doc['message.keyword'].value.length()
Without the quotes.
makudu
(mahesh reddy)
February 16, 2022, 8:50am
7
"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;
"""
stu
(Stuart Tettemer)
February 16, 2022, 1:38pm
8
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 for more details.
system
(system)
Closed
March 16, 2022, 1:38pm
9
This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.