Skip to content

Commit

Permalink
Merge 4eafafc into 2d9e3af
Browse files Browse the repository at this point in the history
  • Loading branch information
lucaong committed May 15, 2023
2 parents 2d9e3af + 4eafafc commit 68bb989
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Expand Up @@ -2,6 +2,11 @@

`MiniSearch` follows [semantic versioning](https://semver.org/spec/v2.0.0.html).

# unreleased

- Pass stored fields to the `boostDocument` callback function, making it
easier to perform dynamic document boosting.

# v6.0.1

- [fix] The `boost` search option now does not interfere with the `fields`
Expand Down
9 changes: 5 additions & 4 deletions src/MiniSearch.test.js
Expand Up @@ -1109,14 +1109,15 @@ describe('MiniSearch', () => {
expect(results.map(({ id }) => id)).toEqual([2, 1])
})

it('boosts documents by calling boostDocument with document ID and term', () => {
const query = 'divina commedia'
it('boosts documents by calling boostDocument with document ID, term, and stored fields', () => {
const query = 'divina commedia nova'
const boostFactor = 1.234
const boostDocument = jest.fn((id, term) => boostFactor)
const resultsWithoutBoost = ms.search(query)
const results = ms.search(query, { boostDocument })
expect(boostDocument).toHaveBeenCalledWith(1, 'divina')
expect(boostDocument).toHaveBeenCalledWith(1, 'commedia')
expect(boostDocument).toHaveBeenCalledWith(1, 'divina', {})
expect(boostDocument).toHaveBeenCalledWith(1, 'commedia', {})
expect(boostDocument).toHaveBeenCalledWith(3, 'nova', { category: 'poetry' })
expect(results[0].score).toBeCloseTo(resultsWithoutBoost[0].score * boostFactor)
})

Expand Down
11 changes: 7 additions & 4 deletions src/MiniSearch.ts
Expand Up @@ -38,9 +38,12 @@ export type SearchOptions = {
/**
* Function to calculate a boost factor for documents. It takes as arguments
* the document ID, and a term that matches the search in that document, and
* should return a boosting factor.
* the value of the stored fields for the document (if any). It should return
* a boosting factor: a number higher than 1 increases the computed score, a
* number lower than 1 decreases the score, and a falsy value skips the search
* result completely.
*/
boostDocument?: (documentId: any, term: string) => number,
boostDocument?: (documentId: any, term: string, storedFields?: Record<string, unknown>) => number,

/**
* Controls whether to perform prefix search. It can be a simple boolean, or a
Expand Down Expand Up @@ -1572,7 +1575,7 @@ export default class MiniSearch<T = any> {
termWeight: number,
fieldTermData: FieldTermData | undefined,
fieldBoosts: { [field: string]: number },
boostDocumentFn: ((id: any, term: string) => number) | undefined,
boostDocumentFn: ((id: any, term: string, storedFields?: Record<string, unknown>) => number) | undefined,
bm25params: BM25Params,
results: RawResult = new Map()
): RawResult {
Expand All @@ -1595,7 +1598,7 @@ export default class MiniSearch<T = any> {
continue
}

const docBoost = boostDocumentFn ? boostDocumentFn(this._documentIds.get(docId), derivedTerm) : 1
const docBoost = boostDocumentFn ? boostDocumentFn(this._documentIds.get(docId), derivedTerm, this._storedFields.get(docId)) : 1
if (!docBoost) continue

const termFreq = fieldTermFreqs.get(docId)!
Expand Down

0 comments on commit 68bb989

Please sign in to comment.