Elasticsearch painless script error
Asked Answered
G

2

24

I have no Java experience and I have an issue with elasticsearch painless script language. (the name painless it's not well chosen).

For the following code I get the error:

{"lang": "painless",
"inline": "float price = doc['newPrice'] > 0.0 ? doc['price'] / doc['newPrice'] : 0; _score * params.constant * price",
"params": {"constant": 1.2}}}}

Cannot apply [>] operation to types [org.elasticsearch.index.fielddata.ScriptDocValues.Doubles] and [java.lang.Double].

I tied to cast it as float with (float) doc['newPrice'] > 0 with the same error.

Then I changed to "Double price = ((Double)doc['discountPrice'] > 0.0) ? doc['price'] / doc['discountPrice'] : 0; _score * params.constant * price",

And received:

'Cannot cast from [Double] to [double].'

Can somebody help me, tried lots of variations with similar kind of errors. Damn painless language...

Gonzales answered 27/12, 2016 at 15:56 Comment(1)
I agree, this language is harder to use and has more "gotchas" than CMcroberts
C
47

You're simply missing the .value to access the field value.

Your script needs to be like this instead:

double price = doc['newPrice'].value > 0.0 ? doc['price'].value / doc['newPrice'].value : 0; _score * params.constant * price
Cheesecloth answered 27/12, 2016 at 16:4 Comment(3)
Val, do you also know how can I check if a field exists, so I can reference it's value?Gonzales
You can check for doc['newPrice'].size(). If you get 0, then there's no value, otherwise, there's a value you can use.Cheesecloth
@Cheesecloth This is a very interesting question on painless script. #53476536Somnifacient
L
6
doc['newPrice']

is different from

doc['newPrice'].value

You should use the later

Looker answered 4/1, 2018 at 5:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.