# Runtime field error

**URL:** https://discuss.elastic.co/t/runtime-field-error/296234
**Category:** Elasticsearch
**Tags:** painless, runtime-fields
**Created:** [February 3, 2022, 11:06pm UTC](https://discuss.elastic.co/t/runtime-field-error/296234 "2022-02-03T23:06:22Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![gentle\_ghost](https://avatars.discourse-cdn.com/v4/letter/g/ea5d25/32.png) [@gentle\_ghost](https://discuss.elastic.co/u/gentle_ghost)
#### Post date: [February 3, 2022, 11:06pm UTC](https://discuss.elastic.co/t/runtime-field-error/296234/1 "2022-02-03T23:06:22Z")

</div>

Hello,

I'm attempting to create a runtime field that parses values from a field based on a regex. I have a field `event_trigger` that contains some various strings. I'm looking to find all values which contain `giy2` and store in the field `group_a` I'm running into a java class error:

```auto
          "script_stack" : [
            "emit (doc['event_trigger'].valu ...",
            " ^---- HERE"
          ],
          "script" : "emit (doc['event_trigger'].value =~ /.*giy2.*/)",
          "lang" : "painless",
          "position" : {
            "offset" : 6,
            "start" : 0,
            "end" : 31
          },
          "caused_by" : {
            "type" : "class_cast_exception",
            "reason" : "class_cast_exception: Cannot cast from [boolean] to [java.lang.String]."

```

Here is the query that I'm running:

```auto
{
   "runtime_mappings": {
     "group_a": {
       "type": "keyword",
       "script": {
         "source": "emit (doc['event_trigger'].value =~ /.*giy2.*/)"
       }
     }
   },
   "fields": [
     "group_a"
   ],
   "query": {
     "wildcard": {
       "group_a": "*"
     }
   }
 }

```

Any help is appreciated

---

<div class="post-metadata">

### Author: ![Tomo\_M](https://avatars.discourse-cdn.com/v4/letter/t/848f3c/32.png) [@Tomo\_M](https://discuss.elastic.co/u/Tomo_M)
#### Post date: [February 4, 2022, 9:12am UTC](https://discuss.elastic.co/t/runtime-field-error/296234/2 "2022-02-04T09:12:48Z")

</div>

> [@gentle\_ghost](#):
>
> `emit (doc['event_trigger'].value =~ /.*giy2.*/)`

`doc['event_trigger'].value =~ /.*giy2.*/` is only a boolean wheter the value matches the pattern. Maybe what you want to do is something like:

`if (doc['event_trigger'].value =~ /.*giy2.*/) { emit(doc['event_trigger'].value)}`

---

<div class="post-metadata">

### Author: ![gentle\_ghost](https://avatars.discourse-cdn.com/v4/letter/g/ea5d25/32.png) [@gentle\_ghost](https://discuss.elastic.co/u/gentle_ghost)
#### Post date: [February 4, 2022, 5:42pm UTC](https://discuss.elastic.co/t/runtime-field-error/296234/3 "2022-02-04T17:42:40Z")

</div>

Hello,

Thanks! That resolved the issue. I appreciate the help.

Is it possible to cast two capture groups to two separate fields? In the runtime examples it explicitly shows the creation of 1 field. What I'd like to do is something like if `if (doc['event_trigger'].value =~ /.*giy2.*/) { emit(doc['event_trigger'].value = group_a)} else if if (doc['event_trigger'].value =~ /.*giy1.*/) { emit(doc['event_trigger'].value = group_b`

Thanks

---

<div class="post-metadata">

### Author: ![Tomo\_M](https://avatars.discourse-cdn.com/v4/letter/t/848f3c/32.png) [@Tomo\_M](https://discuss.elastic.co/u/Tomo_M)
#### Post date: [February 4, 2022, 10:45pm UTC](https://discuss.elastic.co/t/runtime-field-error/296234/4 "2022-02-04T22:45:36Z")

</div>

> [@gentle\_ghost](#):
>
> emit(doc['event\_trigger'].value = group\_a)

`doc['event_trigger'].value = group_a` is assignment expression and not value. What do you want?

emit works for the specified field and you cannot use two fields. But if you emit hashmap, each keys and values will work as subfield.

---

<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: [March 4, 2022, 10:45pm UTC](https://discuss.elastic.co/t/runtime-field-error/296234/5 "2022-03-04T22:45:38Z")

</div>

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