✨ FullTextSearchCodebaseIndex
This commit is contained in:
parent
539993c456
commit
aae4c0c5e0
2 changed files with 128 additions and 1 deletions
125
core/indexing/FullTextSearch.ts
Normal file
125
core/indexing/FullTextSearch.ts
Normal file
|
|
@ -0,0 +1,125 @@
|
|||
import { Chunk, IndexingProgressUpdate } from "..";
|
||||
import { DatabaseConnection, SqliteDb, tagToString } from "./refreshIndex";
|
||||
import {
|
||||
CodebaseIndex,
|
||||
IndexResultType,
|
||||
IndexTag,
|
||||
MarkCompleteCallback,
|
||||
RefreshIndexResults,
|
||||
} from "./types";
|
||||
|
||||
export class FullTextSearchCodebaseIndex implements CodebaseIndex {
|
||||
artifactId: string = "sqliteFts";
|
||||
|
||||
private async _createTables(db: DatabaseConnection) {
|
||||
await db.exec(`CREATE VIRTUAL TABLE IF NOT EXISTS fts USING fts5(
|
||||
path,
|
||||
content
|
||||
)`);
|
||||
|
||||
await db.exec(`CREATE TABLE IF NOT EXISTS fts_metadata (
|
||||
id INTEGER PRIMARY KEY,
|
||||
path TEXT NOT NULL,
|
||||
cacheKey TEXT NOT NULL,
|
||||
chunkId INTEGER NOT NULL,
|
||||
FOREIGN KEY (chunkId) REFERENCES chunks (id),
|
||||
FOREIGN KEY (id) REFERENCES fts (rowid)
|
||||
)`);
|
||||
}
|
||||
|
||||
async *update(
|
||||
tag: IndexTag,
|
||||
results: RefreshIndexResults,
|
||||
markComplete: MarkCompleteCallback
|
||||
): AsyncGenerator<IndexingProgressUpdate, any, unknown> {
|
||||
const db = await SqliteDb.get();
|
||||
await this._createTables(db);
|
||||
|
||||
for (let i = 0; i < results.compute.length; i++) {
|
||||
const item = results.compute[i];
|
||||
|
||||
// Insert chunks
|
||||
const chunks = await db.all(
|
||||
`SELECT * FROM chunks WHERE path = ? AND cacheKey = ?`,
|
||||
[item.path, item.cacheKey]
|
||||
);
|
||||
|
||||
for (let chunk of chunks) {
|
||||
const { lastID } = await db.run(
|
||||
`INSERT INTO fts (path, content) VALUES (?, ?)`,
|
||||
[item.path, chunk.content]
|
||||
);
|
||||
await db.run(
|
||||
`INSERT INTO fts_metadata (id, path, cacheKey, chunkId) VALUES (?, ?, ?)`,
|
||||
[lastID, item.path, item.cacheKey, chunk.id]
|
||||
);
|
||||
}
|
||||
|
||||
yield {
|
||||
progress: i / results.compute.length,
|
||||
desc: `Indexing ${item.path}`,
|
||||
};
|
||||
markComplete([item], IndexResultType.Compute);
|
||||
}
|
||||
|
||||
// Add tag
|
||||
for (const item of results.addTag) {
|
||||
markComplete([item], IndexResultType.AddTag);
|
||||
}
|
||||
|
||||
// Remove tag
|
||||
for (const item of results.removeTag) {
|
||||
markComplete([item], IndexResultType.RemoveTag);
|
||||
}
|
||||
|
||||
// Delete
|
||||
for (const item of results.del) {
|
||||
const { lastID } = await db.run(
|
||||
`DELETE FROM fts_metadata WHERE path = ? AND cacheKey = ?`,
|
||||
[item.path, item.cacheKey]
|
||||
);
|
||||
await db.run(`DELETE FROM fts WHERE rowid = ?`, [lastID]);
|
||||
|
||||
markComplete([item], IndexResultType.Delete);
|
||||
}
|
||||
}
|
||||
|
||||
async retrieve(
|
||||
tags: IndexTag[],
|
||||
text: string,
|
||||
n: number,
|
||||
directory: string | undefined
|
||||
): Promise<Chunk[]> {
|
||||
const db = await SqliteDb.get();
|
||||
const tagStrings = tags.map(tagToString);
|
||||
|
||||
const results = await db.all(
|
||||
`SELECT fts_metadata.chunkId
|
||||
FROM fts
|
||||
JOIN fts_metadata ON fts.rowid = fts_metadata.id
|
||||
JOIN chunk_tags ON fts_metadata.chunkId = chunk_tags.chunkId
|
||||
WHERE fts MATCH ? AND filtered_chunk_tags.tag IN (${tagStrings
|
||||
.map(() => "?")
|
||||
.join(",")})
|
||||
ORDER BY rank
|
||||
LIMIT ?`,
|
||||
[text, ...tagStrings, n]
|
||||
);
|
||||
|
||||
const chunks = await db.all(
|
||||
`SELECT * FROM chunks WHERE id IN (${results.map(() => "?").join(",")})`,
|
||||
results.map((result) => result.chunkId)
|
||||
);
|
||||
|
||||
return chunks.map((chunk) => {
|
||||
return {
|
||||
filepath: chunk.path,
|
||||
index: chunk.index,
|
||||
startLine: chunk.startLine,
|
||||
endLine: chunk.endLine,
|
||||
content: chunk.content,
|
||||
digest: chunk.cacheKey,
|
||||
};
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
@ -1,3 +1,4 @@
|
|||
import { FullTextSearchCodebaseIndex } from "core/indexing/FullTextSearch";
|
||||
import { LanceDbIndex } from "core/indexing/LanceDbIndex";
|
||||
import { ChunkCodebaseIndex } from "core/indexing/chunk/ChunkCodebaseIndex";
|
||||
import { getComputeDeleteAddRemove } from "core/indexing/refreshIndex";
|
||||
|
|
@ -28,8 +29,9 @@ async function getIndexesToBuild(): Promise<CodebaseIndex[]> {
|
|||
const config = await configHandler.loadConfig(ide);
|
||||
|
||||
const indexes = [
|
||||
new ChunkCodebaseIndex(ide.readFile), // Chunking must come first
|
||||
new LanceDbIndex(config.embeddingsProvider, ide.readFile),
|
||||
new ChunkCodebaseIndex(ide.readFile),
|
||||
new FullTextSearchCodebaseIndex(),
|
||||
];
|
||||
|
||||
return indexes;
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue