File tree Expand file tree Collapse file tree 2 files changed +47
-2
lines changed Expand file tree Collapse file tree 2 files changed +47
-2
lines changed Original file line number Diff line number Diff line change @@ -107,14 +107,14 @@ export class Trie {
107
107
return output ;
108
108
}
109
109
110
- removeDocByWord ( word : string , docID : string ) : boolean {
110
+ removeDocByWord ( word : string , docID : string , exact = false ) : boolean {
111
111
const root = this . root ;
112
112
if ( ! word ) return false ;
113
113
114
114
function removeWord ( node : TrieNode , _word : string , docID : string ) : boolean {
115
115
const [ nodeWord /**_docs*/ ] = node . getWord ( ) ;
116
116
117
- if ( node . end && nodeWord === word ) {
117
+ if ( node . end || ( exact && node . end && nodeWord === word ) ) {
118
118
node . removeDoc ( docID ) ;
119
119
120
120
if ( node . children ?. size ) {
Original file line number Diff line number Diff line change @@ -195,6 +195,51 @@ describe("lyra", () => {
195
195
expect ( searchResult . hits [ 0 ] . id ) . toBe ( id2 ) ;
196
196
} ) ;
197
197
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
+
198
243
it ( "Should preserve identical docs after deletion" , async ( ) => {
199
244
const db = new Lyra ( {
200
245
schema : {
You can’t perform that action at this time.
0 commit comments