# Same code on query script and sort script

**URL:** <https://discuss.elastic.co/t/same-code-on-query-script-and-sort-script/258402>\
**Category:** Elasticsearch\
**Created:** [December 11, 2020, 11:17am UTC](https://discuss.elastic.co/t/same-code-on-query-script-and-sort-script/258402 "2020-12-11T11:17:07Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![SergioRodenas](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sergiorodenas/32/79590_2.png) [@SergioRodenas](https://discuss.elastic.co/u/SergioRodenas)\
**Post date:** [December 11, 2020, 11:17am UTC](https://discuss.elastic.co/t/same-code-on-query-script-and-sort-script/258402/1 "2020-12-11T11:17:07Z")

</div>

Hi, how can I avoid this?

```
GET /repository/_search
{
  "query": {
    "script": {
      "script": {
        "source": "6371 * Math.acos( Math.cos((Math.PI / 180)*(39.9874542)) * Math.cos((Math.PI / 180)*(doc['destinationY'].value)) * Math.cos((Math.PI / 180)*(doc['destinationX'].value) - (Math.PI / 180)*(-0.0830824)) + Math.sin((Math.PI / 180)*(39.9874542)) * Math.sin((Math.PI / 180)*(doc['destinationY'].value))) < 12",
        "lang": "painless"
      }
    }
  },
  "sort": {
    "_script": {
      "type": "number",
      "script": {
        "lang": "painless",
        "source": "6371 * Math.acos( Math.cos((Math.PI / 180)*(39.9874542)) * Math.cos((Math.PI / 180)*(doc['destinationY'].value)) * Math.cos((Math.PI / 180)*(doc['destinationX'].value) - (Math.PI / 180)*(-0.0830824)) + Math.sin((Math.PI / 180)*(39.9874542)) * Math.sin((Math.PI / 180)*(doc['destinationY'].value)))"
      },
      "order": "asc"
    }
  }
}

```

Thanks.

---

<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:** [January 8, 2021, 11:17am UTC](https://discuss.elastic.co/t/same-code-on-query-script-and-sort-script/258402/2 "2021-01-08T11:17:19Z")

</div>

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