Ingest Node - Script Processor not recognizing field as number

Hello!

I'm trying to create a new field (of type double) that is the result of a calculation of another field, that is of type double too.
I'm using filebeat to send the data to an ingest pipeline, and the ingest pipeline has a script processor to do this calculation, as below:

{
"script": {
"lang": "painless",
"source": "ctx.radius.log.Tempo_Conexao = ctx.radius.log.OrigSessionTime / params.param_c",
"params": {
"param_c": 60.0
}
}
}

The field ctx.radius.log.Tempo_Conexao and ctx.radius.log.OrigSessionTime are both of type double, but the script processor is recognizing the field ctx.radius.log.OrigSessionTime as string, so i'm getting the error below:

Cannot apply [/] operation to types [java.lang.String] and [java.lang.Double].

Is there any way to cast the field ctx.radius.log.OrigSessionTime to double?

Thanks in advance!

ES version: 7.0.1

Att,

Gabriel.

Hi there!

Try something like

"script": {
  "lang": "painless",
  "source": """
    def OrigSessionTime = Double.parseDouble(ctx.radius.log.OrigSessionTime);
    ctx.radius.log.Tempo_Conexao = OrigSessionTime / params.param_c;
  """,
  "params": {
    "param_c": 60.0
  }
}

Simulating it in a pipeline it seems to work pretty well:

POST _ingest/pipeline/_simulate
{
  "docs": [
    {
      "_source": {
        "stringy_field": "600",
        "log": {}
      }
    }
  ]
  , "pipeline": {
    "processors": [
      {
        "script": {
          "source": """
            def doubly_field = Double.parseDouble(ctx.stringy_field);
            ctx.log.doubly_field = doubly_field / params.param_c; 
          """,
          "params": {
            "param_c": 60.0
          }
        }
      }
    ]
  }
}

returning:

{
  "docs" : [
    {
      "doc" : {
        "_index" : "_index",
        "_type" : "_doc",
        "_id" : "_id",
        "_source" : {
          "stringy_field" : "600",
          "log" : {
            "doubly_field" : 10.0
          }
        },
        "_ingest" : {
          "timestamp" : "2020-01-07T15:45:20.987Z"
        }
      }
    }
  ]
}

Also, if you have to use it in a ingest pipeline, I'd first look for the presence of all the needed fields before applying anything to them, in order to avoid breaking the script.

Hello Fabio!

It worked like a charm!!

Thanks very much!!!!

Gabriel.

No problem.

Glad I could be of help :wink:

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.