# Is there a better way to perform a "range sort" or "bucket sort" query?

**URL:** <https://discuss.elastic.co/t/is-there-a-better-way-to-perform-a-range-sort-or-bucket-sort-query/145279>\
**Category:** Elasticsearch\
**Created:** [August 21, 2018, 5:06am UTC](https://discuss.elastic.co/t/is-there-a-better-way-to-perform-a-range-sort-or-bucket-sort-query/145279 "2018-08-21T05:06:33Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Morriaty](https://avatars.discourse-cdn.com/v4/letter/m/8e8cbc/32.png) [@Morriaty](https://discuss.elastic.co/u/Morriaty)\
**Post date:** [August 21, 2018, 5:06am UTC](https://discuss.elastic.co/t/is-there-a-better-way-to-perform-a-range-sort-or-bucket-sort-query/145279/1 "2018-08-21T05:06:33Z")

</div>

Hi, we're using ES 5.5.2, say we got this scenario:

One user searched "Christmas presents", and the results were:

```auto
Christmas present A _score: 35.12, price: 100
Christmas present B _score: 34.56, price: 120
Christmas present C _score: 34.33, price: 100
Valentine present A _score: 12.11, price: 80
Valentine present B _score: 12.02, price: 80
Valentine present C _score: 11.45, price: 70

```

If the user clicked "sort by price asc", the first page's results may be not relevant enough.  
We suppose it is better to sort `price` in each `_score` bucket [0, 10), [10, 20), [20, 30), ...

So we changed the search query to:

```json
{
  "from": 0,
  "aggs": {},
  "query": {
    # .....
  },
  "sort": {
    "_script": {
      "type": "number",
      "script": {
        "lang": "painless",
        "inline": "doc['price'].value + Math.floor(_score / 10.0) * 100000"
      },
      "order": "asc"
    }
  },
  "track_scores": true
}

```

Or is there a better way to perform like this? Thank you for help!

---

<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 18, 2018, 5:06am UTC](https://discuss.elastic.co/t/is-there-a-better-way-to-perform-a-range-sort-or-bucket-sort-query/145279/2 "2018-09-18T05:06:34Z")

</div>

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