krisk / Fuse

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

Incorrect refIndex #785

Open Taliss opened 2 days ago

Taliss commented 2 days ago

Is there an existing issue for this?

Description of the bug

Hello there and thank you for the great lib.

I have the following scenario using fuse v7.0.0 ( not able to select it in the template ); The returned nested refIndex in matches doesn't look to be zero-based.

Here is a single item from my data set:


    title: 'Most popular functions and keywords',
    name: 'most-popular',
    subSections: [
      {
        name:'parentsOfIssuesInQuery',
        description: 'Find the parents of the issues returned from the subquery (defined in parentheses).',
        examples: [
          {
            note: "Find all the parents of epics in my ACME project:",
            snippet: [`issue in parentsOfIssuesInQuery("project='ACME' and type=Epic")`]
          }]
      },
       {
          name:'linkedIssuesOfQuery',
          description: 'Find issues linked to the issues returned from the subquery (defined in parentheses).',
          examples: [
            {
              note: "Finds issues that block my project ACME:",
              snippet: [`issue in linkedIssuesOfQuery("project=ACME", "is blocked by")`],
            },
            {
              note: "Finds issues that are linked with epics that are in To Do:",
              snippet: [`issue in linkedIssuesOfQuery("type=Epic AND status='To Do'")`],
            }
          ]
      },
      { ... a couple more sections here with same structure }
    ],
  },

 What I have as options ( keys most importnat ):
 const CheatsheetFuse = new Fuse([sections[0]], {
  includeScore: true,
  includeMatches: true,
  minMatchCharLength: 3,
  keys: [
    'title',
    'subSections.examples.note',
    'subSections.examples.snippet'
  ]
});```

Now searching for something like `isue` ( fuzzy ) returns me correct matches, but with the following refIndex ?

key: "subSections.examples.note"
refIndex: 2
value: "Finds issues that are linked with epics that are in To Do:"

Now this is correct, it's inside the second example, but why the refIndex is not zero-based ?
Also is there a way to understand the position of the match when it is inside nested structures ( in this case - example or even example.snippets[] ) Do I have to go with more normalized data and multiple index instances and fire multiple searches to achieve that ?

### The Fuse.js version where this bug is happening.

6.6.2

### Is this a regression?

- [ ] This is a regression bug

### Which version did this behavior use to work in?

None

### Steps To Reproduce

Search for issue in the example from above

### Expected behavior

I would expect refIndexes for nested arrays to start from 0, currently it looks like they start from 1 ?

### Screenshots

<img width="954" alt="Screenshot 2024-11-15 at 10 59 25" src="https://github.com/user-attachments/assets/cc0d1a09-3bc4-470f-8882-0bce1d743f7d">

### Additional context

_No response_
uncenter commented 1 day ago

(Looks like you didn't end the code block syntax correctly btw)