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

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 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

resolved:

wrong type: _doc needs to be just doc

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.

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