forked from lioensky/VCPToolBox
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvectorSearchWorker.js
More file actions
49 lines (39 loc) · 1.74 KB
/
Copy pathvectorSearchWorker.js
File metadata and controls
49 lines (39 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// vectorSearchWorker.js
const { parentPort } = require('worker_threads');
const path = require('path');
const fs = require('fs').promises;
const { HierarchicalNSW } = require('hnswlib-node');
async function performSearch(workerData) {
const { diaryName, queryVector, k, efSearch, vectorStorePath } = workerData;
try {
const safeFileNameBase = Buffer.from(diaryName, 'utf-8').toString('base64url');
const indexPath = path.join(vectorStorePath, `${safeFileNameBase}.bin`);
const mapPath = path.join(vectorStorePath, `${safeFileNameBase}_map.json`);
// 1. 加载索引和映射文件
const index = new HierarchicalNSW('l2', queryVector.length);
await index.readIndex(indexPath);
const mapData = await fs.readFile(mapPath, 'utf-8');
const chunkMap = JSON.parse(mapData);
// 2. 验证索引状态
if (index.getCurrentCount() === 0) {
parentPort.postMessage({ status: 'success', results: [] });
return;
}
// 3. 设置搜索参数并执行搜索
if (typeof index.setEf === 'function') {
index.setEf(efSearch);
}
const result = index.searchKnn(queryVector, k);
if (!result || !result.neighbors) {
throw new Error('Search returned invalid result.');
}
// 4. 整理并返回结果
const searchResults = result.neighbors.map(label => chunkMap[label]).filter(Boolean);
parentPort.postMessage({ status: 'success', results: searchResults });
} catch (error) {
parentPort.postMessage({ status: 'error', error: error.message });
}
}
parentPort.on('message', (data) => {
performSearch(data);
});