Skip to content

Commit 13687c7

Browse files
author
Ilia Travnikov
committed
update
1 parent e851f4f commit 13687c7

File tree

1 file changed

+17
-13
lines changed

1 file changed

+17
-13
lines changed

src/services/code-index/vector-store/valkey-search-client.ts

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { t } from "../../../i18n"
77

88
export class ValkeySearchVectorStore implements IVectorStore {
99
private readonly vectorSize: number
10-
private readonly DISTANCE_METRIC = "L2"
10+
private readonly DISTANCE_METRIC = "COSINE"
1111
private client: Redis | null = null
1212
private isInitializing = false
1313
private readonly indexName: string
@@ -349,20 +349,24 @@ export class ValkeySearchVectorStore implements IVectorStore {
349349

350350
async clearCollection(): Promise<void> {
351351
await this.ensureConnected()
352-
const result = await this.client?.sendCommand(
353-
new Command("FT.SEARCH", [this.indexName, "*", "NOCONTENT", "LIMIT", "0", "1000000"], {
354-
replyEncoding: "utf8",
355-
}),
356-
)
352+
if (!this.client) {
353+
return
354+
}
357355

358-
if (Array.isArray(result) && result.length > 1) {
359-
const pipeline = this.client?.pipeline()
360-
for (let i = 1; i < result.length; i++) {
361-
const docId = result[i] as string
362-
pipeline?.call("DEL", [docId])
356+
let cursor = "0"
357+
do {
358+
// Using 'SCAN' to iterate through keys with the given prefix
359+
const scanResult = await this.client.scan(cursor, "MATCH", `${this.indexName}:*`, "COUNT", 100)
360+
cursor = scanResult[0]
361+
const keys = scanResult[1]
362+
363+
if (keys.length > 0) {
364+
// Deleting the found keys in a pipeline for efficiency
365+
const pipeline = this.client.pipeline()
366+
keys.forEach((key) => pipeline.del(key))
367+
await pipeline.exec()
363368
}
364-
await pipeline?.exec()
365-
}
369+
} while (cursor !== "0")
366370
}
367371

368372
async collectionExists(): Promise<boolean> {

0 commit comments

Comments
 (0)