Skip to content

Commit ed0ece5

Browse files
authored
fix: #860 unhandled error when using a wrong property for vector search (#971)
1 parent 7bfc3f5 commit ed0ece5

5 files changed

Lines changed: 32 additions & 2 deletions

File tree

packages/orama/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,4 +177,4 @@
177177
]
178178
},
179179
"module": "./dist/esm/index.js"
180-
}
180+
}

packages/orama/src/errors.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ const errors = {
2929
UNKNOWN_GROUP_BY_PROPERTY: `Unknown groupBy property "%s".`,
3030
INVALID_GROUP_BY_PROPERTY: `Invalid groupBy property "%s". Allowed types: "%s", but given "%s".`,
3131
UNKNOWN_FILTER_PROPERTY: `Unknown filter property "%s".`,
32+
UNKNOWN_VECTOR_PROPERTY: `Unknown vector property "%s". Make sure the property exists in the schema and is configured as a vector.`,
3233
INVALID_VECTOR_SIZE: `Vector size must be a number greater than 0. Got "%s" instead.`,
3334
INVALID_VECTOR_VALUE: `Vector value must be a number greater than 0. Got "%s" instead.`,
3435
INVALID_INPUT_VECTOR: `Property "%s" was declared as a %s-dimensional vector, but got a %s-dimensional vector instead.\nInput vectors must be of the size declared in the schema, as calculating similarity between vectors of different sizes can lead to unexpected results.`,

packages/orama/src/methods/search-vector.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ export function innerVectorSearch<T extends AnyOrama, ResultDocument = TypedDocu
2121
}
2222

2323
const vectorIndex = orama.data.index.vectorIndexes[vector!.property]
24+
if (!vectorIndex) {
25+
throw createError('UNKNOWN_VECTOR_PROPERTY', vector!.property)
26+
}
27+
2428
const vectorSize = vectorIndex.node.size
2529

2630
if (vector?.value.length !== vectorSize) {

packages/orama/tests/search-vector.test.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,31 @@ t.test('search', async (t) => {
173173
t.same((results2.hits[1].document as any).vectors.embedding_2, [0.2, 0.2, 0.21, 0.21, 0.21, 0.21])
174174
t.same((results2.hits[2].document as any).vectors.embedding_2, [0.2, 0.02, 0.1, 0.1, 0.1, 0.1])
175175
})
176+
177+
t.test('should throw an error when using unknown vector property', async (t) => {
178+
const db = await create({
179+
schema: {
180+
title: 'string',
181+
embedding: 'vector[5]'
182+
} as const
183+
})
184+
185+
await insertMultiple(db, [{ title: 'foo', embedding: [1, 1, 1, 1, 1] }])
186+
187+
try {
188+
await search(db, {
189+
mode: 'vector',
190+
vector: {
191+
value: [1, 1, 1, 1, 1],
192+
property: 'nonexistent_vector'
193+
}
194+
})
195+
t.fail('Should have thrown an error')
196+
} catch (err: any) {
197+
t.ok(err.message.includes('Unknown vector property "nonexistent_vector"'), 'error message contains property name')
198+
t.same(err.code, 'UNKNOWN_VECTOR_PROPERTY', 'correct error code')
199+
}
200+
})
176201
})
177202

178203
t.test('vector search with where clause', async (t) => {

packages/tokenizers/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,4 @@
8282
"tshy": "^3.0.2",
8383
"tsx": "^4.19.2"
8484
}
85-
}
85+
}

0 commit comments

Comments
 (0)