Repository navigation
LSM_VECTOR "quantization": "BINARY" scores the query against a bit ? median : 0 reconstruction instead of a Hamming distance, so a record queried by its own vector is ranked behind other records (first hit for 43 of 100 queries, recall@10 0.19 at efSearch 200) #8960
Description
With BINARY quantization the index stores one bit per dimension (at or above the vector's own median, or below it) and, at search time, reconstructs each stored vector as bit ? median : 0 (dequantizeFromBinary) and scores that with the index's similarity against the unquantized query. The javadoc of VectorQuantizationType.BINARY says it "Uses Hamming distance instead of cosine/euclidean"; no Hamming distance is computed on this path, and nothing reranks on the stored floats afterwards.
The reconstruction loses the sign of the data. For a vector centred on zero the median is a small number of either sign, and a negative median turns every set bit into a negative component. A = [0.9, -0.1, -0.2, -0.8] has median -0.15 and is reconstructed as [-0.15, -0.15, 0, 0], whose cosine to A is -0.46: queried by its own vector, A is reported at distance 1.46 (the far side of the sphere) and ranks behind B = [0.9, 0.5, 0.1, 0.2], whose true distance is 0.55. Under a Hamming distance over the bits, A would be at distance 0 from its own query. On 2,000 random 64-dimension vectors, the same queries find their own record first 100 times out of 100 with no quantization and with INT8, and 43 times with BINARY; recall@10 against exact cosine is 0.19 at efSearch 200. In a separate run over 20,000 random 64-dimension vectors at efSearch 200, BINARY gave recall@10 0.16 under COSINE, 0.008 under EUCLIDEAN, and 0.03 under DOT_PRODUCT (unit vectors), where INT8 gave 0.98, 0.98, and 0.97. The existing test binaryQuantizationBasicSearch asserts only that the search does not throw.
Repro
javac -cp 'lib/*' -d cls BinaryQuantizationSelfMatch.java
java -Xmx8g -XX:+UseCompactObjectHeaders --add-modules=jdk.incubator.vector --enable-native-access=ALL-UNNAMED -cp 'lib/*:cls' BinaryQuantizationSelfMatch /tmp/binary-db
lib/ holds the engine jar and its dependencies. On Temurin 21 the same command without -XX:+UseCompactObjectHeaders.
import com.arcadedb.database.*;
import com.arcadedb.index.*;
import com.arcadedb.index.vector.*;
import com.arcadedb.utility.Pair;
import java.util.*;
public class BinaryQuantizationSelfMatch {
public static void main(String[] args) {
DatabaseFactory factory = new DatabaseFactory(args[0]);
if (factory.exists())
factory.open().drop();
Database db = factory.create();
// 1) one record queried by its own vector
for (String quantization : List.of("NONE", "BINARY")) {
String type = "Small" + quantization;
createType(db, type, 4, quantization);
db.transaction(() -> {
db.newDocument(type).set("name", "A", "vector", new float[] { 0.9f, -0.1f, -0.2f, -0.8f }).save();
db.newDocument(type).set("name", "B", "vector", new float[] { 0.9f, 0.5f, 0.1f, 0.2f }).save();
});
List<String> hits = new ArrayList<>();
for (Pair<RID, Float> hit : index(db, type).findNeighborsFromVector(new float[] { 0.9f, -0.1f, -0.2f, -0.8f }, 2, 100))
hits.add(hit.getFirst().asDocument().getString("name") + " at " + hit.getSecond());
System.out.println(quantization + ", query = A's own vector: " + hits);
}
// 2) 2000 random 64-dimension vectors, each index queried by 100 of its own stored vectors
Random random = new Random(42);
List<float[]> vectors = new ArrayList<>();
for (int i = 0; i < 2000; i++) {
float[] v = new float[64];
for (int d = 0; d < 64; d++)
v[d] = random.nextFloat() * 2 - 1;
vectors.add(v);
}
for (String quantization : List.of("NONE", "INT8", "BINARY")) {
String type = "Doc" + quantization;
createType(db, type, 64, quantization);
List<RID> rids = new ArrayList<>();
db.transaction(() -> {
for (float[] v : vectors)
rids.add(db.newDocument(type).set("vector", v).save().getIdentity());
});
int selfFirst = 0, hitsInExactTop10 = 0;
for (int q = 0; q < 100; q++) {
float[] query = vectors.get(q * 20);
List<Pair<RID, Float>> hits = index(db, type).findNeighborsFromVector(query, 10, 200);
if (hits.get(0).getFirst().equals(rids.get(q * 20)))
selfFirst++;
List<Integer> order = new ArrayList<>();
for (int i = 0; i < vectors.size(); i++)
order.add(i);
order.sort(Comparator.comparingDouble(i -> -cosine(query, vectors.get(i))));
Set<RID> exactTop10 = new HashSet<>();
for (int i = 0; i < 10; i++)
exactTop10.add(rids.get(order.get(i)));
for (Pair<RID, Float> hit : hits)
if (exactTop10.contains(hit.getFirst()))
hitsInExactTop10++;
}
System.out.printf("%-6s: the queried record is the first hit for %d of 100 queries, recall@10 %.2f (efSearch 200)%n", quantization,
selfFirst, hitsInExactTop10 / 1000.0);
}
db.close();
}
static void createType(Database db, String type, int dimensions, String quantization) {
db.command("sql", "CREATE DOCUMENT TYPE " + type + " BUCKETS 1");
db.command("sql", "CREATE PROPERTY " + type + ".vector ARRAY_OF_FLOATS");
db.command("sql", "CREATE INDEX ON " + type + " (vector) LSM_VECTOR METADATA { \"dimensions\": " + dimensions
+ ", \"similarity\": \"COSINE\", \"quantization\": \"" + quantization + "\" }");
}
static LSMVectorIndex index(Database db, String type) {
return (LSMVectorIndex) ((TypeIndex) db.getSchema().getIndexByName(type + "[vector]")).getIndexesOnBuckets()[0];
}
static double cosine(float[] a, float[] b) {
double dot = 0, na = 0, nb = 0;
for (int i = 0; i < a.length; i++) {
dot += a[i] * b[i];
na += a[i] * a[i];
nb += b[i] * b[i];
}
return dot / Math.sqrt(na * nb);
}
}Output on main at bda510bca0, embedded, Temurin 25 (with compact object headers):
NONE, query = A's own vector: [A at 0.0, B at 0.5505092]
BINARY, query = A's own vector: [B at 0.5381198, A at 1.4618802]
NONE : the queried record is the first hit for 100 of 100 queries, recall@10 1.00 (efSearch 200)
INT8 : the queried record is the first hit for 100 of 100 queries, recall@10 0.99 (efSearch 200)
BINARY: the queried record is the first hit for 43 of 100 queries, recall@10 0.19 (efSearch 200)
Temurin 21 prints the same lines except 42 of 100 for BINARY (the graph build is not deterministic, so the BINARY count moves by one or two between runs).
Versions affected: main bda510bca0, main 417314c18 (2026-09-17), and 26.9.1 print the same first two lines ([B at 0.5381198, A at 1.4618802] for BINARY) and 45 and 43 of 100 for BINARY with recall@10 0.19 and 0.18.
Also on main at 9319980f60 (2026-10-02, after #8947), both JDKs: the same two lines, and 43 (Temurin 25) and 44 (Temurin 21) of 100 for BINARY with recall@10 0.19.
Expected: a record queried by its own vector comes back first, as it does with NONE and INT8, at a distance that does not place it on the far side of the query. Either the bits are compared with a Hamming distance (the query binarized by the same rule), as the javadoc says, or the reconstruction keeps the sign (for example the mean of the components above and below the median instead of median and 0), with a rerank on the stored float vectors if the intent is "approximate search with reranking".
Activity
Merge pull request #8969 from ArcadeData/fix/8960-8961-8962-8963-vect…
lvca commentedon Oct 2, 2026
Confirmed and fixed in #8969 (26.10.1). A
BINARYquantized index lost the sign of each vector, so a record could rank behind unrelated ones even when queried by its own vector. Vectors are now compared by their sign pattern and a search reranks the candidates on the stored vectors, so the queried record comes first at distance 0. Searches on aBINARYindex do extra record reads per query, and existingBINARYindexes should be rebuilt (REBUILD INDEX) for full recall.❤️ Thanks for helping make ArcadeDB better! ArcadeDB is free and open source, sustained by its community. If it's useful to you or your company, please consider becoming a sponsor to keep development going.
Vector and unique index fixes: ArcadeData#8960 ArcadeData#8961 Arcade…
tae898 commentedon Oct 3, 2026
Verified on main at
66541f57d9(which carries the #8969 mergea556c94456and the #8971 merge66541f57d9) againstbda510bca0, the build this was filed on, with the issue's reproduction, on Temurin 25 with-XX:+UseCompactObjectHeadersand on Temurin 21:BINARYquantizationbda510bca066541f57d9[B at 0.538, A at 1.462][A at 0.0, B at 0.551]NONE and INT8 are unchanged (100 of 100, recall 1.00 and 0.99). The Python bindings suite is green on a wheel built with that engine. Thanks!
Vector and unique index fixes: #8960 #8961 #8962 #8963