2023-05-23 18:53:53 +00:00
|
|
|
import { assertEquals } from "../../test_deps.ts";
|
|
|
|
import { BatchKVStore, SimpleSearchEngine } from "./engine.ts";
|
|
|
|
|
2023-08-30 19:12:33 +00:00
|
|
|
class InMemoryBatchKVStore implements BatchKVStore {
|
|
|
|
private store = new Map<string, any>();
|
2023-05-23 18:53:53 +00:00
|
|
|
|
2023-08-30 19:12:33 +00:00
|
|
|
get(keys: string[]): Promise<(any | undefined)[]> {
|
|
|
|
const results: (any | undefined)[] = keys.map((key) => this.store.get(key));
|
2023-05-23 18:53:53 +00:00
|
|
|
return Promise.resolve(results);
|
|
|
|
}
|
|
|
|
|
2023-08-30 19:12:33 +00:00
|
|
|
queryPrefix(prefix: string): Promise<[string, any][]> {
|
|
|
|
const results: [string, any][] = [];
|
|
|
|
for (const [key, value] of this.store.entries()) {
|
|
|
|
if (key.startsWith(prefix)) {
|
|
|
|
results.push([key, value]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Promise.resolve(results);
|
|
|
|
}
|
|
|
|
|
|
|
|
set(entries: Map<string, any>): Promise<void> {
|
2023-05-23 18:53:53 +00:00
|
|
|
for (const [key, value] of entries) {
|
|
|
|
this.store.set(key, value);
|
|
|
|
}
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
|
2023-08-30 19:12:33 +00:00
|
|
|
delete(keys: string[]): Promise<void> {
|
2023-05-23 18:53:53 +00:00
|
|
|
for (const key of keys) {
|
|
|
|
this.store.delete(key);
|
|
|
|
}
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Deno.test("Test full text search", async () => {
|
|
|
|
const engine = new SimpleSearchEngine(
|
|
|
|
new InMemoryBatchKVStore(),
|
|
|
|
new InMemoryBatchKVStore(),
|
|
|
|
);
|
|
|
|
|
|
|
|
await engine.indexDocument({ id: "1", text: "The quick brown fox" });
|
|
|
|
await engine.indexDocument({ id: "2", text: "jumps over the lazy dogs" });
|
|
|
|
await engine.indexDocument({
|
|
|
|
id: "3",
|
|
|
|
text: "Hello world, jumping jump jumps",
|
|
|
|
});
|
|
|
|
await engine.indexDocument({ id: "4", text: "TypeScript is awesome" });
|
|
|
|
await engine.indexDocument({ id: "5", text: "The brown dogs jumps zęf" });
|
|
|
|
|
|
|
|
console.log(engine.index);
|
|
|
|
|
|
|
|
const results = await engine.search("Brown fox");
|
|
|
|
console.log(results);
|
|
|
|
assertEquals(results.length, 2);
|
|
|
|
assertEquals(results[0].id, "1");
|
|
|
|
assertEquals(results[0].score, 2);
|
|
|
|
assertEquals(results[1].id, "5");
|
|
|
|
assertEquals(results[1].score, 1);
|
|
|
|
|
|
|
|
const results2 = await engine.search("jump");
|
|
|
|
console.log(results2);
|
|
|
|
assertEquals(results2.length, 3);
|
|
|
|
|
|
|
|
await engine.deleteDocument("3");
|
|
|
|
const results3 = await engine.search("jump");
|
|
|
|
console.log(results3);
|
|
|
|
assertEquals(results3.length, 2);
|
|
|
|
|
|
|
|
const results4 = await engine.search("zęf");
|
|
|
|
console.log(results4);
|
|
|
|
assertEquals(results4.length, 1);
|
|
|
|
});
|