Pipeline that add field with string length


(Sergio PEIXOTO ROSA) #1

Hello,

I want to create a pipeline who receive a document with a string field ("field_str") and return a new field in the same document ("field_length") with the string length.

By example :

INPUT :

{
"field_str" : "some text"
}

OUTPUT
{
"field_str" : "some text",
"field_length" : 9
}

Please, I need your help :slight_smile:


(Jake Landis) #2

Is this for a Logstash pipeline, or an Ingest node pipeline ?


(Sergio PEIXOTO ROSA) #3

Thanks for your attention.
It's for an ingest node pipeline.


(Abdon Pijpelink) #4

You can use a pipeline with a script processor to do that:

PUT _ingest/pipeline/my_pipeline
{
  "processors": [
    {
      "script": {
        "source": """
if(ctx.containsKey("field_str")) {
  ctx.field_length = ctx.field_str.length();
} else {
  ctx.field_length = 0;
}
"""
      }
    }
  ]
}

POST _ingest/pipeline/my_pipeline/_simulate
{
  "docs": [
    {
      "_source": {
        "field_str": "some text"
      }
    }
  ]
}

(Sergio PEIXOTO ROSA) #5

Thank you very much !!!
It works :slight_smile: