elastic/elasticsearch · error · IndexOutOfBoundsException

ValuesSource array index {ordinal} out of bounds

Error message

ValuesSource array index {ordinal} out of bounds

What it means

Thrown by NumericArrayValuesSource.getField when the requested `ordinal` exceeds the bounds of the values array. NOTE: the guard `ordinal > names.length` is an off-by-one bug — it should be `>=` — so the explicit message is only emitted for ordinals strictly greater than the length; an ordinal exactly equal to names.length passes the guard and throws a raw ArrayIndexOutOfBoundsException from values[ordinal] on the next line. This indicates a mismatch between the number of fields configured on the array aggregation and the ordinal requested by the aggregator.

Source

Thrown at modules/aggregations/src/main/java/org/elasticsearch/aggregations/metric/ArrayValuesSource.java:39

 */
public abstract class ArrayValuesSource<VS extends ValuesSource> {
    protected final MultiValueMode multiValueMode;
    protected String[] names;
    protected VS[] values;

    public static class NumericArrayValuesSource extends ArrayValuesSource<ValuesSource.Numeric> {
        public NumericArrayValuesSource(Map<String, ValuesSource.Numeric> valuesSources, MultiValueMode multiValueMode) {
            super(valuesSources, multiValueMode);
            if (valuesSources != null) {
                this.values = valuesSources.values().toArray(new ValuesSource.Numeric[0]);
            } else {
                this.values = new ValuesSource.Numeric[0];
            }
        }

        public DoubleValues getField(final int ordinal, LeafReaderContext ctx) throws IOException {
            if (ordinal > names.length) {
                throw new IndexOutOfBoundsException("ValuesSource array index " + ordinal + " out of bounds");
            }
            return multiValueMode.select(values[ordinal].doubleValues(ctx));
        }
    }

    public static class BytesArrayValuesSource extends ArrayValuesSource<ValuesSource.Bytes> {
        public BytesArrayValuesSource(Map<String, ValuesSource.Bytes> valuesSources, MultiValueMode multiValueMode) {
            super(valuesSources, multiValueMode);
            this.values = valuesSources.values().toArray(new ValuesSource.Bytes[0]);
        }

        public Object getField(final int ordinal, LeafReaderContext ctx) throws IOException {
            return values[ordinal].bytesValues(ctx);
        }
    }

    public static class GeoPointValuesSource extends ArrayValuesSource<ValuesSource.GeoPoint> {
        public GeoPointValuesSource(Map<String, ValuesSource.GeoPoint> valuesSources, MultiValueMode multiValueMode) {

View on GitHub (pinned to db6a809a66)

Solutions

  1. Ensure the valuesSources map passed to the ArrayValuesSource contains every field the aggregator will request by ordinal.
  2. In custom aggregations, keep the ordinal-to-field mapping in sync with the registered fields.
  3. If you are an end user (not a plugin author), report this with the aggregation config — it usually indicates a bug in the aggregation implementation.
  4. Validate field counts in your aggregator's build step before reading leaves.
Defensive patterns

Strategy: validation

Validate before calling

if (ordinal < 0 || ordinal >= values.length) {
  throw new IndexOutOfBoundsException("ordinal " + ordinal + " not in [0," + values.length + ")");
}

Prevention

When it happens

Trigger: An internal mismatch where a multi-field metric aggregation (e.g. matrix_stats, percentile ranks over multiple fields, or a custom array-source aggregator) requests a field ordinal that was not registered. Reached when the valuesSources map provided to NumericArrayValuesSource has fewer entries than the ordinal index passed to getField.

Common situations: Custom plugins implementing ArrayValuesSource-backed aggregations with inconsistent field/ordinal bookkeeping. Bugs in aggregation wiring after a refactor that changed the field set. Edge cases where a field is omitted from configuration but the aggregator still asks for it.

Related errors


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