nextapps-de / flexsearch

Next-Generation full text search library for Browser and Node.js
Apache License 2.0
12.54k stars 492 forks source link

v0.7.0 - v0.7.11: can not determine when the exporting process has been completed #235

Closed haysclark closed 3 years ago

haysclark commented 3 years ago

The new export((key, data) => ()) API that uses callbacks makes it impossible to know when the export-ing process is complete.

ts-thomas commented 3 years ago

Thanks for the report. I will add a Promise around the whole job in the next version.

browniefed commented 3 years ago

On a similar topic of import/export, I've got no idea why this would/wouldn't work. It states that import is async but I cannot tell whether that is true or not as it just returns undefined rather than a Promise or a callback, or something.

Just in general I cannot seem to get import/export working with a filesystem or just in memory at all.

Basically my goal is to write write key as a filename and write the data, then at a future point import the index and do a search. That just doesn't seem to work.

const { Index } = require("flexsearch");

const index = new Index();
const index2 = new Index();

// Loop and index bunch-o-content
index.add(item.title, content);

const result = index.search("textinput");

index.export((key, data) => {
// Just testing if we can recreate a search
  index2.import(key, data);
// Saving for future
  fs.writeFile(`./index/${key}`, data);
});

setTimeout(() => {
  const res = index2.search("textinput");
  console.log(result);
  console.log(res);
}, 1000);

// Index initial result // Index 2 no results even after import

[
// Returns results from initial Index
]
// res has no results even though we imported and did the same search.
[]

Using: "flexsearch": "^0.7.11",

joebochill commented 3 years ago

I'm having the exact same issue as @browniefed with export/import. There's always zero results on the index doing the importing. I'm sure I'm doing something wrong, but I'm not sure what.

jlvandenhout commented 3 years ago

From the documentation I should be able to use the asynchronous fs functions, as they return Promises as required by the documentation for asynchronous exports, like so:

import fs from 'fs/promises'
import { Document } from 'flexsearch'

const index = new Document(
  // Document schema...
)

// Fill index with data...

index.export((key, data) => fs.writeFile(key, data))

And it does, but only writes out one file called reg filled with a small JSON object containing only numbers. If I just JSON.stringify the index, I get a whole lot of more information. I'm sure I'm misunderstanding something how to export properly?

cutiful commented 3 years ago

@browniefed @joebochill I have the same issue, figured that it's a bug related to key names. I think that @ts-thomas meant to use the field variable for document fields, but didn't account for recursion here: https://github.com/nextapps-de/flexsearch/blob/65b027ca316ceefd8ea89f472561a0f91179b2f3/src/serialize.js#L85

If a field is passed, this code appends the next key to it and calls itself with the result as the next field. It would work in a loop, with a static field. But it recurses and ends up appending each next key to it.

This code seems to work for me with a simple, non-Document index (not exactly what I'm running, but, for the sake of simplicity):

const index = new Index();

for (const key of Object.keys(localStorage))
  index.import(key.split(".").slice(-1)[0], localStorage.getItem(key));
index.search(query);

What I did is removed the erroneously appended keys from the key string.