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

Import/export broken in 0.7.21? #290

Closed
alangibson opened this issue Nov 13, 2021 · 4 comments
Closed

Import/export broken in 0.7.21? #290

alangibson opened this issue Nov 13, 2021 · 4 comments
Assignees
Labels
bug Something isn't working

Comments

@alangibson
Copy link

I can't seem to get index export/import working in 0.7.21. If I export an index and immediately import it, then I never get any search results. I'm not sure if I'm reading the docs wrong, or if this really is a bug.

The following test fails:

`import { assert } from 'chai';
import { Index, IndexSearchResult } from 'flexsearch';

describe('flexsearch', () => {

it('should search after export and import', async () => {
    
    // Given
    const exportIndex = new Index();
    const importIndex = new Index();
    await exportIndex.addAsync('one', 'stuff and junk');
    await exportIndex.addAsync('two', 'derp derp');

    // When
    await exportIndex.export(async (key, value) => {
        await importIndex.import(key, value);
    });
    const r: IndexSearchResult = await importIndex.searchAsync('junk');
    
    // Then
    assert.equal(r.length, 1);
});

});`

@alangibson alangibson changed the title Import/export broken in 0.7.21 Import/export broken in 0.7.21? Nov 13, 2021
@alangibson
Copy link
Author

I think I figured it out. There were actually 3 problems. This test now passes:

` it('should search after export and import', async () => {

    // Given
    const exportIndex = new Index();
    const importIndex = new Index();
    await exportIndex.addAsync('one', 'stuff and junk');
    await exportIndex.addAsync('two', 'derp derp');

    // When
    
    // We need to fix the key names, so store them here
    const d: { [key: string]: string } = {};
    // Contrary to the docs, neither export() nor import() are actually 
    // declared async
    exportIndex.export((key: string|number, value) => {
        // Keys don't match between import() and export()
        // export() nests keys like reg, reg.cfg, reg.cfg.map, and reg.cfg.map.ctx
        // but import() wants them flat like reg, cfg, map, ctx
        const k = key.toString().split('.').pop() || '';
        d[k] = value;
        importIndex.import(k, value);
    });

    // We have to sleep because of function async() in serialize.js
    // It looks like some homemade async. There is a comment stating
    // "await isn't supported by ES5"
    await new Promise(resolve => setTimeout(resolve, 3000));

    const r: IndexSearchResult = await importIndex.searchAsync('junk');

    // Then
    assert.equal(r.length, 1);
});`

@Properko
Copy link

Properko commented Nov 16, 2021

I'm having the same problem with export asynchronicity.
I check for exporting store.json before resolving the promise, which seems to always be the last key to go, but it's pretty ugly to rely on the internals like this. I find it preferable to timeouts though.

const exportAsync = (docIndex, filePath) =>
  new Promise((resolve, reject) => {
    try {
      return docIndex.export(async (key, data) => {
        try {
          await fsPromises.mkdir(filePath, { recursive: true });
          const filename = `${filePath}/${key}.json`;
          await fsPromises.writeFile(filename, data !== undefined ? data : '');
          if (key === 'store') {
            resolve(); // store is the last to go, but this relies on internals and assumes no error occurs in the process :(
          }
        } catch (err) {
          reject(err);
        }
      });
    } catch (err) {
      reject(err);
    }
  });

@o0101
Copy link

o0101 commented Dec 16, 2021

@alangibson thanks! This worked for me, too. Un-nesting the keys and using the last step in the key path, works a treat. Thank you! 😛 😉 xx 😜

    exportIndex.export((key: string|number, value) => {
        // Keys don't match between import() and export()
        // export() nests keys like reg, reg.cfg, reg.cfg.map, and reg.cfg.map.ctx
        // but import() wants them flat like reg, cfg, map, ctx
        const k = key.toString().split('.').pop() || '';
        d[k] = value;
        importIndex.import(k, value);
    });

✔️ SOLVED

@ts-thomas
Copy link
Contributor

This is now fixed in v0.7.23
Further improvements to provide Promise.all() compatible export ist coming in next version.

ts-thomas added a commit that referenced this issue Oct 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants