# Mapping Error with Run Time Field

**URL:** <https://discuss.elastic.co/t/mapping-error-with-run-time-field/330231>\
**Category:** Elasticsearch\
**Tags:** runtime-fields\
**Created:** [April 18, 2023, 2:58pm UTC](https://discuss.elastic.co/t/mapping-error-with-run-time-field/330231 "2023-04-18T14:58:26Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![JeremyP](https://avatars.discourse-cdn.com/v4/letter/j/43a26b/32.png) [@JeremyP](https://discuss.elastic.co/u/JeremyP)\
**Post date:** [April 18, 2023, 2:58pm UTC](https://discuss.elastic.co/t/mapping-error-with-run-time-field/330231/1 "2023-04-18T14:58:27Z")

</div>

Hello,

I'm having issues with runtime fields with Elastic. I have this run time field working fine in Kibana, however, I need this field to be present in the database. I simplified the script, mostly to redact it's sensitive content and I'm able to duplicate it. What I am trying to do, is create a new run time field based on a keyword match to emit a keyword value.

```auto
 PUT test_index
 {
   "mappings": {
     "properties": {
       "dept_code": {
         "type": "keyword"}
     }
   } 
 }
 
 POST test_index/_bulk
 {"index":{"_id":"1"}}
 {"dept_code":"999"}
 {"index":{"_id":"2"}}
 {"dept_code":"998"}
 
 PUT test_index/_mapping
{
  "mappings": {
    "runtime": {
      "test_script": {
        "type": "keyword",
        "script": {
		  "source": "if (doc['dept_code.keyword'].contains('999')) emit('match_999'); if (doc['dept_code.keyword'].contains('998')) emit('match_998');"
		}
	  }
	}
  }
}

```

When I apply the run time field, I receive the following error:

```auto
{
  "error": {
    "root_cause": [
      {
        "type": "mapper_parsing_exception",
        "reason": "Root mapping definition has unsupported parameters: [mappings : {runtime={test_script={type=keyword, script={source=if (doc['dept_code.keyword'].contains('999')) emit('match_999'); if (doc['dept_code.keyword'].contains('998')) emit('match_998');}}}}]"
      }
    ],
    "type": "mapper_parsing_exception",
    "reason": "Failed to parse mapping: Root mapping definition has unsupported parameters: [mappings : {runtime={test_script={type=keyword, script={source=if (doc['dept_code.keyword'].contains('999')) emit('match_999'); if (doc['dept_code.keyword'].contains('998')) emit('match_998');}}}}]",
    "caused_by": {
      "type": "mapper_parsing_exception",
      "reason": "Root mapping definition has unsupported parameters: [mappings : {runtime={test_script={type=keyword, script={source=if (doc['dept_code.keyword'].contains('999')) emit('match_999'); if (doc['dept_code.keyword'].contains('998')) emit('match_998');}}}}]"
    }
  },
  "status": 400
}

```

Not sure what I'm doing wrong. The script is simple enough.

Thanks for any assistance you can provide.

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [April 18, 2023, 3:35pm UTC](https://discuss.elastic.co/t/mapping-error-with-run-time-field/330231/2 "2023-04-18T15:35:19Z")

</div>

Have a look at [Update mapping API | Elasticsearch Guide [8.7] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-put-mapping.html)

I think you need to change your call to:

```auto
PUT test_index/_mapping
{
    "runtime": {
      "test_script": {
        "type": "keyword",
        "script": {
		  "source": "if (doc['dept_code.keyword'].contains('999')) emit('match_999'); if (doc['dept_code.keyword'].contains('998')) emit('match_998');"
		}
	  }
	}
}

```

---

<div class="post-metadata">

**Author:** ![JeremyP](https://avatars.discourse-cdn.com/v4/letter/j/43a26b/32.png) [@JeremyP](https://discuss.elastic.co/u/JeremyP)\
**Post date:** [April 18, 2023, 3:57pm UTC](https://discuss.elastic.co/t/mapping-error-with-run-time-field/330231/3 "2023-04-18T15:57:53Z")

</div>

Thanks! That fixed the error. The run time field is not showing up in the documents however, but perhaps I need to re-index the data?

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [April 18, 2023, 4:19pm UTC](https://discuss.elastic.co/t/mapping-error-with-run-time-field/330231/4 "2023-04-18T16:19:24Z")

</div>

A runtime field does not alter the \_source field. So you need to explicitly ask for this field (or query it).

---

<div class="post-metadata">

**Author:** ![JeremyP](https://avatars.discourse-cdn.com/v4/letter/j/43a26b/32.png) [@JeremyP](https://discuss.elastic.co/u/JeremyP)\
**Post date:** [April 18, 2023, 5:25pm UTC](https://discuss.elastic.co/t/mapping-error-with-run-time-field/330231/5 "2023-04-18T17:25:18Z")

</div>

Thank you. I'm very new to run time fields. I appreciate your help. There is an error in my painless script, so I'll add the fix to this and mark the issue resolved. The field name is incorrect.

```auto
PUT test_index/_mapping
{
    "runtime": {
      "test_script": {
        "type": "keyword",
        "script": {
		  "source": "if (doc['dept_code'].contains('999')) emit('match_999'); if (doc['dept_code'].contains('998')) emit('match_998');"
		}
	  }
	}
}

```

---

<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:** [May 16, 2023, 5:25pm UTC](https://discuss.elastic.co/t/mapping-error-with-run-time-field/330231/6 "2023-05-16T17:25:21Z")

</div>

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