gpt4 book ai didi

elasticsearch - 数学函数不适用于ElasticSearch脚本中的运算符

转载 作者:行者123 更新时间:2023-12-03 02:36:45 28 4
gpt4 key购买 nike

ES版本:number: "7.0.1",
以下查询不起作用。它返回illegal_argument_exception(下面的完整堆栈)。

   {
"query": {
"function_score": {
"query": {
"query_string": {
"query": "SOME_QUERY"
}
},
"script_score": {
"script": {
"source": "Math.log(doc['SOME_FIELD'].value + 1)"
}
}
}
}
}

我虽然 doc['SOME_FIELD'].value可能存在一些问题,但显然在使用运算符时 Math.*函数存在语法问题(在这种情况下为 +):
  • "source": "Math.log(doc['SOME_FIELD'].value + 1)"不起作用
  • "source": "Math.log(1 + 1)"不起作用


  • "source": "doc['SOME_FIELD'].value"作品
  • "source": "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']。value中的值可能不是数字(NaN)。根据期望的数字类型,您可以(并且应该)使用regex评估脚本中的值,以确定其是否正确解析,如下所示:

    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;
    请记住,如果要运行此脚本,则需要将脚本全部放在一行上。
    另外,您还需要在集群上启用正则表达式(如果在lasticsearch yaml文件中)。

    关于elasticsearch - 数学函数不适用于ElasticSearch脚本中的运算符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58731498/

    28 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com