Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions package-lock.json
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not against this change but probably should not happen in this pr.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's a byproduct of whoever committed the code-guidelines not forming the lockfile quite completely, it appears to be safe to me. Nothing was upgraded, it just added the checksum and specific resolved source.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 14 additions & 3 deletions resources/indexes/HierarchicalNavigableSmallWorld.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { cosineDistance, euclideanDistance } from './vector.ts';
import { cosineDistance, euclideanDistance, dotProductDistance } from './vector.ts';
import { FLOAT32_OPTIONS } from 'msgpackr';
import { loggerWithTag } from '../../utility/logging/logger.ts';
import { ClientError } from '../../utility/errors/hdbError.js';
Expand Down Expand Up @@ -52,7 +52,12 @@ export class HierarchicalNavigableSmallWorld {
// (we would actually like to use float16 if it were available)
this.indexStore.encoder.useFloat32 = FLOAT32_OPTIONS.ALWAYS;
}
this.distance = options?.distance === 'euclidean' ? euclideanDistance : cosineDistance;
this.distance =
options?.distance === 'euclidean'
? euclideanDistance
: options?.distance === 'dotProduct'
? dotProductDistance
: cosineDistance;
if (options) {
// allow all the HNSW parameters to be configured/tuned
if (options.M !== undefined) {
Expand Down Expand Up @@ -457,6 +462,7 @@ export class HierarchicalNavigableSmallWorld {
let distanceFunction: (a: number[], b: number[]) => number;
if (distance === 'cosine') distanceFunction = cosineDistance;
else if (distance === 'euclidean') distanceFunction = euclideanDistance;
else if (distance === 'dotProduct') distanceFunction = dotProductDistance;
else if (distance) throw new ClientError('Unknown distance function');
else distanceFunction = this.distance;
if (!target) throw new ClientError('A target vector must be provided for an HNSW query');
Expand Down Expand Up @@ -637,7 +643,12 @@ export class HierarchicalNavigableSmallWorld {

let distanceFunction = this.distance;
if (sortDefinition.type)
distanceFunction = sortDefinition.distance === 'euclidean' ? euclideanDistance : cosineDistance;
distanceFunction =
sortDefinition.distance === 'euclidean'
? euclideanDistance
: sortDefinition.distance === 'dotProduct'
? dotProductDistance
: cosineDistance;
const distance = distanceFunction(sortDefinition.target, vector);
vectorDistances.set(entry, distance);
return distance;
Expand Down
17 changes: 17 additions & 0 deletions resources/indexes/vector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,20 @@ export function cosineDistance(a: number[], b: number[]): number {

return 1 - dotProduct / (magnitudeA * magnitudeB || 1);
}

export function dotProductDistance(a: number[], b: number[]): number {
if (!Array.isArray(a) || !Array.isArray(b)) {
throw new Error('Inner product comparison requires an array');
}

let dotProduct = 0;
const length = Math.max(a.length, b.length);

for (let i = 0; i < length; i++) {
const va = a[i] || 0;
const vb = b[i] || 0;
dotProduct += va * vb;
}

return -dotProduct;
}
53 changes: 53 additions & 0 deletions unitTests/resources/vectorIndex.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,59 @@ describe('HierarchicalNavigableSmallWorld indexing', () => {
);
assert.equal(results[0].id, 2);
});
it('produces different rankings under cosine, euclidean, and dot product metrics', async () => {
const records = [
{ id: 0, name: 'A', vector: [0.1, 0.1] }, // best cosine (direction match)
{ id: 1, name: 'B', vector: [1.2, 0.8] }, // best euclidean (closest in space)
{ id: 2, name: 'C', vector: [7.0, 8.0] }, // best dot product (max projection)
];

await HNSWTest.dropTable?.();

HNSWTest = table({
table: 'HNSWMetricTest',
database: 'test',
attributes: [
{ name: 'id', isPrimaryKey: true },
{ name: 'name', indexed: true },
{ name: 'vector', indexed: { type: 'HNSW' }, type: 'Array' },
],
});

for (let r of records) {
await HNSWTest.put(r.id, r);
}

const target = [1, 1];

const cosine = await fromAsync(
HNSWTest.search({
sort: { attribute: 'vector', target, distance: 'cosine' },
select: ['id'],
limit: 1,
})
);

const euclidean = await fromAsync(
HNSWTest.search({
sort: { attribute: 'vector', target, distance: 'euclidean' },
select: ['id'],
limit: 1,
})
);

const dot = await fromAsync(
HNSWTest.search({
sort: { attribute: 'vector', target, distance: 'dotProduct' },
select: ['id'],
limit: 1,
})
);

assert.equal(cosine[0].id, 0);
assert.equal(euclidean[0].id, 1);
assert.equal(dot[0].id, 2);
});
after(() => {
HNSWTest.dropTable();
});
Expand Down
Loading