1
0
mirror of https://github.com/nextapps-de/flexsearch.git synced 2025-08-08 07:06:59 +02:00
Files
Thomas Wilkerling 103f617ad5 v0.8.2
2025-05-21 13:43:38 +02:00

143 lines
3.1 KiB
HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, height=device-height">
<title>Example: browser-legacy-document-persistent</title>
</head>
<body style="white-space: pre">
<script src="https://cdn.jsdelivr.net/gh/nextapps-de/flexsearch@0.8.2/dist/flexsearch.bundle.min.js"></script>
<script>
(async function(){
// 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 FlexSearch.IndexedDB("my-store");
// create the document index
const index = new FlexSearch.Document({
// hint: the encoder is shared for both index fields
// because primaryTitle and originalTitle has almost
// equal content, otherwise you should set the encoder
// option to each of the field options separately
encoder: FlexSearch.Charset.LatinBalance,
document: {
id: "tconst",
store: true,
index: [{
field: "primaryTitle",
tokenize: "forward"
},{
field: "originalTitle",
tokenize: "forward"
}],
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>