# Unable to sort results using script\_score function

**URL:** https://discuss.elastic.co/t/unable-to-sort-results-using-script-score-function/194946
**Category:** Elasticsearch
**Created:** [August 13, 2019, 3:19am UTC](https://discuss.elastic.co/t/unable-to-sort-results-using-script-score-function/194946 "2019-08-13T03:19:07Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![aniruddha.raje](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/aniruddha.raje/32/52148_2.png) [@aniruddha.raje](https://discuss.elastic.co/u/aniruddha.raje)
#### Post date: [August 13, 2019, 3:19am UTC](https://discuss.elastic.co/t/unable-to-sort-results-using-script-score-function/194946/1 "2019-08-13T03:19:08Z")

</div>

I have a field called **price** in my elasticsearch index whose values I need to convert before sorting -

```
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 4,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "products",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "prod_id" : 1,
          "currency" : "USD",
          "price" : 1
        }
      },
      {
        "_index" : "products",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 1.0,
        "_source" : {
          "prod_id" : 2,
          "currency" : "INR",
          "price" : 60
        }
      },
      {
        "_index" : "products",
        "_type" : "_doc",
        "_id" : "3",
        "_score" : 1.0,
        "_source" : {
          "prod_id" : 3,
          "currency" : "EUR",
          "price" : 2
        }
      },
      {
        "_index" : "products",
        "_type" : "_doc",
        "_id" : "5",
        "_score" : 1.0,
        "_source" : {
          "prod_id" : 5,
          "currency" : "MYR",
          "price" : 1
        }
      }
    ]
  }
}

```

As the `currency field` for each product is different,  
  
I plan to **convert each product's price into USD** ,  
  
Using the `script_score` function, as recommended here -

> <https://stackoverflow.com/questions/32058673/elastic-search-sort-preprocessing/32063301#32063301>

**I tried the following query -**

```
GET products/_search
{
	"query": {
		"function_score": {
			"query": {
				"match_all": {}
			},
			"functions": [{
				"script_score": {
					"script": {
						"params": {
							"USD": 1,
							"SGD": 0.72,
							"MYR": 0.24,
							"INR": 0.014,
							"EUR": 1.12
						},
						"source": "doc['price'].value * params.doc['currency']"
					}
				}
			}]
		}
	},
	"sort": [
    {
      "_score": {
        "order": "desc"
      }
    }
  ]
}

```

**I'm getting an error -**

```
{
  "error": {
    "root_cause": [
      {
        "type": "script_exception",
        "reason": "runtime error",
        "script_stack": [
          "doc['price'].value * params.doc['currency']",
          " ^---- HERE"
        ],
        "script": "doc['price'].value * params.doc['currency']",
        "lang": "painless"
      }
    ],
    "type": "search_phase_execution_exception",
    "reason": "all shards failed",
    "phase": "query",
    "grouped": true,
    "failed_shards": [
      {
        "shard": 0,
        "index": "products",
        "node": "5-fQ27BhSUKycVJ2SwyH4A",
        "reason": {
          "type": "script_exception",
          "reason": "runtime error",
          "script_stack": [
            "doc['price'].value * params.doc['currency']",
            " ^---- HERE"
          ],
          "script": "doc['price'].value * params.doc['currency']",
          "lang": "painless",
          "caused_by": {
            "type": "class_cast_exception",
            "reason": "Cannot apply [*] operation to types [java.lang.Long] and [org.elasticsearch.index.fielddata.ScriptDocValues.Strings]."
          }
        }
      }
    ]
  },
  "status": 400
}

```

**Expected product sequence -**

1. **prod\_id 3** , \_score 2.24 = `2(price) * 1.12 (USD multiplier)`  
2. **prod\_id 1** , \_score 1 = `1(price) * 1 (USD multiplier) = 1`  
3. **prod\_id 2** , \_score 0.84 = `60(price) * 0.014 (USD multiplier)`  
4. **prod\_id 5** , \_score 0.24 = `1(price) * 0.24 (USD multiplier)`  

**Index Mapping -**

```
{
  "mapping": {
    "properties": {
      "currency": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword",
            "ignore_above": 256
          }
        },
        "fielddata": true
      },
      "price": {
        "type": "long"
      },
      "prod_id": {
        "type": "long"
      }
    }
  }
}

```

**Can someone please help for this use case query?**

---

<div class="post-metadata">

### Author: ![abdon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abdon/32/9195_2.png) [@abdon](https://discuss.elastic.co/u/abdon)
#### Post date: [August 13, 2019, 8:49am UTC](https://discuss.elastic.co/t/unable-to-sort-results-using-script-score-function/194946/2 "2019-08-13T08:49:26Z")

</div>

Your Painless script is not quite correct. Try this:

```auto
doc['price'].value * params[doc['currency.keyword'].value]

```

---

<div class="post-metadata">

### Author: ![lrynek](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/lrynek/32/53435_2.png) [@lrynek](https://discuss.elastic.co/u/lrynek)
#### Post date: [September 2, 2019, 10:27am UTC](https://discuss.elastic.co/t/unable-to-sort-results-using-script-score-function/194946/3 "2019-09-02T10:27:57Z")

</div>

Apart of the main topic here, please look at the proposed new feature to enable precise retrieval of computed function value in `function score` query `_explanation`. Thanks!  
👉 [https://github.com/elastic/elasticsearch/issues/46007](https://github.com/elastic/elasticsearch/issues/46007)

---

<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 30, 2019, 10:27am UTC](https://discuss.elastic.co/t/unable-to-sort-results-using-script-score-function/194946/4 "2019-09-30T10:27:58Z")

</div>

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