krisk / Fuse

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

Do I need to load all the documents again every time I load an existing index from a file? #747

Closed rafaelcamaram closed 1 month ago

rafaelcamaram commented 7 months ago

Is there an existing issue for this?

Your question

I'm currently storing an index in a .json file and I'd like to be able to read it later (once the app restarts) and be able to query against it again.

Unfortunately, it's not returning the items metadata when I read the index from file. I'm assuming that this is because fuse.js doesn't store the item's metadata but only the indexes?

if (fs.existsSync('./fuse-index.json')) {
    // File exists, so read it from file and create a new fuse
    const fsFile = JSON.parse(fs.readFileSync('./fuse-index.json', 'utf8'));

    const storedIndex = Fuse.parseIndex<Moment>(fsFile);

    // Do I need to add all my data here again?
    database = new Fuse<Moment>([], fuseOptions, storedIndex);
  } else {
    // File does not exists, create a new index from scratch
    database = new Fuse<Moment>([], fuseOptions);

    updateLocalIndex();
  }

The first time I run the app, it probably do everything in memory (and that's why it works fine) and then it outputs me the result below when I try to query:

[
      {
        item: {
          date: '2023-11-06T15:05:41.764Z',
          text: [{ text: 'My Search', x1: 1, x2: 2, y1: 3, y2: 4 }],
          sourceName: 'My Search',
          image: 'string',
        },
        refIndex: 0,
        matches: [
          { indices: [[0, 5]], value: 'My Search', key: 'sourceName' },
          { indices: [[0, 5]], value: 'My Search', key: 'text.text', refIndex: 0 },
        ],
        score: 1.0947644252537633e-47,
      },
    ];

But, when I open the app again and it reads from disk, it didn't return me the item property:

[
      {
        refIndex: 0,
        matches: [
          { indices: [[0, 5]], value: 'My Search', key: 'sourceName' },
          { indices: [[0, 5]], value: 'My Search', key: 'text.text', refIndex: 0 },
        ],
        score: 1.0947644252537633e-47,
      },
    ];

Question: is there any way to make fuse.js to store the metadata and get it working?

cc: @krisk

inmativ commented 6 months ago

I want to use Fuse for my task and I had a similar question. Is it necessary to pass as the first argument all the data that is already present in the index? Is there any way to make the first argument easier?

I have about 100 text documents. I want to pre-index them and don’t want to drag them entirely in runtime.

github-actions[bot] commented 2 months ago

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