Skip to content
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

Zero division when computing scores after removing all documents from an index. #766

Closed
kushuh opened this issue Aug 22, 2024 · 6 comments
Closed

Comments

@kushuh
Copy link

kushuh commented Aug 22, 2024

Describe the bug

I had a weird issues where all scores would suddendly switch to NaN. I found the culprit after some investigation:

https://github.com/askorama/orama/blob/980eb586ebaffbfc3879cf1d3d7c642e6e14c2eb/packages/orama/src/components/index.ts#L165

Basically, when removing the last document from an index, (docsCount - 1) becomes 0, which results in a NaN that screws up every following update.

I managed to fix the issue locally by changing the line to:

if (docsCount > 1) {
  index.avgFieldLength[prop] =
      (index.avgFieldLength[prop] * docsCount - index.fieldLengths[prop][internalId]!) / (docsCount - 1);
} else {
  index.avgFieldLength[prop] = undefined;
}

To Reproduce

Just remove all documents from an index:

const id = await insert(db, document);
await remove(db, id);

Expected behavior

If I add documents again to an emptied index, scores should compute normally.

Environment Info

OS: Windows 11
Node: v22.3.0
Orama: 2.0.23

Affected areas

Data Insertion, Search

Additional context

Its not just when the index is empty. Even after adding back some documents, scores will remain NaN forever, as long as the index has been emptied once.

@micheleriva
Copy link
Member

Hi @kushuh, thank you so much for spotting this. Would you be up for opening a PR to fix this since you already fixed it locally? It would be immensely appreciated.

@kushuh
Copy link
Author

kushuh commented Sep 11, 2024

No problem, I'll open it by tonight

@micheleriva
Copy link
Member

Very appreciated. Thanks!

@kushuh
Copy link
Author

kushuh commented Sep 12, 2024

Hi @micheleriva, I tried to open the PR just now but I don't have permission to push a new branch

@micheleriva
Copy link
Member

@kushuh you should probably fork and open a PR!

@micheleriva
Copy link
Member

Fixed in #789, released in Orama v2.1.1. Thanks again!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants