feat: expose use_index in LanceDB OSS

This commit is contained in:
Will Jones
2023-10-13 11:17:10 -07:00
parent 683824f1e9
commit f9ccefb032
8 changed files with 71 additions and 3 deletions

View File

@@ -33,6 +33,7 @@ export class Query<T = number[]> {
private _filter?: string
private _metricType?: MetricType
protected readonly _embeddings?: EmbeddingFunction<T>
private _useIndex: boolean
constructor (query: T, tbl?: any, embeddings?: EmbeddingFunction<T>) {
this._tbl = tbl
@@ -44,6 +45,7 @@ export class Query<T = number[]> {
this._filter = undefined
this._metricType = undefined
this._embeddings = embeddings
this._useIndex = true
}
/***
@@ -102,6 +104,20 @@ export class Query<T = number[]> {
return this
}
/**
* Whether or not to use the ANN index for this query. If set to false,
* the query will run exact KNN on a full scan of the table. The default is
* true (use the index).
*
* Setting this option to false is not currently supported by LanceDB Cloud.
*
* @param value Whether or not to use the index for this query.
*/
useIndex (value: boolean): Query<T> {
this._useIndex = value
return this
}
/**
* Execute the query and return the results as an Array of Objects
*/

View File

@@ -117,6 +117,19 @@ describe('LanceDB client', function () {
assert.isUndefined(results[0].name)
assert.isUndefined(results[0].price)
})
it('can choose to do flat search', async function () {
const uri = await createTestDB()
const con = await lancedb.connect(uri)
const table = await con.openTable('vectors')
const results = await table.search([0.1, 0.3]).useIndex(false).execute()
assert.equal(results.length, 2)
assert.equal(results[0].price, 10)
const vector = results[0].vector as Float32Array
assert.approximately(vector[0], 0.0, 0.2)
assert.approximately(vector[0], 0.1, 0.3)
})
})
describe('when creating a new dataset', function () {
@@ -385,11 +398,13 @@ describe('Query object', function () {
.metricType(MetricType.Cosine)
.refineFactor(100)
.select(['a', 'b'])
.useIndex(false)
.nprobes(20) as Record<string, any>
assert.equal(query._limit, 1)
assert.equal(query._metricType, MetricType.Cosine)
assert.equal(query._refineFactor, 100)
assert.equal(query._nprobes, 20)
assert.equal(query._useIndex, false)
assert.deepEqual(query._select, ['a', 'b'])
})
})