lucaong / minisearch

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

Add addFields and removeFields methods #187

Open lucaong opened 1 year ago

lucaong commented 1 year ago

Resolves: #170

This methods add/remove fields to an existing document.

This is useful to patch some fields in an existing document without having to replace it.

Example:

addFields

const miniSearch = new MiniSearch({ fields: ['title', 'text', 'author'] })

miniSearch.add({ id: 1, title: 'Neuromancer' })

miniSearch.addFields(1, {
  text: 'The sky above the port was the color of television, tuned to a dead channel.',
  author: 'William Gibson'
})

// The above is equivalent to:
miniSearch.add({
  id: 1,
  title: 'Neuromancer',
  text: 'The sky above the port was the color of television, tuned to a dead channel.',
  author: 'William Gibson'
})

removeFields

const miniSearch = new MiniSearch({ fields: ['title', 'text', 'author'] })

miniSearch.add({
  id: 1,
  title: 'Neuromancer',
  text: 'The sky above the port was the color of television, tuned to a dead channel.',
  author: 'William Gibson'
})

miniSearch.removeFields(1, {
  text: 'The sky above the port was the color of television, tuned to a dead channel.',
  author: 'William Gibson'
})

// The above is equivalent to:
miniSearch.add({
  id: 1,
  title: 'Neuromancer'
})