Zero division when computing scores after removing all documents from an index.
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.