Skip to content

[Search] sorting scripted string fields throws error #72655

@nickofthyme

Description

@nickofthyme

Kibana version:
7.5 and 7.8

Describe the bug:
Sorting a string type scripted field in discover throws an error.

Steps to reproduce:

  1. Add any sample data set
  2. Add a string type scripted field. Something simple like...
return 'Testing';
  1. Go to discover
  2. Add the scripted field to the Selected fields
  3. Try sorting the scripted field column

Expected behavior:
Sorting of scripted string fields

Screenshots (if relevant):
Screen Recording 2020-07-21 at 09 38 AM

Errors in browser console (if relevant):

search_phase_execution_exception
all shards failed

Error: Bad Request
    at Fetch.fetchResponse (http://localhost:5601/zhv/9007199254740991/bundles/core/core.entry.js:57468:13)
    at async interceptResponse (http://localhost:5601/zhv/9007199254740991/bundles/core/core.entry.js:57880:10)
    at async http://localhost:5601/zhv/9007199254740991/bundles/core/core.entry.js:57366:39

Provide logs and/or server output (if relevant):
ES request error

"root_cause" : [
  {
    "type" : "script_exception",
    "reason" : "compile error",
    "script_stack" : [
      "return 'Testing';",
      "       ^---- HERE"
    ],
    "script" : "return 'Testing';",
    "lang" : "painless",
    "position" : {
      "offset" : 7,
      "start" : 0,
      "end" : 17
    }
  }
],

Any additional context:

The discovered request appears to be setting the wrong type (i.e. number) for the scripted field which is causing the error. When changed to the correct type (i.e. string), the request is successful.

GET test/_search
{
  "version": true,
  "size": 500,
  "sort": [
    {
      "_score": {
        "order": "desc"
      }
    },
    {
      "_script": {
        "script": {
          "source": "return 'Testing';",
          "lang": "painless"
        },
-        "type": "number",
+        "type": "string",
        "order": "desc"
      }
    }
  ],
  "stored_fields": [
    "*"
  ],
  "script_fields": {
    "scriptTest": {
      "script": {
        "source": "if ( doc['prediction'].value == 3) {\nreturn 'High';\n} else {\nreturn 'Information Only';\n}",
        "lang": "painless"
      }
    },
    "myField": {
      "script": {
        "source": "return 'Testing';",
        "lang": "painless"
      }
    }
  },
  "docvalue_fields": [],
  "_source": {
    "excludes": []
  },
  "query": {
    "bool": {
      "must": [],
      "filter": [
        {
          "match_all": {}
        }
      ],
      "should": [],
      "must_not": []
    }
  },
  "highlight": {
    "pre_tags": [
      "@kibana-highlighted-field@"
    ],
    "post_tags": [
      "@/kibana-highlighted-field@"
    ],
    "fields": {
      "*": {}
    },
    "fragment_size": 2147483647
  }
}

Metadata

Metadata

Assignees

Labels

Feature:SearchQuerying infrastructure in KibanabugFixes for quality problems that affect the customer experience

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions