Skip to content

Commit 3d37c9d

Browse files
committed
Added explanation of sort order and default behavior
1 parent 29ac261 commit 3d37c9d

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

docs/reference/elasticsearch/rest-apis/sort-search-results.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,22 @@ GET /my-index-000001/_search
3636
{
3737
"sort" : [
3838
{ "post_date" : {"order" : "asc", "format": "strict_date_optional_time_nanos"}},
39-
"user",
4039
{ "name" : "desc" },
4140
{ "age" : "desc" },
41+
"user",
4242
"_score"
4343
],
4444
"query" : {
4545
"term" : { "user" : "kimchy" }
4646
}
4747
}
4848
```
49+
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:
50+
* It first sorts by "post_date".
51+
* If two documents have the same "post_date", it moves to the next sort field "name".
52+
* If those are still the same, it checks "age", then "user", and so on.
53+
54+
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.
4955

5056
::::{note}
5157
`_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).

0 commit comments

Comments
 (0)