krisk / Fuse

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

Question about result #562

Closed mikeerickson closed 3 years ago

mikeerickson commented 3 years ago

I am giving this module a try, but I am having some difficulty understanding the results. I am sure this is pilot error, so if you could enlighten me on what is actually happening, it will help me better understand.

In the following example, the results are listed at the bottom. Specifically, when providing a search term of "Kira ABC", I am "expecting" results to include all three items, when in fact it is only return the last two items (associated to Kira) but omitting the first one (associated to ABC)

If I do a second query of "ABC Kira" it does return the expected rows which leads to my confusion and needing more information.

Again, I am sure this is pilot error, but I cant seem to find anything in the documentation which documents the order and precedence of queries.

And, I am not able to find anything which documents what refIndex value indicates

const { dd } = require('dumper.js')
const Fuse = require('../dist/fuse')

const customerList = [
  {
    title: 'ABC Company',
    contact: { firstName: 'Mike', lastName: 'Erickson' }
  },
  {
    title: 'Another Company',
    contact: { firstName: 'Mike', lastName: 'Erickson' }
  },
  {
    title: 'XYZ Company',
    contact: { firstName: 'Kira', lastName: 'Erickson' }
  },
  {
    title: 'Foobar Company',
    contact: { firstName: 'Kira', lastName: 'Erickson' }
  }
]

const setup = (itemList, overwriteOptions) => {
  const list = itemList || []
  const options = { ...{}, ...overwriteOptions }

  return new Fuse(list, options)
}

let fuse = setup(customerList, {
  score: true,
  keys: ['title', 'contact.firstName']
})

let result = fuse.search('Kire ABC')

Results 1

/Users/mikee/Documents/code/js/Fuse/sandbox/simple.js:33:
array (size=2) [
    [0] => object (size=2) {
        'item' => object (size=2) {
            'title' => string "XYZ Company" (length=11),
            'contact' => object (size=2) {
                'firstName' => string "Kira" (length=4),
                'lastName' => string "Erickson" (length=8),
            },
        },
        'refIndex' => int 1,
    },
    [1] => object (size=2) {
        'item' => object (size=2) {
            'title' => string "Foobar Company" (length=14),
            'contact' => object (size=2) {
                'firstName' => string "Kira" (length=4),
                'lastName' => string "Erickson" (length=8),
            },
        },
        'refIndex' => int 2,
    },
]

Result 2

/Users/mikee/Documents/code/js/Fuse/sandbox/simple.js:39:
array (size=4) [
    [0] => object (size=2) {
        'item' => object (size=2) {
            'title' => string "Foobar Company" (length=14),
            'contact' => object (size=2) {
                'firstName' => string "Kira" (length=4),
                'lastName' => string "Erickson" (length=8),
            },
        },
        'refIndex' => int 3,
    },
    [1] => object (size=2) {
        'item' => object (size=2) {
            'title' => string "XYZ Company" (length=11),
            'contact' => object (size=2) {
                'firstName' => string "Kira" (length=4),
                'lastName' => string "Erickson" (length=8),
            },
        },
        'refIndex' => int 2,
    },
    [2] => object (size=2) {
        'item' => object (size=2) {
            'title' => string "ABC Company" (length=11),
            'contact' => object (size=2) {
                'firstName' => string "Mike" (length=4),
                'lastName' => string "Erickson" (length=8),
            },
        },
        'refIndex' => int 0,
    },
    [3] => object (size=2) {
        'item' => object (size=2) {
            'title' => string "Another Company" (length=15),
            'contact' => object (size=2) {
                'firstName' => string "Mike" (length=4),
                'lastName' => string "Erickson" (length=8),
            },
        },
        'refIndex' => int 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

danisztls commented 3 years ago

@mikeerickson refIndex is the index of the item on the index object. Try pre-indexing with Fuse.createIndex instead of letting Fuje.js do it behind the scenes.