mirror of
https://github.com/nextapps-de/flexsearch.git
synced 2025-08-07 22:56:59 +02:00
132 lines
2.7 KiB
HTML
132 lines
2.7 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-module-document-worker</title>
|
|
</head>
|
|
<body style="white-space: pre">
|
|
<script type="module">
|
|
|
|
// you can't load from CDN because of Same-Origin-Policy
|
|
import { Document } from "../../../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 the document index
|
|
const index = new Document({
|
|
worker: true,
|
|
document: {
|
|
id: "tconst",
|
|
store: true,
|
|
index: [{
|
|
field: "primaryTitle",
|
|
tokenize: "forward",
|
|
encoder: "LatinBalance"
|
|
},{
|
|
field: "originalTitle",
|
|
tokenize: "forward",
|
|
encoder: "LatinBalance"
|
|
}],
|
|
tag: [{
|
|
field: "startYear"
|
|
},{
|
|
field: "genres"
|
|
}]
|
|
}
|
|
});
|
|
|
|
// add test data
|
|
for(let i = 0; i < data.length; i++){
|
|
await index.add(data[i]);
|
|
}
|
|
|
|
// 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> |