# Script sorting retun type exception

**URL:** <https://discuss.elastic.co/t/script-sorting-retun-type-exception/246660>\
**Category:** Elasticsearch\
**Created:** [August 27, 2020, 4:13pm UTC](https://discuss.elastic.co/t/script-sorting-retun-type-exception/246660 "2020-08-27T16:13:40Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Muhammad\_Zeeshan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/muhammad_zeeshan/32/95304_2.png) [@Muhammad\_Zeeshan](https://discuss.elastic.co/u/Muhammad_Zeeshan)\
**Post date:** [August 27, 2020, 4:13pm UTC](https://discuss.elastic.co/t/script-sorting-retun-type-exception/246660/1 "2020-08-27T16:13:41Z")

</div>

I'm trying to sort query results based on script. As nested \_script has type number, for missing value return null throwing exception and return 0 or -1 are useless. Here is error message "Cannot cast null to a primitive type [double]"

---

<div class="post-metadata">

**Author:** ![Muhammad\_Zeeshan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/muhammad_zeeshan/32/95304_2.png) [@Muhammad\_Zeeshan](https://discuss.elastic.co/u/Muhammad_Zeeshan)\
**Post date:** [August 27, 2020, 4:17pm UTC](https://discuss.elastic.co/t/script-sorting-retun-type-exception/246660/2 "2020-08-27T16:17:31Z")

</div>

sorting as

"sort": [{  
"\_script": {  
"nested": {  
"path": "currency\_prices"  
},  
"type": "number",  
"order": "asc",

```
        "script": {
          "lang": "painless",
          "inline": "if(doc['currency_prices.currency'].value != params.currency){ return null; }else{ return doc['currency_prices.price'].value }",
          "params": {
            "currency": "USD"
          }
      }
    }
			}]

```

any help please?

---

<div class="post-metadata">

**Author:** ![mayya](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mayya/32/83147_2.png) [@mayya](https://discuss.elastic.co/u/mayya)\
**Post date:** [August 27, 2020, 9:49pm UTC](https://discuss.elastic.co/t/script-sorting-retun-type-exception/246660/3 "2020-08-27T21:49:47Z")

</div>

> [@Muhammad\_Zeeshan](#):
>
> ```auto
> "inline": "if(doc['currency_prices.currency'].value != params.currency){ return null; }else{ return doc['currency_prices.price'].value }",
> 
> ```

Your script should return some proper numeric values, not null. You should redesign your script to return some proper numeric value, e.g `0`.

If you need to filter some nested documents and exclude them from sorting you can use a filter:

```js
"nested" : {
        "path" : "my_nested",
        "filter": {
          "match": {"currency_prices.currency": "USD"}
        }
}

```

I would redesign a sort part of your request into something like this:

```js
"sort": {
    "_script": {
      "order": "asc",
      "nested": {
        "path": "currency_prices",
        "filter": {
          "match": {
            "currency_prices.currency": "USD"
          }
        }
      },
      "type": "number",
      "script": {
        "source": "doc['currency_prices.price'].value"
      }
    }
  }

```

---

<div class="post-metadata">

**Author:** ![Muhammad\_Zeeshan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/muhammad_zeeshan/32/95304_2.png) [@Muhammad\_Zeeshan](https://discuss.elastic.co/u/Muhammad_Zeeshan)\
**Post date:** [August 28, 2020, 5:00pm UTC](https://discuss.elastic.co/t/script-sorting-retun-type-exception/246660/4 "2020-08-28T17:00:25Z")

</div>

thanks @mayya i used mode: max for sorting which worked exactly as expected. But, i guess my approach is a bit expensive in terms of response time. Will try your script for sure. Also i will be very thankful if you can tell me the better one. thanks again

here is my script version

"sort": {  
"\_script": {  
"nested": {  
"path": "currency\_prices"  
},  
"type": "number",  
"order": "asc",  
"mode" : "max",  
"script": {  
"lang": "painless",  
"inline": "def price = -1; if (doc['currency\_prices.currency'].value == params.currency) { price = doc['currency\_prices.price'].value;} price;",  
"params": {  
"currency": "CAD"  
}  
}  
}  
}

---

<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:** [September 25, 2020, 5:00pm UTC](https://discuss.elastic.co/t/script-sorting-retun-type-exception/246660/5 "2020-09-25T17:00:31Z")

</div>

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