-
-
Notifications
You must be signed in to change notification settings - Fork 499
/
Copy pathindex.html
140 lines (125 loc) · 2.93 KB
/
index.html
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, height=device-height">
<title>Example: browser-module-document-persistent</title>
</head>
<body style="white-space: pre">
<script type="module">
import { Document, Charset, IndexedDB } from "https://cdn.jsdelivr.net/gh/nextapps-de/[email protected]/dist/flexsearch.bundle.module.min.js";
// some test data
const data = [{
"tconst": "tt0000001",
"titleType": "short",
"primaryTitle": "Carmencita",
"originalTitle": "Carmencita",
"isAdult": 0,
"startYear": "1894",
"endYear": "",
"runtimeMinutes": "1",
"genres": [
"Documentary",
"Short"
]
},{
"tconst": "tt0000002",
"titleType": "short",
"primaryTitle": "Le clown et ses chiens",
"originalTitle": "Le clown et ses chiens",
"isAdult": 0,
"startYear": "1892",
"endYear": "",
"runtimeMinutes": "5",
"genres": [
"Animation",
"Short"
]
}];
// create DB instance with namespace
const db = new IndexedDB("my-store");
// create the document index
const index = new Document({
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"
}]
}
});
await index.mount(db);
// await document.destroy();
// await document.mount(db);
// add test data
for(let i = 0; i < data.length; i++){
index.add(data[i]);
}
// transfer changes (bulk)
await index.commit();
// perform a query
const result = await index.search({
query: "karmen",
tag: {
"startYear": "1894",
"genres": [
"Documentary",
"Short"
]
},
enrich: true
});
// display results
console.log(result);
log(JSON.stringify(result, null, 2));
log("\n-------------------------------------\n");
// perform a query + merge results
const merged = await index.search({
query: "karmen",
tag: {
"startYear": "1894",
"genres": [
"Documentary",
"Short"
]
},
enrich: true,
merge: true
});
// display results
console.log(merged);
log(JSON.stringify(merged, null, 2));
log("\n-------------------------------------\n");
// perform a query + get suggestions
const suggestions = await index.search({
query: "karmen or clown or not found",
tag: {
// no data for this category:
"genres": "Movie"
},
suggest: true,
enrich: true,
merge: true
});
// display results
console.log(suggestions);
log(JSON.stringify(suggestions, null, 2));
function log(str){
document.body.appendChild(
document.createTextNode(str + "\n")
);
}
</script>
</body>
</html>