You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[FEATURE] Stabilize preferred SQL implicit sorting method #3064
Is your feature request related to a problem?
Coming from #3061, there seems to be a few places in-code where we implicitly sort records when ORDER BY clauses aren't supplied by a user. Currently we use _doc, a per-shard incrementing counter for each indexed document. This works fine for single-shard indices, but causes unstable sorting in some cases when there are multiple shards involved. As a workaround, I'm adding a secondary sort by _id in cases where it's known that this causes issues, but this has a large performance impact (~4x latency in local testing).
What solution would you like?
There should be a single source of truth for what the "implicit sorting" key is, and that implicit sort should be guaranteed unique per-document. This can probably go in a helper library somewhere, I'm just not quite acquainted enough with the plugin to know where that would be.
What alternatives have you considered?
Sort just by ID, throw out _doc: this might be faster than trying to sort by two keys, but throws out all ordering.
Sort by @timestamp: Not guaranteed to be present on all indices anymore.
Some other field that bundles the rough chronology of _doc with information on shards: doesn't yet exist to my knowledge.
Do you have any additional context?
See the comments on #3061
Activity
Swiddis commentedon Oct 9, 2024
Example reproducer script for the issue (before 3061 fix):
ss4o_logs.json
{ "index_patterns": ["ss4o_logs-*-*"], "data_stream": {}, "template": { "mappings": { "_meta": { "version": "1.0.0", "catalog": "observability", "type": "logs", "component": "log", "correlations": [ { "field": "spanId", "foreign-schema": "traces", "foreign-field": "spanId" }, { "field": "traceId", "foreign-schema": "traces", "foreign-field": "traceId" } ] }, "_source": { "enabled": true }, "dynamic_templates": [ { "resources_map": { "mapping": { "type": "keyword" }, "path_match": "resource.*" } }, { "attributes_map": { "mapping": { "type": "keyword" }, "path_match": "attributes.*" } }, { "instrumentation_scope_attributes_map": { "mapping": { "type": "keyword" }, "path_match": "instrumentationScope.attributes.*" } } ], "properties": { "severity": { "properties": { "number": { "type": "long" }, "text": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } } } }, "attributes": { "type": "object", "properties": { "data_stream": { "properties": { "dataset": { "ignore_above": 128, "type": "keyword" }, "namespace": { "ignore_above": 128, "type": "keyword" }, "type": { "ignore_above": 56, "type": "keyword" } } } } }, "body": { "type": "text" }, "@timestamp": { "type": "date" }, "observedTimestamp": { "type": "date" }, "observerTime": { "type": "alias", "path": "observedTimestamp" }, "traceId": { "ignore_above": 256, "type": "keyword" }, "spanId": { "ignore_above": 256, "type": "keyword" }, "schemaUrl": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } }, "instrumentationScope": { "properties": { "name": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 128 } } }, "version": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } }, "dropped_attributes_count": { "type": "integer" }, "schemaUrl": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } } } }, "event": { "properties": { "domain": { "ignore_above": 256, "type": "keyword" }, "name": { "ignore_above": 256, "type": "keyword" }, "source": { "ignore_above": 256, "type": "keyword" }, "category": { "ignore_above": 256, "type": "keyword" }, "type": { "ignore_above": 256, "type": "keyword" }, "kind": { "ignore_above": 256, "type": "keyword" }, "result": { "ignore_above": 256, "type": "keyword" }, "exception": { "properties": { "message": { "ignore_above": 1024, "type": "keyword" }, "type": { "ignore_above": 256, "type": "keyword" }, "stacktrace": { "type": "text" } } } } } } }, "settings": { "index": { "mapping": { "total_fields": { "limit": 10000 } }, "refresh_interval": "5s", "number_of_shards": 4 } } }, "version": 1, "_meta": { "description": "Simple Schema For Observability", "catalog": "observability", "type": "logs", "correlations": [ { "field": "spanId", "foreign-schema": "traces", "foreign-field": "spanId" }, { "field": "traceId", "foreign-schema": "traces", "foreign-field": "traceId" } ] } }Swiddis commentedon Oct 9, 2024
Actually, it looks like_idalready is chronological by default (-> time based UUID). It probably isn't a bad idea to use that directly instead of trying to hack_doc.Tested it, using just
_idis actually significantly slower than using both_docand_id.LantaoJin commentedon Aug 11, 2025
I think our problem could be resolved after opensearch-project/OpenSearch#18924 merged.