# Scripted field cannot cast from String to Double

**URL:** <https://discuss.elastic.co/t/scripted-field-cannot-cast-from-string-to-double/272967>\
**Category:** Elasticsearch\
**Tags:** painless\
**Created:** [May 13, 2021, 10:42pm UTC](https://discuss.elastic.co/t/scripted-field-cannot-cast-from-string-to-double/272967 "2021-05-13T22:42:38Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Matt\_Klaver](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/matt_klaver/32/76875_2.png) [@Matt\_Klaver](https://discuss.elastic.co/u/Matt_Klaver)\
**Post date:** [May 13, 2021, 10:42pm UTC](https://discuss.elastic.co/t/scripted-field-cannot-cast-from-string-to-double/272967/1 "2021-05-13T22:42:38Z")

</div>

I'm trying to do some scripting on a field we have... when I preview the results it works fine, but when I go to "Discover" I get the following error: `[class_cast_exception] class_cast_exception: Cannot cast from [java.lang.String] to [double].`  
Note: my scripted field is named `org_name`.

```auto
def substr = '';
def index = 0;
def url = doc['url.full'].value;
index = url.indexOf(".");
while(index != -1) {
        substr = url.substring(url.indexOf(".")+1);
        if(substr.indexOf(".") != -1){
            url = substr;
            index = substr.indexOf(".");
        } else {
            return url.substring(0, index);
        }
    }
} else {
    return "";
}

```

The goal of this script is to take something like `https://www.google.com` and turn it into `google` without the use of regex. The "preview results" shows me the following:

```auto
  "_id": "{my doc ID goes here}",
  "org_name": [
   "robinhood"
  ]
 }

```

but when I go into the Discover tab I get the error above.

---

<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:** [June 10, 2021, 10:43pm UTC](https://discuss.elastic.co/t/scripted-field-cannot-cast-from-string-to-double/272967/2 "2021-06-10T22:43:23Z")

</div>

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