Skip to content

Commit

Permalink
fix other issues caused by fields or terms matching default propertie…
Browse files Browse the repository at this point in the history
…s of object
  • Loading branch information
lucaong committed Jun 25, 2020
1 parent 4d0cb2c commit b6082f5
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/MiniSearch.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ class MiniSearch {
*/
add (document) {
const { extractField, tokenize, processTerm, fields, idField } = this._options
if (document[idField] == null) {
if (getOwnProperty(document, idField) == null) {
throw new Error(`MiniSearch: document does not have ID field "${idField}"`)
}
const shortDocumentId = addDocumentId(this, document[idField])
Expand Down Expand Up @@ -254,7 +254,7 @@ class MiniSearch {
remove (document) {
const { tokenize, processTerm, extractField, fields, idField } = this._options

if (document[idField] == null) {
if (getOwnProperty(document, idField) == null) {
throw new Error(`MiniSearch: document does not have ID field "${idField}"`)
}

Expand All @@ -265,7 +265,7 @@ class MiniSearch {
throw new Error(`MiniSearch: cannot remove document with ID ${document[idField]}: it is not in the index`)
}

fields.filter(field => document[field] != null).forEach(field => {
fields.filter(field => getOwnProperty(document, field) != null).forEach(field => {
const tokens = tokenize(extractField(document, field) || '', field)

tokens.forEach(term => {
Expand Down Expand Up @@ -572,7 +572,7 @@ class MiniSearch {
options = { ...this._options.searchOptions, ...options }

const boosts = (options.fields || this._options.fields).reduce((boosts, field) =>
({ ...boosts, [field]: boosts[field] || 1 }), options.boost || {})
({ ...boosts, [field]: getOwnProperty(boosts, field) || 1 }), options.boost || {})

const {
boostDocument,
Expand Down
35 changes: 35 additions & 0 deletions src/MiniSearch.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ describe('MiniSearch', () => {
}).toThrowError('MiniSearch: document does not have ID field "foo"')
})

it('throws error if the document does not have the ID field, even when named like a default object property', () => {
const ms = new MiniSearch({ idField: 'constructor', fields: ['title', 'text'] })
expect(() => {
ms.add({ text: 'I do not have an ID' })
}).toThrowError('MiniSearch: document does not have ID field "constructor"')
})

it('rejects falsy terms', () => {
const processTerm = term => term === 'foo' ? null : term
const ms = new MiniSearch({ fields: ['title', 'text'], processTerm })
Expand Down Expand Up @@ -174,6 +181,23 @@ describe('MiniSearch', () => {
}).toThrowError('MiniSearch: document does not have ID field "foo"')
})

it('throws error if the document does not have the ID field, even if named like a default property of object', () => {
const ms = new MiniSearch({ idField: 'constructor', fields: ['title', 'text'] })
expect(() => {
ms.remove({ text: 'I do not have an ID' })
}).toThrowError('MiniSearch: document does not have ID field "constructor"')
})

it('does not crash when the document has field named like default properties of object', () => {
const ms = new MiniSearch({ fields: ['constructor'] })
const document = { id: 1 }
ms.add(document)

expect(() => {
ms.remove(document)
}).not.toThrowError()
})

it('does not reassign IDs', () => {
ms.remove(documents[0])
ms.add(documents[0])
Expand Down Expand Up @@ -423,6 +447,17 @@ describe('MiniSearch', () => {
expect(results[0].score).toBeGreaterThan(results[1].score)
})

it('computes a meaningful score when fields are named liked default properties of object', () => {
const ms = new MiniSearch({ fields: ['constructor'] })
ms.add({ id: 1, constructor: 'something' })
ms.add({ id: 1, constructor: 'something else' })

const results = ms.search('something')
results.forEach((result) => {
expect(Number.isFinite(result.score)).toBe(true)
})
})

it('searches in the given fields', () => {
const results = ms.search('vita', { fields: ['title'] })
expect(results).toHaveLength(1)
Expand Down

0 comments on commit b6082f5

Please sign in to comment.