Search code examples
elasticsearchkibanaelastic-stackcalculated-columns

How to get value in nested field using Kibana scripted field?


I'm new to Elastic Stack. Here, I'm trying to get the value for "pressure" and then convert it to numeric value(string⇒numeric) using Kibana scripted field. I tried scripted field, but it didn't work for me.

Any idea? I really appreciate your support in advance. One of my data records is as below.

{
  "_index": "production",
  "_type": "_doc",
  "_id": "4570df7a0d4ec1b0e624e868a5861a0f1a9a7f6c35fdsssafafsa734fb152f4bed",
  "_version": 1,
  "_score": null,
  "_source": {
    "factorycode": "AM-NY",
    "productcode": "STR",
    "lastupdatetime": "2020-05-28T04:16:17.590Z",
    "@timestamp": "2020-05-28T04:14:48.000Z",
    "massproduction": {
      "errorcode": null,
      "equipment": "P17470110",
      "operatorldap": null,
      "machinetime": null,
      "quantity": "1",
      "externalfilename": null,
      "errorcomment": null,
      "datas": {
        "data": [
          {
            "value": "45.4",
            "id": "001",
            "name": "pressure"
          },
          {
            "value": "0.45",
            "id": "002",
            "name": "current"
          }
        ]
      },
      "ladderver": null,
      "eid": null,
      "setupid": null,
      "model": "0",
      "identificationtagid": null,
      "workid": "GD606546sf0B002020040800198",
      "reuse": {
        "num": "0"
      },
      "registrydate": "2020-05-28T13:14:48",
      "product": "GD604564550B00",
      "line": "STRS001",
      "judge": "1",
      "cycletime": null,
      "processcode": "OP335",
      "registryutcdate": "2020-04-28T04:14:48",
      "name": "massproduction"
    }
  },
  "fields": {
    "massproduction.registrydate": [
      "2020-05-28T13:14:48.000Z"
    ],
    "@timestamp": [
      "2020-05-28T04:14:48.000Z"
    ],
    "lastupdatetime": [
      "2020-05-28T04:16:17.590Z"
    ],
    "registrydate": [
      "2020-05-28T13:14:48.000Z"
    ],
    "massproduction.registryutcdate": [
      "2020-05-28T04:14:48.000Z"
    ],
    "registryutcdate": [
      "2020-05-28T04:14:48.000Z"
    ]
  },
  "sort": [
    158806546548000
  ]
}

This is my "painless" scripted field in Kibana.

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

Solution

  • You can use Float.parseFloat(value) to convert string to float

    if(params._source.massproduction!=null && params._source.massproduction.datas!=null &&params._source.massproduction.datas.data.size()>0)
      {
        def data = params._source.massproduction.datas.data;
         if(data instanceof ArrayList)
         {
           for(item in data)
            {
              if(item.name=='pressure')
              {
                return  Float.parseFloat(item.value);
              }
            }
         }else
         {
            if(data.name=='pressure')
            {
              return  Float.parseFloat(data.value);
            }
         }
      }
      return 0;