-
-
Notifications
You must be signed in to change notification settings - Fork 499
/
Copy pathindex.js
73 lines (58 loc) · 1.83 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
const { Index } = require("flexsearch");
(async function(){
// 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 = {
tokenize: "forward"
};
// create a simple index which can store id-content-pairs
let index = new Index(config);
// 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 data to the index
data.forEach((item, id) => {
index.add(id, item);
});
// perform query
let result = index.search("cute cat");
// display results
result.forEach(i => {
console.log(data[i]);
});
// -----------------------
// EXPORT
// -----------------------
const fs = require("fs").promises;
await fs.mkdir("./export/").catch(e => {});
await index.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
index = new Index(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");
index.import(file, data);
}));
// perform query
result = index.search("cute cat");
// display results
console.log("-------------------------------------");
result.forEach(i => {
console.log(data[i]);
});
}());