# Use of length function in split processor

**URL:** <https://discuss.elastic.co/t/use-of-length-function-in-split-processor/287972>\
**Category:** Elasticsearch\
**Tags:** painless\
**Created:** [October 29, 2021, 6:39am UTC](https://discuss.elastic.co/t/use-of-length-function-in-split-processor/287972 "2021-10-29T06:39:26Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![varunbsgaur](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/varunbsgaur/32/125005_2.png) [@varunbsgaur](https://discuss.elastic.co/u/varunbsgaur)\
**Post date:** [October 29, 2021, 6:39am UTC](https://discuss.elastic.co/t/use-of-length-function-in-split-processor/287972/1 "2021-10-29T06:39:26Z")

</div>

How can we use "length" to only extract last two values of the string in the given script:-

POST \_ingest/pipeline/\_simulate  
{  
"pipeline" :  
{  
"description": "_description",  
"processors": [  
{  
"split": {  
"field": "f1",  
"separator": "_",  
"target\_field":"temp"  
},  
"script": "ctx.ln=ctx.temp.length ;ctx.f2 = ctx.temp[2]; ctx.f3= ctx.temp[3]",  
"remove": {  
"field": "temp"  
}  
}  
]

},  
"docs": [  
{  
"\_source":{  
"f1":"HP2B\_UE9\_Simple Request\_CE-PartnerAgent"  
}  
}  
]  
}

---

<div class="post-metadata">

**Author:** ![aaron-nimocks](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/aaron-nimocks/32/73965_2.png) [@aaron-nimocks](https://discuss.elastic.co/u/aaron-nimocks)\
**Post date:** [November 1, 2021, 2:26pm UTC](https://discuss.elastic.co/t/use-of-length-function-in-split-processor/287972/2 "2021-11-01T14:26:46Z")

</div>

What is the value of the new field you want? `2B`?

Will the data always look like `xx2B_xx_xx xxx_xxx_xx`?

```auto
"script": "ctx.f2 = ctx.temp[2] + '' + ctx.temp[3]"

```

This will get the 3rd and 4th character in the string and concat them together.

```auto
"f2" : "2B"

```

If it's always in the same position you can use substring also.

```auto
POST _ingest/pipeline/_simulate
{
  "pipeline": {
    "description": "description",
    "processors": [
      {
        "script": "ctx.f2 = ctx.f1.substring(2, 4);"
      }
    ]
  },
  "docs": [
    {
      "_source": {
        "f1": "HP2B_UE9_Simple Request_CE-PartnerAgent"
      }
    }
  ]
}

```

**Output**

```auto
"f2" : "2B"

```

---

<div class="post-metadata">

**Author:** ![varunbsgaur](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/varunbsgaur/32/125005_2.png) [@varunbsgaur](https://discuss.elastic.co/u/varunbsgaur)\
**Post date:** [November 2, 2021, 6:52am UTC](https://discuss.elastic.co/t/use-of-length-function-in-split-processor/287972/3 "2021-11-02T06:52:41Z")

</div>

Thank you for the reply Aaron.

We actually require the last two values which are : "Simple Request" and "CE-PartnerAgent" from the complete string.

How can we achieve that?

---

<div class="post-metadata">

**Author:** ![aaron-nimocks](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/aaron-nimocks/32/73965_2.png) [@aaron-nimocks](https://discuss.elastic.co/u/aaron-nimocks)\
**Post date:** [November 2, 2021, 12:10pm UTC](https://discuss.elastic.co/t/use-of-length-function-in-split-processor/287972/4 "2021-11-02T12:10:02Z")

</div>

Ahh I get it now. You just need to add a `_` as your separator.

```auto
"split": {
 "field": "f1",
 "separator": "_", <---------
 "target_field": "temp"
}

```

This will give you an array of 4 items.

```auto
"temp" : [
 "HP2B",
 "UE9",
 "Simple Request",
 "CE-PartnerAgent"
]

```

Then the rest of your code works. Here is the full example.

```auto
POST _ingest/pipeline/_simulate
{
  "pipeline": {
    "description": "description",
    "processors": [
      {
        "split": {
          "field": "f1",
          "separator": "_",
          "target_field": "temp"
        },
        "script": "ctx.f2 = ctx.temp[2]; ctx.f3= ctx.temp[3]",
        "remove": {
          "field": "temp"
        }
      }
    ]
  },
  "docs": [
    {
      "_source": {
        "f1": "HP2B_UE9_Simple Request_CE-PartnerAgent"
      }
    }
  ]
}

```

**Output**

```auto
"f1" : "HP2B_UE9_Simple Request_CE-PartnerAgent",
"f2" : "Simple Request",
"f3" : "CE-PartnerAgent"

```

---

<div class="post-metadata">

**Author:** ![varunbsgaur](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/varunbsgaur/32/125005_2.png) [@varunbsgaur](https://discuss.elastic.co/u/varunbsgaur)\
**Post date:** [November 3, 2021, 10:20am UTC](https://discuss.elastic.co/t/use-of-length-function-in-split-processor/287972/5 "2021-11-03T10:20:36Z")

</div>

Thank you soo much Aaron this really helped alot!!.

But I have a additional query . What shall we do when the array "f1" is different everytime .

And we want last two values of the array everytime dynamically.

Eg: f1="a\_b\_c\_d\_e" f1="a\_b\_c"  
O/p: f2="d" f3="e" f2="b" f3="c"

---

<div class="post-metadata">

**Author:** ![aaron-nimocks](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/aaron-nimocks/32/73965_2.png) [@aaron-nimocks](https://discuss.elastic.co/u/aaron-nimocks)\
**Post date:** [November 3, 2021, 11:05am UTC](https://discuss.elastic.co/t/use-of-length-function-in-split-processor/287972/6 "2021-11-03T11:05:38Z")

</div>

It can be done like this to do it dynamically in case your array structure changes.

```auto
"script": """
 ctx.f2 = ctx.temp[ctx.temp.length - 2];
 ctx.f3 = ctx.temp[ctx.temp.length - 1];
"""

```

---

<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 1, 2021, 11:06am UTC](https://discuss.elastic.co/t/use-of-length-function-in-split-processor/287972/7 "2021-12-01T11:06:08Z")

</div>

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