# Scripted fields sort

**URL:** <https://discuss.elastic.co/t/scripted-fields-sort/243306>\
**Category:** Kibana\
**Created:** [July 31, 2020, 6:00am UTC](https://discuss.elastic.co/t/scripted-fields-sort/243306 "2020-07-31T06:00:04Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![111369](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/111369/32/73136_2.png) [@111369](https://discuss.elastic.co/u/111369)\
**Post date:** [July 31, 2020, 6:00am UTC](https://discuss.elastic.co/t/scripted-fields-sort/243306/1 "2020-07-31T06:00:04Z")

</div>

Hello,  
I've faced the following issue:

I've created a script field which returns String type

 ![script field](https://us1.discourse-cdn.com/elastic/original/3X/a/2/a2670a4bc9442692ae7d2e48311bf513f83181a3.jpeg)

But when I try to make sorting on this field by the Kibana means there's an error

> "Error loading data  
> [script\_exception] compile error"

I saw the reuqest, which was leaded to elastic:

```auto
{
  "version": true,
  "size": 500,
  "sort": [
    {
      "event_date": {
        "order": "desc",
        "unmapped_type": "boolean"
      }
    },
    {
      "_script": {
        "script": {
          "source": "if (doc.containsKey('message.keyword')) {\n//if (doc['message.keyword'].size() == 0) return 'field not found';\ndef path = doc['message.keyword'].value;\nString[] message = /\\n/.split(path);\nString param = 'IP = ';\n for (int i = 0; i < message.length; i++) {\n int index = message[i].indexOf(param);\n if (index > -1) {\n return message[i].substring(index+param.length());\n // return 'ее';\n }\n }\nreturn '';\n} else return '';",
          "lang": "painless"
        },
        "type": "number",
        "order": "asc"
      }
    }
  ],
.........
 }

```

And I've found out that Kibana gives this filed "type": "number"  
If i send the request directly to elastic, by changing "type": "number", to "type": "string" Then the request is running.

Could you please, expain, what's the problem?

---

<div class="post-metadata">

**Author:** ![matw](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/matw/32/13913_2.png) [@matw](https://discuss.elastic.co/u/matw)\
**Post date:** [July 31, 2020, 1:57pm UTC](https://discuss.elastic.co/t/scripted-fields-sort/243306/2 "2020-07-31T13:57:15Z")

</div>

Hi and welcome to our community!

This seems to be a bug, have a look here:

> <https://github.com/elastic/kibana/issues/73928>
>
> Kibana version:
> master, 7.x, user reported it started failing 7.7
> Describe the bug:
> Using a scripted field to sort by doesn't work when the...

Best,  
Matthias

---

<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:** [August 28, 2020, 1:57pm UTC](https://discuss.elastic.co/t/scripted-fields-sort/243306/3 "2020-08-28T13:57:18Z")

</div>

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