diff --git a/src/MiniSearch.test.js b/src/MiniSearch.test.js index e7febaf7..936cb074 100644 --- a/src/MiniSearch.test.js +++ b/src/MiniSearch.test.js @@ -1159,6 +1159,25 @@ describe('MiniSearch', () => { expect(results.every(({ category }) => category === 'poetry')).toBe(true) }) + it('allows customizing BM25+ parameters', () => { + const ms = new MiniSearch({ fields: ['text'], searchOptions: { _bm25: { k: 1.2, b: 0.7, d: 0.5 } } }) + const documents = [ + { id: 1, text: 'something very very very cool' }, + { id: 2, text: 'something cool' } + ] + ms.addAll(documents) + + expect(ms.search('very')[0].score).toBeGreaterThan(ms.search('very', { _bm25: { k: 1, b: 0.7, d: 0.5 } })[0].score) + expect(ms.search('something')[1].score).toBeGreaterThan(ms.search('something', { _bm25: { k: 1.2, b: 1, d: 0.5 } })[1].score) + expect(ms.search('something')[1].score).toBeGreaterThan(ms.search('something', { _bm25: { k: 1.2, b: 0.7, d: 0.1 } })[1].score) + + // Defaults are taken from the searchOptions passed to the constructor + const other = new MiniSearch({ fields: ['text'], searchOptions: { _bm25: { k: 1, b: 0.7, d: 0.5 } } }) + other.addAll(documents) + + expect(other.search('very')).toEqual(ms.search('very', { _bm25: { k: 1, b: 0.7, d: 0.5 } })) + }) + describe('when passing a query tree', () => { it('searches according to the given combination', () => { const results = ms.search({ @@ -1514,7 +1533,7 @@ describe('MiniSearch', () => { expect(ms.search('queen', { fuzzy: 1, prefix: true })[0].song).toEqual('Killer Queen') }) }) - }) + }) describe('default tokenization', () => { it('splits on non-alphanumeric taking diacritics into account', () => {