Check pattern in scripted field

I have a field "a" that sometimes gets values that satisfy the following pattern: "AAA_BBB_CCC_DDD".

The relevant feature of this pattern is the occurrence of the four underscores "_". The lenght of the A's, B's, C's and D's may vary.

I'm trying to define a scripted field that provides a certain output when the value of field "a" satisfies this pattern (i.e. contains four underscores). The problem is that I'm not describing the pattern correctly.

My idea is:

def c =""; 
def b = doc['a.keyword'].value;
def first = b.indexOf('_');
if (doc['a.keyword'].value=='*_*_*_*') 
{c = b.substring(0,first);}
else {c = 0;}
return c

This scripted field now outputs 0 for all entries and this shouldn't be the case. The condition(doc['a.keyword'].value=='*_*_*_*') is never satisfied, which implies that the relevant pattern is not being well described by '*_*_*_*'.

Any ideas or suggestions on how to do this?

Thanks in advance

If you are trying to check against a regex style pattern, the syntax is incorrect and should be something like: if (doc['a.keyword'].value ==~ /\w+_\w+_\w+_\w+/)

Thank you, Angelo!

In my case, regexes are disabled. Is there a way to check for patterns, without enabling regexes?

Not that I'm aware of, also given that you are using *'s in your example, also implies that you want to use regex's so you would have to enable support for that. Otherwise you might have to work around it by splitting, count the number of values and decide if that meets your need and take the first ...

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