elastic/elasticsearch · error · IllegalArgumentException

Value [${text}] is out of range for a short

Error message

Value [${text}] is out of range for a short

What it means

Thrown by AbstractXContentParser.parseShort() when the decimal string representation, parsed as a double, falls outside the short range [-32768, 32767]. The method parses the string as a double first (to handle scientific notation and decimals uniformly) and then range-checks against Short.MIN_VALUE and Short.MAX_VALUE before narrowing.

Source

Thrown at libs/x-content/src/main/java/org/elasticsearch/xcontent/support/AbstractXContentParser.java:130

    }

    protected abstract boolean doBooleanValue() throws IOException;

    @Override
    public short shortValue() throws IOException {
        return shortValue(DEFAULT_NUMBER_COERCE_POLICY);
    }

    /**
     * Parses a {@code short} from its decimal string representation, using the same semantics as the
     * document-indexing path. The value is first parsed as a {@code double} and then narrowed; values
     * outside [{@value Short#MIN_VALUE}, {@value Short#MAX_VALUE}] throw
     * {@link IllegalArgumentException}.
     */
    public static short parseShort(String text) {
        double doubleValue = Double.parseDouble(text);
        if (doubleValue < Short.MIN_VALUE || doubleValue > Short.MAX_VALUE) {
            throw new IllegalArgumentException("Value [" + text + "] is out of range for a short");
        }
        return (short) doubleValue;
    }

    @Override
    public short shortValue(boolean coerce) throws IOException {
        Token token = currentToken();
        if (token == Token.VALUE_STRING) {
            checkCoerceString(coerce, Short.class);
            XContentString numericText = optimizedText();
            checkNumericStringLength(numericText.stringLength());
            return parseShort(numericText.string());
        }
        short result = doShortValue();
        ensureNumberConversion(coerce, result, Short.class);
        return result;
    }

View on GitHub (pinned to db6a809a66)

Solutions

  1. Ensure the value is within [-32768, 32767].
  2. Change the field mapping from short to integer or long if values can exceed the short range.
  3. Validate the value range client-side before submitting.
  4. Round or clamp the value if it should always fit in a short.

Example fix

// before — value exceeds short range
{ "port": 99999 }

// after — value within short range
{ "port": 8080 }
Defensive patterns

Strategy: validation

Validate before calling

// Before sending, verify the value fits in short range
public static void validateShortRange(Object value, String fieldName) {
    if (value instanceof Number n) {
        short s = n.shortValue();
        if (s < Short.MIN_VALUE || s > Short.MAX_VALUE || n.doubleValue() < Short.MIN_VALUE || n.doubleValue() > Short.MAX_VALUE) {
            throw new IllegalArgumentException(fieldName + " value " + n + " is out of range for short [-32768, 32767]");
        }
    }
}

Try / catch

try {
    short value = parser.shortValue();
} catch (IllegalArgumentException e) {
    if (e.getMessage().contains("out of range for a short")) {
        throw new BadRequestException(e.getMessage());
    }
    throw e;
}

Prevention

When it happens

Trigger: Calling shortValue() on a parser whose current value is a numeric string or number exceeding 32767 or below -32768. For example, parsing a field value of "99999" or "-40000" as a short.

Common situations: Field mapped as short receiving values outside the valid range. Port numbers, small counters, or legacy fields that used short but now receive larger values. Client not validating range before sending.

Related errors


AI-assisted analysis of elastic/elasticsearch@db6a809a66 (2026-08-12). Data as JSON: /api/errors/b411cd5a963a5fb0. Report an issue: GitHub.