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

fix: fixes zero division when computing scores after removing all documents from an index #792

Merged
merged 3 commits into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions packages/orama/src/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,11 @@ export async function removeDocumentScoreParameters(
): Promise<void> {
const internalId = getInternalDocumentId(index.sharedInternalDocumentStore, id)

index.avgFieldLength[prop] =
(index.avgFieldLength[prop] * docsCount - index.fieldLengths[prop][internalId]!) / (docsCount - 1)
if (docsCount > 1) {
index.avgFieldLength[prop] = (index.avgFieldLength[prop] * docsCount - index.fieldLengths[prop][internalId]!) / (docsCount - 1);
} else {
index.avgFieldLength[prop] = undefined as unknown as number;
}
index.fieldLengths[prop][internalId] = undefined
index.frequencies[prop][internalId] = undefined
}
Expand Down
17 changes: 17 additions & 0 deletions packages/orama/tests/remove.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -353,3 +353,20 @@ async function createSimpleDB() {

return [db, id1, id2, id3, id4] as const
}

t.test('test case for #766: Zero division when computing scores after removing all documents from an index.', async (t) => {
const db = await create({
schema: {
name: 'string'
} as const
})

const id = await insert(db, { name: 'test' })

const success = await remove(db, id)

await insert(db, { name: 'foo' })
await insert(db, { name: 'bar' })

t.ok(success)
})
Loading