Pipeline that add field with string length

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:

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

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

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"
      }
    }
  ]
}

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

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