# How to create a scripted field using wildcard

**URL:** https://discuss.elastic.co/t/how-to-create-a-scripted-field-using-wildcard/79812
**Category:** Kibana
**Created:** [March 23, 2017, 9:03pm UTC](https://discuss.elastic.co/t/how-to-create-a-scripted-field-using-wildcard/79812 "2017-03-23T21:03:10Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![elasticheart](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/elasticheart/32/65189_2.png) [@elasticheart](https://discuss.elastic.co/u/elasticheart)
#### Post date: [March 23, 2017, 9:03pm UTC](https://discuss.elastic.co/t/how-to-create-a-scripted-field-using-wildcard/79812/1 "2017-03-23T21:03:10Z")

</div>

Hi,

I am using ELK GA 5.0.0. I have manged to create a painless scripted field `triggered` in Kibana like;

```
doc['Action'].value == 'PostA' ? 1 : 0

```

I have some selective actions like `Post404`, `Post200` etc. I cant say the exact names since it increases. What I want is, when my `doc['Action'].value` has values `PostA`, `PostB`,.. etc or `GetA`, `GetB`,.. etc, i want `doc['Action'].value` to be `1`, else `0`. How can I do this?

Thanks in advance..

---

<div class="post-metadata">

### Author: ![cjcenizal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cjcenizal/32/11216_2.png) [@cjcenizal](https://discuss.elastic.co/u/cjcenizal)
#### Post date: [March 23, 2017, 11:47pm UTC](https://discuss.elastic.co/t/how-to-create-a-scripted-field-using-wildcard/79812/2 "2017-03-23T23:47:30Z")

</div>

Hey there, I think I have a solution for you... can you try create a scripted field like this:

```
['PostA', 'PostB', 'PostC'].contains(doc['Action'].value) ? 1 : 0

```

If you need something more dynamic than a concrete list, you could try using [regionMatches](https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#regionMatches%2Dboolean%2Dint%2Djava.lang.String%2Dint%2Dint%2D) to match anything containing a substring:

```
doc['Action''].value.regionMatches(true, 0, 'Post', 0, 4)

```

Let me know if this helps!

And I'm pretty sure we have your username trademarked, so expect a letter from our lawyers soon (just kidding!!!!). 🙂

Thanks,  
CJ

---

<div class="post-metadata">

### Author: ![elasticheart](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/elasticheart/32/65189_2.png) [@elasticheart](https://discuss.elastic.co/u/elasticheart)
#### Post date: [March 24, 2017, 4:42am UTC](https://discuss.elastic.co/t/how-to-create-a-scripted-field-using-wildcard/79812/3 "2017-03-24T04:42:02Z")

</div>

Hi @cjcenizal , I need a dynamic solution and in your second solution, I can see only `0`, and there is no `1`.

I have created the username without knowing that there is a service in that name. I am unable to change it my settings, it is disabled. 😞

---

<div class="post-metadata">

### Author: ![cjcenizal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cjcenizal/32/11216_2.png) [@cjcenizal](https://discuss.elastic.co/u/cjcenizal)
#### Post date: [March 24, 2017, 4:55am UTC](https://discuss.elastic.co/t/how-to-create-a-scripted-field-using-wildcard/79812/4 "2017-03-24T04:55:52Z")

</div>

Oops, I forgot to add the ternary:

```
doc['Action''].value.regionMatches(true, 0, 'Post', 0, 4) ? 1 : 0

```

Do you see what it's doing? It's checking to see if the substring "Post" is in the first four characters of the "Action" field value. You can also take a look at the docs I linked to see how regionMatches works. You could update this for any kind of substring value you're looking for.

I love your username! 😻 Please don't change it, I was only joking.

Thanks,  
CJ

---

<div class="post-metadata">

### Author: ![elasticheart](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/elasticheart/32/65189_2.png) [@elasticheart](https://discuss.elastic.co/u/elasticheart)
#### Post date: [March 24, 2017, 5:42am UTC](https://discuss.elastic.co/t/how-to-create-a-scripted-field-using-wildcard/79812/5 "2017-03-24T05:42:38Z")

</div>

Thank you so much. 🙂 so lemme keep this username.. 👍

@cjcenizal I have one doubt. I have created a scripted field like below;

```
if (doc['error'].value == 'NONE') { 
  return 0;
}else if (doc['error'].value == 'ERRX') { 
  return 0;
}
return 1;

```

The value is generated fine, but when I try to filter it + or - , I am getting compilation error in discover. Why is this happening?

---

<div class="post-metadata">

### Author: ![cjcenizal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cjcenizal/32/11216_2.png) [@cjcenizal](https://discuss.elastic.co/u/cjcenizal)
#### Post date: [March 24, 2017, 4:37pm UTC](https://discuss.elastic.co/t/how-to-create-a-scripted-field-using-wildcard/79812/6 "2017-03-24T16:37:07Z")

</div>

Hmm. What's the name of the scripted field? Could you share the error with me?

Thanks,  
CJ

---

<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: [April 21, 2017, 4:37pm UTC](https://discuss.elastic.co/t/how-to-create-a-scripted-field-using-wildcard/79812/7 "2017-04-21T16:37:10Z")

</div>

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