lucaong / minisearch

Tiny and powerful JavaScript full-text search engine for browser and Node
https://lucaong.github.io/minisearch/
MIT License
4.81k stars 137 forks source link

Pass stored fields to boostDocument callback #213

Closed lucaong closed 1 year ago

lucaong commented 1 year ago

This makes it more convenient to perform dynamic document boosting:

const documents = [
  {
    id: 1,
    text: 'Some example text',
    boostFactor: 1.5
  },
  {
    id: 2,
    text: 'Some other example text',
    boostFactor: 0.9
  },
  {
    id: 3,
    text: 'Some more example text',
    boostFactor: null
  }
]

// Initialize MiniSearch specifying storeFields
const miniSearch = new MiniSearch({
  fields: ['text'],
  storeFields: ['boostFactor']
})

// Upon search, use the `boostDocument` search option to specify a dynamic boosting factor on the basis of a stored field:
miniSearch.search('example', {
  boostDocument: (_docId, _term, storedFields) => storedFields.boostFactor || 1
})