Using painless script split(string) don't work

I use kibana script filed to split domain. The result is not good.
My code is

The result follow:


However, the code can run in restful format normaly.
like this

result:


So what is the reason, if possible, please instuct me.

Scripted fields are great for experimenting with the data to see if you can pull out additional useful information, but if that information absolutely is useful to you, the real way to solve this problem is at ingestion time. Doing this in Logstash, for example, would be way more efficient.

It is not really clear what your expecting. Based on trying to understand the script, do you want the domain_l2 scripted field in the table to show:

+---------+-------------+
| Date    | mmbiz       |
+-----------------------+
| Date    | mmbiz       |
+-----------------------+
| Date    | localhost   |
+-----------------------+
| Date    | localhost   |
+-----------------------+
| Date    | pingjs      |
+-----------------------+
| Date    | pingjs      |
+---------+-------------+

?

If so, wouldn't a simple regex work better?

My problem is to take out the second level domain.
For example:
I want to split the pingjs.qq.com into the list of [pingjs, qq, com]. Then take the "qq" (the second level domain).
I tried using this code:
https://discuss.elastic.co/uploads/short-url/jFnTVu7yzvPaaxikRsWcthfOPY.png
but the "String level = /\./.split(pingjs.qq.com)" return the same string, without spliting the original string.

I got curious how to do this, so I downloaded the Groovy SDK (since Painless is very similar to Groovy) and played around with some test code in Groovy Console:

Maybe this will work for you:

def dom = doc['domain'].value;
String[] level = dom.split(/\./); // split the string per regex
List ss = Arrays.asList(level);
String l2 = ss[1]; // 1-th index is the "second" level
return l2;

We have similar working code on groovy. However, we need a working painless code. please help us figure out the solution.

Painless is based on Groovy. I found bugs in your original code where the split wasn't being applied on the string correctly, and you were taking the wrong index from the array of split values. Are you saying the solution I suggested isn't working in your scripted field?

You code of "String[] level = dom.split(/./);"
There is '.' in this line of your code, but Painless script needs '\.';
I have tried it server times.

1 Like

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