{"record":{"id":"8130e775a29f5d6b","repo":"elastic/elasticsearch","slug":"field-of-type-cannot-be-cast-to-8130e7","errorCode":null,"errorMessage":"field [{}] of type [{}] cannot be cast to [{}]","messagePattern":"field \\[(.+?)\\] of type \\[(.+?)\\] cannot be cast to \\[(.+?)\\]","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/RerouteProcessor.java","lineNumber":295,"sourceCode":"        public String resolve(IngestDocument ingestDocument) {\n            if (fieldReference != null) {\n                String value = ingestDocument.getFieldValue(fieldReference, String.class, true);\n                if (value == null) {\n                    value = getStringFieldValueInDottedNotation(ingestDocument);\n                }\n                return sanitizer.apply(value);\n            } else {\n                return value;\n            }\n        }\n\n        private String getStringFieldValueInDottedNotation(IngestDocument ingestDocument) {\n            String value = null;\n            Object valueObject = ingestDocument.getCtxMap().get(fieldReference);\n            if (valueObject instanceof String) {\n                value = (String) valueObject;\n            } else if (valueObject != null) {\n                throw new IllegalArgumentException(\n                    \"field [\"\n                        + fieldReference\n                        + \"] of type [\"\n                        + valueObject.getClass().getName()\n                        + \"] cannot be cast to [\"\n                        + String.class.getName()\n                        + \"]\"\n                );\n            }\n            return value;\n        }\n    }\n}\n","sourceCodeStart":277,"sourceCodeEnd":309,"githubUrl":"https://github.com/elastic/elasticsearch/blob/db6a809a667c081ca1dc7500389d26975573215f/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/RerouteProcessor.java#L277-L309","documentation":"Thrown by DataStreamValueSource.getStringFieldValueInDottedNotation when the resolved field-reference value in the document's context map is non-null but not a String. The reroute processor needs string values for data stream name components.","triggerScenarios":"The field referenced by '{{field}}' in a reroute type/dataset/namespace config resolves to a non-string object (e.g., a number, boolean, object, or list) in the document.","commonSituations":"Referencing a numeric field like port or count as a namespace. Referencing an object/array field. Field type changes in upstream data causing a previously-string field to become numeric.","solutions":["Ensure the referenced field contains a string value before the reroute processor runs.","Use a convert processor upstream to cast the field to string.","Reference a different field that is guaranteed to be a string."],"exampleFix":"// before: env_id is numeric\n{\n  \"convert\": { \"field\": \"env_id\", \"type\": \"string\" } },\n{\n  \"reroute\": { \"namespace\": \"{{env_id}}\" } }\n// after\n{\n  \"convert\": { \"field\": \"env_id\", \"type\": \"string\" } },\n{\n  \"reroute\": { \"namespace\": \"{{env_id}}\" } }","handlingStrategy":"type-guard","validationCode":"// Verify the referenced field is a String before reroute resolves it\nObject v = document.getFieldValue(\"env_id\", Object.class, true);\nif (v != null && !(v instanceof String)) {\n    // convert upstream or pick a different field\n}","typeGuard":"boolean isStringField(IngestDocument doc, String field) {\n    Object v = doc.getFieldValue(field, Object.class, true);\n    return v == null || v instanceof String;\n}","tryCatchPattern":"try {\n    // run reroute processor with field reference\n} catch (IllegalArgumentException e) {\n    if (e.getMessage().contains(\"cannot be cast to\")) {\n        // add a convert processor to cast the field to string upstream\n    } else { throw e; }\n}","preventionTips":["Add a convert processor to cast referenced fields to string before reroute.","Verify field types in the document mapping before referencing them in data-stream segments.","Avoid referencing numeric or object fields as data-stream components."],"tags":["elasticsearch","ingest-pipeline","reroute","data-stream","type-mismatch","validation"],"analyzedSha":"db6a809a667c081ca1dc7500389d26975573215f","analyzedAt":"2026-08-12T01:39:14.192Z","schemaVersion":2},"datasetVersion":"2026-08-12T08:17:17.861Z"}