{"record":{"id":"525989953bf67153","repo":"elastic/elasticsearch","slug":"vector-dimensions-differ","errorCode":null,"errorMessage":"vector dimensions differ: {}!={}","messagePattern":"vector dimensions differ: (.+?)!=(.+?)","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"libs/simdvec/src/main/java/org/elasticsearch/simdvec/ESVectorUtil.java","lineNumber":421,"sourceCode":"     * @return the inner product of the two vectors\n     */\n    public static float ipFloatByte(float[] q, byte[] d) {\n        if (q.length != d.length) {\n            throw new IllegalArgumentException(\"vector dimensions incompatible: \" + q.length + \"!= \" + d.length);\n        }\n        return IMPL.ipFloatByte(q, d);\n    }\n\n    /**\n     * AND bit count computed over signed bytes.\n     * Copied from Lucene's XOR implementation\n     * @param a bytes containing a vector\n     * @param b bytes containing another vector, of the same dimension\n     * @return the value of the AND bit count of the two vectors\n     */\n    public static int andBitCount(byte[] a, byte[] b) {\n        if (a.length != b.length) {\n            throw new IllegalArgumentException(\"vector dimensions differ: \" + a.length + \"!=\" + b.length);\n        }\n        try {\n            return (int) BIT_COUNT_MH.invokeExact(a, b);\n        } catch (Throwable e) {\n            if (e instanceof Error err) {\n                throw err;\n            } else if (e instanceof RuntimeException re) {\n                throw re;\n            } else {\n                throw new RuntimeException(e);\n            }\n        }\n    }\n\n    /**\n     * Hamming similarity between two equal-length bit vectors, matching Lucene's\n     * {@code FlatBitVectorsScorer}: {@code (numBits - xorBitCount) / numBits}.\n     */","sourceCodeStart":403,"sourceCodeEnd":439,"githubUrl":"https://github.com/elastic/elasticsearch/blob/db6a809a667c081ca1dc7500389d26975573215f/libs/simdvec/src/main/java/org/elasticsearch/simdvec/ESVectorUtil.java#L403-L439","documentation":"Thrown by ESVectorUtil.andBitCount when the two signed-byte vectors differ in length. andBitCount computes the bitwise AND population count, which requires identical byte arrays. The guard runs before invoking the method-handle kernel that assumes equal lengths.","triggerScenarios":"Calling ESVectorUtil.andBitCount(byte[] a, byte[] b) with a.length != b.length. Occurs in scoring paths that compare two bit-packed vectors (e.g., hamming/and-based similarity) where the query and document bit encodings disagree on packed length.","commonSituations":"Two bit vectors indexed with different dims; one vector was truncated or zero-padded during ingestion; quantization/encoding path produced inconsistent byte counts; query and document came from different element_type=bit mappings.","solutions":["Ensure both byte arrays have identical length before calling.","Validate that both vectors share the same logical dimension and element_type.","Pad the shorter vector with zeros or reject the mismatch upstream rather than at the scorer."],"exampleFix":"// before\nint andCount = ESVectorUtil.andBitCount(a, b);\n// after\nif (a.length != b.length) {\n    throw new IllegalArgumentException(\"bit vectors differ: \" + a.length + \" vs \" + b.length);\n}\nint andCount = ESVectorUtil.andBitCount(a, b);","handlingStrategy":"validation","validationCode":"if (a.length != b.length) {\n    throw new IllegalArgumentException(\"bit vectors differ: \" + a.length + \" vs \" + b.length);\n}\nint andCount = ESVectorUtil.andBitCount(a, b);","typeGuard":"static boolean sameLength(byte[] a, byte[] b) {\n    return a != null && b != null && a.length == b.length;\n}","tryCatchPattern":null,"preventionTips":["Index bit vectors with a single canonical encoder to guarantee identical packed lengths.","Reject mismatched-length bit vectors at the scorer dispatcher rather than at the SIMD boundary.","Add length assertions in unit tests for any new bit-vector similarity."],"tags":["vector","dimension-mismatch","simd","bit-vector","and-bitcount"],"analyzedSha":"db6a809a667c081ca1dc7500389d26975573215f","analyzedAt":"2026-08-12T01:39:14.192Z","schemaVersion":2},"datasetVersion":"2026-08-12T06:17:24.410Z"}