ES sort in server end when given fileds are equal

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:

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
  }
}

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.

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

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

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?

random_score for function score query 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.

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

{
  "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"
      }
    }
  ]
}
1 Like

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