doc['field'].value 从不返回值

doc['field'].value never returning values

使用 Kibana/Elasticsearch 版本 6.6。

正在尝试 运行 下面简单的无痛脚本:

String val = "Vanished"; 
if(doc.containsKey('type')) { 
    return doc['type'].value;
}
return val;

Preview Results 部分,当我尝试 运行 代码时,First 10 results 部分总是:

[]

但是,如果我像下面这样更改代码:

String val = "Vanished"; 
if(doc.containsKey('type')) { 
    return "Present";
}
return val;

我在同一 Preview Results 部分得到以下结果:

[
 {
  "_id": "Kha1NmkBcY4KotEKXsZz",
  "test112": [
   "Present"
  ]
 },
 {
  "_id": "1oS1NmkBjBc6pl9UX0IW",
  "test112": [
   "Present"
  ]
 },
 {
  "_id": "14S1NmkBjBc6pl9UX0IW",
  "test112": [
   "Present"
  ]
 },
 {
  "_id": "whC1NmkBCa8dRNQVXzEW",
  "test112": [
   "Present"
  ]
 },
 {
  "_id": "X221NmkBZQRXPOstYIHB",
  "test112": [
   "Present"
  ]
 },
 {
  "_id": "Rca1NmkBZrtXVVVdY50r",
  "test112": [
   "Present"
  ]
 },
 {
  "_id": "CMS1NmkBwiujVR8BZAt2",
  "test112": [
   "Present"
  ]
 },
 {
  "_id": "xhC1NmkBCa8dRNQVZTFf",
  "test112": [
   "Present"
  ]
 },
 {
  "_id": "yBC1NmkBCa8dRNQVZTFf",
  "test112": [
   "Present"
  ]
 },
 {
  "_id": "yRC1NmkBCa8dRNQVZTFf",
  "test112": [
   "Present"
  ]
 }
]

有人可以帮忙找出 doc['type'].value 失败的原因吗?

此问题已通过将脚本更改为以下内容得到解决:

String val = "Vanished"; 
if(doc.containsKey('type.keyword')) { 
    return doc['type.keyword'].value;
}
return val;

请注意使用 type.keyword 而不是 type 作为字段名称。