# Script Based Sorting & query-based score

**URL:** <https://discuss.elastic.co/t/script-based-sorting-query-based-score/102677>\
**Category:** Elasticsearch\
**Created:** [October 4, 2017, 10:15am UTC](https://discuss.elastic.co/t/script-based-sorting-query-based-score/102677 "2017-10-04T10:15:25Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pyppe](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pyppe/32/3531_2.png) [@Pyppe](https://discuss.elastic.co/u/Pyppe)\
**Post date:** [October 4, 2017, 10:15am UTC](https://discuss.elastic.co/t/script-based-sorting-query-based-score/102677/1 "2017-10-04T10:15:25Z")

</div>

Hi. Is it possible to combine painless script-based sorting with default query-based score? What I'm after is this:

```auto
{
  "query": {
    "simple_query_string": {
      "fields": ["name"],
      "query": "foobar*"
    }
  },
  "sort": {
    "_script": {
      "type": "number",
      "script": "doc.someValue.value * 1.1 - ${SCORE_OF_THE_QUERY}",
      "order": "desc"
    }
  }
}

```

If not possible, any tips on how to handle this? Basically I need the similarity of the name to have certain importance but also take into account the numerical document values (`someValue` in this example).

Thanks!

---

<div class="post-metadata">

**Author:** ![rjernst](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rjernst/32/6363_2.png) [@rjernst](https://discuss.elastic.co/u/rjernst)\
**Post date:** [October 4, 2017, 3:00pm UTC](https://discuss.elastic.co/t/script-based-sorting-query-based-score/102677/2 "2017-10-04T15:00:29Z")

</div>

You should look at a [function score query with a script\_score](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-function-score-query.html#function-script-score). The original score of a document will be available to the script as `_score`.

---

<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:** [November 1, 2017, 3:09pm UTC](https://discuss.elastic.co/t/script-based-sorting-query-based-score/102677/3 "2017-11-01T15:09:06Z")

</div>

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