Skip to content

Commit 6f665d3

Browse files
authored
Add exact param in remove doc by word method (#41)
* perf(trie): add exact param in remove word * test(lyra): add more delete method cases
1 parent 314ce26 commit 6f665d3

File tree

2 files changed

+47
-2
lines changed

2 files changed

+47
-2
lines changed

packages/lyra/src/prefix-tree/trie.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,14 +107,14 @@ export class Trie {
107107
return output;
108108
}
109109

110-
removeDocByWord(word: string, docID: string): boolean {
110+
removeDocByWord(word: string, docID: string, exact = false): boolean {
111111
const root = this.root;
112112
if (!word) return false;
113113

114114
function removeWord(node: TrieNode, _word: string, docID: string): boolean {
115115
const [nodeWord /**_docs*/] = node.getWord();
116116

117-
if (node.end && nodeWord === word) {
117+
if (node.end || (exact && node.end && nodeWord === word)) {
118118
node.removeDoc(docID);
119119

120120
if (node.children?.size) {

packages/lyra/tests/lyra.test.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,51 @@ describe("lyra", () => {
195195
expect(searchResult.hits[0].id).toBe(id2);
196196
});
197197

198+
it("Shouldn't returns deleted documents", async () => {
199+
const db = new Lyra({
200+
schema: {
201+
txt: "string",
202+
},
203+
stemming: false,
204+
});
205+
206+
await db.insert({ txt: "stelle" });
207+
await db.insert({ txt: "stellle" });
208+
await db.insert({ txt: "scelte" });
209+
210+
const search = await db.search({ term: "stelle" });
211+
212+
const id = search.hits[0].id;
213+
214+
await db.delete(id);
215+
216+
const serach2 = await db.search({ term: "stelle" });
217+
218+
expect(serach2.count).toBe(1);
219+
});
220+
221+
it("Shouldn't affects other document when deleted one", async () => {
222+
const db = new Lyra({
223+
schema: {
224+
txt: "string",
225+
},
226+
stemming: false,
227+
});
228+
229+
await db.insert({ txt: "abc" });
230+
await db.insert({ txt: "abc" });
231+
await db.insert({ txt: "abcd" });
232+
233+
const search = await db.search({ term: "abc", exact: true });
234+
235+
const id = search.hits[0].id;
236+
await db.delete(id);
237+
238+
const search2 = await db.search({ term: "abc" });
239+
240+
expect(search2.count).toBe(1);
241+
});
242+
198243
it("Should preserve identical docs after deletion", async () => {
199244
const db = new Lyra({
200245
schema: {

0 commit comments

Comments
 (0)