mirror of
https://github.com/nextapps-de/flexsearch.git
synced 2025-08-29 00:30:06 +02:00
update examples
This commit is contained in:
62
example/browser-module/basic-persistent/index.html
Normal file
62
example/browser-module/basic-persistent/index.html
Normal file
@@ -0,0 +1,62 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, height=device-height">
|
||||
<title>Example: browser-basic-persistent</title>
|
||||
</head>
|
||||
<body style="white-space: pre">
|
||||
<script type="module">
|
||||
|
||||
import { Index, IndexedDB } from "https://rawcdn.githack.com/nextapps-de/flexsearch/v0.8-preview/dist/flexsearch.bundle.module.min.js";
|
||||
|
||||
// create DB instance with namespace
|
||||
const db = new IndexedDB("my-store");
|
||||
|
||||
// create a simple index which can store id-content-pairs
|
||||
const index = new Index({
|
||||
tokenize: "forward"
|
||||
});
|
||||
|
||||
// mount database to the index
|
||||
await index.mount(db);
|
||||
// await index.destroy();
|
||||
// await index.mount(db);
|
||||
|
||||
// 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'
|
||||
];
|
||||
|
||||
data.forEach((item, id) => {
|
||||
index.add(id, item);
|
||||
});
|
||||
|
||||
// transfer changes (bulk)
|
||||
await index.commit();
|
||||
|
||||
const result = await index.search({
|
||||
query: "cute cat",
|
||||
suggest: true
|
||||
});
|
||||
|
||||
console.log(result);
|
||||
|
||||
result.forEach(i => {
|
||||
log(data[i]);
|
||||
});
|
||||
|
||||
function log(str){
|
||||
document.body.appendChild(
|
||||
document.createTextNode(str + "\n")
|
||||
);
|
||||
}
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
52
example/browser-module/basic-suggestion/index.html
Normal file
52
example/browser-module/basic-suggestion/index.html
Normal file
@@ -0,0 +1,52 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, height=device-height">
|
||||
<title>Example: browser-basic-suggestion</title>
|
||||
</head>
|
||||
<body style="white-space: pre">
|
||||
<script type="module">
|
||||
|
||||
import { Index } from "https://rawcdn.githack.com/nextapps-de/flexsearch/v0.8-preview/dist/flexsearch.light.module.min.js";
|
||||
|
||||
// create a simple index which can store id-content-pairs
|
||||
const index = new Index({
|
||||
tokenize: "forward"
|
||||
});
|
||||
|
||||
// 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'
|
||||
];
|
||||
|
||||
data.forEach((item, id) => {
|
||||
index.add(id, item);
|
||||
});
|
||||
|
||||
// perform query
|
||||
const result = index.search({
|
||||
query: "black or cute or yellow cat",
|
||||
suggest: true
|
||||
});
|
||||
|
||||
// display results
|
||||
result.forEach(i => {
|
||||
console.log(data[i]);
|
||||
log(data[i]);
|
||||
});
|
||||
|
||||
function log(str){
|
||||
document.body.appendChild(
|
||||
document.createTextNode(str + "\n")
|
||||
);
|
||||
}
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
52
example/browser-module/basic-worker-extern-config/index.html
Normal file
52
example/browser-module/basic-worker-extern-config/index.html
Normal file
@@ -0,0 +1,52 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, height=device-height">
|
||||
<title>Example: browser-basic-worker-extern-config</title>
|
||||
</head>
|
||||
<body style="white-space: pre">
|
||||
<script type="module">
|
||||
|
||||
import { Index } from "https://rawcdn.githack.com/nextapps-de/flexsearch/v0.8-preview/dist/flexsearch.bundle.module.min.js";
|
||||
|
||||
// create a simple index which can store id-content-pairs
|
||||
const index = new Index({
|
||||
tokenize: "forward"
|
||||
});
|
||||
|
||||
// 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'
|
||||
];
|
||||
|
||||
data.forEach((item, id) => {
|
||||
index.add(id, item);
|
||||
});
|
||||
|
||||
// perform query
|
||||
const result = await index.search({
|
||||
query: "cute cat",
|
||||
suggest: true
|
||||
});
|
||||
|
||||
// display results
|
||||
result.forEach(i => {
|
||||
console.log(data[i]);
|
||||
log(data[i]);
|
||||
});
|
||||
|
||||
function log(str){
|
||||
document.body.appendChild(
|
||||
document.createTextNode(str + "\n")
|
||||
);
|
||||
}
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
52
example/browser-module/basic-worker/index.html
Normal file
52
example/browser-module/basic-worker/index.html
Normal file
@@ -0,0 +1,52 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, height=device-height">
|
||||
<title>Example: browser-basic-worker</title>
|
||||
</head>
|
||||
<body style="white-space: pre">
|
||||
<script type="module">
|
||||
|
||||
import { Worker as WorkerIndex } from "https://rawcdn.githack.com/nextapps-de/flexsearch/v0.8-preview/dist/flexsearch.bundle.module.min.js";
|
||||
|
||||
// create a simple index which can store id-content-pairs
|
||||
const index = new WorkerIndex({
|
||||
tokenize: "forward"
|
||||
});
|
||||
|
||||
// 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'
|
||||
];
|
||||
|
||||
data.forEach((item, id) => {
|
||||
index.add(id, item);
|
||||
});
|
||||
|
||||
// perform query
|
||||
const result = await index.search({
|
||||
query: "cute cat",
|
||||
suggest: true
|
||||
});
|
||||
|
||||
// display results
|
||||
result.forEach(i => {
|
||||
console.log(data[i]);
|
||||
log(data[i]);
|
||||
});
|
||||
|
||||
function log(str){
|
||||
document.body.appendChild(
|
||||
document.createTextNode(str + "\n")
|
||||
);
|
||||
}
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
55
example/browser-module/basic/index.html
Normal file
55
example/browser-module/basic/index.html
Normal file
@@ -0,0 +1,55 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, height=device-height">
|
||||
<title>Example: browser-basic</title>
|
||||
</head>
|
||||
<body style="white-space: pre">
|
||||
<script type="module">
|
||||
|
||||
import { Index } from "https://rawcdn.githack.com/nextapps-de/flexsearch/v0.8-preview/dist/flexsearch.light.module.min.js";
|
||||
|
||||
// create a simple index which can store id-content-pairs
|
||||
const index = new Index({
|
||||
tokenize: "forward"
|
||||
});
|
||||
|
||||
// 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'
|
||||
];
|
||||
|
||||
data.forEach((item, id) => {
|
||||
index.add(id, item);
|
||||
});
|
||||
|
||||
index.search("cute cats").forEach(i => {
|
||||
const result = data[i];
|
||||
log(result);
|
||||
});
|
||||
|
||||
log("\n----------------------\n");
|
||||
|
||||
index.search({
|
||||
query: "black or cute or yellow cats",
|
||||
suggest: true
|
||||
}).forEach(i => {
|
||||
const result = data[i];
|
||||
log(result);
|
||||
});
|
||||
|
||||
function log(str){
|
||||
document.body.appendChild(
|
||||
document.createTextNode(str + "\n")
|
||||
);
|
||||
}
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
140
example/browser-module/document-persistent/index.html
Normal file
140
example/browser-module/document-persistent/index.html
Normal file
@@ -0,0 +1,140 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, height=device-height">
|
||||
<title>Example: browser-document-persistent</title>
|
||||
</head>
|
||||
<body style="white-space: pre">
|
||||
<script type="module">
|
||||
|
||||
import { Document, Charset, IndexedDB } from "https://rawcdn.githack.com/nextapps-de/flexsearch/v0.8-preview/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>
|
@@ -0,0 +1,17 @@
|
||||
import { Encoder, Charset } from "https://rawcdn.githack.com/nextapps-de/flexsearch/v0.8-preview/dist/flexsearch.bundle.module.min.js";
|
||||
import EnglishPreset from "https://rawcdn.githack.com/nextapps-de/flexsearch/v0.8-preview/dist/module/lang/en.js";
|
||||
|
||||
export default {
|
||||
tokenize: "forward",
|
||||
encoder: new Encoder(
|
||||
Charset.LatinBalance,
|
||||
EnglishPreset,
|
||||
{
|
||||
normalize: function(str){
|
||||
return str.toLowerCase();
|
||||
},
|
||||
filter: false,
|
||||
minlength: 3
|
||||
}
|
||||
)
|
||||
};
|
@@ -0,0 +1,17 @@
|
||||
import { Encoder, Charset } from "https://rawcdn.githack.com/nextapps-de/flexsearch/v0.8-preview/dist/flexsearch.bundle.module.min.js";
|
||||
import EnglishPreset from "https://rawcdn.githack.com/nextapps-de/flexsearch/v0.8-preview/dist/module/lang/en.js";
|
||||
|
||||
export default {
|
||||
tokenize: "forward",
|
||||
encoder: new Encoder(
|
||||
Charset.LatinBalance,
|
||||
EnglishPreset,
|
||||
{
|
||||
normalize: function(str){
|
||||
return str.toLowerCase();
|
||||
},
|
||||
filter: false,
|
||||
minlength: 3
|
||||
}
|
||||
)
|
||||
};
|
130
example/browser-module/document-worker-extern-config/index.html
Normal file
130
example/browser-module/document-worker-extern-config/index.html
Normal file
@@ -0,0 +1,130 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, height=device-height">
|
||||
<title>Example: browser-document-worker-extern-config</title>
|
||||
</head>
|
||||
<body style="white-space: pre">
|
||||
<script type="module">
|
||||
|
||||
import { Document } from "https://rawcdn.githack.com/nextapps-de/flexsearch/v0.8-preview/dist/flexsearch.bundle.module.min.js";
|
||||
const dirname = import.meta.url.replace("/index.html", "");
|
||||
|
||||
// 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 and await (!) for the instance response
|
||||
const index = await new Document({
|
||||
worker: true,
|
||||
document: {
|
||||
id: "tconst",
|
||||
store: true,
|
||||
index: [{
|
||||
field: "primaryTitle",
|
||||
config: dirname + "/config.primaryTitle.js"
|
||||
},{
|
||||
field: "originalTitle",
|
||||
config: dirname + "/config.originalTitle.js"
|
||||
}],
|
||||
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>
|
131
example/browser-module/document-worker/index.html
Normal file
131
example/browser-module/document-worker/index.html
Normal file
@@ -0,0 +1,131 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, height=device-height">
|
||||
<title>Example: browser-document-worker</title>
|
||||
</head>
|
||||
<body style="white-space: pre">
|
||||
<script type="module">
|
||||
|
||||
import { Document } from "https://rawcdn.githack.com/nextapps-de/flexsearch/v0.8-preview/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>
|
129
example/browser-module/document/index.html
Normal file
129
example/browser-module/document/index.html
Normal file
@@ -0,0 +1,129 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, height=device-height">
|
||||
<title>Example: browser-document</title>
|
||||
</head>
|
||||
<body style="white-space: pre">
|
||||
<script type="module">
|
||||
|
||||
import { Document, Charset } from "https://rawcdn.githack.com/nextapps-de/flexsearch/v0.8-preview/dist/flexsearch.compact.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({
|
||||
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"
|
||||
}]
|
||||
}
|
||||
});
|
||||
|
||||
// add test data
|
||||
for(let i = 0; i < data.length; i++){
|
||||
index.add(data[i]);
|
||||
}
|
||||
|
||||
// perform a query
|
||||
const result = 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 = 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 = 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>
|
Reference in New Issue
Block a user