# ES sort in server end when given fileds are equal

**URL:** <https://discuss.elastic.co/t/es-sort-in-server-end-when-given-fileds-are-equal/294517>\
**Category:** Elasticsearch\
**Tags:** eql-elastic-query-language\
**Created:** [January 17, 2022, 2:10am UTC](https://discuss.elastic.co/t/es-sort-in-server-end-when-given-fileds-are-equal/294517 "2022-01-17T02:10:37Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![LIU\_HAI](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/liu_hai/32/79866_2.png) [@LIU\_HAI](https://discuss.elastic.co/u/LIU_HAI)\
**Post date:** [January 17, 2022, 2:10am UTC](https://discuss.elastic.co/t/es-sort-in-server-end-when-given-fileds-are-equal/294517/1 "2022-01-17T02:10:37Z")

</div>

Very simple case, I need to sort docs by one field, say, 'money'. What will the order be if the money of several docs are equal. I use filter to query cuz scores are not necessary. Here's my query example:

```auto
GET /index/_search
{
  "sort": [
    {
      "money": {
        "order": "desc"
      }
    }
  ],
  "bool": {
    "filter": [
      {
        "bool": {
          "must": [
            {
              "bool": {
                "must": [
                  {
                    "term": {
                      "creativeFinalStatus": {
                        "value": true,
                        "boost": 1
                      }
                    }
                  },
                  {
                    "term": {
                      "groupStatus": {
                        "value": true,
                        "boost": 1
                      }
                    }
                  }
                ],
                "adjust_pure_negative": true,
                "boost": 1
              }
            }
          ],
          "adjust_pure_negative": true,
          "boost": 1
        }
      }
    ],
    "adjust_pure_negative": true,
    "boost": 1
  }
}

```

---

<div class="post-metadata">

**Author:** ![Tomo\_M](https://avatars.discourse-cdn.com/v4/letter/t/848f3c/32.png) [@Tomo\_M](https://discuss.elastic.co/u/Tomo_M)\
**Post date:** [January 17, 2022, 3:16am UTC](https://discuss.elastic.co/t/es-sort-in-server-end-when-given-fileds-are-equal/294517/2 "2022-01-17T03:16:06Z")

</div>

Hi,

I have no idea about the order, but as that order is not specified, I don't think designs that rely on it are recommended even if there is in fact some order.

If you need some fixed order, you can use `_doc` as follows.

```auto
"sort": [
    {
      "money": {
        "order": "desc"
      }
    },
    "_doc"
  ]

```

`_doc` is reffered in the first NOTE of the document below.

> **[Sort search results | Elasticsearch Guide \[7.16\] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/current/sort-search-results.html#sort-search-results)**

---

<div class="post-metadata">

**Author:** ![LIU\_HAI](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/liu_hai/32/79866_2.png) [@LIU\_HAI](https://discuss.elastic.co/u/LIU_HAI)\
**Post date:** [January 17, 2022, 3:52am UTC](https://discuss.elastic.co/t/es-sort-in-server-end-when-given-fileds-are-equal/294517/3 "2022-01-17T03:52:03Z")

</div>

Thanks for your reply, what if I need to sort docs by money first and then a random order if docs' money are equal, do you have any idea of it?

---

<div class="post-metadata">

**Author:** ![Tomo\_M](https://avatars.discourse-cdn.com/v4/letter/t/848f3c/32.png) [@Tomo\_M](https://discuss.elastic.co/u/Tomo_M)\
**Post date:** [January 17, 2022, 4:15am UTC](https://discuss.elastic.co/t/es-sort-in-server-end-when-given-fileds-are-equal/294517/4 "2022-01-17T04:15:13Z")

</div>

[`random_score` for function score query](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-function-score-query.html#function-random) is enough for you? It returns random score based on internal Lucene doc ids as a source of randomness, which is renumbered by merges.

If you want differente results even for repeated same query, maybe you have to use some script field with painless.

> [@Random function in Painless?](https://discuss.elastic.co/t/random-function-in-painless/70280):
>
> Hello, What is the way to genereate a random value in Painless? Being able to draw one value from a set would be particularly great - I want to use it to add a new field to my mock data and I need a possibility to add one of the statuses ('Working', 'Archived', 'InReview') to my documents at random.

---

<div class="post-metadata">

**Author:** ![LIU\_HAI](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/liu_hai/32/79866_2.png) [@LIU\_HAI](https://discuss.elastic.co/u/LIU_HAI)\
**Post date:** [January 20, 2022, 11:04am UTC](https://discuss.elastic.co/t/es-sort-in-server-end-when-given-fileds-are-equal/294517/5 "2022-01-20T11:04:53Z")

</div>

After several trials, I've got the idea. Use random\_score and combine the sort as

```auto
{
  "query": {
    "function_score": {
      "query": {
        "bool": {
          "filter": [
            {
              "bool": {
                "must": [
                  {
                    "bool": {
                      "must": [
                        {
                          "term": {
                            "a": {
                              "value": true,
                              "boost": 1
                            }
                          }
                        },
                        {
                          "term": {
                            "b": {
                              "value": true,
                              "boost": 1
                            }
                          }
                        },
                        {
                          "term": {
                            "c": {
                              "value": true,
                              "boost": 1
                            }
                          }
                        }
                      ]
                    }
                  }
                ]
              }
            }
          ]
        }
      },
      "functions": [
        {
          "filter": {
            "match_all": {
              "boost": 1
            }
          },
          "random_score": {
            "seed": 111
          }
        }
      ],
      "score_mode": "multiply",
      "boost_mode": "replace",
      "max_boost": 3.4028235e+38,
      "boost": 1
    }
  },
  "_source": {
    "includes": [
      "bill"
    ],
    "excludes": []
  },
  "sort": [
    {
      "bill": {
        "order": "desc"
      }
    },
    {
      "_score": {
        "order": "desc"
      }
    }
  ]
}

```

---

<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:** [February 17, 2022, 11:05am UTC](https://discuss.elastic.co/t/es-sort-in-server-end-when-given-fileds-are-equal/294517/6 "2022-02-17T11:05:20Z")

</div>

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