问题描述
您需要尝试不使用单引号。
"source": "return (params[String.valueOf(doc.buyingPrice.value)] != null) ? doc.rentPrice.value - params[String.valueOf(doc.buyingPrice.value)] : 0",
^ ^
| |
解决方法
我正在尝试根据文档中的值创建动态参数。
我到目前为止在这里尝试过
body: {
"script_fields": {
"potentialIncome": {
"script": {
"lang": "painless","source": "doc.rentPrice.value - params['doc.buyingPrice.value']","params": {
120000: 1200,150000: 1500
}
}
}
}
}
这引发了以下错误:
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'
我想使参数动态化,以便doc值buyingPrice
确定要扣除的值。
使用ElasticSearch 7.2
一个复杂而糟糕的方法是使用以下脚本
if(doc['buyingPrice'].value==120000){return doc['rentPrice'].value-params['120000']}
else if(doc['buyingPrice'].value==150000){return doc['rentPrice'].value-params['150000']}
Es对象:
{
"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","_id": "2","_source": {
"buyingPrice": 150000,"rentPrice": 500
}
}]
}
}