# Runtime Field Convert String to Number

**URL:** <https://discuss.elastic.co/t/runtime-field-convert-string-to-number/326370>\
**Category:** Kibana\
**Created:** [February 23, 2023, 7:41pm UTC](https://discuss.elastic.co/t/runtime-field-convert-string-to-number/326370 "2023-02-23T19:41:18Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sparker22](https://avatars.discourse-cdn.com/v4/letter/s/db5fbb/32.png) [@sparker22](https://discuss.elastic.co/u/sparker22)\
**Post date:** [February 23, 2023, 7:41pm UTC](https://discuss.elastic.co/t/runtime-field-convert-string-to-number/326370/1 "2023-02-23T19:41:18Z")

</div>

Is there a way to convert string / text to a number in a runtime field script? We have data that is getting ingested into Elastic as keyword / text fields, but I need to convert that data at runtime from a string to number for visualization calculations.

---

<div class="post-metadata">

**Author:** ![sparker22](https://avatars.discourse-cdn.com/v4/letter/s/db5fbb/32.png) [@sparker22](https://discuss.elastic.co/u/sparker22)\
**Post date:** [February 23, 2023, 10:49pm UTC](https://discuss.elastic.co/t/runtime-field-convert-string-to-number/326370/2 "2023-02-23T22:49:05Z")

</div>

I have tried multiple iterations of this simple script, but it always errors out since the keyword is text, and the runtime field will be a Long. I get an error "class\_cast\_exception: cannot implicitly cast def [java.lang.String] to long"

```auto
if (doc['somekey_value.keyword'].size() > 0) {
    def value = doc['somekey_value.keyword'].value;
    if (value != null) {
	        emit(value);
	        return;
    }
}
else {
	emit(0)
}

```

---

<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 23, 2023, 10:49pm UTC](https://discuss.elastic.co/t/runtime-field-convert-string-to-number/326370/3 "2023-03-23T22:49:22Z")

</div>

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