Kibana脚本字段排序



我遇到了以下问题:

我创建了一个返回String类型的脚本字段现场

但当我试图通过Kibana在这个字段上进行排序时,意味着出现了错误

"加载数据时出错[script_exception]编译错误";

然后我转到发现选项卡,选择此字段,单击按此字段排序:发现

错误发生后,我打开"检查"菜单项,查看正在发送的请求:检查

我看到了这个请求,它导致了弹性:

{
"version": true,
"size": 500,
"sort": [
{
"event_date": {
"order": "desc",
"unmapped_type": "boolean"
}
},
{
"_script": {
"script": {
"source": "if (doc.containsKey('message.keyword')) {n//if (doc['message.keyword'].size() == 0) return 'field not found';ndef path = doc['message.keyword'].value;nString[] message = /\n/.split(path);nString param = 'IP = ';n for (int i = 0; i < message.length; i++) {n        int index = message[i].indexOf(param);n        if (index > -1) {n            return message[i].substring(index+param.length());n           // return 'ее';n        }n    }nreturn '';n} else return '';",
"lang": "painless"
},
"type": "number",
"order": "asc"
}
}
],
.........
}

我发现Kibana给了这个领域";类型":"数字";

如果我通过将"type": "number"更改为"type": "string"将请求直接发送到elastic,则请求正在运行。

你能解释一下,出了什么问题吗?

完全错误:

Error: Bad Request
at Fetch._callee3$ (<url>/bundles/commons.bundle.js:3:3997981)
at l (<url>/bundles/kbn-ui-shared-deps/kbn-ui-shared-deps.js:288:970406)
at Generator._invoke (<url>/bundles/kbn-ui-shared-deps/kbn-ui-shared-deps.js:288:970159)
at Generator.forEach.e.<computed> [as next] (<url>/bundles/kbn-ui-shared-deps/kbn-ui-shared-deps.js:288:970763)
at asyncGeneratorStep (<url>/bundles/commons.bundle.js:3:3991504)
at _next <url>/bundles/commons.bundle.js:3:3991815)

和chome控制台我看到回应:

{
"statusCode": 400,
"error": "Bad Request",
"message": "[script_exception] compile error",
"attributes": {
"error": {
"type": "script_exception",
"reason": "compile error",
"script_stack": [
"...         return message[i].substring(index+param.le ...",
"                             ^---- HERE"
],
"script": "if (doc.containsKey('message.keyword')) {n//if (doc['message.keyword'].size() == 0) return 'field not found';ndef path = doc['message.keyword'].value;nString[] message = /\n/.split(path);nString param = 'IP = ';n for (int i = 0; i < message.length; i++) {n        int index = message[i].indexOf(param);n        if (index > -1) {n            return message[i].substring(index+param.length());n           // return 'ее';n        }n    }nreturn '';n} else return '';",
"lang": "painless",
"position": {
"offset": 358,
"start": 333,
"end": 383
},
"caused_by": {
"type": "class_cast_exception",
"reason": "Cannot cast from [java.lang.String] to [double]."
}
}
}
}

提供的错误片段不足以准确说明出了什么问题。

美化您的脚本可以获得

if (doc.containsKey('message.keyword')) {
def path = doc['message.keyword'].value;
String[] message = /\n/.split(path);
String param = 'IP = ';
for (int i = 0; i < message.length; i++) {
int index = message[i].indexOf(param);
if (index > -1) {
return message[i].substring(index + param.length());
}
}
return '';
} else {
return ''
}

并且在所有这些情况下都将返回string。因此,如果您的脚本排序为number类型,但您提供了字符串,ES将抛出异常。

最后,你所说的I saw the request, which was leaded to elastic是什么意思?这个要求是怎么来的?

最新更新