# Shuffle sorted documents

**URL:** <https://discuss.elastic.co/t/shuffle-sorted-documents/335255>\
**Category:** Elasticsearch\
**Created:** [June 5, 2023, 7:28pm UTC](https://discuss.elastic.co/t/shuffle-sorted-documents/335255 "2023-06-05T19:28:51Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Novel\_one](https://avatars.discourse-cdn.com/v4/letter/n/46a35a/32.png) [@Novel\_one](https://discuss.elastic.co/u/Novel_one)\
**Post date:** [June 5, 2023, 7:28pm UTC](https://discuss.elastic.co/t/shuffle-sorted-documents/335255/1 "2023-06-05T19:28:51Z")

</div>

Hi,  
I want to create a promotional box in my marketplace, with the top rated articles. I dont want always to be the same articles, so i want them be shuffled a little by multiplying the article avg rate by a random number between (+1,1 - 0,9).  
Query : match-all, so scorings comes as nulls.  
Thanks in advance!

---

<div class="post-metadata">

**Author:** ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)\
**Post date:** [June 6, 2023, 12:21am UTC](https://discuss.elastic.co/t/shuffle-sorted-documents/335255/2 "2023-06-06T00:21:48Z")

</div>

Welcome to our community! 😃

If you aren't scoring them and you need to use the match all query then you need to do this in your code.

---

<div class="post-metadata">

**Author:** ![Novel\_one](https://avatars.discourse-cdn.com/v4/letter/n/46a35a/32.png) [@Novel\_one](https://discuss.elastic.co/u/Novel_one)\
**Post date:** [June 6, 2023, 8:24am UTC](https://discuss.elastic.co/t/shuffle-sorted-documents/335255/3 "2023-06-06T08:24:00Z")

</div>

do what??  
Thanks!

---

<div class="post-metadata">

**Author:** ![Novel\_one](https://avatars.discourse-cdn.com/v4/letter/n/46a35a/32.png) [@Novel\_one](https://discuss.elastic.co/u/Novel_one)\
**Post date:** [June 6, 2023, 10:10am UTC](https://discuss.elastic.co/t/shuffle-sorted-documents/335255/4 "2023-06-06T10:10:22Z")

</div>

I think I got it:

```auto
GET /search-service/_search
{
  "query": {
    "match_all": {}
  },
  "sort": [
    {
      "_script": {
        "script": {
          "lang": "painless",
          "source": "def randomFactor = (Math.random() * 0.2) + 0.9; return doc['service_valuation_media'].value * randomFactor;"
        },
        "type": "number",
        "order": "desc"
      }
    }
  ],
  "_source": true
}

```

---

<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:** [July 4, 2023, 10:11am UTC](https://discuss.elastic.co/t/shuffle-sorted-documents/335255/5 "2023-07-04T10:11:07Z")

</div>

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