Use of length function in split processor

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

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

Will the data always look like xx2B_xx_xx xxx_xxx_xx?

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

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

"f2" : "2B"

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

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

"f2" : "2B"

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?

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

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

This will give you an array of 4 items.

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

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

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

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

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"

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

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

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