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

Is there a method for exporting an index as a single json object? #299

Closed
budarin opened this issue Dec 9, 2021 · 3 comments
Closed

Is there a method for exporting an index as a single json object? #299

budarin opened this issue Dec 9, 2021 · 3 comments

Comments

@budarin
Copy link

budarin commented Dec 9, 2021

Need to store index as an object in browser caches but not found a method which could help with it

@rpsirois
Copy link

rpsirois commented Apr 4, 2022

#273
#274
#290
#299

function exportIndex(flexSearchIndex) {
  // https://github.com/nextapps-de/flexsearch/issues/299
  // https://github.com/nextapps-de/flexsearch/issues/274
  return new Promise((res, rej) => {
    let pkg = [];
    const expected = new Set([
      "reg",
      "reg.cfg",
      "reg.cfg.map",
      "reg.cfg.map.ctx"
    ]);
    const received = new Set();

    const setsEq = (a, b) => {
      if (a.size != b.size) {
        return false;
      }

      return Array.from(a).every(el => b.has(el));
    };

    flexSearchIndex.export((key, data) => {
      // https://github.com/nextapps-de/flexsearch/issues/290
      // https://github.com/nextapps-de/flexsearch/issues/273
      pkg.push([
        key
          .toString()
          .split(".")
          .pop(),
        data
      ]);
      received.add(key);

      if (setsEq(expected, received)) {
        res(JSON.stringify(pkg));
      }
    });
  });
}

@rxliuli
Copy link

rxliuli commented Sep 21, 2022

Just add a version implemented by ts

function exportIndex(flexSearchIndex: Index) {
  // https://github.com/nextapps-de/flexsearch/issues/299
  // https://github.com/nextapps-de/flexsearch/issues/274
  return new Promise<[string, any][]>((res, rej) => {
    const pkg: [string, any][] = []
    const expected = new Set([
      'reg',
      'reg.cfg',
      'reg.cfg.map',
      'reg.cfg.map.ctx',
    ])
    const received = new Set<string>()

    const setsEq = (a: Set<string>, b: Set<string>) => {
      if (a.size != b.size) {
        return false
      }

      return Array.from(a).every((el) => b.has(el))
    }

    flexSearchIndex.export((key, data) => {
      // https://github.com/nextapps-de/flexsearch/issues/290
      // https://github.com/nextapps-de/flexsearch/issues/273
      pkg.push([key.toString().split('.').pop()!, data])
      received.add(key as string)

      if (setsEq(expected, received)) {
        res(pkg)
      }
    })
  })
}

@ts-thomas
Copy link
Contributor

There will be an update in the next version which returns promises instead of applying inline awaits.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants