Skip to content

Commit

Permalink
Merge 8cca13b into 7accf96
Browse files Browse the repository at this point in the history
  • Loading branch information
lucaong committed Jun 24, 2021
2 parents 7accf96 + 8cca13b commit 3f6bc74
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/MiniSearch.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,19 @@ describe('MiniSearch', () => {
expect(console.warn).not.toHaveBeenCalled()
})

it('cleans up all data of the deleted document', () => {
const otherDocument = { id: 4, title: 'Decameron', text: 'Umana cosa è aver compassione degli afflitti' }
const originalFieldLength = JSON.parse(JSON.stringify(ms._fieldLength))
const originalAverageFieldLength = JSON.parse(JSON.stringify(ms._averageFieldLength))

ms.add(otherDocument)
ms.remove(otherDocument)

expect(ms.documentCount).toEqual(3)
expect(ms._fieldLength).toEqual(originalFieldLength)
expect(ms._averageFieldLength).toEqual(originalAverageFieldLength)
})

it('does not remove terms from other documents', () => {
ms.remove(documents[0])
expect(ms.search('cammin').length).toEqual(1)
Expand Down
11 changes: 11 additions & 0 deletions src/MiniSearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -545,10 +545,13 @@ export default class MiniSearch<T = any> {
this.removeTerm(this._fieldIds[field], shortDocumentId, processedTerm)
}
})

this.removeFieldLength(shortDocumentId, this._fieldIds[field], this.documentCount, tokens.length)
})

delete this._storedFields[shortDocumentId]
delete this._documentIds[shortDocumentId]
delete this._fieldLength[shortDocumentId]
this._documentCount -= 1
}

Expand Down Expand Up @@ -1066,6 +1069,14 @@ export default class MiniSearch<T = any> {
this._averageFieldLength[fieldId] = totalLength / (count + 1)
}

/**
* @ignore
*/
private removeFieldLength (documentId: string, fieldId: number, count: number, length: number): void {
const totalLength = (this._averageFieldLength[fieldId] * count) - length
this._averageFieldLength[fieldId] = totalLength / (count - 1)
}

/**
* @ignore
*/
Expand Down

0 comments on commit 3f6bc74

Please sign in to comment.