# Pipeline that add field with string length

**URL:** <https://discuss.elastic.co/t/pipeline-that-add-field-with-string-length/156503>\
**Category:** Elasticsearch\
**Created:** [November 13, 2018, 3:06pm UTC](https://discuss.elastic.co/t/pipeline-that-add-field-with-string-length/156503 "2018-11-13T15:06:40Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sergio\_PR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sergio_pr/32/42585_2.png) [@Sergio\_PR](https://discuss.elastic.co/u/Sergio_PR)\
**Post date:** [November 13, 2018, 3:06pm UTC](https://discuss.elastic.co/t/pipeline-that-add-field-with-string-length/156503/1 "2018-11-13T15:06:41Z")

</div>

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 🙂

---

<div class="post-metadata">

**Author:** ![jakelandis](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jakelandis/32/36163_2.png) [@jakelandis](https://discuss.elastic.co/u/jakelandis)\
**Post date:** [November 13, 2018, 6:37pm UTC](https://discuss.elastic.co/t/pipeline-that-add-field-with-string-length/156503/2 "2018-11-13T18:37:25Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Sergio\_PR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sergio_pr/32/42585_2.png) [@Sergio\_PR](https://discuss.elastic.co/u/Sergio_PR)\
**Post date:** [November 13, 2018, 8:58pm UTC](https://discuss.elastic.co/t/pipeline-that-add-field-with-string-length/156503/3 "2018-11-13T20:58:20Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![abdon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abdon/32/9195_2.png) [@abdon](https://discuss.elastic.co/u/abdon)\
**Post date:** [November 20, 2018, 10:29am UTC](https://discuss.elastic.co/t/pipeline-that-add-field-with-string-length/156503/4 "2018-11-20T10:29:12Z")

</div>

You can use a pipeline with a [script processor](https://www.elastic.co/guide/en/elasticsearch/reference/current/script-processor.html) to do that:

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

```

---

<div class="post-metadata">

**Author:** ![Sergio\_PR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sergio_pr/32/42585_2.png) [@Sergio\_PR](https://discuss.elastic.co/u/Sergio_PR)\
**Post date:** [November 30, 2018, 10:56am UTC](https://discuss.elastic.co/t/pipeline-that-add-field-with-string-length/156503/5 "2018-11-30T10:56:48Z")

</div>

Thank you very much !!!  
It works 🙂

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [December 28, 2018, 11:08am UTC](https://discuss.elastic.co/t/pipeline-that-add-field-with-string-length/156503/6 "2018-12-28T11:08:31Z")

</div>

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