elasticsearch 数学函数不适用于ElasticSearch脚本中的运算符
ES版本:
elasticsearch 数学函数不适用于ElasticSearch脚本中的运算符,
elasticsearch,
elasticsearch,ES版本:编号:“7.0.1”, 以下查询不起作用。它返回非法参数\u异常(下面是完整堆栈) 我认为doc['some_FIELD'].value可能有问题,但显然Math.*函数在使用运算符时存在语法问题(+) “source:“Math.log(doc['SOME_FIELD'].value+1)”不起作用 “source:“Math.log(1+1)”不起作用 当 “source”:“doc['SOME_FIELD'].value”有效 “源”:“1+1”起作用 “source:“M
编号:“7.0.1”,
以下查询不起作用。它返回非法参数\u异常
(下面是完整堆栈)
我认为doc['some_FIELD'].value可能有问题,但显然Math.*
函数在使用运算符时存在语法问题(+
)
“source:“Math.log(doc['SOME_FIELD'].value+1)”
不起作用
“source:“Math.log(1+1)”
不起作用
当
“source”:“doc['SOME_FIELD'].value”
有效
“源”:“1+1”起作用
“source:“Math.log(1)”
有效
完整堆栈跟踪:
{
"error": {
"root_cause": [
{
"type": "script_exception",
"reason": "compile error",
"script_stack": [
"NaN",
"^---- HERE"
],
"script": "NaN",
"lang": "painless"
}
],
"type": "search_phase_execution_exception",
"reason": "all shards failed",
"phase": "query",
"grouped": true,
"failed_shards": [
{
"shard": 0,
"index": "index",
"node": "69HAL8sMS-afWoTxhMteKw",
"reason": {
"type": "query_shard_exception",
"reason": "script_score: the script could not be loaded",
"index_uuid": "ASy6y5zxRpqMFONSLED6IQ",
"index": "index",
"caused_by": {
"type": "script_exception",
"reason": "compile error",
"script_stack": [
"NaN",
"^---- HERE"
],
"script": "NaN",
"lang": "painless",
"caused_by": {
"type": "illegal_argument_exception",
"reason": "Variable [NaN] is not defined."
}
}
}
}
],
"caused_by": {
"type": "script_exception",
"reason": "compile error",
"script_stack": [
"NaN",
"^---- HERE"
],
"script": "NaN",
"lang": "painless",
"caused_by": {
"type": "illegal_argument_exception",
"reason": "Variable [NaN] is not defined."
}
}
},
"status": 400
}
doc['SOME_FIELD']中的值可能不是数字(NaN)。根据您期望的数字类型,您可以(并且应该)使用正则表达式计算脚本中的值,以确定其是否正确解析,如下所示:
def m = /^([0-9]+)$/.matcher(doc['SOME_FIELD'].value);
if (m.matches()) {
def number = Integer.parseInt(doc['SOME_FIELD'].value);
if (number == SOMENUMBER) return true;
else return false;
} else return false;
请记住,如果您正在运行此脚本,您将希望将脚本全部放在一行上
此外,如果集群上还没有(在elasticsearch yaml文件中)启用正则表达式,则需要在集群上启用正则表达式
def m = /^([0-9]+)$/.matcher(doc['SOME_FIELD'].value);
if (m.matches()) {
def number = Integer.parseInt(doc['SOME_FIELD'].value);
if (number == SOMENUMBER) return true;
else return false;
} else return false;