-
-
Notifications
You must be signed in to change notification settings - Fork 499
/
Copy pathindex.js
101 lines (87 loc) · 2.47 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
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 => {});
// call export
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");
// call import
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);
}());