{"record":{"id":"fa021c3449ec79b4","repo":"elastic/elasticsearch","slug":"for-input-string-stringvalue","errorCode":null,"errorMessage":"For input string: \"${stringValue}\"","messagePattern":"For input string: \"(.+?)\"","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"libs/x-content/src/main/java/org/elasticsearch/xcontent/support/AbstractXContentParser.java","lineNumber":239,"sourceCode":"            // long can have a maximum of 19 digits - any more than that cannot be a long\n            // the scale is stored as the negation, so negative scale -> big number\n            if (bigDecimalValue.scale() < -19) {\n                throw new IllegalArgumentException(\"Value [\" + stringValue + \"] is out of range for a long\");\n            }\n            // large scale -> very small number\n            if (bigDecimalValue.scale() > 19) {\n                if (coerce) {\n                    bigIntegerValue = BigInteger.ZERO;\n                } else {\n                    throw new ArithmeticException(\"Number has a decimal part\");\n                }\n            } else {\n                bigIntegerValue = coerce ? bigDecimalValue.toBigInteger() : bigDecimalValue.toBigIntegerExact();\n            }\n        } catch (ArithmeticException e) {\n            throw new IllegalArgumentException(\"Value [\" + stringValue + \"] has a decimal part\");\n        } catch (NumberFormatException e) {\n            throw new IllegalArgumentException(\"For input string: \\\"\" + stringValue + \"\\\"\");\n        }\n\n        if (bigIntegerValue.compareTo(LONG_MAX_VALUE_AS_BIGINTEGER) > 0 || bigIntegerValue.compareTo(LONG_MIN_VALUE_AS_BIGINTEGER) < 0) {\n            throw new IllegalArgumentException(\"Value [\" + stringValue + \"] is out of range for a long\");\n        }\n\n        assert bigIntegerValue.longValueExact() <= Long.MAX_VALUE; // asserting that no ArithmeticException is thrown\n        return bigIntegerValue.longValue();\n    }\n\n    @Override\n    public long longValue() throws IOException {\n        return longValue(DEFAULT_NUMBER_COERCE_POLICY);\n    }\n\n    @Override\n    public long longValue(boolean coerce) throws IOException {\n        Token token = currentToken();","sourceCodeStart":221,"sourceCodeEnd":257,"githubUrl":"https://github.com/elastic/elasticsearch/blob/db6a809a667c081ca1dc7500389d26975573215f/libs/x-content/src/main/java/org/elasticsearch/xcontent/support/AbstractXContentParser.java#L221-L257","documentation":"Thrown by AbstractXContentParser.toLong when the input string is not a parseable number at all — Long.parseLong fails and then `new BigDecimal(stringValue)` throws NumberFormatException. The catch block wraps it as an IllegalArgumentException. This is the catch-all for entirely non-numeric or malformed numeric input during long conversion.","triggerScenarios":"Calling longValue/longValue(coerce) on a VALUE_STRING token containing text like \"abc\", \"12-34\", \"\", \"NaN\", \"0x1F\", \"1,000\" (with separators), or a value with leading/trailing whitespace. Indexing a non-numeric string into a long-mapped field.","commonSituations":"Schema-less ingest where a field is auto-detected as long on early documents and later receives textual values. Locale-specific number formatting (commas as thousands separators) passed verbatim. Misconfigured ingest pipeline converting a date string into a field mapped as long. Log payloads mixing units (\"42ms\") into numeric fields.","solutions":["Inspect the offending value in the error message and correct the producer to emit a clean integer string.","Add an ingest pipeline with a script/grok processor to normalize the value before indexing.","Re-map the field to keyword if the values are genuinely non-numeric identifiers or labels.","If hex/scientific formats are expected, pre-convert them in your application code before submitting to Elasticsearch."],"exampleFix":"// before: long field receives \"1,000\"\n// after: strip separators in an ingest pipeline before the long field\nPUT _ingest/pipeline/normalize { \"processors\": [ { \"script\": { \"source\": \"ctx.amount = ctx.amount.replaceAll('[^0-9-]', '')\" } } ] }","handlingStrategy":"validation","validationCode":"static boolean isNumeric(String s) {\n  if (s == null || s.isEmpty()) return false;\n  try { new java.math.BigDecimal(s); return true; }\n  catch (NumberFormatException e) { return false; }\n}","typeGuard":null,"tryCatchPattern":"try { parser.longValue(coerce); }\ncatch (IllegalArgumentException e) {\n  if (e.getMessage().startsWith(\"For input string:\")) { /* log and reject the bad value */ }\n  else throw e;\n}","preventionTips":["Sanitize locale-formatted numbers (strip thousands separators) before indexing.","Use ingest pipelines to normalize numeric input.","Detect numeric fields via sampling and re-map to keyword if values are non-numeric."],"tags":["xcontent","parsing","numeric","long","input-validation"],"analyzedSha":"db6a809a667c081ca1dc7500389d26975573215f","analyzedAt":"2026-08-12T01:39:14.192Z","schemaVersion":2},"datasetVersion":"2026-08-12T06:17:24.410Z"}