krisk / Fuse

Lightweight fuzzy-search, in JavaScript
https://fusejs.io/
Apache License 2.0
17.89k stars 759 forks source link

custom id instead of index for large database #531

Closed tesla-cat closed 3 years ago

tesla-cat commented 3 years ago

motivation

it is not efficient to access the entire doc collection when it is large

code

I use index only, without accessing the entire collection


const Fuse = require('fuse.js')

const options = {
  keys: ['title']
}

function updateIndex(oldIndex, newDoc){
  if(oldIndex){
    const myIndex = Fuse.parseIndex(oldIndex)
    const fuse = new Fuse([], options, myIndex)
    fuse.add(newDoc)
    return fuse.getIndex()
  }
  else{
    return Fuse.createIndex(['title'], [newDoc])
  }
}

const idx1 = updateIndex(null, { title: 'hello', id: 'id1' })
console.log(idx1.size())

const idx2 = updateIndex(idx1, { title: 'world', id: 'id2' })
console.log(idx2.size())

const fuse = new Fuse([], options, idx2)
console.log(fuse.search('hel'))
console.log(fuse.search('wor'))
console.log(fuse.search('hello world'))

result

this works but I only get its number index

1
2
[ { item: undefined, refIndex: 0 } ]
[ { item: undefined, refIndex: 1 } ]
[ { item: undefined, refIndex: 0 }, { item: undefined, refIndex: 1 } ]

question

how can I get id1, id2 instead of the numbers 0, 1

github-actions[bot] commented 3 years ago

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days