{"record":{"id":"a008fb902d7d0f44","repo":"elastic/elasticsearch","slug":"can-t-advance-to-doc-using-a008fb","errorCode":null,"errorMessage":"Can't advance to doc using {}","messagePattern":"Can't advance to doc using (.+?)","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"modules/lang-expression/src/main/java/org/elasticsearch/script/expression/ExpressionNumberSortScript.java","lineNumber":81,"sourceCode":"                    return true;\n                }\n            });\n\n            @Override\n            public double execute() {\n                try {\n                    return values.doubleValue();\n                } catch (Exception exception) {\n                    throw new GeneralScriptException(\"Error evaluating \" + exprScript, exception);\n                }\n            }\n\n            @Override\n            public void setDocument(int d) {\n                try {\n                    values.advanceExact(d);\n                } catch (IOException e) {\n                    throw new IllegalStateException(\"Can't advance to doc using \" + exprScript, e);\n                }\n            }\n        };\n    }\n\n    @Override\n    public boolean needs_score() {\n        return needsScores;\n    }\n\n}\n","sourceCodeStart":63,"sourceCodeEnd":93,"githubUrl":"https://github.com/elastic/elasticsearch/blob/db6a809a667c081ca1dc7500389d26975573215f/modules/lang-expression/src/main/java/org/elasticsearch/script/expression/ExpressionNumberSortScript.java#L63-L93","documentation":"Thrown inside ExpressionNumberSortScript.setDocument(d) when values.advanceExact(d) raises IOException, wrapped in IllegalStateException with the expression source text. This is the sort-script counterpart of error 1220 — the DoubleValues cursor cannot be positioned at the requested document in the current segment.","triggerScenarios":"Executing a sort with an expression script on a shard where the underlying segment reader encounters an I/O error advancing to document d. Distinct from 1221, which fires during value retrieval; 1222 fires during cursor positioning.","commonSituations":"Segment reader closed mid-search (e.g., shard refresh or merge during query); corrupted doc-values structures on disk; reader lifecycle mismanagement in custom plugins that wrap expression scripts.","solutions":["Verify shard and index health: GET _cluster/health, GET <index>/_segments — look for corrupted or unusually sized segments.","Retry the query after refreshing: POST <index>/_refresh — if the reader was stale, a new reader resolves it.","If corruption is confirmed, restore the index from a snapshot or reindex into a new index.","Check for concurrent force-merge or similar operations and schedule them outside search hours."],"exampleFix":"// No code-level fix. This is an I/O-level runtime failure.\n// Diagnostic:\n// before: sort by expression fails intermittently\nGET my-index/_segments\n// after: if corruption found, restore:\nPOST _snapshot/my_backup/snap_1/_restore\n{ \"indices\": \"my-index\", \"rename_pattern\": \"my-index\", \"rename_replacement\": \"my-index-restored\" }","handlingStrategy":"try-catch","validationCode":"// Before running a sort query, verify shard health\nGET _cluster/health?level=shards&wait_for_status=yellow","typeGuard":null,"tryCatchPattern":"try {\n    sortScript.setDocument(docId);\n} catch (IllegalStateException e) {\n    logger.warn(\"Sort expression advanceExact failed for doc [{}]: {}\", docId, e.getMessage(), e);\n    // This is typically an I/O or reader lifecycle issue — fail the shard request\n    throw e;\n}","preventionTips":["Do not use index readers after they have been closed by a refresh or merge.","Monitor disk health on data nodes; I/O errors often surface here first.","Avoid running force_merge concurrently with heavy sort queries.","Ensure shard relocation completes before querying if the cluster is under load."],"tags":["expression","script","sort","io","lucene"],"backgroundTag":null,"analyzedSha":"db6a809a667c081ca1dc7500389d26975573215f","analyzedAt":"2026-08-12T01:39:14.192Z","schemaVersion":2},"datasetVersion":"2026-08-12T13:17:24.610Z"}