Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Precomputed index returns no results #624

Closed
GonzaloZiadi opened this issue Feb 13, 2022 · 2 comments
Closed

Precomputed index returns no results #624

GonzaloZiadi opened this issue Feb 13, 2022 · 2 comments
Labels

Comments

@GonzaloZiadi
Copy link

Same as #524. Copying over from there with slight tweaks.

Describe the bug

Empty results when loading precomputed index.

Version

6.5.3

Is this a regression?

don't know

🔬Minimal Reproduction

const Fuse = require('fuse.js');
const fs = require('fs');

const datasets = [{"title":2},{"title":3}];
const options = {
  keys: ['title']
};
var myIndex = Fuse.createIndex(options.keys, datasets);

fs.writeFileSync('./index.json', JSON.stringify(myIndex.toJSON()));

const fuse = new Fuse(datasets, options, myIndex);

var search_term = process.argv[2];
var result = fuse.search(search_term);
console.log(result);
console.log(myIndex);

Additional context

The last print shows that docs, keys, records is empty, if loaded from the precomputed index.

@GonzaloZiadi
Copy link
Author

GonzaloZiadi commented Feb 13, 2022

Nope, I was wrong. This is a bug on me.

Full working code:

// build step
const csvDatabaseFileName = "database.csv";
const fuseIndexName = "fuse-index.json";
const jsonDatabaseName = "database.json";

const csvString = fs.readFileSync(csvDatabaseFileName).toString();

// See https://www.papaparse.com/docs
const papaJson = Papa.parse(csvString, {
  header: true,
  dynamicTyping: true
});
const headers = papaJson["meta"]["fields"];
const json = papaJson["data"];

fs.writeFileSync(jsonDatabaseName, JSON.stringify(papaJson));

// See https://fusejs.io/api/indexing.html
const fuseIndex = Fuse.createIndex(headers, json);
fs.writeFileSync(fuseIndexName, JSON.stringify(fuseIndex.toJSON()));
// index.html (within script tag)
async function start() {
  const fuseIndexJson = await fetch(fuseIndexName)
      .then(response => response.json());
  const fuseIndex = Fuse.parseIndex(fuseIndexJson);

  const json = await fetch(jsonDatabaseName)
      .then(response => response.json());

  const options = {
    includeScore: true,
    keys: json["meta"]["fields"]
  };
  const fuse = new Fuse(json["data"], options, fuseIndex);

  console.log(fuse.search("<pattern>"));
}

start();

Summary

  1. Read in the CSV file
  2. Convert the CSV file to JSON using PapaParse and write the outputted JSON to file
  3. Precompute the Fuse index and write it to file
  4. Parse the index within script in HTML file, perform search

@GonzaloZiadi
Copy link
Author

Sorry about that and thanks for this great library

dbaynard added a commit to dbaynard/Fuse that referenced this issue Oct 16, 2024
It is far from obvious, here and in the examples, how to use this function, and if supplied an incorrect argument it errs, silently.

The simplest way to avoid the confusion is to document the change. Narrowing the type is the first part of that.

See krisk#524 and krisk#624 for people bitten by this.
dbaynard added a commit to dbaynard/Fuse that referenced this issue Oct 17, 2024
It is far from obvious, here and in the examples, how to use this function, and if supplied an incorrect argument it errs, silently.

The simplest way to avoid the confusion is to document the change. Narrowing the type is the first part of that.

See krisk#524 and krisk#624 for people bitten by this.
dbaynard added a commit to dbaynard/Fuse that referenced this issue Oct 17, 2024
Narrowing the type of the parameter from `any` makes it obvious that the
input is an `Object`, rather than a `String`.

With the `any` type as input, it was not obvious how to use this
function.

See krisk#524 and krisk#624 for people bitten by this.
krisk pushed a commit that referenced this issue Feb 3, 2025
Narrowing the type of the parameter from `any` makes it obvious that the
input is an `Object`, rather than a `String`.

With the `any` type as input, it was not obvious how to use this
function.

See #524 and #624 for people bitten by this.
krisk pushed a commit that referenced this issue Feb 3, 2025
Narrowing the type of the parameter from `any` makes it obvious that the
input is an `Object`, rather than a `String`.

With the `any` type as input, it was not obvious how to use this
function.

See #524 and #624 for people bitten by this.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant