Elasticsearch painless script error

20.9k views Asked by At

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...

2

There are 2 answers

3
Val On BEST ANSWER

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
0
Sunil Purushothaman On
doc['newPrice']

is different from

doc['newPrice'].value

You should use the later