1
0
mirror of https://github.com/nextapps-de/flexsearch.git synced 2025-09-25 21:08:59 +02:00
Files
flexsearch/example/nodejs-commonjs/document-export-import/index.js
2025-03-17 01:13:36 +01:00

99 lines
2.4 KiB
JavaScript

const { Document, Charset } = require("flexsearch");
const fs = require("fs").promises;
(async function(){
// loading test data
const data = JSON.parse(await fs.readFile(__dirname + "/data.json", "utf8"));
// you will need to keep the index configuration
// they will not export, also every change to the
// configuration requires a full re-index
const config = {
document: {
id: "tconst",
store: true,
index: [{
field: "primaryTitle",
tokenize: "forward",
encoder: Charset.LatinBalance
},{
field: "originalTitle",
tokenize: "forward",
encoder: Charset.LatinBalance
}],
tag: [{
field: "startYear"
},{
field: "genres"
}]
}
};
// create the document index
let document = new Document(config);
// add test data
for(let i = 0; i < data.length; i++){
document.add(data[i]);
}
// perform a query
let result = document.search({
query: "karmen",
tag: {
"startYear": "1894",
"genres": [
"Documentary",
"Short"
]
},
suggest: true,
enrich: true,
merge: true
});
// output results
console.log(result);
// EXPORT
// -----------------------
await fs.mkdir("./export/").catch(e => {});
await document.export(async function(key, data){
await fs.writeFile("./export/" + key, data, "utf8");
});
// IMPORT
// -----------------------
// create the same type of index you have used by .export()
// along with the same configuration
document = new Document(config);
// load them in parallel
const files = await fs.readdir("./export/");
await Promise.all(files.map(async file => {
const data = await fs.readFile("./export/" + file, "utf8");
await document.import(file, data);
}))
// perform query
result = document.search({
query: "karmen",
tag: {
"startYear": "1894",
"genres": [
"Documentary",
"Short"
]
},
suggest: true,
enrich: true,
merge: true
});
// output results
console.log("-------------------------------------");
console.log(result);
}());