Reputation: 9546
Does Elasticsearch have a defined default sort order for filter
queries if none is specified? Or is it more like an RDBMS without an order by
- i.e. nothing is guaranteed?
From my experiments I appear to be getting my documents back in order of their id
- which is exactly what I want - I am just wondering if this can be relied on?
Upvotes: 0
Views: 1744
Reputation: 976
No, the order cannot be relied on (in ES 7.12.1 at least)!
I've tested in a production environment, where we have a cluster with multiple shards and replicas and even running the simplest query like this returns results in different order on every few requests:
POST /my_index/_search
One way to ensure the same order is to add order by _id
, which seems to bring a small performance hit with it.
Also, I know it's not related to this question, but keep in mind that if you do have scoring in your query and you still get random results, even after adding an order by _id
, the problem is that the scores are randomly generated in a cluster environment. This problem can be solved with adding a parameter to you query:
POST /my_index/_search?search_type=dfs_query_then_fetch
More info and possible solutions can be found here: https://www.elastic.co/guide/en/elasticsearch/reference/7.17/consistent-scoring.html
Upvotes: 0
Reputation: 217274
When you only have filters (i.e. no scoring) and no explicit sort clause, then the documents are returned in index order, i.e. implicitly sorted by the special field named _doc
.
Index order simply means the sequential order in which the documents have been indexed.
If your id
is sequential and you've indexed your documents in the same order as your id
, then what you observe is correct, but it might not always be the case.
Upvotes: 2