{"record":{"id":"5853f3717857b226","repo":"elastic/elasticsearch","slug":"member-variable-does-not-exist-for-date-field","errorCode":null,"errorMessage":"Member variable [{}] does not exist for date field [{}].","messagePattern":"Member variable \\[(.+?)\\] does not exist for date field \\[(.+?)\\]\\.","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"modules/lang-expression/src/main/java/org/elasticsearch/script/expression/DateField.java","lineNumber":53,"sourceCode":"    static final String AVERAGE_METHOD = \"avg\";\n    static final String MEDIAN_METHOD = \"median\";\n    static final String SUM_METHOD = \"sum\";\n    static final String COUNT_METHOD = \"count\";\n\n    // date-specific\n    static final String GET_YEAR_METHOD = \"getYear\";\n    static final String GET_MONTH_METHOD = \"getMonth\";\n    static final String GET_DAY_OF_MONTH_METHOD = \"getDayOfMonth\";\n    static final String GET_HOUR_OF_DAY_METHOD = \"getHourOfDay\";\n    static final String GET_MINUTES_METHOD = \"getMinutes\";\n    static final String GET_SECONDS_METHOD = \"getSeconds\";\n\n    static DoubleValuesSource getVariable(IndexFieldData<?> fieldData, String fieldName, String variable) {\n        return switch (variable) {\n            case VALUE_VARIABLE -> new FieldDataValueSource(fieldData, MultiValueMode.MIN);\n            case EMPTY_VARIABLE -> new EmptyMemberValueSource(fieldData);\n            case LENGTH_VARIABLE -> new CountMethodValueSource(fieldData);\n            default -> throw new IllegalArgumentException(\n                \"Member variable [\" + variable + \"] does not exist for date field [\" + fieldName + \"].\"\n            );\n        };\n    }\n\n    static DoubleValuesSource getMethod(IndexFieldData<?> fieldData, String fieldName, String method) {\n        return switch (method) {\n            case GETVALUE_METHOD -> new FieldDataValueSource(fieldData, MultiValueMode.MIN);\n            case ISEMPTY_METHOD -> new EmptyMemberValueSource(fieldData);\n            case SIZE_METHOD -> new CountMethodValueSource(fieldData);\n            case MINIMUM_METHOD -> new FieldDataValueSource(fieldData, MultiValueMode.MIN);\n            case MAXIMUM_METHOD -> new FieldDataValueSource(fieldData, MultiValueMode.MAX);\n            case AVERAGE_METHOD -> new FieldDataValueSource(fieldData, MultiValueMode.AVG);\n            case MEDIAN_METHOD -> new FieldDataValueSource(fieldData, MultiValueMode.MEDIAN);\n            case SUM_METHOD -> new FieldDataValueSource(fieldData, MultiValueMode.SUM);\n            case COUNT_METHOD -> new CountMethodValueSource(fieldData);\n            case GET_YEAR_METHOD -> new DateMethodValueSource(fieldData, MultiValueMode.MIN, method, Calendar.YEAR);\n            case GET_MONTH_METHOD -> new DateMethodValueSource(fieldData, MultiValueMode.MIN, method, Calendar.MONTH);","sourceCodeStart":35,"sourceCodeEnd":71,"githubUrl":"https://github.com/elastic/elasticsearch/blob/db6a809a667c081ca1dc7500389d26975573215f/modules/lang-expression/src/main/java/org/elasticsearch/script/expression/DateField.java#L35-L71","documentation":"Thrown by DateField.getVariable (lang-expression script engine) when an expression references an unsupported member VARIABLE on a date field. For date fields the only valid variables are 'value', 'empty', and 'length'. Any other variable name in doc['<dateField>'].<var> reaches the default branch.","triggerScenarios":"Using an expression script (lang: expression) like doc['timestamp'].year or doc['timestamp'].foo where 'timestamp' is a date field. Date FIELD variables are limited to value/empty/length; rich date accessors live on the date OBJECT (doc['timestamp'].date.<var>, see DateObject).","commonSituations":"Confusing date-field variables with date-object variables; copying a painless-style accessor into an expression script; assuming getYear-style accessors work as variables rather than methods.","solutions":["Use a supported variable: doc['timestamp'].value, doc['timestamp'].empty, or doc['timestamp'].length.","For date components, use the date object: doc['timestamp'].date.year (variable) or doc['timestamp'].date.getYear (method).","Switch to a supported method on the field: doc['timestamp'].getYear, .getMonth, etc."],"exampleFix":"// before (expression script)\ndoc['timestamp'].year\n\n// after\ndoc['timestamp'].date.year   // or doc['timestamp'].getYear","handlingStrategy":"validation","validationCode":"// Validate expression variable before using it on a date field\nSet<String> OK = Set.of(\"value\", \"empty\", \"length\");\nif (!OK.contains(variable)) {\n    throw new IllegalArgumentException(\"Unsupported date field variable: \" + variable + \"; use .date.<var> for components\");\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["For date fields, only value/empty/length are valid variables.","Use doc['field'].date.<component> for date parts, not doc['field'].<component>.","Keep a cheat-sheet of expression field vs date-object vocabularies."],"tags":["elasticsearch","lang-expression","expression-script","date-field","validation"],"analyzedSha":"db6a809a667c081ca1dc7500389d26975573215f","analyzedAt":"2026-08-12T01:39:14.192Z","schemaVersion":2},"datasetVersion":"2026-08-12T08:17:17.861Z"}