Skip to content

Commit 5219e7d

Browse files
committed
feat: halfvec v2
1 parent 1781385 commit 5219e7d

File tree

1 file changed

+13
-31
lines changed
  • packages/service/common/vectorStore/pg

1 file changed

+13
-31
lines changed

packages/service/common/vectorStore/pg/class.ts

+13-31
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,9 @@ export class PgVectorCtrl {
3535
await PgClient.query(
3636
`CREATE INDEX CONCURRENTLY IF NOT EXISTS create_time_index ON ${DatasetVectorTableName} USING btree(createtime);`
3737
);
38-
// TODO: enable halfvector index
39-
// await PgClient.query(
40-
// `CREATE INDEX CONCURRENTLY IF NOT EXISTS halfvector_index ON ${DatasetVectorTableName} USING hnsw (halfvector halfvec_ip_ops) WITH (m = 32, ef_construction = 128);`
41-
// );
38+
await PgClient.query(
39+
`CREATE INDEX CONCURRENTLY IF NOT EXISTS halfvector_index ON ${DatasetVectorTableName} USING hnsw (halfvector halfvec_ip_ops) WITH (m = 32, ef_construction = 128);`
40+
);
4241

4342
addLog.info('init pg successful');
4443
} catch (error) {
@@ -49,11 +48,9 @@ export class PgVectorCtrl {
4948
const { teamId, datasetId, collectionId, vector, retry = 3 } = props;
5049

5150
try {
52-
// TODO: remove vector
5351
const { rowCount, rows } = await PgClient.insert(DatasetVectorTableName, {
5452
values: [
5553
[
56-
{ key: 'vector', value: `[${vector}]` },
5754
{ key: 'halfvector', value: `[${vector}]` },
5855
{ key: 'team_id', value: String(teamId) },
5956
{ key: 'dataset_id', value: String(datasetId) },
@@ -180,33 +177,18 @@ export class PgVectorCtrl {
180177
// );
181178
// console.log(explan[2].rows);
182179

183-
// TODO: use halfvector
184-
// const results: any = await PgClient.query(
185-
// `
186-
// BEGIN;
187-
// SET LOCAL hnsw.ef_search = ${global.systemEnv?.pgHNSWEfSearch || 100};
188-
// select id, collection_id, halfvector <#> '[${vector}]' AS score
189-
// from ${DatasetVectorTableName}
190-
// where team_id='${teamId}'
191-
// AND dataset_id IN (${datasetIds.map((id) => `'${String(id)}'`).join(',')})
192-
// ${filterCollectionIdSql}
193-
// ${forbidCollectionSql}
194-
// order by score limit ${limit};
195-
// COMMIT;`
196-
// );
197-
198180
const results: any = await PgClient.query(
199181
`
200-
BEGIN;
201-
SET LOCAL hnsw.ef_search = ${global.systemEnv?.pgHNSWEfSearch || 100};
202-
select id, collection_id, vector <#> '[${vector}]' AS score
203-
from ${DatasetVectorTableName}
204-
where team_id='${teamId}'
205-
AND dataset_id IN (${datasetIds.map((id) => `'${String(id)}'`).join(',')})
206-
${filterCollectionIdSql}
207-
${forbidCollectionSql}
208-
order by score limit ${limit};
209-
COMMIT;`
182+
BEGIN;
183+
SET LOCAL hnsw.ef_search = ${global.systemEnv?.pgHNSWEfSearch || 100};
184+
select id, collection_id, halfvector <#> '[${vector}]' AS score
185+
from ${DatasetVectorTableName}
186+
where team_id='${teamId}'
187+
AND dataset_id IN (${datasetIds.map((id) => `'${String(id)}'`).join(',')})
188+
${filterCollectionIdSql}
189+
${forbidCollectionSql}
190+
order by score limit ${limit};
191+
COMMIT;`
210192
);
211193

212194
const rows = results?.[2]?.rows as PgSearchRawType[];

0 commit comments

Comments
 (0)