mirror of
https://github.com/nextapps-de/flexsearch.git
synced 2025-08-12 00:54:49 +02:00
worker index basic support export/import
This commit is contained in:
29
example/nodejs-commonjs/basic-worker-export-import/config.js
Normal file
29
example/nodejs-commonjs/basic-worker-export-import/config.js
Normal file
@@ -0,0 +1,29 @@
|
||||
const { Encoder } = require("flexsearch");
|
||||
const fs = require("fs").promises;
|
||||
|
||||
(async function(){
|
||||
await fs.mkdir("./export/").catch(e => {});
|
||||
}());
|
||||
|
||||
module.exports = {
|
||||
tokenize: "forward",
|
||||
encoder: new Encoder({
|
||||
normalize: function(str){
|
||||
return str.toLowerCase();
|
||||
}
|
||||
}),
|
||||
export: async function(key, data){
|
||||
|
||||
await fs.writeFile("./export/" + key, data, "utf8");
|
||||
},
|
||||
import: async function(index){
|
||||
|
||||
let files = await fs.readdir("./export/");
|
||||
files = await Promise.all(files);
|
||||
|
||||
for(let i = 0; i < files.length; i++){
|
||||
const data = await fs.readFile("./export/" + files[i], "utf8");
|
||||
index.import(files[i], data);
|
||||
}
|
||||
}
|
||||
};
|
68
example/nodejs-commonjs/basic-worker-export-import/index.js
Normal file
68
example/nodejs-commonjs/basic-worker-export-import/index.js
Normal file
@@ -0,0 +1,68 @@
|
||||
const { Worker: WorkerIndex } = require("flexsearch");
|
||||
|
||||
(async function(){
|
||||
|
||||
// create a simple index which can store id-content-pairs
|
||||
// and await (!) for the worker response
|
||||
let index = await new WorkerIndex({
|
||||
tokenize: "forward",
|
||||
config: __dirname + "/config.js"
|
||||
});
|
||||
|
||||
// some test data
|
||||
const data = [
|
||||
'cats abcd efgh ijkl mnop qrst uvwx cute',
|
||||
'cats abcd efgh ijkl mnop qrst cute',
|
||||
'cats abcd efgh ijkl mnop cute',
|
||||
'cats abcd efgh ijkl cute',
|
||||
'cats abcd efgh cute',
|
||||
'cats abcd cute',
|
||||
'cats cute'
|
||||
];
|
||||
|
||||
// add test data
|
||||
data.forEach((item, id) => {
|
||||
index.add(id, item);
|
||||
});
|
||||
|
||||
// perform query
|
||||
let result = await index.search({
|
||||
query: "cute cat",
|
||||
});
|
||||
|
||||
// display results
|
||||
result.forEach(i => {
|
||||
console.log(data[i]);
|
||||
});
|
||||
|
||||
// -----------------------
|
||||
// EXPORT
|
||||
// -----------------------
|
||||
|
||||
await index.export(function(){
|
||||
// do nothing here
|
||||
});
|
||||
|
||||
index = await new WorkerIndex({
|
||||
tokenize: "forward",
|
||||
config: __dirname + "/config.js"
|
||||
});
|
||||
|
||||
// -----------------------
|
||||
// IMPORT
|
||||
// -----------------------
|
||||
|
||||
await index.import();
|
||||
|
||||
// perform query
|
||||
result = await index.search({
|
||||
query: "cute cat",
|
||||
});
|
||||
|
||||
// display results
|
||||
console.log("-------------------------------------");
|
||||
result.forEach(i => {
|
||||
console.log(data[i]);
|
||||
});
|
||||
|
||||
}());
|
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"name": "nodejs-commonjs-basic-worker-extern-config",
|
||||
"dependencies": {
|
||||
"flexsearch": "^0.8.105"
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user