# Return docs with specific value to the top and order the rest by rand

**URL:** <https://discuss.elastic.co/t/return-docs-with-specific-value-to-the-top-and-order-the-rest-by-rand/153041>\
**Category:** Elasticsearch\
**Created:** [October 18, 2018, 4:06pm UTC](https://discuss.elastic.co/t/return-docs-with-specific-value-to-the-top-and-order-the-rest-by-rand/153041 "2018-10-18T16:06:38Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Oscar\_Sanchez\_Sanche](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/oscar_sanchez_sanche/32/36678_2.png) [@Oscar\_Sanchez\_Sanche](https://discuss.elastic.co/u/Oscar_Sanchez_Sanche)\
**Post date:** [October 18, 2018, 4:06pm UTC](https://discuss.elastic.co/t/return-docs-with-specific-value-to-the-top-and-order-the-rest-by-rand/153041/1 "2018-10-18T16:06:38Z")

</div>

Hi there, I would like ES to return documents that have a field `is_sticky` with value `true` to the top on a match all query. The rest of the results that have a `false` value should be ordered by rand.

Currently, I got the is\_sticky docs return to the top by sending this request:

```
{
    "query": {
        "function_score": {
          "query": { "match_all": {} },
          "functions": [
              {
                  "filter": { "match": { "is_sticky": "true" } },
                  "weight": 2
              }
          ]
        }
    }
}

```

The rest of the docs with value `is_sticky: false` are not returned by rand (probably because i did not used `random_score`), but I'm not really sure. How could I accomplish this?

Thanks in advance, and hope you have a great day.

---

<div class="post-metadata">

**Author:** ![mayya](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mayya/32/83147_2.png) [@mayya](https://discuss.elastic.co/u/mayya)\
**Post date:** [November 13, 2018, 10:05pm UTC](https://discuss.elastic.co/t/return-docs-with-specific-value-to-the-top-and-order-the-rest-by-rand/153041/2 "2018-11-13T22:05:47Z")

</div>

Hi there! This query should help you with your task:

```auto
{
  "query": {
    "function_score": {
      "query": { 
        "match_all": {}
      },
      "functions": [
        {
          "filter": { "match": { "is_sticky": "true" } },
          "weight": 2
        },
        {
          "random_score" : {}
        }
      ],
      "boost_mode" : "replace",
      "score_mode" : "sum"
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![Oscar\_Sanchez\_Sanche](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/oscar_sanchez_sanche/32/36678_2.png) [@Oscar\_Sanchez\_Sanche](https://discuss.elastic.co/u/Oscar_Sanchez_Sanche)\
**Post date:** [November 19, 2018, 6:07pm UTC](https://discuss.elastic.co/t/return-docs-with-specific-value-to-the-top-and-order-the-rest-by-rand/153041/3 "2018-11-19T18:07:41Z")

</div>

Hi mayya,

Appreciate your reply. I'll test this out and see how it goes.

Thank you very much!

---

<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:** [December 17, 2018, 6:07pm UTC](https://discuss.elastic.co/t/return-docs-with-specific-value-to-the-top-and-order-the-rest-by-rand/153041/4 "2018-12-17T18:07:44Z")

</div>

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