简体   繁体   English

如何在 Kibana(7.6) 中为嵌套文件编写脚本字段

[英]How to write a scripted field for nested fileds in Kibana(7.6)

I'm a newbie to Elasticsearch and Kibana and I want to write a scripted field to get the nested field value.我是 Elasticsearch 和 Kibana 的新手,我想编写一个脚本字段来获取嵌套字段值。 I tried the following but it did not work.我尝试了以下但没有奏效。

doc['dadatas.datas_name.keyword:pressure'].value doc['dadatas.datas_name.keyword:pressure'].value

I want to get the pressure value for each doc (record).我想获取每个文档(记录)的压力值。

The output should be 12.56 output 应该是 12.56

My doc looks like this.我的文档看起来像这样。

Thanks in advance!!提前致谢!!

{
  "_index":"process_data",
  "_type":"_doc",
  "_id":"UzFHVDM1MTAxMTk5QSAyMDAyMDAwOTUzN19TVDNBMjBfU1QzQTIwMTAwXzIwMjAtMDItMThUMTk6NDc6MzM=",
  "_version":1,
  "_score":1,
  "_source":{
    "eid":"",
    "line":"Line 1",
    "equipment":"Equipment 1",
    "ladderver":"",
    "registrydate":"2020-02-18T19:47:33",
    "registryutcdate":"2020-02-18T10:47:33",
    "setupid":"",
    "workid":"2345743",
    "datas":[
      {
        "datas_id":"001",
        "name":"cycletime",
        "value":"13.9"
      },
      {
        "datas_id":"002",
        "name":"machinetime",
        "value":"10.7"
      },
      {
        "datas_id":"003",
        "name":"pressure",
        "value":"12.56"
      }
    ]
  }
}
{
  "script_fields": {
    "pressure": {
      "script": {
        "source": "for(item in params._source.datas){ if(item.name=='pressure')return item.value;} return '';"
      }
    }
  }
}

Script fields are calculated on fly.脚本字段是即时计算的。 So better option would be to extract field from inner_hits所以更好的选择是从 inner_hits 中提取字段

Query:询问:

{
  "query": {
    "nested": {
      "path": "datas",
      "query": {
        "term": {
          "datas.name.keyword": {
            "value": "pressure"
          }
        }
      },
      "inner_hits": {}
    }
  }
}

Result:结果:

"hits" : [
      {
        "_index" : "index5",
        "_type" : "_doc",
        "_id" : "kf6xw3EB8jeMa7x6RMwZ",
        "_score" : 0.9808291,
        "_source" : {
          "eid" : "",
          "line" : "Line 1",
          "equipment" : "Equipment 1",
          "ladderver" : "",
          "registrydate" : "2020-02-18T19:47:33",
          "registryutcdate" : "2020-02-18T10:47:33",
          "setupid" : "",
          "workid" : "2345743",
          "datas" : [
            {
              "datas_id" : "001",
              "name" : "cycletime",
              "value" : "13.9"
            },
            {
              "datas_id" : "002",
              "name" : "machinetime",
              "value" : "10.7"
            },
            {
              "datas_id" : "003",
              "name" : "pressure",
              "value" : "12.56"
            }
          ]
        },
        "inner_hits" : {
          "datas" : {
            "hits" : {
              "total" : {
                "value" : 1,
                "relation" : "eq"
              },
              "max_score" : 0.9808291,
              "hits" : [
                {
                  "_index" : "index5",
                  "_type" : "_doc",
                  "_id" : "kf6xw3EB8jeMa7x6RMwZ",
                  "_nested" : {
                    "field" : "datas",
                    "offset" : 2
                  },
                  "_score" : 0.9808291,
                  "_source" : {
                    "datas_id" : "003",
                    "name" : "pressure",
                    "value" : "12.56"
                  }
                }
              ]
            }
          }
        }
      }
    ]
  }

EDIT 1: My answer was for query in dev tools For Index pattern script field, you need to add below编辑1:我的答案是在开发工具中查询对于索引模式脚本字段,您需要在下面添加

for(item in params._source.datas)
{
    if(item.name=='pressure'){
      return item.value;
   }
}
return 0;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM