{"record":{"id":"af14e7632c4ecec0","repo":"elastic/elasticsearch","slug":"field-is-null-cannot-sort","errorCode":null,"errorMessage":"field [{}] is null, cannot sort.","messagePattern":"field \\[(.+?)\\] is null, cannot sort\\.","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/SortProcessor.java","lineNumber":92,"sourceCode":"    String getField() {\n        return field;\n    }\n\n    SortOrder getOrder() {\n        return order;\n    }\n\n    String getTargetField() {\n        return targetField;\n    }\n\n    @Override\n    @SuppressWarnings(\"unchecked\")\n    public IngestDocument execute(IngestDocument document) {\n        List<? extends Comparable<Object>> list = document.getFieldValue(field, List.class);\n\n        if (list == null) {\n            throw new IllegalArgumentException(\"field [\" + field + \"] is null, cannot sort.\");\n        }\n\n        List<? extends Comparable<Object>> copy = new ArrayList<>(list);\n\n        if (order.equals(SortOrder.ASCENDING)) {\n            Collections.sort(copy);\n        } else {\n            Collections.sort(copy, Collections.reverseOrder());\n        }\n\n        document.setFieldValue(targetField, copy);\n        return document;\n    }\n\n    @Override\n    public String getType() {\n        return TYPE;\n    }","sourceCodeStart":74,"sourceCodeEnd":110,"githubUrl":"https://github.com/elastic/elasticsearch/blob/db6a809a667c081ca1dc7500389d26975573215f/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/SortProcessor.java#L74-L110","documentation":"Thrown by SortProcessor.execute when the configured field resolves to a null value (the field is absent or explicitly null). The processor cannot sort a null list, so it fails fast. Note this processor has no ignore_missing option.","triggerScenarios":"Running the sort processor on a document where the configured source field is absent or null. Unlike split/user-agent processors, sort does not support ignore_missing, so any null input is fatal.","commonSituations":"Optional list fields that are sometimes absent. Documents where the field was removed by an upstream processor. Schema-optional array fields.","solutions":["Wrap the sort processor in an on-failure handler to tolerate missing fields.","Add a conditional check (script/grok processor) to only route documents with the field present to this pipeline.","Use a different processor pattern that supports ignore_missing, or pre-populate the field with an empty list."],"exampleFix":"// before: sort fails when 'events' is absent\n{\n  \"sort\": { \"field\": \"events\", \"target_field\": \"events_sorted\", \"order\": \"asc\" }\n}\n// after: guard with a conditional pipeline branch or on-failure\n{\n  \"pipeline\": {\n    \"processors\": [\n      { \"if\": \"ctx.events != null\", \"sort\": { \"field\": \"events\", \"target_field\": \"events_sorted\", \"order\": \"asc\" } }\n    ]\n  }\n}","handlingStrategy":"validation","validationCode":"// Guard the sort processor with a conditional check\n// In pipeline JSON, use an 'if' condition:\n// { \"if\": \"ctx.events != null\", \"sort\": { ... } }\nObject val = document.getFieldValue(\"events\", Object.class, true);\nif (val == null) {\n    // skip sort; field is absent\n}","typeGuard":"boolean hasSortabbleList(IngestDocument doc, String field) {\n    Object v = doc.getFieldValue(field, Object.class, true);\n    return v instanceof List;\n}","tryCatchPattern":"try {\n    // run sort processor\n} catch (IllegalArgumentException e) {\n    if (e.getMessage().contains(\"is null, cannot sort\")) {\n        // add an 'if' condition or on-failure handler\n    } else { throw e; }\n}","preventionTips":["Wrap the sort processor in an 'if' condition checking the field is non-null.","Use an on-failure handler since sort has no ignore_missing option.","Pre-populate optional list fields with empty lists upstream."],"tags":["elasticsearch","ingest-pipeline","sort","validation","null-handling"],"analyzedSha":"db6a809a667c081ca1dc7500389d26975573215f","analyzedAt":"2026-08-12T01:39:14.192Z","schemaVersion":2},"datasetVersion":"2026-08-12T06:17:24.410Z"}