From 3d37c9d7231cf9f35fc2bd9ba9f49570c29f31f1 Mon Sep 17 00:00:00 2001 From: Kofi Bartlett Date: Wed, 19 Mar 2025 13:02:22 +0900 Subject: [PATCH 1/2] Added explanation of sort order and default behavior --- .../elasticsearch/rest-apis/sort-search-results.md | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/docs/reference/elasticsearch/rest-apis/sort-search-results.md b/docs/reference/elasticsearch/rest-apis/sort-search-results.md index 4d76986a6beb2..16ba046601132 100644 --- a/docs/reference/elasticsearch/rest-apis/sort-search-results.md +++ b/docs/reference/elasticsearch/rest-apis/sort-search-results.md @@ -36,9 +36,9 @@ GET /my-index-000001/_search { "sort" : [ { "post_date" : {"order" : "asc", "format": "strict_date_optional_time_nanos"}}, - "user", { "name" : "desc" }, { "age" : "desc" }, + "user", "_score" ], "query" : { @@ -46,6 +46,12 @@ GET /my-index-000001/_search } } ``` +For multiple sort fields, each field defined in the sort array acts as a tie-breaker for the ones listed before it. The order of the fields matters: +* It first sorts by "post_date". +* If two documents have the same "post_date", it moves to the next sort field "name". +* If those are still the same, it checks "age", then "user", and so on. + +By default, Elasticsearch sorts `numeric` fields in descending order and `string` fields in ascending order unless you explicitly specify otherwise. All three formats shown in the example above are valid. Some make the sort order explicit, while others rely on Elasticsearch’s default behavior. ::::{note} `_doc` has no real use-case besides being the most efficient sort order. So if you don’t care about the order in which documents are returned, then you should sort by `_doc`. This especially helps when [scrolling](/reference/elasticsearch/rest-apis/paginate-search-results.md#scroll-search-results). From 18109783c4e244b2a2fa7efe7617a3a1f9e835a2 Mon Sep 17 00:00:00 2001 From: Kofi B Date: Sat, 12 Apr 2025 09:39:56 +0800 Subject: [PATCH 2/2] Update docs/reference/elasticsearch/rest-apis/sort-search-results.md Co-authored-by: Liam Thompson <32779855+leemthompo@users.noreply.github.com> --- .../elasticsearch/rest-apis/sort-search-results.md | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/docs/reference/elasticsearch/rest-apis/sort-search-results.md b/docs/reference/elasticsearch/rest-apis/sort-search-results.md index 16ba046601132..812fc68d54aff 100644 --- a/docs/reference/elasticsearch/rest-apis/sort-search-results.md +++ b/docs/reference/elasticsearch/rest-apis/sort-search-results.md @@ -46,10 +46,15 @@ GET /my-index-000001/_search } } ``` -For multiple sort fields, each field defined in the sort array acts as a tie-breaker for the ones listed before it. The order of the fields matters: -* It first sorts by "post_date". -* If two documents have the same "post_date", it moves to the next sort field "name". -* If those are still the same, it checks "age", then "user", and so on. +Order matters when defining multiple sort fields, because {{es}} attempts to sort on the first field in the array. Each subsequent field in the array is only used if the previous fields result in a tie. If documents have identical values across all specified sort fields, {{es}} uses the document ID as the final tie-breaker. + +Here's how the example query attempts to sort results: + +- First by `post_date` +- If `post_date` values are identical, sorts by `name` +- If both `post_date` and `name` are identical, sorts by `age` +- If the first three fields are identical, sorts by `user` +- If all previous fields are identical, sorts by `_score` By default, Elasticsearch sorts `numeric` fields in descending order and `string` fields in ascending order unless you explicitly specify otherwise. All three formats shown in the example above are valid. Some make the sort order explicit, while others rely on Elasticsearch’s default behavior.