Elasticsearch脚本字段动态参数选择

问题描述

您需要尝试不使用单引号。

"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
            }
        }]
    }
}

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...