-
-
Notifications
You must be signed in to change notification settings - Fork 307
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add exact param in remove doc by word method #41
Add exact param in remove doc by word method #41
Conversation
This is perfect, thank you @mateonunez 🙏 |
const id = search.hits[0].id; | ||
await db.delete(id); | ||
|
||
const search2 = await db.search({ term: "abc" }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line should also use exact: true
, otherwise the tests passes even if #40 is not fixed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But it shouldn't be 0, that's the bug in #40. It should be 1, because there is a second document containing "abc"
that should not be deleted.
The test adds two documents containing "abc"
and then deletes one of them. The other one should still be there, and not be deleted.
At a glance (but I did not spend much time delving into the code, so I might be wrong), the issue is that the term should not be deleted unless all the documents associated to it were deleted. Instead, the code sets node.end = true
, effectively removing the term, even if it still has documents associated to it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Got the point. The issue should be opened again.
const root = this.root; | ||
if (!word) return false; | ||
|
||
function removeWord(node: TrieNode, _word: string, docID: string): boolean { | ||
const [nodeWord /**_docs*/] = node.getWord(); | ||
|
||
if (node.end && nodeWord === word) { | ||
if (node.end || (exact && node.end && nodeWord === word)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#38 cannot be caused by that, because it occurs even if no document is deleted, so this code path is never called in the reproduction.
|
||
const serach2 = await db.search({ term: "stelle" }); | ||
|
||
expect(serach2.count).toBe(1); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This assertion should check that the search hits do not include the deleted ID, not that the count is 1.
This PR fixes #39 and #40.
I've implemented a new param in the
removeDocByWord
method used inremove
method to prevent removing words that not exactly matches the document's word.I also added 2 cases to the tests.