# Scripted Fields - Not Working with String because of "Set fielddata=true"

**URL:** https://discuss.elastic.co/t/scripted-fields-not-working-with-string-because-of-set-fielddata-true/167864
**Category:** Kibana
**Created:** [February 11, 2019, 1:25pm UTC](https://discuss.elastic.co/t/scripted-fields-not-working-with-string-because-of-set-fielddata-true/167864 "2019-02-11T13:25:38Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Datakids](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/datakids/32/39622_2.png) [@Datakids](https://discuss.elastic.co/u/Datakids)
#### Post date: [February 11, 2019, 1:25pm UTC](https://discuss.elastic.co/t/scripted-fields-not-working-with-string-because-of-set-fielddata-true/167864/1 "2019-02-11T13:25:38Z")

</div>

Hi @ all,  
I tried following:

Language: painless  
Type: String

Using this fields in my index creates errors:

```
if (doc['PS_Status'].value == 'Failed') { 
return "Failed";
} else if (doc['PS_Status'].value == 'OK') { 
return "OK"; 
}

```

Following works fine:

```
if (doc['Gross_Value'].value > 1000) { 
return "<1000 €" 
} else if (doc['Gross_Value'].value > 1000) { 
return ">1000 €" 
}

```

What should I do to get results with string based queries?

Error:

> blabla "caused\_by":{"type":"illegal\_argument\_exception","reason":"Fielddata is disabled on text fields by default. Set fielddata=true on [PS\_Status] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory. Alternatively use a keyword field instead."}}}]},"status":500}...

Yeah do it:

```
PUT date/_mapping/_doc
{
  "properties": {
    "PS_Status": { 
      "type": "text",
      "fielddata": true
    }
  }
}

```

doh'

```
{
  "error": {
    "root_cause": [
      {
        "type": "illegal_argument_exception",
        "reason": "Rejecting mapping update to [date] as the final mapping would have more than 1 type: [_doc, doc]"
      }
    ],
    "type": "illegal_argument_exception",
    "reason": "Rejecting mapping update to [date] as the final mapping would have more than 1 type: [_doc, doc]"
  },
  "status": 400
}

```

hmm, failed, lost etc...

No any example even the this [Verwendung von Painless in geskripteten Kibana-Feldern | Elastic Blog](https://www.elastic.co/de/blog/using-painless-kibana-scripted-fields) tells me why I can't do this!  
There's no mention of what to do anywhere when running in this trouble!

Thanks for help  
Regards

---

<div class="post-metadata">

### Author: ![Datakids](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/datakids/32/39622_2.png) [@Datakids](https://discuss.elastic.co/u/Datakids)
#### Post date: [February 11, 2019, 1:34pm UTC](https://discuss.elastic.co/t/scripted-fields-not-working-with-string-because-of-set-fielddata-true/167864/2 "2019-02-11T13:34:56Z")

</div>

resolved:

wrong type: \_doc needs to be just doc

---

<div class="post-metadata">

### Author: ![Joe\_Fleming](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/joe_fleming/32/3561_2.png) [@Joe\_Fleming](https://discuss.elastic.co/u/Joe_Fleming)
#### Post date: [February 12, 2019, 5:58pm UTC](https://discuss.elastic.co/t/scripted-fields-not-working-with-string-because-of-set-fielddata-true/167864/3 "2019-02-12T17:58:57Z")

</div>

I was just going to follow up and point that out. In 6.x, you can only have a single type per index, and `doc` is the one being used. Your request was trying to add a `_doc` type, hence the error about multiple types.

Also mentioned in that error message, you can avoid enabling fielddata (and save a bunch of memory) by indexing both the analyzed value (like you are now) and the non-analyzed value. Than you can use the non-analyzed value in the script without having to enable fielddata.

---

<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 12, 2019, 5:58pm UTC](https://discuss.elastic.co/t/scripted-fields-not-working-with-string-because-of-set-fielddata-true/167864/4 "2019-03-12T17:58:58Z")

</div>

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