I'm trying to have a dynamic param based on a value in the document.
I tried so far this here
body: {
"script_fields": {
"potentialIncome": {
"script": {
"lang": "painless",
"source": "doc.rentPrice.value - params['doc.buyingPrice.value']",
"params": {
120000: 1200,
150000: 1500
}
}
}
}
}
This throws me following error:
type: 'script_exception',
reason: 'runtime error',
script_stack:
[ 'doc.rentPrice.value - params[\'doc.buyingPrice.value\']',
' ^---- HERE' ],
script: 'doc.rentPrice.value - params[\'doc.buyingPrice.value\']',
lang: 'painless'
I would like to have params dynamic in a way that the doc value buyingPrice decides which value to deduct.
Using ElasticSearch 7.2
A complicated and bad way is to use following script
if(doc['buyingPrice'].value==120000){return doc['rentPrice'].value-params['120000']}
else if(doc['buyingPrice'].value==150000){return doc['rentPrice'].value-params['150000']}
The Es object:
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 2,
"relation": "eq"
},
"max_score": 1.0,
"hits": [{
"_index": "immo",
"_type": "objects",
"_id": "1",
"_score": 1.0,
"_source": {
"buyingPrice": 120000,
"rentPrice": 500
}
}, {
"_index": "immo",
"_type": "objects",
"_id": "2",
"_score": 1.0,
"_source": {
"buyingPrice": 150000,
"rentPrice": 500
}
}]
}
}