From 103f617ad5ce4a8c6d528454abcdb5ed2c77b4aa Mon Sep 17 00:00:00 2001 From: Thomas Wilkerling Date: Wed, 21 May 2025 10:57:43 +0200 Subject: [PATCH] v0.8.2 --- CHANGELOG.md | 7 + README.md | 78 +- dist/db/clickhouse/index.cjs | 162 +- dist/db/indexeddb/index.cjs | 1412 ++++--- dist/db/mongodb/index.cjs | 1447 +++---- dist/db/postgres/index.cjs | 1988 +++++---- dist/db/redis/index.cjs | 1177 +++--- dist/db/sqlite/index.cjs | 1656 ++++---- dist/flexsearch.bundle.debug.js | 3698 +++++++++-------- dist/flexsearch.bundle.min.js | 194 +- dist/flexsearch.bundle.module.debug.js | 3692 ++++++++-------- dist/flexsearch.bundle.module.min.js | 196 +- dist/flexsearch.compact.debug.js | 1503 +++---- dist/flexsearch.compact.min.js | 96 +- dist/flexsearch.compact.module.debug.js | 1501 +++---- dist/flexsearch.compact.module.min.js | 98 +- dist/flexsearch.es5.debug.js | 3535 ++++++++-------- dist/flexsearch.es5.min.js | 274 +- dist/flexsearch.light.debug.js | 206 +- dist/flexsearch.light.min.js | 24 +- dist/flexsearch.light.module.debug.js | 206 +- dist/flexsearch.light.module.min.js | 24 +- dist/module-debug/bundle.js | 12 +- dist/module-debug/cache.js | 57 +- dist/module-debug/db/indexeddb/index.js | 3 - dist/module-debug/db/mongodb/index.js | 86 +- dist/module-debug/db/redis/index.js | 14 +- dist/module-debug/document.js | 1 - dist/module-debug/document/search.js | 59 +- dist/module-debug/index/remove.js | 15 +- dist/module-debug/intersect.js | 2 +- dist/module-debug/resolve/and.js | 94 +- dist/module-debug/resolve/default.js | 18 +- dist/module-debug/resolve/handler.js | 200 +- dist/module-debug/resolve/not.js | 45 +- dist/module-debug/resolve/or.js | 36 +- dist/module-debug/resolve/xor.js | 43 +- dist/module-debug/resolver.js | 132 +- dist/module-debug/type.js | 13 +- dist/module-debug/worker.js | 15 +- dist/module-debug/worker/handler.js | 6 + dist/module-debug/worker/node.js | 6 + dist/module-min/bundle.js | 2 +- dist/module-min/cache.js | 2 +- dist/module-min/db/indexeddb/index.js | 2 +- dist/module-min/db/mongodb/index.js | 2 +- dist/module-min/db/redis/index.js | 2 +- dist/module-min/document/search.js | 2 +- dist/module-min/index/remove.js | 2 +- dist/module-min/intersect.js | 2 +- dist/module-min/resolve/and.js | 2 +- dist/module-min/resolve/default.js | 2 +- dist/module-min/resolve/handler.js | 2 +- dist/module-min/resolve/not.js | 2 +- dist/module-min/resolve/or.js | 2 +- dist/module-min/resolve/xor.js | 2 +- dist/module-min/resolver.js | 2 +- dist/module-min/type.js | 2 +- dist/module-min/worker.js | 2 +- dist/module-min/worker/handler.js | 2 +- dist/module-min/worker/node.js | 2 +- dist/module/bundle.js | 12 +- dist/module/cache.js | 57 +- dist/module/db/indexeddb/index.js | 3 - dist/module/db/mongodb/index.js | 86 +- dist/module/db/redis/index.js | 14 +- dist/module/document.js | 1 - dist/module/document/search.js | 59 +- dist/module/index/remove.js | 15 +- dist/module/intersect.js | 2 +- dist/module/resolve/and.js | 94 +- dist/module/resolve/default.js | 18 +- dist/module/resolve/handler.js | 200 +- dist/module/resolve/not.js | 45 +- dist/module/resolve/or.js | 36 +- dist/module/resolve/xor.js | 43 +- dist/module/resolver.js | 127 +- dist/module/type.js | 13 +- dist/module/worker.js | 13 +- dist/module/worker/handler.js | 6 + dist/module/worker/node.js | 6 + dist/node/node.js | 6 + dist/node/node.mjs | 6 + doc/0.8.0.md | 1857 --------- doc/resolver.md | 162 +- .../basic-persistent/index.html | 2 +- .../browser-legacy/basic-resolver/index.html | 2 +- .../basic-suggestion/index.html | 2 +- .../browser-legacy/basic-worker/index.html | 2 +- .../document-highlighting/index.html | 2 +- .../document-persistent/index.html | 2 +- .../document-resolver/index.html | 99 + .../browser-legacy/document-worker/index.html | 2 +- example/browser-legacy/document/index.html | 2 +- .../browser-legacy/language-pack/index.html | 4 +- .../basic-persistent/index.html | 2 +- .../browser-module/basic-resolver/index.html | 2 +- .../basic-suggestion/index.html | 2 +- .../basic-worker-extern-config/config.js | 2 +- example/browser-module/basic/index.html | 2 +- .../document-highlighting/index.html | 2 +- .../document-persistent/index.html | 2 +- .../document-resolver/index.html | 100 + .../config.originalTitle.js | 4 +- .../config.primaryTitle.js | 4 +- example/browser-module/document/index.html | 2 +- .../browser-module/language-pack/index.html | 4 +- .../.document-worker-persistent/package.json | 2 +- .../basic-export-import/package.json | 2 +- .../basic-persistent/package.json | 2 +- .../nodejs-commonjs/basic-resolver/index.js | 30 +- .../basic-resolver/package.json | 2 +- .../basic-suggestion/package.json | 2 +- .../basic-worker-extern-config/package.json | 2 +- .../nodejs-commonjs/basic-worker/package.json | 2 +- example/nodejs-commonjs/basic/package.json | 2 +- .../document-export-import/package.json | 2 +- .../document-persistent/package.json | 2 +- .../document-resolver/README.md | 7 + .../document-resolver/index.js | 79 + .../document-resolver/package.json | 6 + .../package.json | 2 +- .../package.json | 2 +- .../document-worker/package.json | 2 +- example/nodejs-commonjs/document/package.json | 2 +- .../language-pack/package.json | 2 +- .../basic-export-import/package.json | 2 +- .../nodejs-esm/basic-persistent/package.json | 2 +- example/nodejs-esm/basic-resolver/index.js | 30 +- .../nodejs-esm/basic-resolver/package.json | 2 +- .../nodejs-esm/basic-suggestion/package.json | 2 +- .../basic-worker-extern-config/package.json | 2 +- example/nodejs-esm/basic-worker/package.json | 2 +- example/nodejs-esm/basic/package.json | 2 +- .../document-export-import/package.json | 2 +- .../document-persistent/package.json | 2 +- .../nodejs-esm/document-resolver/README.md | 7 + example/nodejs-esm/document-resolver/index.js | 79 + .../nodejs-esm/document-resolver/package.json | 7 + .../package.json | 2 +- .../package.json | 2 +- example/nodejs-esm/document/package.json | 2 +- example/nodejs-esm/language-pack/package.json | 2 +- index.d.ts | 314 +- package-lock.json | 4 +- package.json | 4 +- src/bundle.js | 12 +- src/cache.js | 72 +- src/db/mongodb/index.js | 142 +- src/db/redis/index.js | 24 +- src/document.js | 1 - src/document/search.js | 79 +- src/index/remove.js | 19 +- src/intersect.js | 3 +- src/resolve/and.js | 123 +- src/resolve/default.js | 46 +- src/resolve/handler.js | 243 +- src/resolve/not.js | 76 +- src/resolve/or.js | 63 +- src/resolve/xor.js | 75 +- src/resolver.js | 179 +- src/type.js | 13 +- src/worker.js | 46 +- src/worker/handler.js | 6 + src/worker/node.js | 6 + src/worker/node.mjs | 6 + task/babel.js | 7 +- task/build.js | 2 + test/cache.js | 10 +- test/highlight.js | 104 +- test/issues.js | 135 + test/persistent.js | 803 +++- test/resolver.js | 997 ++++- test/types.ts | 396 +- 174 files changed, 16528 insertions(+), 14980 deletions(-) delete mode 100644 doc/0.8.0.md create mode 100644 example/browser-legacy/document-resolver/index.html create mode 100644 example/browser-module/document-resolver/index.html create mode 100644 example/nodejs-commonjs/document-resolver/README.md create mode 100644 example/nodejs-commonjs/document-resolver/index.js create mode 100644 example/nodejs-commonjs/document-resolver/package.json create mode 100644 example/nodejs-esm/document-resolver/README.md create mode 100644 example/nodejs-esm/document-resolver/index.js create mode 100644 example/nodejs-esm/document-resolver/package.json diff --git a/CHANGELOG.md b/CHANGELOG.md index 263f336..970a32f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,13 @@ ### Current Version +### v0.8.2 + +- Config-Serialized Query Caches, Improved caching strategy for Document indexes and Resolver +- Resolver Async Processing Workflow (including Queuing) +- Extended Resolver Support: Worker, Persistent, Cache +- Extended Result Highlighting: Boundaries, Ellipsis, Alignment +- Improved TypeScript Typings - Improved Stemmer Handling - Improved Result Highlighting - Use multi-language charset normalization as the default `Encoder` diff --git a/README.md b/README.md index 30aa984..c8fdeb0 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ -FlexSearch v0.8: [Overview and Migration Guide](doc/0.8.0.md) - + +Getting instant help by the DeepWiki AI assistant: [Ask DeepWiki.com](https://deepwiki.com/nextapps-de/flexsearch)

@@ -313,104 +313,104 @@ The **_dist_** folder is located in: `node_modules/flexsearch/dist/` flexsearch.bundle.min.js - Download - https://cdn.jsdelivr.net/gh/nextapps-de/flexsearch@0.8.1/dist/flexsearch.bundle.min.js + Download + https://cdn.jsdelivr.net/gh/nextapps-de/flexsearch@0.8.2/dist/flexsearch.bundle.min.js flexsearch.bundle.debug.js - Download - https://cdn.jsdelivr.net/gh/nextapps-de/flexsearch@0.8.1/dist/flexsearch.bundle.debug.js + Download + https://cdn.jsdelivr.net/gh/nextapps-de/flexsearch@0.8.2/dist/flexsearch.bundle.debug.js flexsearch.bundle.module.min.js - Download - https://cdn.jsdelivr.net/gh/nextapps-de/flexsearch@0.8.1/dist/flexsearch.bundle.module.min.js + Download + https://cdn.jsdelivr.net/gh/nextapps-de/flexsearch@0.8.2/dist/flexsearch.bundle.module.min.js flexsearch.bundle.module.debug.js - Download - https://cdn.jsdelivr.net/gh/nextapps-de/flexsearch@0.8.1/dist/flexsearch.bundle.module.debug.js + Download + https://cdn.jsdelivr.net/gh/nextapps-de/flexsearch@0.8.2/dist/flexsearch.bundle.module.debug.js flexsearch.compact.min.js - Download - https://cdn.jsdelivr.net/gh/nextapps-de/flexsearch@0.8.1/dist/flexsearch.compact.min.js + Download + https://cdn.jsdelivr.net/gh/nextapps-de/flexsearch@0.8.2/dist/flexsearch.compact.min.js flexsearch.compact.debug.js - Download - https://cdn.jsdelivr.net/gh/nextapps-de/flexsearch@0.8.1/dist/flexsearch.compact.debug.js + Download + https://cdn.jsdelivr.net/gh/nextapps-de/flexsearch@0.8.2/dist/flexsearch.compact.debug.js flexsearch.compact.module.min.js - Download - https://cdn.jsdelivr.net/gh/nextapps-de/flexsearch@0.8.1/dist/flexsearch.compact.module.min.js + Download + https://cdn.jsdelivr.net/gh/nextapps-de/flexsearch@0.8.2/dist/flexsearch.compact.module.min.js flexsearch.compact.module.debug.js - Download - https://cdn.jsdelivr.net/gh/nextapps-de/flexsearch@0.8.1/dist/flexsearch.compact.module.debug.js + Download + https://cdn.jsdelivr.net/gh/nextapps-de/flexsearch@0.8.2/dist/flexsearch.compact.module.debug.js flexsearch.light.min.js - Download - https://cdn.jsdelivr.net/gh/nextapps-de/flexsearch@0.8.1/dist/flexsearch.light.min.js + Download + https://cdn.jsdelivr.net/gh/nextapps-de/flexsearch@0.8.2/dist/flexsearch.light.min.js **** flexsearch.light.debug.js - Download - https://cdn.jsdelivr.net/gh/nextapps-de/flexsearch@0.8.1/dist/flexsearch.light.debug.js + Download + https://cdn.jsdelivr.net/gh/nextapps-de/flexsearch@0.8.2/dist/flexsearch.light.debug.js flexsearch.light.module.min.js - Download - https://cdn.jsdelivr.net/gh/nextapps-de/flexsearch@0.8.1/dist/flexsearch.light.module.min.js + Download + https://cdn.jsdelivr.net/gh/nextapps-de/flexsearch@0.8.2/dist/flexsearch.light.module.min.js flexsearch.light.module.debug.js - Download - https://cdn.jsdelivr.net/gh/nextapps-de/flexsearch@0.8.1/dist/flexsearch.light.module.debug.js + Download + https://cdn.jsdelivr.net/gh/nextapps-de/flexsearch@0.8.2/dist/flexsearch.light.module.debug.js flexsearch.es5.min.js - Download - https://cdn.jsdelivr.net/gh/nextapps-de/flexsearch@0.8.1/dist/flexsearch.es5.min.js + Download + https://cdn.jsdelivr.net/gh/nextapps-de/flexsearch@0.8.2/dist/flexsearch.es5.min.js flexsearch.es5.debug.js - Download - https://cdn.jsdelivr.net/gh/nextapps-de/flexsearch@0.8.1/dist/flexsearch.es5.debug.js + Download + https://cdn.jsdelivr.net/gh/nextapps-de/flexsearch@0.8.2/dist/flexsearch.es5.debug.js Javascript Modules (ESM) - Download - https://cdn.jsdelivr.net/gh/nextapps-de/flexsearch@0.8.1/dist/module/ + Download + https://cdn.jsdelivr.net/gh/nextapps-de/flexsearch@0.8.2/dist/module/ Javascript Modules Minified (ESM) - Download - https://cdn.jsdelivr.net/gh/nextapps-de/flexsearch@0.8.1/dist/module-min/ + Download + https://cdn.jsdelivr.net/gh/nextapps-de/flexsearch@0.8.2/dist/module-min/ Javascript Modules Debug (ESM) - Download - https://cdn.jsdelivr.net/gh/nextapps-de/flexsearch@0.8.1/dist/module-debug/ + Download + https://cdn.jsdelivr.net/gh/nextapps-de/flexsearch@0.8.2/dist/module-debug/ @@ -702,7 +702,7 @@ You can also load modules via CDN: ```html ``` @@ -801,6 +801,7 @@ The documentation will refer to several examples. A list of all examples: - [basic-export-import](example/nodejs-commonjs/basic-export-import) - [document](example/nodejs-commonjs/document) - [document-persistent](example/nodejs-commonjs/document-persistent) +- [document-resolver](example/nodejs-commonjs/document-resolver) - [document-worker](example/nodejs-commonjs/document-worker) - [document-worker-extern-config](example/nodejs-commonjs/document-worker-extern-config) - [document-export-import](example/nodejs-commonjs/document-export-import) @@ -821,6 +822,7 @@ The documentation will refer to several examples. A list of all examples: - [basic-export-import](example/nodejs-esm/basic-export-import) - [document](example/nodejs-esm/document) - [document-persistent](example/nodejs-esm/document-persistent) +- [document-resolver](example/nodejs-esm/document-resolver) - [document-worker](example/nodejs-esm/document-worker) - [document-worker-extern-config](example/nodejs-esm/document-worker-extern-config) - [document-export-import](example/nodejs-esm/document-export-import) @@ -841,6 +843,7 @@ The documentation will refer to several examples. A list of all examples: - [document](example/browser-legacy/document) - [document-highlighting](example/browser-legacy/document-highlighting) - [document-persistent](example/browser-legacy/document-persistent) +- [document-resolver](example/browser-legacy/document-resolver) - [document-worker](example/browser-legacy/document-worker) - [language-pack](example/browser-legacy/language-pack) @@ -857,6 +860,7 @@ The documentation will refer to several examples. A list of all examples: - [document](example/browser-module/document) - [document-highlighting](example/browser-module/document-highlighting) - [document-persistent](example/browser-module/document-persistent) +- [document-resolver](example/browser-module/document-resolver) - [document-worker](example/browser-module/document-worker) - [document-worker-extern-config](example/browser-module/document-worker-extern-config) - [language-pack](example/browser-module/language-pack) diff --git a/dist/db/clickhouse/index.cjs b/dist/db/clickhouse/index.cjs index 8d16360..acd2f7e 100644 --- a/dist/db/clickhouse/index.cjs +++ b/dist/db/clickhouse/index.cjs @@ -89,14 +89,14 @@ function ClickhouseDB(name, config = {}){ if(!name){ console.info("Default storage space was used, because a name was not passed."); } - //field = "Test-456"; + this.id = "flexsearch" + (name ? "_" + sanitize(name) : ""); this.field = config.field ? "_" + sanitize(config.field) : ""; - // Clickhouse does not support ALTER TABLE to upgrade - // the type of the ID when it is a part of the merge key + + this.type = config.type ? types[config.type.toLowerCase()] : "String"; if(!this.type) throw new Error("Unknown type of ID '" + config.type + "'"); - //this.trx = false; + this.support_tag_search = true; this.db = Index || (Index = config.db || null); Object.assign(defaults, config); @@ -104,7 +104,7 @@ function ClickhouseDB(name, config = {}){ this.db && delete defaults.db; } ClickhouseDB.prototype.mount = function(flexsearch){ - //if(flexsearch.constructor === Document){ + if(flexsearch.index){ return flexsearch.mount(this); } @@ -141,7 +141,7 @@ ClickhouseDB.prototype.open = async function(){ id ${this.type} ) ENGINE = MergeTree - /*PRIMARY KEY (key)*/ + ORDER BY (key, id); `, { params: { name: this.id + ".map" + this.field }}).toPromise(); break; @@ -155,7 +155,7 @@ ClickhouseDB.prototype.open = async function(){ id ${this.type} ) ENGINE = MergeTree - /*PRIMARY KEY (ctx, key)*/ + ORDER BY (ctx, key, id); `).toPromise(); break; @@ -167,7 +167,7 @@ ClickhouseDB.prototype.open = async function(){ id ${this.type} ) ENGINE = MergeTree - /*PRIMARY KEY (ctx, key)*/ + ORDER BY (tag, id); `).toPromise(); break; @@ -198,7 +198,7 @@ ClickhouseDB.prototype.open = async function(){ }; ClickhouseDB.prototype.close = function(){ - //DB && DB.close(); + this.db = Index = null; return this; }; @@ -363,7 +363,7 @@ ClickhouseDB.prototype.has = async function(id){ const result = await this.db.query(` SELECT 1 FROM ${this.id}.reg - WHERE id = {id:${this.type /*=== "number" ? "Int32" : "String"*/}} + WHERE id = {id:${this.type }} LIMIT 1`, { params: { id }} ).toPromise(); @@ -403,7 +403,7 @@ ClickhouseDB.prototype.search = function(flexsearch, query, limit = 100, offset ${ enrich ? ", doc" : "" } FROM ( SELECT id, count(*) as count, - ${ suggest ? "SUM" : "SUM" /*"MIN"*/ }(res) as res + ${ suggest ? "SUM" : "SUM" }(res) as res FROM ${ this.id }.ctx${ this.field } WHERE ${ where } GROUP BY id @@ -417,31 +417,31 @@ ClickhouseDB.prototype.search = function(flexsearch, query, limit = 100, offset ${ offset ? "OFFSET " + offset : "" } `, { params }).toPromise(); - // for(let i = 1; i < query.length; i++){ - // where += (where ? " UNION ALL " : "") + ` - // SELECT id, res - // FROM ${this.id}.ctx${this.field} - // WHERE ctx = {ctx${i}:String} AND key = {key${i}:String} - // `; - // term = query[i]; - // const swap = flexsearch.bidirectional && (term > keyword); - // params["ctx" + i] = swap ? term : keyword; - // params["key" + i] = swap ? keyword : term; - // keyword = term; - // } - // - // rows = await this.db.query(` - // SELECT id, res - // FROM ( - // SELECT id, ${suggest ? "SUM" : "MIN"}(res) as res, count(*) as count - // FROM (${where}) as t - // GROUP BY id - // ORDER BY ${suggest ? "count DESC, res" : "res"} - // LIMIT ${limit} - // OFFSET ${offset} - // ) as r - // ${suggest ? "" : "WHERE count = " + (query.length - 1)} - // `, { params }).toPromise(); + + + + + + + + + + + + + + + + + + + + + + + + + } else { @@ -469,7 +469,7 @@ ClickhouseDB.prototype.search = function(flexsearch, query, limit = 100, offset ${ enrich ? ", doc" : "" } FROM ( SELECT id, count(*) as count, - ${ suggest ? "SUM" : "SUM" /*"MIN"*/ }(res) as res + ${ suggest ? "SUM" : "SUM" }(res) as res FROM ${ this.id }.map${ this.field } WHERE ${ where } GROUP BY id @@ -483,26 +483,26 @@ ClickhouseDB.prototype.search = function(flexsearch, query, limit = 100, offset ${ offset ? "OFFSET " + offset : "" } `, { params }).toPromise(); - // for(let i = 0; i < query.length; i++){ - // params["key" + i] = query[i]; - // where += (where ? " UNION ALL " : "") + ` - // SELECT id, res - // FROM ${ this.id }.map${ this.field } - // WHERE key = {key${i}:String} - // `; - // } - // rows = await this.db.query(` - // SELECT id, res - // FROM ( - // SELECT id, ${suggest ? "SUM" : "MIN"}(res) as res, count(*) as count - // FROM (${where}) as t - // GROUP BY id - // ORDER BY ${suggest ? "count DESC, res" : "res"} - // LIMIT ${limit} - // OFFSET ${offset} - // ) as r - // ${ suggest ? "" : "WHERE count = " + query.length } - // `, { params }).toPromise(); + + + + + + + + + + + + + + + + + + + + } return rows.then(function(rows){ return create_result(rows, resolve, enrich); @@ -510,21 +510,21 @@ ClickhouseDB.prototype.search = function(flexsearch, query, limit = 100, offset }; ClickhouseDB.prototype.info = function(){ - // todo + }; ClickhouseDB.prototype.transaction = function(task){ - // not supported + return task.call(this); }; ClickhouseDB.prototype.commit = async function(flexsearch, _replace, _append){ - // process cleanup tasks + if(_replace){ await this.clear(); - // there are just removals in the task queue + flexsearch.commit_task = []; } else { @@ -532,7 +532,7 @@ ClickhouseDB.prototype.commit = async function(flexsearch, _replace, _append){ flexsearch.commit_task = []; for(let i = 0, task; i < tasks.length; i++){ task = tasks[i]; - // there are just removals in the task queue + if(task.clear){ await this.clear(); _replace = true; @@ -562,7 +562,7 @@ ClickhouseDB.prototype.commit = async function(flexsearch, _replace, _append){ const arr = item[1]; for(let i = 0, ids; i < arr.length; i++){ if((ids = arr[i]) && ids.length){ - //this.type || (this.type = typeof ids[0]); + for(let j = 0; j < ids.length; j++){ data.push({ key: key, @@ -655,24 +655,24 @@ ClickhouseDB.prototype.commit = async function(flexsearch, _replace, _append){ } } - // TODO - // await this.db.insert(`INSERT INTO ${this.id}.cfg${this.field} (cfg)`, [{ - // cfg: JSON.stringify({ - // "encode": typeof flexsearch.encode === "string" ? flexsearch.encode : "", - // "charset": typeof flexsearch.charset === "string" ? flexsearch.charset : "", - // "tokenize": flexsearch.tokenize, - // "resolution": flexsearch.resolution, - // "minlength": flexsearch.minlength, - // "optimize": flexsearch.optimize, - // "fastupdate": flexsearch.fastupdate, - // "encoder": flexsearch.encoder, - // "context": { - // "depth": flexsearch.depth, - // "bidirectional": flexsearch.bidirectional, - // "resolution": flexsearch.resolution_ctx - // } - // }) - // }]).toPromise(); + + + + + + + + + + + + + + + + + + promises.length && await Promise.all(promises); diff --git a/dist/db/indexeddb/index.cjs b/dist/db/indexeddb/index.cjs index a8c8ab1..25fe192 100644 --- a/dist/db/indexeddb/index.cjs +++ b/dist/db/indexeddb/index.cjs @@ -1,723 +1,703 @@ 'use strict'; -/** - * @param {*} value - * @param {*} default_value - * @param {*=} merge_value - * @return {*} - */ - -function create_object(){ - return Object.create(null); +/** + * @param {*} value + * @param {*} default_value + * @param {*=} merge_value + * @return {*} + */ + +function create_object(){ + return Object.create(null); +} + +/** + * @param {Map|Set} val + * @param {boolean=} stringify + * @return {Array} + */ + +function toArray(val, stringify){ + const result = []; + for(const key of val.keys()){ + result.push(key); + } + return result; } -/** - * @param {Map|Set} val - * @param {boolean=} stringify - * @return {Array} - */ - -function toArray(val, stringify){ - const result = []; - for(const key of val.keys()){ - result.push(key); - } - return result; -} - -// COMPILER BLOCK --> - -const VERSION = 1; -const IndexedDB = typeof window !== "undefined" && ( - window.indexedDB || - window.mozIndexedDB || - window.webkitIndexedDB || - window.msIndexedDB -); -const fields = ["map", "ctx", "tag", "reg", "cfg"]; - -/** - * @param {!string} str - * @return {string} - */ -function sanitize(str) { - return str.toLowerCase().replace(/[^a-z0-9_\-]/g, ""); -} - -const Index = create_object(); - -/** - * @param {string|PersistentOptions=} name - * @param {PersistentOptions=} config - * @constructor - * @implements StorageInterface - */ - -function IdxDB(name, config = {}){ - if(!this || this.constructor !== IdxDB){ - return new IdxDB(name, config); - } - if(typeof name === "object"){ - config = /** @type PersistentOptions */ (name); - name = name.name; - } - if(!name){ - console.info("Default storage space was used, because a name was not passed."); - } - this.id = "flexsearch" + (name ? ":" + sanitize(name) : ""); - this.field = config.field ? sanitize(config.field) : ""; - this.type = config.type; - this.support_tag_search = false; - this.fastupdate = false; - this.db = null; - this.trx = {}; -} -IdxDB.prototype.mount = function(flexsearch){ - //if(flexsearch.constructor === Document){ - if(flexsearch.index){ - return flexsearch.mount(this); - } - flexsearch.db = this; - return this.open(); -}; - -IdxDB.prototype.open = function(){ - - if(this.db) return this.db; - let self = this; - - navigator.storage && - navigator.storage.persist(); - - // return this.db = new Promise(function(resolve, reject){ - - Index[self.id] || (Index[self.id] = []); - Index[self.id].push(self.field); - - const req = IndexedDB.open(self.id, VERSION); - - /** @this {IDBOpenDBRequest} */ - req.onupgradeneeded = function(event){ - - const db = self.db = this.result; - - // Using Indexes + IDBKeyRange on schema map => [key, res, id] performs - // too bad and blows up amazingly in size - // The schema map:key => [res][id] is currently used instead - // In fact that bypass the idea of a storage solution, - // IndexedDB is such a poor contribution :( - for(let i = 0, ref; i < fields.length; i++){ - ref = fields[i]; - for(let j = 0, field; j < Index[self.id].length; j++){ - field = Index[self.id][j]; - db.objectStoreNames.contains(ref + (ref !== "reg" ? (field ? ":" + field : "") : "")) || - db.createObjectStore(ref + (ref !== "reg" ? (field ? ":" + field : "") : ""));//{ autoIncrement: true /*keyPath: "id"*/ } - //.createIndex("idx", "ids", { multiEntry: true, unique: false }); - } - } - - // switch(event.oldVersion){ // existing db version - // case 0: - // // version 0 means that the client had no database - // // perform initialization - // case 1: - // // client had version 1 - // // update - // } - }; - - return self.db = promisfy(req, function(result){ - self.db = result; //event.target.result; - self.db.onversionchange = function(){ - //database is outdated - self.close(); - }; - }); - - // req.onblocked = function(event) { - // // this event shouldn't trigger if we handle onversionchange correctly - // // it means that there's another open connection to the same database - // // and it wasn't closed after db.onversionchange triggered for it - // console.error("blocked", event); - // reject(); - // }; - // - // req.onerror = function(event){ - // console.error(this.error, event); - // reject(); - // }; - // - // req.onsuccess = function(event){ - // self.db = this.result; //event.target.result; - // self.db.onversionchange = function(){ - // //database is outdated - // self.close(); - // }; - // resolve(self); - // }; - // }); -}; - -IdxDB.prototype.close = function(){ - this.db && this.db.close(); - this.db = null; -}; - -/** - * @return {!Promise} - */ -IdxDB.prototype.destroy = function(){ - const req = IndexedDB.deleteDatabase(this.id); - return promisfy(req); -}; - -// IdxDB.prototype.set = function(ref, key, ctx, data){ -// const transaction = this.db.transaction(ref, "readwrite"); -// const map = transaction.objectStore(ref); -// const req = map.put(data, ctx ? ctx + ":" + key : key); -// return transaction;//promisfy(req, callback); -// }; - -// IdxDB.prototype.delete = function(ref, key, ctx){ -// const transaction = this.db.transaction(ref, "readwrite"); -// const map = transaction.objectStore(ref); -// const req = map.delete(ctx ? ctx + ":" + key : key); -// return transaction;//promisfy(req, callback); -// }; - -/** - * @return {!Promise} - */ -IdxDB.prototype.clear = function(){ - - const stores = []; - - for(let i = 0, ref; i < fields.length; i++){ - ref = fields[i]; - for(let j = 0, field; j < Index[this.id].length; j++){ - field = Index[this.id][j]; - stores.push(ref + (ref !== "reg" ? (field ? ":" + field : "") : "")); - } - } - - const transaction = this.db.transaction(stores, "readwrite"); - - for(let i = 0; i < stores.length; i++){ - transaction.objectStore(stores[i]).clear(); - } - return promisfy(transaction); -}; - -/** - * @param {!string} key - * @param {string=} ctx - * @param {number=} limit - * @param {number=} offset - * @param {boolean=} resolve - * @param {boolean=} enrich - * @return {!Promise} - */ -IdxDB.prototype.get = function(key, ctx, limit = 0, offset = 0, resolve = true, enrich = false){ - const transaction = this.db.transaction((ctx ? "ctx" : "map") + (this.field ? ":" + this.field : ""), "readonly"); - const map = transaction.objectStore((ctx ? "ctx" : "map") + (this.field ? ":" + this.field : "")); - const req = map.get(ctx ? ctx + ":" + key : key); - const self = this; - return promisfy(req).then(function(res){ - let result = []; - if(!res || !res.length) return result; - if(resolve){ - if(!limit && !offset && res.length === 1){ - return res[0]; - } - for(let i = 0, arr; i < res.length; i++){ - if((arr = res[i]) && arr.length){ - if(offset >= arr.length){ - offset -= arr.length; - continue; - } - const end = limit - ? offset + Math.min(arr.length - offset, limit) - : arr.length; - for(let j = offset; j < end; j++){ - result.push(arr[j]); - } - offset = 0; - if(result.length === limit){ - break; - } - } - } - return enrich - ? self.enrich(result) - : result; - } - else { - return res; - } - }); -}; - -{ - - /** - * @param {!string} tag - * @param {number=} limit - * @param {number=} offset - * @param {boolean=} enrich - * @return {!Promise} - */ - IdxDB.prototype.tag = function(tag, limit = 0, offset = 0, enrich = false){ - const transaction = this.db.transaction("tag" + (this.field ? ":" + this.field : ""), "readonly"); - const map = transaction.objectStore("tag" + (this.field ? ":" + this.field : "")); - const req = map.get(tag); - const self = this; - return promisfy(req).then(function(ids){ - if(!ids || !ids.length || offset >= ids.length) return []; - if(!limit && !offset) return ids; - const result = ids.slice(offset, offset + limit); - return enrich - ? self.enrich(result) - : result; - }); - }; -} - -{ - - /** - * @param {SearchResults} ids - * @return {!Promise} - */ - IdxDB.prototype.enrich = function(ids){ - if(typeof ids !== "object"){ - ids = [ids]; - } - const transaction = this.db.transaction("reg", "readonly"); - const map = transaction.objectStore("reg"); - const promises = []; - for(let i = 0; i < ids.length; i++){ - promises[i] = promisfy(map.get(ids[i])); - } - return Promise.all(promises).then(function(docs){ - for(let i = 0; i < docs.length; i++){ - docs[i] = { - "id": ids[i], - "doc": docs[i] ? JSON.parse(docs[i]) : null - }; - } - return docs; - }); - }; -} - -/** - * @param {number|string} id - * @return {!Promise} - */ -IdxDB.prototype.has = function(id){ - const transaction = this.db.transaction("reg", "readonly"); - const map = transaction.objectStore("reg"); - const req = map.getKey(id); - return promisfy(req).then(function(result){ - return !!result; - }); -}; - -IdxDB.prototype.search = null; - -// IdxDB.prototype.has = function(ref, key, ctx){ -// const transaction = this.db.transaction(ref, "readonly"); -// const map = transaction.objectStore(ref); -// const req = map.getKey(ctx ? ctx + ":" + key : key); -// return promisfy(req); -// }; - -IdxDB.prototype.info = function(){ - // todo -}; - -/** - * @param {!string} ref - * @param {!string} modifier - * @param {!Function} task - */ - -IdxDB.prototype.transaction = function(ref, modifier, task){ - - const key = ref + (ref !== "reg" ? (this.field ? ":" + this.field : "") : ""); - /** - * @type {IDBObjectStore} - */ - let store = this.trx[key + ":" + modifier]; - if(store) return task.call(this, store); - let transaction = this.db.transaction(key, modifier); - /** - * @type {IDBObjectStore} - */ - this.trx[key + ":" + modifier] = store = transaction.objectStore(key); - const promise = task.call(this, store); - this.trx[key + ":" + modifier] = null; - - return promisfy(transaction).finally(function(){ - transaction = store = null; - return promise; - }); - - // return new Promise((resolve, reject) => { - // transaction.onerror = (err) => { - // transaction.abort(); - // transaction = store = null; - // reject(err); - // //db.close; - // }; - // transaction.oncomplete = (res) => { - // transaction = store = null; - // resolve(res || true); - // //db.close; - // }; - // const promise = task.call(this, store); - // // transactions can just be used within the same event loop - // // the indexeddb is such a stupid tool :( - // this.trx[key + ":" + modifier] = null; - // return promise; - // }); -}; - -IdxDB.prototype.commit = async function(flexsearch, _replace, _append){ - - // process cleanup tasks - if(_replace){ - await this.clear(); - // there are just removals in the task queue - flexsearch.commit_task = []; - } - else { - let tasks = flexsearch.commit_task; - flexsearch.commit_task = []; - for(let i = 0, task; i < tasks.length; i++){ - /** @dict */ - task = tasks[i]; - // there are just removals in the task queue - if(task["clear"]){ - await this.clear(); - _replace = true; - break; - } - else { - tasks[i] = task["del"]; - } - } - if(!_replace){ - if(!_append){ - tasks = tasks.concat(toArray(flexsearch.reg)); - } - tasks.length && await this.remove(tasks); - } - } - - if(!flexsearch.reg.size){ - return; - } - - await this.transaction("map", "readwrite", function(store){ - - for(const item of flexsearch.map){ - - const key = item[0]; - const value = item[1]; - if(!value.length) continue; - - if(_replace){ - store.put(value, key); - continue; - } - - store.get(key).onsuccess = function(){ - let result = this.result; - let changed; - if(result && result.length){ - const maxlen = Math.max(result.length, value.length); - for(let i = 0, res, val; i < maxlen; i++){ - val = value[i]; - if(val && val.length){ - res = result[i]; - if(res && res.length){ - for(let j = 0; j < val.length; j++){ - res.push(val[j]); - } - changed = 1; - //result[i] = res.concat(val); - //result[i] = new Set([...result[i], ...value[i]]); - //result[i] = result[i].union(new Set(value[i])); - } - else { - result[i] = val; - changed = 1; - //result[i] = new Set(value[i]) - } - } - } - } - else { - result = value; - changed = 1; - //result = []; - //for(let i = 0; i < value.length; i++){ - // if(value[i]) result[i] = new Set(value[i]); - //} - } - - changed && - store.put(result, key); - }; - } - }); - - await this.transaction("ctx", "readwrite", function(store){ - - for(const ctx of flexsearch.ctx){ - const ctx_key = ctx[0]; - const ctx_value = ctx[1]; - - for(const item of ctx_value){ - const key = item[0]; - const value = item[1]; - if(!value.length) continue; - - if(_replace){ - store.put(value, ctx_key + ":" + key); - continue; - } - - store.get(ctx_key + ":" + key).onsuccess = function(){ - let result = this.result; - let changed; - if(result && result.length){ - const maxlen = Math.max(result.length, value.length); - for(let i = 0, res, val; i < maxlen; i++){ - val = value[i]; - if(val && val.length){ - res = result[i]; - if(res && res.length){ - for(let j = 0; j < val.length; j++){ - res.push(val[j]); - } - //result[i] = res.concat(val); - changed = 1; - } - else { - result[i] = val; - changed = 1; - } - } - } - } - else { - result = value; - changed = 1; - } - - changed && - store.put(result, ctx_key + ":" + key); - }; - } - } - }); - - if(flexsearch.store){ - await this.transaction("reg", "readwrite", function(store){ - for(const item of flexsearch.store){ - const id = item[0]; - const doc = item[1]; - store.put(typeof doc === "object" - ? JSON.stringify(doc) - : 1 - , id); - } - }); - } - else if(!flexsearch.bypass){ - await this.transaction("reg", "readwrite", function(store){ - for(const id of flexsearch.reg.keys()){ - store.put(1, id); - } - }); - } - - if(flexsearch.tag){ - await this.transaction("tag", "readwrite", function(store){ - for(const item of flexsearch.tag){ - const tag = item[0]; - const ids = item[1]; - if(!ids.length) continue; - - store.get(tag).onsuccess = function(){ - let result = this.result; - result = result && result.length - ? result.concat(ids) - : ids; - store.put(result, tag); - }; - } - }); - } - - // TODO - // await this.transaction("cfg", "readwrite", function(store){ - // store.put({ - // "charset": flexsearch.charset, - // "tokenize": flexsearch.tokenize, - // "resolution": flexsearch.resolution, - // "fastupdate": flexsearch.fastupdate, - // "compress": flexsearch.compress, - // "encoder": { - // "minlength": flexsearch.encoder.minlength - // }, - // "context": { - // "depth": flexsearch.depth, - // "bidirectional": flexsearch.bidirectional, - // "resolution": flexsearch.resolution_ctx - // } - // }, "current"); - // }); - - flexsearch.map.clear(); - flexsearch.ctx.clear(); - { - flexsearch.tag && - flexsearch.tag.clear(); - } - { - flexsearch.store && - flexsearch.store.clear(); - } - flexsearch.document || - flexsearch.reg.clear(); -}; - -/** - * @param {IDBCursorWithValue} cursor - * @param {Array} ids - * @param {boolean=} _tag - */ - -function handle(cursor, ids, _tag){ - - const arr = cursor.value; - let changed; - let count = 0; - - for(let x = 0, result; x < arr.length; x++){ - // tags has no resolution layer - if((result = _tag ? arr : arr[x])){ - for(let i = 0, pos, id; i < ids.length; i++){ - id = ids[i]; - pos = result.indexOf(id); - if(pos >= 0){ - changed = 1; - if(result.length > 1){ - result.splice(pos, 1); - } - else { - arr[x] = []; - break; - } - } - } - - count += result.length; - } - if(_tag) break; - } - - if(!count){ - cursor.delete(); - //store.delete(cursor.key); - } - else if(changed){ - //await new Promise(resolve => { - cursor.update(arr);//.onsuccess = resolve; - //}); - } - - cursor.continue(); -} - -/** - * @param {Array} ids - * @return {!Promise} - */ -IdxDB.prototype.remove = function(ids){ - - const self = this; - - if(typeof ids !== "object"){ - ids = [ids]; - } - - return /** @type {!Promise} */(Promise.all([ - self.transaction("map", "readwrite", function(store){ - store.openCursor().onsuccess = function(){ - const cursor = this.result; - cursor && handle(cursor, ids); - }; - }), - self.transaction("ctx", "readwrite", function(store){ - store.openCursor().onsuccess = function(){ - const cursor = this.result; - cursor && handle(cursor, ids); - }; - }), - self.transaction("tag", "readwrite", function(store){ - store.openCursor().onsuccess = function(){ - const cursor = this.result; - cursor && handle(cursor, ids, /* tag? */ true); - }; - }), - // let filtered = []; - self.transaction("reg", "readwrite", function(store){ - for(let i = 0; i < ids.length; i++){ - store.delete(ids[i]); - } - // return new Promise(resolve => { - // store.openCursor().onsuccess = function(){ - // const cursor = this.result; - // if(cursor){ - // const id = cursor.value; - // if(ids.includes(id)){ - // filtered.push(id); - // cursor.delete(); - // } - // cursor.continue(); - // } - // else{ - // resolve(); - // } - // }; - // }); - }) - // ids = filtered; - ])); -}; - -/** - * @param {IDBRequest|IDBOpenDBRequest} req - * @param {Function=} callback - * @return {!Promise} - */ - -function promisfy(req, callback){ - return new Promise((resolve, reject) => { - // oncomplete is used for transaction - /** @this {IDBRequest|IDBOpenDBRequest} */ - req.onsuccess = req.oncomplete = function(){ - callback && callback(this.result); - callback = null; - resolve(this.result); - }; - req.onerror = req.onblocked = reject; - req = null; - }); +const VERSION = 1; +const IndexedDB = typeof window !== "undefined" && ( + window.indexedDB || + window.mozIndexedDB || + window.webkitIndexedDB || + window.msIndexedDB +); +const fields = ["map", "ctx", "tag", "reg", "cfg"]; + +/** + * @param {!string} str + * @return {string} + */ +function sanitize(str) { + return str.toLowerCase().replace(/[^a-z0-9_\-]/g, ""); +} + +const Index = create_object(); + +/** + * @param {string|PersistentOptions=} name + * @param {PersistentOptions=} config + * @constructor + * @implements StorageInterface + */ + +function IdxDB(name, config = {}){ + if(!this || this.constructor !== IdxDB){ + return new IdxDB(name, config); + } + if(typeof name === "object"){ + config = /** @type PersistentOptions */ (name); + name = name.name; + } + if(!name){ + console.info("Default storage space was used, because a name was not passed."); + } + this.id = "flexsearch" + (name ? ":" + sanitize(name) : ""); + this.field = config.field ? sanitize(config.field) : ""; + this.type = config.type; + this.support_tag_search = false; + this.fastupdate = false; + this.db = null; + this.trx = {}; +} +IdxDB.prototype.mount = function(flexsearch){ + + if(flexsearch.index){ + return flexsearch.mount(this); + } + flexsearch.db = this; + return this.open(); +}; + +IdxDB.prototype.open = function(){ + + if(this.db) return this.db; + let self = this; + + navigator.storage && + navigator.storage.persist(); + + + + Index[self.id] || (Index[self.id] = []); + Index[self.id].push(self.field); + + const req = IndexedDB.open(self.id, VERSION); + + /** @this {IDBOpenDBRequest} */ + req.onupgradeneeded = function(event){ + + const db = self.db = this.result; + + + + + + + for(let i = 0, ref; i < fields.length; i++){ + ref = fields[i]; + for(let j = 0, field; j < Index[self.id].length; j++){ + field = Index[self.id][j]; + db.objectStoreNames.contains(ref + (ref !== "reg" ? (field ? ":" + field : "") : "")) || + db.createObjectStore(ref + (ref !== "reg" ? (field ? ":" + field : "") : "")); + + } + } + + + + + + + + + + }; + + return self.db = promisfy(req, function(result){ + self.db = result; + self.db.onversionchange = function(){ + + self.close(); + }; + }); + + + + + + + + + + + + + + + + + + + + + + + +}; + +IdxDB.prototype.close = function(){ + this.db && this.db.close(); + this.db = null; +}; + +/** + * @return {!Promise} + */ +IdxDB.prototype.destroy = function(){ + const req = IndexedDB.deleteDatabase(this.id); + return promisfy(req); +}; + + + +/** + * @return {!Promise} + */ +IdxDB.prototype.clear = function(){ + + const stores = []; + + for(let i = 0, ref; i < fields.length; i++){ + ref = fields[i]; + for(let j = 0, field; j < Index[this.id].length; j++){ + field = Index[this.id][j]; + stores.push(ref + (ref !== "reg" ? (field ? ":" + field : "") : "")); + } + } + + const transaction = this.db.transaction(stores, "readwrite"); + + for(let i = 0; i < stores.length; i++){ + transaction.objectStore(stores[i]).clear(); + } + return promisfy(transaction); +}; + +/** + * @param {!string} key + * @param {string=} ctx + * @param {number=} limit + * @param {number=} offset + * @param {boolean=} resolve + * @param {boolean=} enrich + * @return {!Promise} + */ +IdxDB.prototype.get = function(key, ctx, limit = 0, offset = 0, resolve = true, enrich = false){ + const transaction = this.db.transaction((ctx ? "ctx" : "map") + (this.field ? ":" + this.field : ""), "readonly"); + const map = transaction.objectStore((ctx ? "ctx" : "map") + (this.field ? ":" + this.field : "")); + const req = map.get(ctx ? ctx + ":" + key : key); + const self = this; + return promisfy(req).then(function(res){ + let result = []; + if(!res || !res.length) return result; + if(resolve){ + if(!limit && !offset && res.length === 1){ + return res[0]; + } + for(let i = 0, arr; i < res.length; i++){ + if((arr = res[i]) && arr.length){ + if(offset >= arr.length){ + offset -= arr.length; + continue; + } + const end = limit + ? offset + Math.min(arr.length - offset, limit) + : arr.length; + for(let j = offset; j < end; j++){ + result.push(arr[j]); + } + offset = 0; + if(result.length === limit){ + break; + } + } + } + return SUPPORT_STORE && enrich + ? self.enrich(result) + : result; + } + else { + return res; + } + }); +}; + +if(SUPPORT_TAGS){ + + /** + * @param {!string} tag + * @param {number=} limit + * @param {number=} offset + * @param {boolean=} enrich + * @return {!Promise} + */ + IdxDB.prototype.tag = function(tag, limit = 0, offset = 0, enrich = false){ + const transaction = this.db.transaction("tag" + (this.field ? ":" + this.field : ""), "readonly"); + const map = transaction.objectStore("tag" + (this.field ? ":" + this.field : "")); + const req = map.get(tag); + const self = this; + return promisfy(req).then(function(ids){ + if(!ids || !ids.length || offset >= ids.length) return []; + if(!limit && !offset) return ids; + const result = ids.slice(offset, offset + limit); + return SUPPORT_STORE && enrich + ? self.enrich(result) + : result; + }); + }; +} + +if(SUPPORT_STORE){ + + /** + * @param {SearchResults} ids + * @return {!Promise} + */ + IdxDB.prototype.enrich = function(ids){ + if(typeof ids !== "object"){ + ids = [ids]; + } + const transaction = this.db.transaction("reg", "readonly"); + const map = transaction.objectStore("reg"); + const promises = []; + for(let i = 0; i < ids.length; i++){ + promises[i] = promisfy(map.get(ids[i])); + } + return Promise.all(promises).then(function(docs){ + for(let i = 0; i < docs.length; i++){ + docs[i] = { + "id": ids[i], + "doc": docs[i] ? JSON.parse(docs[i]) : null + }; + } + return docs; + }); + }; +} + +/** + * @param {number|string} id + * @return {!Promise} + */ +IdxDB.prototype.has = function(id){ + const transaction = this.db.transaction("reg", "readonly"); + const map = transaction.objectStore("reg"); + const req = map.getKey(id); + return promisfy(req).then(function(result){ + return !!result; + }); +}; + +IdxDB.prototype.search = null; + + +IdxDB.prototype.info = function(){ + +}; + +/** + * @param {!string} ref + * @param {!string} modifier + * @param {!Function} task + */ + +IdxDB.prototype.transaction = function(ref, modifier, task){ + + const key = ref + (ref !== "reg" ? (this.field ? ":" + this.field : "") : ""); + /** + * @type {IDBObjectStore} + */ + let store = this.trx[key + ":" + modifier]; + if(store) return task.call(this, store); + let transaction = this.db.transaction(key, modifier); + /** + * @type {IDBObjectStore} + */ + this.trx[key + ":" + modifier] = store = transaction.objectStore(key); + const promise = task.call(this, store); + this.trx[key + ":" + modifier] = null; + + return promisfy(transaction).finally(function(){ + transaction = store = null; + return promise; + }); + + + + + + + + + + + + + + + + + + + +}; + +IdxDB.prototype.commit = async function(flexsearch, _replace, _append){ + + + if(_replace){ + await this.clear(); + + flexsearch.commit_task = []; + } + else { + let tasks = flexsearch.commit_task; + flexsearch.commit_task = []; + for(let i = 0, task; i < tasks.length; i++){ + /** @dict */ + task = tasks[i]; + + if(task["clear"]){ + await this.clear(); + _replace = true; + break; + } + else { + tasks[i] = task["del"]; + } + } + if(!_replace){ + if(!_append){ + tasks = tasks.concat(toArray(flexsearch.reg)); + } + tasks.length && await this.remove(tasks); + } + } + + if(!flexsearch.reg.size){ + return; + } + + await this.transaction("map", "readwrite", function(store){ + + for(const item of flexsearch.map){ + + const key = item[0]; + const value = item[1]; + if(!value.length) continue; + + if(_replace){ + store.put(value, key); + continue; + } + + store.get(key).onsuccess = function(){ + let result = this.result; + let changed; + if(result && result.length){ + const maxlen = Math.max(result.length, value.length); + for(let i = 0, res, val; i < maxlen; i++){ + val = value[i]; + if(val && val.length){ + res = result[i]; + if(res && res.length){ + for(let j = 0; j < val.length; j++){ + res.push(val[j]); + } + changed = 1; + + + + } + else { + result[i] = val; + changed = 1; + + } + } + } + } + else { + result = value; + changed = 1; + + + + + } + + changed && + store.put(result, key); + }; + } + }); + + await this.transaction("ctx", "readwrite", function(store){ + + for(const ctx of flexsearch.ctx){ + const ctx_key = ctx[0]; + const ctx_value = ctx[1]; + + for(const item of ctx_value){ + const key = item[0]; + const value = item[1]; + if(!value.length) continue; + + if(_replace){ + store.put(value, ctx_key + ":" + key); + continue; + } + + store.get(ctx_key + ":" + key).onsuccess = function(){ + let result = this.result; + let changed; + if(result && result.length){ + const maxlen = Math.max(result.length, value.length); + for(let i = 0, res, val; i < maxlen; i++){ + val = value[i]; + if(val && val.length){ + res = result[i]; + if(res && res.length){ + for(let j = 0; j < val.length; j++){ + res.push(val[j]); + } + + changed = 1; + } + else { + result[i] = val; + changed = 1; + } + } + } + } + else { + result = value; + changed = 1; + } + + changed && + store.put(result, ctx_key + ":" + key); + }; + } + } + }); + + if(SUPPORT_STORE && flexsearch.store){ + await this.transaction("reg", "readwrite", function(store){ + for(const item of flexsearch.store){ + const id = item[0]; + const doc = item[1]; + store.put(typeof doc === "object" + ? JSON.stringify(doc) + : 1 + , id); + } + }); + } + else if(!flexsearch.bypass){ + await this.transaction("reg", "readwrite", function(store){ + for(const id of flexsearch.reg.keys()){ + store.put(1, id); + } + }); + } + + if(SUPPORT_TAGS && flexsearch.tag){ + await this.transaction("tag", "readwrite", function(store){ + for(const item of flexsearch.tag){ + const tag = item[0]; + const ids = item[1]; + if(!ids.length) continue; + + store.get(tag).onsuccess = function(){ + let result = this.result; + result = result && result.length + ? result.concat(ids) + : ids; + store.put(result, tag); + }; + } + }); + } + + + + + + + + + + + + + + + + + + + + + flexsearch.map.clear(); + flexsearch.ctx.clear(); + if(SUPPORT_TAGS){ + flexsearch.tag && + flexsearch.tag.clear(); + } + if(SUPPORT_STORE){ + flexsearch.store && + flexsearch.store.clear(); + } + flexsearch.document || + flexsearch.reg.clear(); +}; + +/** + * @param {IDBCursorWithValue} cursor + * @param {Array} ids + * @param {boolean=} _tag + */ + +function handle(cursor, ids, _tag){ + + const arr = cursor.value; + let changed; + let count = 0; + + for(let x = 0, result; x < arr.length; x++){ + + if((result = _tag ? arr : arr[x])){ + for(let i = 0, pos, id; i < ids.length; i++){ + id = ids[i]; + pos = result.indexOf(id); + if(pos >= 0){ + changed = 1; + if(result.length > 1){ + result.splice(pos, 1); + } + else { + arr[x] = []; + break; + } + } + } + + count += result.length; + } + if(_tag) break; + } + + if(!count){ + cursor.delete(); + + } + else if(changed){ + + cursor.update(arr); + + } + + cursor.continue(); +} + +/** + * @param {Array} ids + * @return {!Promise} + */ +IdxDB.prototype.remove = function(ids){ + + const self = this; + + if(typeof ids !== "object"){ + ids = [ids]; + } + + return /** @type {!Promise} */(Promise.all([ + self.transaction("map", "readwrite", function(store){ + store.openCursor().onsuccess = function(){ + const cursor = this.result; + cursor && handle(cursor, ids); + }; + }), + self.transaction("ctx", "readwrite", function(store){ + store.openCursor().onsuccess = function(){ + const cursor = this.result; + cursor && handle(cursor, ids); + }; + }), + SUPPORT_TAGS && self.transaction("tag", "readwrite", function(store){ + store.openCursor().onsuccess = function(){ + const cursor = this.result; + cursor && handle(cursor, ids, true); + }; + }), + + self.transaction("reg", "readwrite", function(store){ + for(let i = 0; i < ids.length; i++){ + store.delete(ids[i]); + } + + + + + + + + + + + + + + + + + }) + + ])); +}; + +/** + * @param {IDBRequest|IDBOpenDBRequest} req + * @param {Function=} callback + * @return {!Promise} + */ + +function promisfy(req, callback){ + return new Promise((resolve, reject) => { + + /** @this {IDBRequest|IDBOpenDBRequest} */ + req.onsuccess = req.oncomplete = function(){ + callback && callback(this.result); + callback = null; + resolve(this.result); + }; + req.onerror = req.onblocked = reject; + req = null; + }); } module.exports = IdxDB; diff --git a/dist/db/mongodb/index.cjs b/dist/db/mongodb/index.cjs index 3e22c66..eb6e5b2 100644 --- a/dist/db/mongodb/index.cjs +++ b/dist/db/mongodb/index.cjs @@ -2,721 +2,742 @@ var mongodb = require('mongodb'); -/** - * @param {*} value - * @param {*} default_value - * @param {*=} merge_value - * @return {*} - */ - -/** - * @param {Map|Set} val - * @param {boolean=} stringify - * @return {Array} - */ - -function toArray(val, stringify){ - const result = []; - for(const key of val.keys()){ - result.push(key); - } - return result; +/** + * @param {*} value + * @param {*} default_value + * @param {*=} merge_value + * @return {*} + */ + +/** + * @param {Map|Set} val + * @param {boolean=} stringify + * @return {Array} + */ + +function toArray(val, stringify){ + const result = []; + for(const key of val.keys()){ + result.push(key); + } + return result; } -const defaults = { - host: "localhost", - port: "27017", - user: null, - pass: null -}; -const fields = ["map", "ctx", "tag", "reg", "cfg"]; - -function sanitize(str) { - return str.toLowerCase().replace(/[^a-z0-9_\-]/g, ""); -} - -let CLIENT; -let Index = Object.create(null); - -/** - * @constructor - * @implements StorageInterface - */ - -function MongoDB(name, config = {}){ - if(!this || this.constructor !== MongoDB){ - return new MongoDB(name, config); - } - if(typeof name === "object"){ - config = name; - name = name.name; - } - if(!name){ - console.info("Default storage space was used, because a name was not passed."); - } - this.id = "flexsearch" + (name ? "-" + sanitize(name) : ""); - this.field = config.field ? "-" + sanitize(config.field) : ""; - this.type = config.type || ""; - this.db = config.db || Index[this.id] || CLIENT || null; - this.trx = false; - this.support_tag_search = true; - Object.assign(defaults, config); - this.db && delete defaults.db; -} -// MongoDB.mount = function(flexsearch){ -// return new this().mount(flexsearch); -// }; - -MongoDB.prototype.mount = function(flexsearch){ - //if(flexsearch.constructor === Document){ - if(flexsearch.index){ - return flexsearch.mount(this); - } - flexsearch.db = this; - return this.open(); -}; - -async function createCollection(db, ref, field){ - switch(ref){ - case "map": - await db.createCollection("map" + field); - await db.collection("map" + field).createIndex({ key: 1 }); - await db.collection("map" + field).createIndex({ id: 1 }); - break; - case "ctx": - await db.createCollection("ctx" + field); - await db.collection("ctx" + field).createIndex({ ctx: 1, key: 1 }); - await db.collection("ctx" + field).createIndex({ id: 1 }); - break; - case "tag": - await db.createCollection("tag" + field); - await db.collection("tag" + field).createIndex({ tag: 1 }); - await db.collection("tag" + field).createIndex({ id: 1 }); - break; - case "reg": - await db.createCollection("reg"); - await db.collection("reg").createIndex({ id: 1 }); - break; - case "cfg": - await db.createCollection("cfg" + field); - } -} - -MongoDB.prototype.open = async function(){ - - if(!this.db){ - if(!(this.db = Index[this.id])){ - if(!(this.db = CLIENT)){ - - let url = defaults.url; - if(!url){ - url = defaults.user - ? `mongodb://${ defaults.user }:${ defaults.pass }@${ defaults.host }:${ defaults.port }` - : `mongodb://${ defaults.host }:${ defaults.port }`; - } - this.db = CLIENT = new mongodb.MongoClient(url); - await this.db.connect(); - } - } - } - - if(this.db.db){ - this.db = Index[this.id] = this.db.db(this.id); - } - - const collections = await this.db.listCollections().toArray(); - - for(let i = 0; i < fields.length; i++){ - let found = false; - for(let j = 0; j < collections.length; j++){ - if(collections[j].name === fields[i] + (fields[i] !== "reg" ? this.field : "")){ - found = true; - break; - } - } - if(!found){ - await createCollection(this.db, fields[i], this.field); - } - } - - return this.db; -}; - -MongoDB.prototype.close = function(){ - //CLIENT && CLIENT.close(); - this.db = CLIENT = null; - Index[this.id] = null; - return this; -}; - -MongoDB.prototype.destroy = function(){ - return Promise.all([ - this.db.dropCollection("map" + this.field), - this.db.dropCollection("ctx" + this.field), - this.db.dropCollection("tag" + this.field), - this.db.dropCollection("cfg" + this.field), - this.db.dropCollection("reg") - ]); -}; - -async function clear(ref){ - await this.db.dropCollection(ref); - await createCollection(this.db, ref, this.field); -} - -MongoDB.prototype.clear = function(){ - return Promise.all([ - clear.call(this, "map" + this.field), - clear.call(this, "ctx" + this.field), - clear.call(this, "tag" + this.field), - clear.call(this, "cfg" + this.field), - clear.call(this, "reg") - ]); -}; - -function create_result(rows, resolve, enrich){ - if(resolve){ - if(!enrich) for(let i = 0; i < rows.length; i++){ - rows[i] = rows[i].id; - } - return rows; - } - else { - const arr = []; - for(let i = 0, row; i < rows.length; i++){ - row = rows[i]; - arr[row.res] || (arr[row.res] = []); - arr[row.res].push(enrich - ? row - : row.id - ); - } - return arr; - } -} - -MongoDB.prototype.get = async function(key, ctx, limit = 0, offset = 0, resolve = true, enrich = false, tags){ - let rows; - let params = ctx ? { ctx, key } : { key }; - if(!enrich && !tags){ - rows = await this.db.collection((ctx ? "ctx" : "map") + this.field) - .find(params, { projection: { _id: 0, res: 1, id: 1 }, limit, skip: offset }) - .toArray(); - } - else { - - const project = { _id: 0, id: 1 }; - const stmt = [ - { $match: params } - ]; - - if(!resolve){ - project["res"] = 1; - } - - if(enrich){ - project["doc"] = "$doc.doc"; - stmt.push( - { $lookup: { - from: "reg", - localField: "id", - foreignField: "id", - as: "doc" - } }, - { $unwind: { - path: "$doc", - preserveNullAndEmptyArrays: true - } } - ); - } - - if(tags){ - - const match = {}; - for(let i = 0, count = 1; i < tags.length; i += 2){ - project["tag" + count] = "$tag" + count + ".tag"; - match["tag" + count] = tags[i + 1]; - stmt.push( - { $lookup: { - from: "tag-" + sanitize(tags[i]), - localField: "id", - foreignField: "id", - as: "tag" + count - } } - ); - count++; - } - - stmt.push( - { $project: project }, - { $match: match }, - { $project: { id: 1, doc: 1 } } - ); - } - else { - stmt.push( - { $project: project } - ); - } - - stmt.push( - { $sort: { res: 1 } }, - { $skip: offset}, - { $limit: limit } - ); - - rows = []; - const result = await this.db.collection((ctx ? "ctx" : "map") + this.field).aggregate(stmt); - - while(true/*await rows.hasNext()*/){ - const row = await result.next(); - if(row) rows.push(row); - else break; - } - } - - return create_result(rows, resolve, enrich); -}; - -MongoDB.prototype.tag = async function(tag, limit = 0, offset = 0, enrich = false){ - - if(!enrich){ - - const rows = await this.db.collection("tag" + this.field) - .find({ tag }, { projection: { _id: 0, id: 1 }, limit, skip: offset }) - .toArray(); - return create_result(rows, true, false); - } - else { - - const stmt = [ - { $match: { tag } }, - { $skip: offset}, - { $limit: limit }, - { $lookup: { - from: "reg", - localField: "id", - foreignField: "id", - as: "doc" - } }, - { $project: { _id: 0, id: 1, doc: "$doc.doc" } }, - { $unwind: { - path: "$doc", - preserveNullAndEmptyArrays: true - } } - ]; - - let rows = []; - const result = await this.db.collection("tag" + this.field).aggregate(stmt); - - while(true/*await rows.hasNext()*/){ - const row = await result.next(); - if(row) rows.push(row); - else break; - } - - return rows; - } -}; - -MongoDB.prototype.enrich = function(ids){ - if(typeof ids !== "object"){ - ids = [ids]; - } - return this.db.collection("reg") - .find({ id: { $in: ids } }, { projection: { _id: 0, id: 1, doc: 1 } }) - .toArray(); -}; - -MongoDB.prototype.has = function(id){ - return this.db.collection("reg").countDocuments({ id }, { limit: 1 }).then(function(result){ - return !!result; - }); -}; - -MongoDB.prototype.search = async function(flexsearch, query, limit = 100, offset = 0, suggest = false, resolve = true, enrich = false, tags){ - - let result = [], rows; - - if(query.length > 1 && flexsearch.depth){ - - let params = []; - let keyword = query[0]; - let term; - - for(let i = 1; i < query.length; i++){ - term = query[i]; - const swap = flexsearch.bidirectional && (term > keyword); - params.push({ - ctx: swap ? term : keyword, - key: swap ? keyword : term - }); - keyword = term; - } - - - const project = { _id: 1 }; - if(!resolve) project["res"] = 1; - if(enrich) project["doc"] = 1; - - const stmt = [ - { $match: { $or: params } }, - { $group: { - _id: "$id", - count: { $sum: 1 }, - res: suggest ? { $sum: "$res" } : { $sum /*$min*/: "$res" } - } } - ]; - - suggest || stmt.push( - { $match: { count: query.length - 1 } } - ); - - if(enrich){ - project["doc"] = "$doc.doc"; - stmt.push( - { $lookup: { - from: "reg", - localField: "_id", - foreignField: "id", - as: "doc" - } }, - { $unwind: { - path: "$doc", - preserveNullAndEmptyArrays: true - } } - ); - } - - if(tags){ - - const match = {}; - for(let i = 0, count = 1; i < tags.length; i += 2){ - project["tag" + count] = "$tag" + count + ".tag"; - match["tag" + count] = tags[i + 1]; - stmt.push( - { $lookup: { - from: "tag-" + sanitize(tags[i]), - localField: "_id", - foreignField: "id", - as: "tag" + count - } } - ); - count++; - } - - stmt.push( - //{ $project: project }, - { $match: match } - ); - } - - stmt.push( - { $sort: suggest - ? { count: -1, res: 1} - : { res: 1 } }, - { $skip: offset}, - { $limit: limit } - ); - - // if(tags){ - // project = { _id: 1 }; - // if(!resolve) project["res"] = 1; - // if(enrich) project["doc"] = 1; - // } - - stmt.push( - { $project: project } - ); - - rows = await this.db.collection("ctx" + this.field).aggregate(stmt); - } - else { - - const project = { _id: 1 }; - if(!resolve) project["res"] = 1; - if(enrich) project["doc"] = 1; - - const stmt = [ - { $match: { - key: { $in: query } - } }, - { $group: { - _id: "$id", - count: { $sum: 1 }, - res: suggest ? { $sum: "$res" } : { $sum /*$min*/: "$res" } - } } - ]; - - suggest || stmt.push( - { $match: { count: query.length } } - ); - - if(enrich){ - project["doc"] = "$doc.doc"; - stmt.push( - { $lookup: { - from: "reg", - localField: "_id", - foreignField: "id", - as: "doc" - } }, - { $unwind: { - path: "$doc", - preserveNullAndEmptyArrays: true - } } - ); - } - - if(tags){ - - const match = {}; - for(let i = 0, count = 1; i < tags.length; i += 2){ - project["tag" + count] = "$tag" + count + ".tag"; - match["tag" + count] = tags[i + 1]; - stmt.push( - { $lookup: { - from: "tag-" + sanitize(tags[i]), - localField: "_id", - foreignField: "id", - as: "tag" + count - } } - ); - count++; - } - - stmt.push( - //{ $project: project }, - { $match: match } - ); - } - - stmt.push( - { $sort: suggest - ? { count: -1, res: 1 } - : { res: 1 } }, - { $skip: offset}, - { $limit: limit } - ); - - // if(tags){ - // project = { _id: 1 }; - // if(!resolve) project["res"] = 1; - // if(enrich) project["doc"] = 1; - // } - - stmt.push( - { $project: project } - ); - - rows = await this.db.collection("map" + this.field).aggregate(stmt); - } - - while(true/*await rows.hasNext()*/) { - const row = await rows.next(); - if(row){ - if(resolve && !enrich){ - result.push(row._id); - } - else { - row.id = row._id; - delete row._id; - result.push(row); - } - } - else break; - } - - if(resolve && !enrich){ - return result; - } - else { - return create_result(result, resolve, enrich); - } -}; - -MongoDB.prototype.info = function(){ - // todo -}; - -MongoDB.prototype.transaction = function(task){ - // not supported - return task.call(this); -}; - -MongoDB.prototype.commit = async function(flexsearch, _replace, _append){ - - // process cleanup tasks - if(_replace){ - await this.clear(); - // there are just removals in the task queue - flexsearch.commit_task = []; - } - else { - let tasks = flexsearch.commit_task; - flexsearch.commit_task = []; - for(let i = 0, task; i < tasks.length; i++){ - task = tasks[i]; - // there are just removals in the task queue - if(task.clear){ - await this.clear(); - _replace = true; - break; - } - else { - tasks[i] = task.del; - } - } - if(!_replace){ - if(!_append){ - tasks = tasks.concat(toArray(flexsearch.reg)); - } - tasks.length && await this.remove(tasks); - } - } - - if(!flexsearch.reg.size){ - return; - } - - const promises = []; - - if(flexsearch.map.size){ - let data = []; - for(const item of flexsearch.map){ - const key = item[0]; - const arr = item[1]; - for(let i = 0, ids; i < arr.length; i++){ - if((ids = arr[i]) && ids.length){ - this.type || (this.type = typeof ids[0]); - for(let j = 0; j < ids.length; j++){ - data.push({ - key: key, - res: i, - id: ids[j] - }); - } - } - } - } - if(data.length){ - promises.push( - this.db.collection("map" + this.field).insertMany(data) - ); - } - } - - if(flexsearch.ctx.size){ - let data = []; - for(const ctx of flexsearch.ctx){ - const ctx_key = ctx[0]; - const ctx_value = ctx[1]; - for(const item of ctx_value){ - const key = item[0]; - const arr = item[1]; - for(let i = 0, ids; i < arr.length; i++){ - if((ids = arr[i]) && ids.length){ - for(let j = 0; j < ids.length; j++){ - data.push({ - ctx: ctx_key, - key: key, - res: i, - id: ids[j] - }); - } - } - } - } - } - if(data.length){ - promises.push( - this.db.collection("ctx" + this.field).insertMany(data) - ); - } - } - - if(flexsearch.tag){ - let data = []; - for(const item of flexsearch.tag){ - const tag = item[0]; - const ids = item[1]; - if(!ids.length) continue; - for(let j = 0; j < ids.length; j++){ - data.push({ tag, id: ids[j] }); - } - } - if(data.length){ - promises.push( - this.db.collection("tag" + this.field).insertMany(data) - ); - } - } - - let data = []; - if(flexsearch.store){ - for(const item of flexsearch.store.entries()){ - const id = item[0]; - const doc = item[1]; - data.push({ id, doc }); - } - } - else if(!flexsearch.bypass){ - for(const id of flexsearch.reg.keys()){ - data.push({ id }); - } - } - if(data.length){ - promises.push( - this.db.collection("reg").insertMany(data) - ); - } - - promises.length && await Promise.all(promises); - - flexsearch.map.clear(); - flexsearch.ctx.clear(); - flexsearch.tag && - flexsearch.tag.clear(); - flexsearch.store && - flexsearch.store.clear(); - flexsearch.document || - flexsearch.reg.clear(); - - // TODO - // await this.db.collection("cfg" + this.field).insertOne({ - // "encode": typeof flexsearch.encode === "string" ? flexsearch.encode : "", - // "charset": typeof flexsearch.charset === "string" ? flexsearch.charset : "", - // "tokenize": flexsearch.tokenize, - // "resolution": flexsearch.resolution, - // "minlength": flexsearch.minlength, - // "optimize": flexsearch.optimize, - // "fastupdate": flexsearch.fastupdate, - // "encoder": flexsearch.encoder, - // "context": { - // "depth": flexsearch.depth, - // "bidirectional": flexsearch.bidirectional, - // "resolution": flexsearch.resolution_ctx - // } - // }); -}; - -MongoDB.prototype.remove = function(ids){ - - if(!ids && ids !== 0) return; - if(typeof ids !== "object"){ - ids = [ids]; - } - - // if(this.type !== "string" && typeof ids[0] !== "number"){ - // ids = ids.map(item => parseInt(item, 10)); - // } - - return Promise.all([ - this.db.collection("map" + this.field).deleteMany({ "id": { "$in": ids }}), - this.db.collection("ctx" + this.field).deleteMany({ "id": { "$in": ids }}), - this.db.collection("tag" + this.field).deleteMany({ "id": { "$in": ids }}), - this.db.collection("reg").deleteMany({ "id": { "$in": ids }}) - ]); +const defaults = { + host: "localhost", + port: "27017", + user: null, + pass: null +}; +const fields = ["map", "ctx", "tag", "reg", "cfg"]; + +function sanitize(str) { + return str.toLowerCase().replace(/[^a-z0-9_\-]/g, ""); +} + +let CLIENT; +let Index = Object.create(null); + +/** + * @constructor + * @implements StorageInterface + */ + +function MongoDB(name, config = {}){ + if(!this || this.constructor !== MongoDB){ + return new MongoDB(name, config); + } + if(typeof name === "object"){ + config = name; + name = name.name; + } + if(!name){ + console.info("Default storage space was used, because a name was not passed."); + } + this.id = "flexsearch" + (name ? "-" + sanitize(name) : ""); + this.field = config.field ? "-" + sanitize(config.field) : ""; + this.type = config.type || ""; + this.db = config.db || Index[this.id] || CLIENT || null; + this.trx = false; + this.support_tag_search = true; + Object.assign(defaults, config); + this.db && delete defaults.db; +} + +MongoDB.prototype.mount = function(flexsearch){ + + if(flexsearch.index){ + return flexsearch.mount(this); + } + flexsearch.db = this; + return this.open(); +}; + +async function createCollection(db, ref, field){ + switch(ref){ + case "map": + await db.createCollection("map" + field); + await db.collection("map" + field).createIndex({ key: 1 }); + await db.collection("map" + field).createIndex({ id: 1 }); + break; + case "ctx": + await db.createCollection("ctx" + field); + await db.collection("ctx" + field).createIndex({ ctx: 1, key: 1 }); + await db.collection("ctx" + field).createIndex({ id: 1 }); + break; + case "tag": + await db.createCollection("tag" + field); + await db.collection("tag" + field).createIndex({ tag: 1 }); + await db.collection("tag" + field).createIndex({ id: 1 }); + break; + case "reg": + await db.createCollection("reg"); + await db.collection("reg").createIndex({ id: 1 }); + break; + case "cfg": + await db.createCollection("cfg" + field); + } +} + +MongoDB.prototype.open = async function(){ + + if(!this.db){ + if(!(this.db = Index[this.id])){ + if(!(this.db = CLIENT)){ + + let url = defaults.url; + if(!url){ + url = defaults.user + ? `mongodb://${ defaults.user }:${ defaults.pass }@${ defaults.host }:${ defaults.port }` + : `mongodb://${ defaults.host }:${ defaults.port }`; + } + this.db = CLIENT = new mongodb.MongoClient(url); + await this.db.connect(); + } + } + } + + if(this.db.db){ + this.db = Index[this.id] = this.db.db(this.id); + } + + const collections = await this.db.listCollections().toArray(); + + for(let i = 0; i < fields.length; i++){ + let found = false; + for(let j = 0; j < collections.length; j++){ + if(collections[j].name === fields[i] + (fields[i] !== "reg" ? this.field : "")){ + found = true; + break; + } + } + if(!found){ + await createCollection(this.db, fields[i], this.field); + } + } + + return this.db; +}; + +MongoDB.prototype.close = function(){ + + this.db = CLIENT = null; + Index[this.id] = null; + return this; +}; + +MongoDB.prototype.destroy = function(){ + return Promise.all([ + this.db.dropCollection("map" + this.field), + this.db.dropCollection("ctx" + this.field), + this.db.dropCollection("tag" + this.field), + this.db.dropCollection("cfg" + this.field), + this.db.dropCollection("reg") + ]); +}; + +async function clear(ref){ + await this.db.dropCollection(ref); + await createCollection(this.db, ref, this.field); +} + +MongoDB.prototype.clear = function(){ + return Promise.all([ + clear.call(this, "map" + this.field), + clear.call(this, "ctx" + this.field), + clear.call(this, "tag" + this.field), + clear.call(this, "cfg" + this.field), + clear.call(this, "reg") + ]); +}; + +function create_result(rows, resolve, enrich){ + const _id = rows[0] && typeof rows[0]._id !== "undefined"; + if(resolve){ + if(!enrich || _id) for(let i = 0, row; i < rows.length; i++){ + row = rows[i]; + if(enrich){ + const id = row._id; + delete row._id; + row.id = id; + } + else { + rows[i] = _id ? row._id : row.id; + } + } + return rows; + } + else { + const arr = []; + for(let i = 0, row, res; i < rows.length; i++){ + row = rows[i]; + res = row.res; + (arr[res] || (arr[res] = [])).push( + _id ? row._id : row.id + ); + } + return arr; + } +} + +MongoDB.prototype.get = async function(key, ctx, limit = 0, offset = 0, resolve = true, enrich = false, tags){ + + let rows; + let params = ctx ? { ctx, key } : { key }; + + if(!enrich && !tags){ + + const stmt = { + projection: { _id: 0, res: 1, id: 1 } + }; + + limit && (stmt.limit = limit); + offset && (stmt.skip = offset); + + rows = await this.db.collection((ctx ? "ctx" : "map") + this.field) + .find(params, stmt) + .toArray(); + } + else { + + const project = { _id: 0, id: 1 }; + const stmt = [ + { $match: params } + ]; + + if(!resolve){ + project["res"] = 1; + } + + if(enrich){ + project["doc"] = "$doc.doc"; + stmt.push( + { $lookup: { + from: "reg", + localField: "id", + foreignField: "id", + as: "doc" + } }, + { $unwind: { + path: "$doc", + preserveNullAndEmptyArrays: true + } } + ); + } + + if(tags){ + + const match = {}; + for(let i = 0, count = 1; i < tags.length; i += 2){ + project["tag" + count] = "$tag" + count + ".tag"; + match["tag" + count] = tags[i + 1]; + stmt.push( + { $lookup: { + from: "tag-" + sanitize(tags[i]), + localField: "id", + foreignField: "id", + as: "tag" + count + } } + ); + count++; + } + + stmt.push( + { $project: project }, + { $match: match }, + { $project: { id: 1, doc: 1 } } + ); + } + else { + stmt.push( + { $project: project } + ); + } + + stmt.push( + { $sort: { res: 1 } } + ); + + limit && stmt.push( + { $limit: limit } + ); + offset && stmt.push( + { $skip: offset} + ); + + rows = []; + const result = await this.db.collection((ctx ? "ctx" : "map") + this.field).aggregate(stmt); + + while(true){ + const row = await result.next(); + if(row) rows.push(row); + else break; + } + } + + return create_result(rows, resolve, enrich); +}; + +MongoDB.prototype.tag = async function(tag, limit = 0, offset = 0, enrich = false){ + + let rows; + + if(!enrich){ + + const stmt = { + projection: { _id: 0, id: 1 } + }; + + limit && (stmt.limit = limit); + offset && (stmt.skip = offset); + + rows = await this.db.collection("tag" + this.field) + .find({ tag }, stmt) + .toArray(); + } + else { + + const stmt = [ + { $match: { tag } } + ]; + + limit && stmt.push( + { $limit: limit } + ); + offset && stmt.push( + { $skip: offset} + ); + + stmt.push( + { $lookup: { + from: "reg", + localField: "id", + foreignField: "id", + as: "doc" + } }, + { $project: { _id: 0, id: 1, doc: "$doc.doc" } }, + { $unwind: { + path: "$doc", + preserveNullAndEmptyArrays: true + } } + ); + + rows = []; + const result = await this.db.collection("tag" + this.field).aggregate(stmt); + + while(true){ + const row = await result.next(); + if(row) rows.push(row); + else break; + } + } + + create_result(rows, true, enrich); +}; + +MongoDB.prototype.enrich = function(ids){ + if(typeof ids !== "object"){ + ids = [ids]; + } + return this.db.collection("reg") + .find({ id: { $in: ids } }, { projection: { _id: 0, id: 1, doc: 1 } }) + .toArray(); +}; + +MongoDB.prototype.has = function(id){ + return this.db.collection("reg").countDocuments({ id }, { limit: 1 }).then(function(result){ + return !!result; + }); +}; + +MongoDB.prototype.search = async function(flexsearch, query, limit = 100, offset = 0, suggest = false, resolve = true, enrich = false, tags){ + + let result = [], rows; + + if(query.length > 1 && flexsearch.depth){ + + let params = []; + let keyword = query[0]; + let term; + + for(let i = 1; i < query.length; i++){ + term = query[i]; + const swap = flexsearch.bidirectional && (term > keyword); + params.push({ + ctx: swap ? term : keyword, + key: swap ? keyword : term + }); + keyword = term; + } + + + const project = { _id: 1 }; + if(!resolve) project["res"] = 1; + if(enrich) project["doc"] = 1; + + const stmt = [ + { $match: { $or: params } }, + { $group: { + _id: "$id", + count: { $sum: 1 }, + res: suggest ? { $sum: "$res" } : { $sum : "$res" } + } } + ]; + + suggest || stmt.push( + { $match: { count: query.length - 1 } } + ); + + if(enrich){ + project["doc"] = "$doc.doc"; + stmt.push( + { $lookup: { + from: "reg", + localField: "_id", + foreignField: "id", + as: "doc" + } }, + { $unwind: { + path: "$doc", + preserveNullAndEmptyArrays: true + } } + ); + } + + if(tags){ + + const match = {}; + for(let i = 0, count = 1; i < tags.length; i += 2){ + project["tag" + count] = "$tag" + count + ".tag"; + match["tag" + count] = tags[i + 1]; + stmt.push( + { $lookup: { + from: "tag-" + sanitize(tags[i]), + localField: "_id", + foreignField: "id", + as: "tag" + count + } } + ); + count++; + } + + stmt.push( + { $match: match } + ); + } + + stmt.push( + { $sort: suggest + ? { count: -1, res: 1} + : { res: 1 } } + ); + limit && stmt.push( + { $limit: limit } + ); + offset && stmt.push( + { $skip: offset} + ); + stmt.push( + { $project: project } + ); + + rows = await this.db.collection("ctx" + this.field).aggregate(stmt); + } + else { + + const project = { _id: 1 }; + if(!resolve) project["res"] = 1; + if(enrich) project["doc"] = 1; + + const stmt = [ + { $match: { + key: { $in: query } + } }, + { $group: { + _id: "$id", + count: { $sum: 1 }, + res: suggest ? { $sum: "$res" } : { $sum : "$res" } + } } + ]; + + suggest || stmt.push( + { $match: { count: query.length } } + ); + + if(enrich){ + project["doc"] = "$doc.doc"; + stmt.push( + { $lookup: { + from: "reg", + localField: "_id", + foreignField: "id", + as: "doc" + } }, + { $unwind: { + path: "$doc", + preserveNullAndEmptyArrays: true + } } + ); + } + + if(tags){ + + const match = {}; + for(let i = 0, count = 1; i < tags.length; i += 2){ + project["tag" + count] = "$tag" + count + ".tag"; + match["tag" + count] = tags[i + 1]; + stmt.push( + { $lookup: { + from: "tag-" + sanitize(tags[i]), + localField: "_id", + foreignField: "id", + as: "tag" + count + } } + ); + count++; + } + + stmt.push( + { $match: match } + ); + } + + stmt.push( + { $sort: suggest + ? { count: -1, res: 1 } + : { res: 1 } + } + ); + limit && stmt.push( + { $limit: limit } + ); + offset && stmt.push( + { $skip: offset} + ); + stmt.push( + { $project: project } + ); + + rows = await this.db.collection("map" + this.field).aggregate(stmt); + } + + while(true) { + const row = await rows.next(); + if(row){ + if(resolve && enrich){ + row.id = row._id; + delete row._id; + } + result.push(row); + } + else break; + } + + return create_result(result, resolve, enrich); +}; + +MongoDB.prototype.info = function(){ + +}; + +MongoDB.prototype.transaction = function(task){ + + return task.call(this); +}; + +MongoDB.prototype.commit = async function(flexsearch, _replace, _append){ + + + if(_replace){ + await this.clear(); + + flexsearch.commit_task = []; + } + else { + let tasks = flexsearch.commit_task; + flexsearch.commit_task = []; + for(let i = 0, task; i < tasks.length; i++){ + task = tasks[i]; + + if(task.clear){ + await this.clear(); + _replace = true; + break; + } + else { + tasks[i] = task.del; + } + } + if(!_replace){ + if(!_append){ + tasks = tasks.concat(toArray(flexsearch.reg)); + } + tasks.length && await this.remove(tasks); + } + } + + if(!flexsearch.reg.size){ + return; + } + + const promises = []; + + if(flexsearch.map.size){ + let data = []; + for(const item of flexsearch.map){ + const key = item[0]; + const arr = item[1]; + for(let i = 0, ids; i < arr.length; i++){ + if((ids = arr[i]) && ids.length){ + this.type || (this.type = typeof ids[0]); + for(let j = 0; j < ids.length; j++){ + data.push({ + key: key, + res: i, + id: ids[j] + }); + } + } + } + } + if(data.length){ + promises.push( + this.db.collection("map" + this.field).insertMany(data) + ); + } + } + + if(flexsearch.ctx.size){ + let data = []; + for(const ctx of flexsearch.ctx){ + const ctx_key = ctx[0]; + const ctx_value = ctx[1]; + for(const item of ctx_value){ + const key = item[0]; + const arr = item[1]; + for(let i = 0, ids; i < arr.length; i++){ + if((ids = arr[i]) && ids.length){ + for(let j = 0; j < ids.length; j++){ + data.push({ + ctx: ctx_key, + key: key, + res: i, + id: ids[j] + }); + } + } + } + } + } + if(data.length){ + promises.push( + this.db.collection("ctx" + this.field).insertMany(data) + ); + } + } + + if(flexsearch.tag){ + let data = []; + for(const item of flexsearch.tag){ + const tag = item[0]; + const ids = item[1]; + if(!ids.length) continue; + for(let j = 0; j < ids.length; j++){ + data.push({ tag, id: ids[j] }); + } + } + if(data.length){ + promises.push( + this.db.collection("tag" + this.field).insertMany(data) + ); + } + } + + let data = []; + if(flexsearch.store){ + for(const item of flexsearch.store.entries()){ + const id = item[0]; + const doc = item[1]; + data.push({ id, doc }); + } + } + else if(!flexsearch.bypass){ + for(const id of flexsearch.reg.keys()){ + data.push({ id }); + } + } + if(data.length){ + promises.push( + this.db.collection("reg").insertMany(data) + ); + } + + promises.length && await Promise.all(promises); + + flexsearch.map.clear(); + flexsearch.ctx.clear(); + flexsearch.tag && + flexsearch.tag.clear(); + flexsearch.store && + flexsearch.store.clear(); + flexsearch.document || + flexsearch.reg.clear(); + + + + + + + + + + + + + + + + + +}; + +MongoDB.prototype.remove = function(ids){ + + if(!ids && ids !== 0) return; + if(typeof ids !== "object"){ + ids = [ids]; + } + + + + + + return Promise.all([ + this.db.collection("map" + this.field).deleteMany({ "id": { "$in": ids }}), + this.db.collection("ctx" + this.field).deleteMany({ "id": { "$in": ids }}), + this.db.collection("tag" + this.field).deleteMany({ "id": { "$in": ids }}), + this.db.collection("reg").deleteMany({ "id": { "$in": ids }}) + ]); }; module.exports = MongoDB; diff --git a/dist/db/postgres/index.cjs b/dist/db/postgres/index.cjs index c6c6253..ba7a078 100644 --- a/dist/db/postgres/index.cjs +++ b/dist/db/postgres/index.cjs @@ -2,1045 +2,959 @@ var pg_promise = require('pg-promise'); -/** - * @param {*} value - * @param {*} default_value - * @param {*=} merge_value - * @return {*} - */ - -function concat(arrays){ - return [].concat.apply([], arrays); +/** + * @param {*} value + * @param {*} default_value + * @param {*=} merge_value + * @return {*} + */ + +function concat(arrays){ + return [].concat.apply([], arrays); +} + +/** + * @param {Map|Set} val + * @param {boolean=} stringify + * @return {Array} + */ + +function toArray(val, stringify){ + const result = []; + for(const key of val.keys()){ + result.push(key); + } + return result; } -/** - * @param {Map|Set} val - * @param {boolean=} stringify - * @return {Array} - */ - -function toArray(val, stringify){ - const result = []; - for(const key of val.keys()){ - result.push(key); - } - return result; -} - -const defaults = { - schema: "flexsearch", - user: "postgres", - pass: "postgres", - name: "postgres", - host: "localhost", - port: "5432" +const defaults = { + schema: "flexsearch", + user: "postgres", + pass: "postgres", + name: "postgres", + host: "localhost", + port: "5432" +}; + +const pgp = pg_promise(); +const MAXIMUM_QUERY_VARS = 16000; +const fields = ["map", "ctx", "reg", "tag", "cfg"]; +const types = { + "text": "text", + "char": "text", + "varchar": "text", + "string": "text", + "number": "int", + "numeric": "int", + "integer": "int", + "smallint": "int", + "tinyint": "int", + "mediumint": "int", + "int": "int", + "int8": "int", + "uint8": "int", + "int16": "int", + "uint16": "int", + "int32": "int", + "uint32": "bigint", + "int64": "bigint", + "bigint": "bigint" +}; + +function sanitize(str) { + return str.toLowerCase().replace(/[^a-z0-9_]/g, ""); +} + +let DB; + +/** + * @constructor + * @implements StorageInterface + */ + +function PostgresDB(name, config = {}){ + if(!this || this.constructor !== PostgresDB){ + return new PostgresDB(name, config); + } + if(typeof name === "object"){ + config = name; + name = config.name; + } + if(!name){ + console.info("Default storage space was used, because a name was not passed."); + } + this.id = (config.schema ? sanitize(config.schema) : defaults.schema) + (name ? "_" + sanitize(name) : ""); + this.field = config.field ? "_" + sanitize(config.field) : ""; + this.type = config.type ? types[config.type.toLowerCase()] : "text"; + this.support_tag_search = true; + if(!this.type) throw new Error("Unknown type of ID '" + config.type + "'"); + this.db = DB || (DB = config.db || null); + Object.assign(defaults, config); + this.db && delete defaults.db; +} +PostgresDB.prototype.mount = function(flexsearch){ + + if(flexsearch.index){ + return flexsearch.mount(this); + } + flexsearch.db = this; + return this.open(); +}; + +PostgresDB.prototype.open = async function(){ + + if(!this.db) { + this.db = DB || ( + DB = pgp(`postgres://${defaults.user}:${encodeURIComponent(defaults.pass)}@${defaults.host}:${defaults.port}/${defaults.name}`) + ); + } + + const exist = await this.db.oneOrNone(` + SELECT EXISTS ( + SELECT 1 + FROM information_schema.schemata + WHERE schema_name = '${this.id}' + ); + `); + if(!exist || !exist.exists){ + await this.db.none(`CREATE SCHEMA IF NOT EXISTS ${ this.id };`); + } + + for(let i = 0; i < fields.length; i++){ + const exist = await this.db.oneOrNone(` + SELECT EXISTS ( + SELECT 1 FROM pg_tables + WHERE schemaname = '${this.id}' AND tablename = '${fields[i] + (fields[i] !== "reg" ? this.field : "")}' + ); + `); + if(exist && exist.exists) continue; + + const type = this.type === "text" + ? "varchar(128)" + : this.type; + + switch(fields[i]){ + case "map": + await this.db.none(` + CREATE TABLE IF NOT EXISTS ${this.id}.map${this.field}( + key varchar(128) NOT NULL, + res smallint NOT NULL, + id ${type} NOT NULL + ); + CREATE INDEX IF NOT EXISTS ${this.id}_map_index${this.field} + ON ${this.id}.map${this.field} (key); + CREATE INDEX IF NOT EXISTS ${this.id}_map_id${this.field} + ON ${this.id}.map${this.field} (id); + `); + break; + + case "ctx": + await this.db.none(` + CREATE TABLE IF NOT EXISTS ${this.id}.ctx${this.field}( + ctx varchar(128) NOT NULL, + key varchar(128) NOT NULL, + res smallint NOT NULL, + id ${type} NOT NULL + ); + CREATE INDEX IF NOT EXISTS ${this.id}_ctx_index${this.field} + ON ${this.id}.ctx${this.field} (ctx, key); + CREATE INDEX IF NOT EXISTS ${this.id}_ctx_id${this.field} + ON ${this.id}.ctx${this.field} (id); + `); + break; + + case "tag": + await this.db.none(` + CREATE TABLE IF NOT EXISTS ${this.id}.tag${this.field}( + tag varchar(128) NOT NULL, + id ${type} NOT NULL + ); + CREATE INDEX IF NOT EXISTS ${this.id}_tag_index${this.field} + ON ${this.id}.tag${this.field} (tag); + CREATE INDEX IF NOT EXISTS ${this.id}_tag_id${this.field} + ON ${this.id}.tag${this.field} (id); + `); + break; + + case "reg": + await this.db.none(` + CREATE TABLE IF NOT EXISTS ${this.id}.reg( + id ${type} NOT NULL + CONSTRAINT ${this.id}_reg_pk + PRIMARY KEY, + doc text DEFAULT NULL + ); + `).catch(e => { + + + }); + break; + + case "cfg": + await this.db.none(` + CREATE TABLE IF NOT EXISTS ${this.id}.cfg${this.field}( + cfg text NOT NULL + ); + `); + break; + } + } + + return this.db; +}; + +PostgresDB.prototype.close = function(){ + + this.db = null; + return this; +}; + +PostgresDB.prototype.destroy = function(){ + return this.db.none(` + DROP TABLE IF EXISTS ${this.id}.map${this.field}; + DROP TABLE IF EXISTS ${this.id}.ctx${this.field}; + DROP TABLE IF EXISTS ${this.id}.tag${this.field}; + DROP TABLE IF EXISTS ${this.id}.cfg${this.field}; + DROP TABLE IF EXISTS ${this.id}.reg; + `); +}; + +PostgresDB.prototype.clear = function(){ + return this.db.none(` + TRUNCATE TABLE ${this.id}.map${ this.field }; + TRUNCATE TABLE ${this.id}.ctx${ this.field }; + TRUNCATE TABLE ${this.id}.tag${ this.field }; + TRUNCATE TABLE ${this.id}.cfg${ this.field }; + TRUNCATE TABLE ${this.id}.reg; + `); +}; + +function create_result(rows, resolve, enrich){ + if(resolve){ + for(let i = 0; i < rows.length; i++){ + if(enrich){ + if(rows[i].doc){ + rows[i].doc = JSON.parse(rows[i].doc); + } + } + else { + rows[i] = rows[i].id; + } + } + return rows; + } + else { + const arr = []; + for(let i = 0, row; i < rows.length; i++){ + row = rows[i]; + arr[row.res] || (arr[row.res] = []); + arr[row.res].push(enrich + ? row + : row.id + ); + } + return arr; + } +} + +PostgresDB.prototype.get = function(key, ctx, limit = 0, offset = 0, resolve = true, enrich = false, tags){ + let rows; + let stmt = ''; + let params = ctx ? [ctx, key] : [key]; + let table = this.id + (ctx ? ".ctx" : ".map") + this.field; + if(tags){ + for(let i = 0, count = params.length + 1; i < tags.length; i+=2){ + stmt += ` AND ${ table }.id IN (SELECT id FROM ${ this.id }.tag_${ sanitize(tags[i]) } WHERE tag = $${ count++ })`; + params.push(tags[i + 1]); + } + } + if(ctx){ + rows = this.db.any(` + SELECT ${ table }.id + ${ resolve ? "" : ", res" } + ${ enrich ? ", doc" : "" } + FROM ${ table } + ${ enrich ? ` + LEFT JOIN ${ this.id }.reg ON ${ this.id }.reg.id = ${ table }.id + ` : "" } + WHERE ctx = $1 AND key = $2 ${stmt} + ORDER BY res + ${ limit ? "LIMIT " + limit : "" } + ${ offset ? "OFFSET " + offset : "" }`, + params + ); + } + else { + rows = this.db.any(` + SELECT ${ table }.id + ${ resolve ? "" : ", res" } + ${ enrich ? ", doc" : "" } + FROM ${ table } + ${ enrich ? ` + LEFT JOIN ${ this.id }.reg ON ${ this.id }.reg.id = ${ table }.id + ` : "" } + WHERE key = $1 ${stmt} + ORDER BY res + ${ limit ? "LIMIT " + limit : "" } + ${ offset ? "OFFSET " + offset : "" }`, + params + ); + } + return rows.then(function(rows){ + return create_result(rows, resolve, enrich); + }); +}; + +PostgresDB.prototype.tag = function(tag, limit = 0, offset = 0, enrich = false){ + const table = this.id + ".tag" + this.field; + const promise = this.db.any(` + SELECT ${ table }.id + ${ enrich ? ", doc" : "" } + FROM ${ table } + ${ enrich ? ` + LEFT JOIN ${ this.id }.reg ON ${ this.id }.reg.id = ${ table }.id + ` : "" } + WHERE tag = $1 + ${ limit ? "LIMIT " + limit : "" } + ${ offset ? "OFFSET " + offset : "" }`, + [tag] + ); + enrich || promise.then(function(rows){ + return create_result(rows, true, false); + }); + return promise; +}; + +PostgresDB.prototype.enrich = async function(ids){ + let result = []; + if(typeof ids !== "object"){ + ids = [ids]; + } + for(let count = 0; count < ids.length;){ + const chunk = ids.length - count > MAXIMUM_QUERY_VARS + ? ids.slice(count, count + MAXIMUM_QUERY_VARS) + : count ? ids.slice(count) : ids; + count += chunk.length; + let stmt = ""; + for(let i = 1; i <= chunk.length; i++){ + stmt += (stmt ? "," : "") + "$" + i; + } + const res = await this.db.any(` + SELECT id, doc + FROM ${ this.id }.reg + WHERE id IN (${ stmt })`, + ids + ); + if(res && res.length){ + for(let i = 0, doc; i < res.length; i++){ + if((doc = res[i].doc)){ + res[i].doc = JSON.parse(doc); + } + } + result.push(res); + } + } + return result.length === 1 + ? result[0] + : result.length > 1 + ? concat(result) + : result; +}; + +PostgresDB.prototype.has = function(id){ + return this.db.oneOrNone("SELECT EXISTS(SELECT 1 FROM " + this.id + ".reg WHERE id = $1)", [id]).then(function(result){ + return !!(result && result.exists); + }); +}; + +PostgresDB.prototype.search = function(flexsearch, query, limit = 100, offset = 0, suggest = false, resolve = true, enrich = false, tags){ + + let rows; + + if(query.length > 1 && flexsearch.depth){ + + let where = ""; + let params = []; + let keyword = query[0]; + let term; + let count = 1; + + + for(let i = 1; i < query.length; i++){ + term = query[i]; + const swap = flexsearch.bidirectional && (term > keyword); + where += (where ? " OR " : "") + `(ctx = $${ count++ } AND key = $${ count++ })`; + params.push(swap ? term : keyword, swap ? keyword : term); + keyword = term; + } + + if(tags){ + where = "(" + where + ")"; + for(let i = 0; i < tags.length; i+=2){ + where += ` AND id IN (SELECT id FROM ${ this.id }.tag_${ sanitize(tags[i]) } WHERE tag = $${ count++ })`; + params.push(tags[i + 1]); + } + } + + rows = this.db.any(` + SELECT r.id + ${ resolve ? "" : ", res" } + ${ enrich ? ", doc" : "" } + FROM ( + SELECT id, count(*) as count, + ${ suggest ? "SUM" : "SUM" }(res) as res + FROM ${ this.id }.ctx${ this.field } + WHERE ${ where } + GROUP BY id + ) as r + ${ enrich ? ` + LEFT JOIN ${ this.id }.reg ON ${ this.id }.reg.id = r.id + ` : "" } + ${ suggest ? "" : "WHERE count = " + (query.length - 1) } + ORDER BY ${ suggest ? "count DESC, res" : "res" } + ${ limit ? "LIMIT " + limit : "" } + ${ offset ? "OFFSET " + offset : "" } + `, params); + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + } + else { + + let where = ""; + let count = 1; + let query_length = query.length; + for(let i = 0; i < query_length; i++){ + where += (where ? "," : "") + "$" + count++; + } + where = "key " + (query_length > 1 ? "IN (" + where + ")" : "= " + where ); + + if(tags){ + where = "(" + where + ")"; + for(let i = 0; i < tags.length; i+=2){ + where += ` AND id IN (SELECT id FROM ${ this.id }.tag_${ sanitize(tags[i]) } WHERE tag = $${ count++ })`; + query.push(tags[i + 1]); + } + } + + rows = this.db.any(` + SELECT r.id + ${ resolve ? "" : ", res" } + ${ enrich ? ", doc" : "" } + FROM ( + SELECT id, count(*) as count, + ${ suggest ? "SUM" : "SUM" }(res) as res + FROM ${ this.id }.map${ this.field } + WHERE ${ where } + GROUP BY id + ) as r + ${ enrich ? ` + LEFT JOIN ${ this.id }.reg ON ${ this.id }.reg.id = r.id + ` : "" } + ${ suggest ? "" : "WHERE count = " + query_length } + ORDER BY ${ suggest ? "count DESC, res" : "res" } + ${ limit ? "LIMIT " + limit : "" } + ${ offset ? "OFFSET " + offset : "" } + `, query); + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + } + + return rows.then(function(rows){ + return create_result(rows, resolve, enrich); + }); +}; + +PostgresDB.prototype.info = function(){ + +}; + + +PostgresDB.prototype.transaction = function(task){ + const self = this; + return this.db.tx(function(trx){ + return task.call(self, trx); + }); +}; + + +PostgresDB.prototype.commit = async function(flexsearch, _replace, _append){ + + + if(_replace){ + await this.clear(); + + flexsearch.commit_task = []; + } + else { + let tasks = flexsearch.commit_task; + flexsearch.commit_task = []; + for(let i = 0, task; i < tasks.length; i++){ + task = tasks[i]; + + if(task.clear){ + await this.clear(); + _replace = true; + break; + } + else { + tasks[i] = task.del; + } + } + if(!_replace){ + if(!_append){ + tasks = tasks.concat(toArray(flexsearch.reg)); + } + tasks.length && await this.remove(tasks); + } + + + } + + if(!flexsearch.reg.size){ + return; + } + + await this.transaction(function(trx){ + + const batch = []; + + + + if(flexsearch.store){ + let data = []; + let stmt = new pgp.helpers.ColumnSet(["id", "doc"],{ + table: this.id + ".reg" + }); + for(const item of flexsearch.store.entries()){ + const id = item[0]; + const doc = item[1]; + + + data.push({ id, doc: doc && JSON.stringify(doc) }); + if(data.length === MAXIMUM_QUERY_VARS){ + let insert = pgp.helpers.insert(data, stmt); + batch.push( + trx.none(insert.replace(/^(insert into )"([^"]+)"/, '$1 $2')) + ); + data = []; + } + } + if(data.length){ + let insert = pgp.helpers.insert(data, stmt); + batch.push( + trx.none(insert.replace(/^(insert into )"([^"]+)"/, '$1 $2')) + ); + } + + + + + + + + + + + + } + + + + else if(!flexsearch.bypass){ + let data = []; + let stmt = new pgp.helpers.ColumnSet(["id"],{ + table: this.id + ".reg" + }); + for(const id of flexsearch.reg.keys()){ + + + data.push({ id }); + if(data.length === MAXIMUM_QUERY_VARS){ + let insert = pgp.helpers.insert(data, stmt); + batch.push( + trx.none(insert.replace(/^(insert into )"([^"]+)"/, '$1 $2')) + ); + data = []; + } + } + if(data.length){ + let insert = pgp.helpers.insert(data, stmt); + batch.push( + trx.none(insert.replace(/^(insert into )"([^"]+)"/, '$1 $2')) + ); + } + } + + + + if(flexsearch.map.size){ + let data = []; + let stmt = new pgp.helpers.ColumnSet(["key", "res", "id"], { + table: this.id + ".map" + this.field + }); + for(const item of flexsearch.map){ + const key = item[0]; + const arr = item[1]; + + for(let i = 0, ids; i < arr.length; i++){ + if((ids = arr[i]) && ids.length){ + + + + for(let j = 0; j < ids.length; j++){ + + + + data.push({ + key: key, + res: i, + id: ids[j] + }); + if(data.length === MAXIMUM_QUERY_VARS){ + let insert = pgp.helpers.insert(data, stmt); + batch.push( + trx.none(insert.replace(/^(insert into )"([^"]+)"/, '$1 $2')) + ); + data = []; + } + } + } + } + } + if(data.length){ + let insert = pgp.helpers.insert(data, stmt); + batch.push( + trx.none(insert.replace(/^(insert into )"([^"]+)"/, '$1 $2')) + ); + } + } + + + + if(flexsearch.ctx.size){ + let data = []; + let stmt = new pgp.helpers.ColumnSet(["ctx", "key", "res", "id"], { + table: this.id + ".ctx" + this.field + }); + for(const ctx of flexsearch.ctx){ + const ctx_key = ctx[0]; + const ctx_value = ctx[1]; + + for(const item of ctx_value){ + const key = item[0]; + const arr = item[1]; + + for(let i = 0, ids; i < arr.length; i++){ + if((ids = arr[i]) && ids.length){ + + + for(let j = 0; j < ids.length; j++){ + + + + data.push({ + ctx: ctx_key, + key: key, + res: i, + id: ids[j] + }); + if(data.length === MAXIMUM_QUERY_VARS){ + let insert = pgp.helpers.insert(data, stmt); + batch.push( + trx.none(insert.replace(/^(insert into )"([^"]+)"/, '$1 $2')) + ); + data = []; + } + } + } + } + } + } + if(data.length){ + let insert = pgp.helpers.insert(data, stmt); + batch.push( + trx.none(insert.replace(/^(insert into )"([^"]+)"/, '$1 $2')) + ); + } + } + + + + if(flexsearch.tag){ + let data = []; + let stmt = new pgp.helpers.ColumnSet(["tag", "id"],{ + table: this.id + ".tag" + this.field + }); + for(const item of flexsearch.tag){ + const tag = item[0]; + const ids = item[1]; + if(!ids.length) continue; + for(let j = 0; j < ids.length; j++){ + data.push({ tag, id: ids[j] }); + if(data.length === MAXIMUM_QUERY_VARS){ + let insert = pgp.helpers.insert(data, stmt); + batch.push( + trx.none(insert.replace(/^(insert into )"([^"]+)"/, '$1 $2')) + ); + data = []; + } + } + } + if(data.length){ + let insert = pgp.helpers.insert(data, stmt); + batch.push( + trx.none(insert.replace(/^(insert into )"([^"]+)"/, '$1 $2')) + ); + } + } + + + + + + + + + + + + + + + + + + + + + + + + + if(batch.length){ + return trx.batch(batch); + } + }); + + flexsearch.map.clear(); + flexsearch.ctx.clear(); + flexsearch.tag && + flexsearch.tag.clear(); + flexsearch.store && + flexsearch.store.clear(); + flexsearch.document || + flexsearch.reg.clear(); +}; + +PostgresDB.prototype.remove = function(ids){ + + if(!ids && ids !== 0){ + return; + } + if(typeof ids !== "object"){ + ids = [ids]; + } + if(!ids.length){ + return; + } + + + + + + + + + + + + + + + + + + return this.transaction(function(trx){ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + ids = [ids]; + return trx.batch([ + trx.none({ text: "DELETE FROM " + this.id + ".map" + this.field + " WHERE id = ANY ($1)", rowMode: "array" }, ids), + trx.none({ text: "DELETE FROM " + this.id + ".ctx" + this.field + " WHERE id = ANY ($1)", rowMode: "array" }, ids), + trx.none({ text: "DELETE FROM " + this.id + ".tag" + this.field + " WHERE id = ANY ($1)", rowMode: "array" }, ids), + trx.none({ text: "DELETE FROM " + this.id + ".reg WHERE id = ANY ($1)", rowMode: "array" }, ids) + ]); + + + + + + + + + + + + + + + + + + + + + + + + + + + + + }); }; -const pgp = pg_promise(/*{ noWarnings: true }*/); -const MAXIMUM_QUERY_VARS = 16000; -const fields = ["map", "ctx", "reg", "tag", "cfg"]; -const types = { - "text": "text", - "char": "text", - "varchar": "text", - "string": "text", - "number": "int", - "numeric": "int", - "integer": "int", - "smallint": "int", - "tinyint": "int", - "mediumint": "int", - "int": "int", - "int8": "int", - "uint8": "int", - "int16": "int", - "uint16": "int", - "int32": "int", - "uint32": "bigint", - "int64": "bigint", - "bigint": "bigint" -}; - -function sanitize(str) { - return str.toLowerCase().replace(/[^a-z0-9_]/g, ""); -} - -let DB; - -/** - * @constructor - * @implements StorageInterface - */ - -function PostgresDB(name, config = {}){ - if(!this || this.constructor !== PostgresDB){ - return new PostgresDB(name, config); - } - if(typeof name === "object"){ - config = name; - name = config.name; - } - if(!name){ - console.info("Default storage space was used, because a name was not passed."); - } - this.id = (config.schema ? sanitize(config.schema) : defaults.schema) + (name ? "_" + sanitize(name) : ""); - this.field = config.field ? "_" + sanitize(config.field) : ""; - this.type = config.type ? types[config.type.toLowerCase()] : "text"; - this.support_tag_search = true; - if(!this.type) throw new Error("Unknown type of ID '" + config.type + "'"); - this.db = DB || (DB = config.db || null); - Object.assign(defaults, config); - this.db && delete defaults.db; -} -PostgresDB.prototype.mount = function(flexsearch){ - //if(flexsearch.constructor === Document){ - if(flexsearch.index){ - return flexsearch.mount(this); - } - flexsearch.db = this; - return this.open(); -}; - -PostgresDB.prototype.open = async function(){ - - if(!this.db) { - this.db = DB || ( - DB = pgp(`postgres://${defaults.user}:${encodeURIComponent(defaults.pass)}@${defaults.host}:${defaults.port}/${defaults.name}`) - ); - } - - const exist = await this.db.oneOrNone(` - SELECT EXISTS ( - SELECT 1 - FROM information_schema.schemata - WHERE schema_name = '${this.id}' - ); - `); - if(!exist || !exist.exists){ - await this.db.none(`CREATE SCHEMA IF NOT EXISTS ${ this.id };`); - } - - for(let i = 0; i < fields.length; i++){ - const exist = await this.db.oneOrNone(` - SELECT EXISTS ( - SELECT 1 FROM pg_tables - WHERE schemaname = '${this.id}' AND tablename = '${fields[i] + (fields[i] !== "reg" ? this.field : "")}' - ); - `); - if(exist && exist.exists) continue; - - const type = this.type === "text" - ? "varchar(128)" - : this.type; - - switch(fields[i]){ - case "map": - await this.db.none(` - CREATE TABLE IF NOT EXISTS ${this.id}.map${this.field}( - key varchar(128) NOT NULL, - res smallint NOT NULL, - id ${type} NOT NULL - ); - CREATE INDEX IF NOT EXISTS ${this.id}_map_index${this.field} - ON ${this.id}.map${this.field} (key); - CREATE INDEX IF NOT EXISTS ${this.id}_map_id${this.field} - ON ${this.id}.map${this.field} (id); - `); - break; - - case "ctx": - await this.db.none(` - CREATE TABLE IF NOT EXISTS ${this.id}.ctx${this.field}( - ctx varchar(128) NOT NULL, - key varchar(128) NOT NULL, - res smallint NOT NULL, - id ${type} NOT NULL - ); - CREATE INDEX IF NOT EXISTS ${this.id}_ctx_index${this.field} - ON ${this.id}.ctx${this.field} (ctx, key); - CREATE INDEX IF NOT EXISTS ${this.id}_ctx_id${this.field} - ON ${this.id}.ctx${this.field} (id); - `); - break; - - case "tag": - await this.db.none(` - CREATE TABLE IF NOT EXISTS ${this.id}.tag${this.field}( - tag varchar(128) NOT NULL, - id ${type} NOT NULL - ); - CREATE INDEX IF NOT EXISTS ${this.id}_tag_index${this.field} - ON ${this.id}.tag${this.field} (tag); - CREATE INDEX IF NOT EXISTS ${this.id}_tag_id${this.field} - ON ${this.id}.tag${this.field} (id); - `); - break; - - case "reg": - await this.db.none(` - CREATE TABLE IF NOT EXISTS ${this.id}.reg( - id ${type} NOT NULL - CONSTRAINT ${this.id}_reg_pk - PRIMARY KEY, - doc text DEFAULT NULL - ); - `).catch(e => { - // document indexes will try to create same registry table - // and the "IF NOT EXISTS" did not apply on parallel - }); - break; - - case "cfg": - await this.db.none(` - CREATE TABLE IF NOT EXISTS ${this.id}.cfg${this.field}( - cfg text NOT NULL - ); - `); - break; - } - } - - return this.db; -}; - -PostgresDB.prototype.close = function(){ - //DB && DB.close && DB.close(); - this.db = /*DB =*/ null; - return this; -}; - -PostgresDB.prototype.destroy = function(){ - return this.db.none(` - DROP TABLE IF EXISTS ${this.id}.map${this.field}; - DROP TABLE IF EXISTS ${this.id}.ctx${this.field}; - DROP TABLE IF EXISTS ${this.id}.tag${this.field}; - DROP TABLE IF EXISTS ${this.id}.cfg${this.field}; - DROP TABLE IF EXISTS ${this.id}.reg; - `); -}; - -PostgresDB.prototype.clear = function(){ - return this.db.none(` - TRUNCATE TABLE ${this.id}.map${ this.field }; - TRUNCATE TABLE ${this.id}.ctx${ this.field }; - TRUNCATE TABLE ${this.id}.tag${ this.field }; - TRUNCATE TABLE ${this.id}.cfg${ this.field }; - TRUNCATE TABLE ${this.id}.reg; - `); -}; - -function create_result(rows, resolve, enrich){ - if(resolve){ - for(let i = 0; i < rows.length; i++){ - if(enrich){ - if(rows[i].doc){ - rows[i].doc = JSON.parse(rows[i].doc); - } - } - else { - rows[i] = rows[i].id; - } - } - return rows; - } - else { - const arr = []; - for(let i = 0, row; i < rows.length; i++){ - row = rows[i]; - arr[row.res] || (arr[row.res] = []); - arr[row.res].push(enrich - ? row - : row.id - ); - } - return arr; - } -} - -PostgresDB.prototype.get = function(key, ctx, limit = 0, offset = 0, resolve = true, enrich = false, tags){ - let rows; - let stmt = ''; - let params = ctx ? [ctx, key] : [key]; - let table = this.id + (ctx ? ".ctx" : ".map") + this.field; - if(tags){ - for(let i = 0, count = params.length + 1; i < tags.length; i+=2){ - stmt += ` AND ${ table }.id IN (SELECT id FROM ${ this.id }.tag_${ sanitize(tags[i]) } WHERE tag = $${ count++ })`; - params.push(tags[i + 1]); - } - } - if(ctx){ - rows = this.db.any(` - SELECT ${ table }.id - ${ resolve ? "" : ", res" } - ${ enrich ? ", doc" : "" } - FROM ${ table } - ${ enrich ? ` - LEFT JOIN ${ this.id }.reg ON ${ this.id }.reg.id = ${ table }.id - ` : "" } - WHERE ctx = $1 AND key = $2 ${stmt} - ORDER BY res - ${ limit ? "LIMIT " + limit : "" } - ${ offset ? "OFFSET " + offset : "" }`, - params - ); - } - else { - rows = this.db.any(` - SELECT ${ table }.id - ${ resolve ? "" : ", res" } - ${ enrich ? ", doc" : "" } - FROM ${ table } - ${ enrich ? ` - LEFT JOIN ${ this.id }.reg ON ${ this.id }.reg.id = ${ table }.id - ` : "" } - WHERE key = $1 ${stmt} - ORDER BY res - ${ limit ? "LIMIT " + limit : "" } - ${ offset ? "OFFSET " + offset : "" }`, - params - ); - } - return rows.then(function(rows){ - return create_result(rows, resolve, enrich); - }); -}; - -PostgresDB.prototype.tag = function(tag, limit = 0, offset = 0, enrich = false){ - const table = this.id + ".tag" + this.field; - const promise = this.db.any(` - SELECT ${ table }.id - ${ enrich ? ", doc" : "" } - FROM ${ table } - ${ enrich ? ` - LEFT JOIN ${ this.id }.reg ON ${ this.id }.reg.id = ${ table }.id - ` : "" } - WHERE tag = $1 - ${ limit ? "LIMIT " + limit : "" } - ${ offset ? "OFFSET " + offset : "" }`, - [tag] - ); - enrich || promise.then(function(rows){ - return create_result(rows, true, false); - }); - return promise; -}; - -PostgresDB.prototype.enrich = async function(ids){ - let result = []; - if(typeof ids !== "object"){ - ids = [ids]; - } - for(let count = 0; count < ids.length;){ - const chunk = ids.length - count > MAXIMUM_QUERY_VARS - ? ids.slice(count, count + MAXIMUM_QUERY_VARS) - : count ? ids.slice(count) : ids; - count += chunk.length; - let stmt = ""; - for(let i = 1; i <= chunk.length; i++){ - stmt += (stmt ? "," : "") + "$" + i; - } - const res = await this.db.any(` - SELECT id, doc - FROM ${ this.id }.reg - WHERE id IN (${ stmt })`, - ids - ); - if(res && res.length){ - for(let i = 0, doc; i < res.length; i++){ - if((doc = res[i].doc)){ - res[i].doc = JSON.parse(doc); - } - } - result.push(res); - } - } - return result.length === 1 - ? result[0] - : result.length > 1 - ? concat(result) - : result; -}; - -PostgresDB.prototype.has = function(id){ - return this.db.oneOrNone("SELECT EXISTS(SELECT 1 FROM " + this.id + ".reg WHERE id = $1)", [id]).then(function(result){ - return !!(result && result.exists); - }); -}; - -PostgresDB.prototype.search = function(flexsearch, query, limit = 100, offset = 0, suggest = false, resolve = true, enrich = false, tags){ - - let rows; - - if(query.length > 1 && flexsearch.depth){ - - let where = ""; - let params = []; - let keyword = query[0]; - let term; - let count = 1; - // variant new - - for(let i = 1; i < query.length; i++){ - term = query[i]; - const swap = flexsearch.bidirectional && (term > keyword); - where += (where ? " OR " : "") + `(ctx = $${ count++ } AND key = $${ count++ })`; - params.push(swap ? term : keyword, swap ? keyword : term); - keyword = term; - } - - if(tags){ - where = "(" + where + ")"; - for(let i = 0; i < tags.length; i+=2){ - where += ` AND id IN (SELECT id FROM ${ this.id }.tag_${ sanitize(tags[i]) } WHERE tag = $${ count++ })`; - params.push(tags[i + 1]); - } - } - - rows = this.db.any(` - SELECT r.id - ${ resolve ? "" : ", res" } - ${ enrich ? ", doc" : "" } - FROM ( - SELECT id, count(*) as count, - ${ suggest ? "SUM" : "SUM" /*"MIN"*/ }(res) as res - FROM ${ this.id }.ctx${ this.field } - WHERE ${ where } - GROUP BY id - ) as r - ${ enrich ? ` - LEFT JOIN ${ this.id }.reg ON ${ this.id }.reg.id = r.id - ` : "" } - ${ suggest ? "" : "WHERE count = " + (query.length - 1) } - ORDER BY ${ suggest ? "count DESC, res" : "res" } - ${ limit ? "LIMIT " + limit : "" } - ${ offset ? "OFFSET " + offset : "" } - `, params); - - // variant 1 - - // for(let i = 1, count = 1; i < query.length; i++){ - // where += (where ? " UNION " : "") + ` - // SELECT id, res - // FROM ${this.id}.ctx${this.field} - // WHERE ctx = $${count++} AND key = $${count++} - // `; - // term = query[i]; - // const swap = flexsearch.bidirectional && (term > keyword); - // params.push( - // swap ? term : keyword, - // swap ? keyword : term - // ); - // keyword = term; - // } - // - // rows = await db.any(` - // SELECT id, res - // FROM ( - // SELECT id, ${suggest ? "SUM" : "MIN"}(res) as res, count(*) as count - // FROM (${where}) as t - // GROUP BY id - // ORDER BY ${suggest ? "count DESC, res" : "res"} - // LIMIT ${limit} - // OFFSET ${offset} - // ) as r - // ${suggest ? "" : "WHERE count = " + (query.length - 1)} - // `, params); - - } - else { - - let where = ""; - let count = 1; - let query_length = query.length; - for(let i = 0; i < query_length; i++){ - where += (where ? "," : "") + "$" + count++; - } - where = "key " + (query_length > 1 ? "IN (" + where + ")" : "= " + where ); - - if(tags){ - where = "(" + where + ")"; - for(let i = 0; i < tags.length; i+=2){ - where += ` AND id IN (SELECT id FROM ${ this.id }.tag_${ sanitize(tags[i]) } WHERE tag = $${ count++ })`; - query.push(tags[i + 1]); - } - } - - rows = this.db.any(` - SELECT r.id - ${ resolve ? "" : ", res" } - ${ enrich ? ", doc" : "" } - FROM ( - SELECT id, count(*) as count, - ${ suggest ? "SUM" : "SUM" /*"MIN"*/ }(res) as res - FROM ${ this.id }.map${ this.field } - WHERE ${ where } - GROUP BY id - ) as r - ${ enrich ? ` - LEFT JOIN ${ this.id }.reg ON ${ this.id }.reg.id = r.id - ` : "" } - ${ suggest ? "" : "WHERE count = " + query_length } - ORDER BY ${ suggest ? "count DESC, res" : "res" } - ${ limit ? "LIMIT " + limit : "" } - ${ offset ? "OFFSET " + offset : "" } - `, query); - - // variant 1 - // for(let i = 1; i <= query.length; i++){ - // where += (where ? " UNION " : "") + ` - // SELECT id, res - // FROM ${ this.id }.map${ this.field } - // WHERE key = $${i} - // `; - // } - // rows = await db.any(` - // SELECT id, res - // FROM ( - // SELECT id, ${suggest ? "SUM" : "MIN"}(res) as res, count(*) as count - // FROM (${where}) as t - // GROUP BY id - // ORDER BY ${suggest ? "count DESC, res" : "res"} - // LIMIT ${limit} - // OFFSET ${offset} - // ) as r - // ${ suggest ? "" : "WHERE count = " + query.length } - // `, query); - - // variant 2 - // for(let i = 1; i <= query.length; i++){ - // where += (where ? " AND EXISTS " : "") + `(SELECT FROM ${this.id}.map${this.field} as d WHERE d.id = t.id AND d.key = $` + i + ")"; - // } - // rows = await db.any(` - // SELECT t.id, min(t.res) - // FROM ${this.id}.map${this.field} AS t - // WHERE EXISTS ${where} - // GROUP BY t.id - // LIMIT ${limit || 100} - // OFFSET ${offset || 0} - // `, query); - - // variant 3 - // for(let i = 1; i <= query.length; i++){ - // where += (where ? " INTERSECT " : "") + `SELECT id FROM ${this.id}.map${this.field} WHERE key = $` + i; - // } - // rows = await db.any(` - // WITH filtering (id) AS(${where}) - // SELECT t.id, min(t.res) - // FROM ${this.id}.map${this.field} AS t - // JOIN filtering USING (id) - // GROUP BY t.id - // LIMIT ${limit || 100} - // OFFSET ${offset || 0} - // `, query); - - // variant 4 - // for(let i = 1; i <= query.length; i++){ - // where += (where ? " INTERSECT " : "") + `SELECT id FROM ${this.id}.map${this.field} WHERE key = $` + i; - // } - // rows = await db.any(` - // SELECT id, min(res) - // FROM ${this.id}.map${this.field} - // WHERE id IN (${where}) - // GROUP BY id - // LIMIT ${limit || 100} - // OFFSET ${offset || 0} - // `, query); - } - - return rows.then(function(rows){ - return create_result(rows, resolve, enrich); - }); -}; - -PostgresDB.prototype.info = function(){ - // todo -}; - -// PostgresDB.prototype.transaction = async function(task){ -// const self = this; -// if(TRX){ -// return TRX.then(function(){ -// return self.transaction(task); -// //task.call(self, TRX); -// }); -// } -// TRX = await this.db.tx(async function(trx){ -// await task.call(self, trx); -// }); -// TRX = null; -// }; - -PostgresDB.prototype.transaction = function(task){ - const self = this; - return this.db.tx(function(trx){ - return task.call(self, trx); - }); -}; - -// PostgresDB.prototype.transaction = async function(task){ -// if(TRX){ -// return await task.call(this, TRX); -// } -// const self = this; -// return this.db.tx(async function(trx){ -// await task.call(self, TRX = trx); -// TRX = null; -// }); -// }; - -PostgresDB.prototype.commit = async function(flexsearch, _replace, _append){ - - // process cleanup tasks - if(_replace){ - await this.clear(); - // there are just removals in the task queue - flexsearch.commit_task = []; - } - else { - let tasks = flexsearch.commit_task; - flexsearch.commit_task = []; - for(let i = 0, task; i < tasks.length; i++){ - task = tasks[i]; - // there are just removals in the task queue - if(task.clear){ - await this.clear(); - _replace = true; - break; - } - else { - tasks[i] = task.del; - } - } - if(!_replace){ - if(!_append){ - tasks = tasks.concat(toArray(flexsearch.reg)); - } - tasks.length && await this.remove(tasks); - } - - //console.log("tasks:", tasks) - } - - if(!flexsearch.reg.size){ - return; - } - - await this.transaction(function(trx){ - - const batch = []; - - // Datastore + Registry - - if(flexsearch.store){ - let data = []; - let stmt = new pgp.helpers.ColumnSet(["id", "doc"],{ - table: this.id + ".reg" - }); - for(const item of flexsearch.store.entries()){ - const id = item[0]; - const doc = item[1]; - // const migration = checkMigration.call(this, id); - // migration && await migration; - data.push({ id, doc: doc && JSON.stringify(doc) }); - if(data.length === MAXIMUM_QUERY_VARS){ - let insert = pgp.helpers.insert(data, stmt); - batch.push( - trx.none(insert.replace(/^(insert into )"([^"]+)"/, '$1 $2')) - ); - data = []; - } - } - if(data.length){ - let insert = pgp.helpers.insert(data, stmt); - batch.push( - trx.none(insert.replace(/^(insert into )"([^"]+)"/, '$1 $2')) - ); - } - // while(data.length){ - // let next; - // if(data.length > MAXIMUM_QUERY_VARS){ - // next = data.slice(MAXIMUM_QUERY_VARS); - // data = data.slice(0, MAXIMUM_QUERY_VARS); - // } - // let insert = pgp.helpers.insert(data, stmt); - // trx.none(insert.replace(/^(insert into )"([^"]+)"/, '$1 $2')); - // if(next) data = next; - // else break; - // } - } - - // Registry Only - - else if(!flexsearch.bypass){ - let data = []; - let stmt = new pgp.helpers.ColumnSet(["id"],{ - table: this.id + ".reg" - }); - for(const id of flexsearch.reg.keys()){ - // const migration = checkMigration.call(this, id); - // migration && await migration; - data.push({ id }); - if(data.length === MAXIMUM_QUERY_VARS){ - let insert = pgp.helpers.insert(data, stmt); - batch.push( - trx.none(insert.replace(/^(insert into )"([^"]+)"/, '$1 $2')) - ); - data = []; - } - } - if(data.length){ - let insert = pgp.helpers.insert(data, stmt); - batch.push( - trx.none(insert.replace(/^(insert into )"([^"]+)"/, '$1 $2')) - ); - } - } - - // Default Index - - if(flexsearch.map.size){ - let data = []; - let stmt = new pgp.helpers.ColumnSet(["key", "res", "id"], { - table: this.id + ".map" + this.field - }); - for(const item of flexsearch.map){ - const key = item[0]; - const arr = item[1]; - - for(let i = 0, ids; i < arr.length; i++){ - if((ids = arr[i]) && ids.length){ - //this.type || (this.type = typeof ids[0]); - // let stmt = "($1,$2,$3)"; - // let params = [key, i, ids[0]]; - for(let j = 0; j < ids.length; j++){ - // stmt += ",($1,$2,$3)"; - // params.push(key, i, ids[j]); - //trx.none(`INSERT INTO ${config.schema}.map${self.field} (key, res, id) VALUES ($1,$2,$3)`, [key, i, ids[j]]); - data.push({ - key: key, - res: i, - id: ids[j] - }); - if(data.length === MAXIMUM_QUERY_VARS){ - let insert = pgp.helpers.insert(data, stmt); - batch.push( - trx.none(insert.replace(/^(insert into )"([^"]+)"/, '$1 $2')) - ); - data = []; - } - } - } - } - } - if(data.length){ - let insert = pgp.helpers.insert(data, stmt); - batch.push( - trx.none(insert.replace(/^(insert into )"([^"]+)"/, '$1 $2')) - ); - } - } - - // Context Index - - if(flexsearch.ctx.size){ - let data = []; - let stmt = new pgp.helpers.ColumnSet(["ctx", "key", "res", "id"], { - table: this.id + ".ctx" + this.field - }); - for(const ctx of flexsearch.ctx){ - const ctx_key = ctx[0]; - const ctx_value = ctx[1]; - - for(const item of ctx_value){ - const key = item[0]; - const arr = item[1]; - - for(let i = 0, ids; i < arr.length; i++){ - if((ids = arr[i]) && ids.length){ - // let stmt = "(?,?,?)"; - // let params = [ctx_key + ":" + key, i, ids[0]]; - for(let j = 0; j < ids.length; j++){ - // stmt += ",(?,?,?)"; - // params.push(ctx_key + ":" + key, i, ids[j]); - //trx.none("INSERT INTO " + config.schema + ".ctx" + self.field + " (ctx, key, res, id) VALUES ($1,$2,$3,$4)", [ctx_key, key, i, ids[j]]); - data.push({ - ctx: ctx_key, - key: key, - res: i, - id: ids[j] - }); - if(data.length === MAXIMUM_QUERY_VARS){ - let insert = pgp.helpers.insert(data, stmt); - batch.push( - trx.none(insert.replace(/^(insert into )"([^"]+)"/, '$1 $2')) - ); - data = []; - } - } - } - } - } - } - if(data.length){ - let insert = pgp.helpers.insert(data, stmt); - batch.push( - trx.none(insert.replace(/^(insert into )"([^"]+)"/, '$1 $2')) - ); - } - } - - // Tag Index - - if(flexsearch.tag){ - let data = []; - let stmt = new pgp.helpers.ColumnSet(["tag", "id"],{ - table: this.id + ".tag" + this.field - }); - for(const item of flexsearch.tag){ - const tag = item[0]; - const ids = item[1]; - if(!ids.length) continue; - for(let j = 0; j < ids.length; j++){ - data.push({ tag, id: ids[j] }); - if(data.length === MAXIMUM_QUERY_VARS){ - let insert = pgp.helpers.insert(data, stmt); - batch.push( - trx.none(insert.replace(/^(insert into )"([^"]+)"/, '$1 $2')) - ); - data = []; - } - } - } - if(data.length){ - let insert = pgp.helpers.insert(data, stmt); - batch.push( - trx.none(insert.replace(/^(insert into )"([^"]+)"/, '$1 $2')) - ); - } - } - - // Field Configuration - - // TODO - // trx.none("INSERT INTO " + this.id + ".cfg" + this.field + " (cfg) VALUES ($1)", [ - // JSON.stringify({ - // "encode": typeof flexsearch.encode === "string" ? flexsearch.encode : "", - // "charset": typeof flexsearch.charset === "string" ? flexsearch.charset : "", - // "tokenize": flexsearch.tokenize, - // "resolution": flexsearch.resolution, - // "minlength": flexsearch.minlength, - // "optimize": flexsearch.optimize, - // "fastupdate": flexsearch.fastupdate, - // "encoder": flexsearch.encoder, - // "context": { - // "depth": flexsearch.depth, - // "bidirectional": flexsearch.bidirectional, - // "resolution": flexsearch.resolution_ctx - // } - // }) - // ]); - - //return Promise.all(batch); - - if(batch.length){ - return trx.batch(batch); - } - }); - - flexsearch.map.clear(); - flexsearch.ctx.clear(); - flexsearch.tag && - flexsearch.tag.clear(); - flexsearch.store && - flexsearch.store.clear(); - flexsearch.document || - flexsearch.reg.clear(); -}; - -PostgresDB.prototype.remove = function(ids){ - - if(!ids && ids !== 0){ - return; - } - if(typeof ids !== "object"){ - ids = [ids]; - } - if(!ids.length){ - return; - } - - // ids = [ids]; - // return this.db.none( - // "DELETE FROM " + this.id + ".map" + this.field + " WHERE id = ANY ($1);" + - // "DELETE FROM " + this.id + ".ctx" + this.field + " WHERE id = ANY ($1);" + - // "DELETE FROM " + this.id + ".tag" + this.field + " WHERE id = ANY ($1);" + - // "DELETE FROM " + this.id + ".reg WHERE id = ANY ($1);", [ids] - // ); - - // ids = [ids]; - // return Promise.all([ - // this.db.none({ text: "DELETE FROM " + this.id + ".map" + this.field + " WHERE id = ANY ($1)", rowMode: "array" }, ids), - // this.db.none({ text: "DELETE FROM " + this.id + ".ctx" + this.field + " WHERE id = ANY ($1)", rowMode: "array" }, ids), - // this.db.none({ text: "DELETE FROM " + this.id + ".tag" + this.field + " WHERE id = ANY ($1)", rowMode: "array" }, ids), - // this.db.none({ text: "DELETE FROM " + this.id + ".reg WHERE id = ANY ($1)", rowMode: "array" }, ids) - // ]); - - return this.transaction(function(trx){ - - //console.log("remove:", ids); - - // ids = [ids]; - // trx.none({ text: "DELETE FROM " + this.id + ".map" + this.field + " WHERE id = ANY ($1)", rowMode: "array" }, ids); - // trx.none({ text: "DELETE FROM " + this.id + ".ctx" + this.field + " WHERE id = ANY ($1)", rowMode: "array" }, ids); - // trx.none({ text: "DELETE FROM " + this.id + ".tag" + this.field + " WHERE id = ANY ($1)", rowMode: "array" }, ids); - // trx.none({ text: "DELETE FROM " + this.id + ".reg WHERE id = ANY ($1)", rowMode: "array" }, ids); - - // ids = [ids]; - // trx.none("DELETE FROM " + this.id + ".map" + this.field + " WHERE id = ANY ($1)", ids); - // trx.none("DELETE FROM " + this.id + ".ctx" + this.field + " WHERE id = ANY ($1)", ids); - // trx.none("DELETE FROM " + this.id + ".tag" + this.field + " WHERE id = ANY ($1)", ids); - // trx.none("DELETE FROM " + this.id + ".reg WHERE id = ANY ($1)", ids); - // return; - - // return trx.none( - // "DELETE FROM " + this.id + ".map" + this.field + " WHERE id = ANY ($1);" + - // "DELETE FROM " + this.id + ".ctx" + this.field + " WHERE id = ANY ($1);" + - // "DELETE FROM " + this.id + ".tag" + this.field + " WHERE id = ANY ($1);" + - // "DELETE FROM " + this.id + ".reg WHERE id = ANY ($1);", [ids] - // ); - - // while(ids.length){ - // let next; - // let stmt = ""; - // let chunk = 0; - // let migration; - // for(let i = 0; i < ids.length; i++){ - // stmt += (stmt ? "," : "") + "$" + (i + 1); // + "::text"; - // // maximum count of variables supported - // if(++chunk === MAXIMUM_QUERY_VARS){ - // next = ids.slice(MAXIMUM_QUERY_VARS); - // ids = ids.slice(0, MAXIMUM_QUERY_VARS); - // break; - // } - // } - // - // trx.batch([ - // trx.none("DELETE FROM " + this.id + ".map" + this.field + " WHERE id IN (" + stmt + ")", ids), - // trx.none("DELETE FROM " + this.id + ".ctx" + this.field + " WHERE id IN (" + stmt + ")", ids), - // trx.none("DELETE FROM " + this.id + ".tag" + this.field + " WHERE id IN (" + stmt + ")", ids), - // trx.none("DELETE FROM " + this.id + ".reg WHERE id IN (" + stmt + ")", ids) - // ]); - // - // // trx.none( - // // "DELETE FROM " + this.id + ".map" + this.field + " WHERE id IN (" + stmt + ");" + - // // "DELETE FROM " + this.id + ".ctx" + this.field + " WHERE id IN (" + stmt + ");" + - // // "DELETE FROM " + this.id + ".tag" + this.field + " WHERE id IN (" + stmt + ");" + - // // "DELETE FROM " + this.id + ".reg WHERE id IN (" + stmt + ");", ids - // // ); - // - // if(next) ids = next; - // else break; - // } - - ids = [ids]; - return trx.batch([ - trx.none({ text: "DELETE FROM " + this.id + ".map" + this.field + " WHERE id = ANY ($1)", rowMode: "array" }, ids), - trx.none({ text: "DELETE FROM " + this.id + ".ctx" + this.field + " WHERE id = ANY ($1)", rowMode: "array" }, ids), - trx.none({ text: "DELETE FROM " + this.id + ".tag" + this.field + " WHERE id = ANY ($1)", rowMode: "array" }, ids), - trx.none({ text: "DELETE FROM " + this.id + ".reg WHERE id = ANY ($1)", rowMode: "array" }, ids) - ]); - - // ids = [ids]; - // return trx.batch([ - // trx.none("DELETE FROM " + this.id + ".map" + this.field + " WHERE id = ANY ($1)", ids), - // trx.none("DELETE FROM " + this.id + ".ctx" + this.field + " WHERE id = ANY ($1)", ids), - // trx.none("DELETE FROM " + this.id + ".tag" + this.field + " WHERE id = ANY ($1)", ids), - // trx.none("DELETE FROM " + this.id + ".reg WHERE id = ANY ($1)", ids) - // ]); - - // return trx.batch([ - // trx.none("DELETE FROM " + this.id + ".map" + this.field + " WHERE id IN ($1:csv)", [ids]), - // trx.none("DELETE FROM " + this.id + ".ctx" + this.field + " WHERE id IN ($1:csv)", [ids]), - // trx.none("DELETE FROM " + this.id + ".tag" + this.field + " WHERE id IN ($1:csv)", [ids]), - // trx.none("DELETE FROM " + this.id + ".reg WHERE id IN ($1:csv)", [ids]) - // ]); - - // const type = self.type === "text" ? "text" : "int"; - // return trx.batch([ - // trx.none("DELETE FROM " + this.id + ".map" + self.field + " WHERE id = ANY($1::" + type + "[])", [ids]), - // trx.none("DELETE FROM " + this.id + ".ctx" + self.field + " WHERE id = ANY($1::" + type + "[])", [ids]), - // trx.none("DELETE FROM " + this.id + ".reg WHERE id = ANY($1::" + type + "[])", [ids]) - // ]); - - // return trx.batch([ - // trx.none("DELETE FROM " + this.id + ".map" + self.field + " WHERE id = ANY($1)", [ids]), - // trx.none("DELETE FROM " + this.id + ".ctx" + self.field + " WHERE id = ANY($1)", [ids]), - // trx.none("DELETE FROM " + this.id + ".reg WHERE id = ANY($1)", [ids]) - // ]); - }); -}; - -// if(this.type === "int" && typeof ids[0] === "string"){ -// ids = ids.map(item => parseInt(item, 10)); -// } -// if(this.type === "bigint" && typeof ids[0] === "string"){ -// ids = ids.map(item => BigInt(item)); -// } -// if(this.type === "string" && typeof ids[0] === "number"){ -// ids = ids.map(item => item + ""); -// } -// this.type !== "string" && checkMigration.call(this, ids[0]); - -// function checkMigration(id){ -// if(this.type !== "string"){ -// if(typeof id === "object"){ -// id = id[0]; -// } -// if(typeof id === "string"){ -// this.type = "string"; -// return upgradeTextId.call(this); -// } -// if(this.type !== "bigint"){ -// if(id > 2 ** 31 - 1){ -// this.type = "bigint"; -// return upgradeBigIntId.call(this); -// } -// } -// } -// } -// -// function upgradeTextId(){ -// return this.db.none(` -// ALTER TABLE ${this.id}.map${this.field} -// ALTER COLUMN id type varchar(128) -// USING id::text; -// ALTER TABLE ${this.id}.ctx${this.field} -// ALTER COLUMN id type varchar(128) -// USING id::text; -// ALTER TABLE ${this.id}.tag${this.field} -// ALTER COLUMN id type varchar(128) -// USING id::text; -// ALTER TABLE ${this.id}.reg -// ALTER COLUMN id type varchar(128) -// USING id::text; -// `); -// } -// -// function upgradeBigIntId(){ -// return this.db.none(` -// ALTER TABLE ${this.id}.map${this.field} -// ALTER COLUMN id type bigint -// USING id::bigint; -// ALTER TABLE ${this.id}.ctx${this.field} -// ALTER COLUMN id type bigint -// USING id::bigint; -// ALTER TABLE ${this.id}.tag${this.field} -// ALTER COLUMN id type bigint -// USING id::bigint; -// ALTER TABLE ${this.id}.reg -// ALTER COLUMN id type bigint -// USING id::bigint; -// `); -// } - module.exports = PostgresDB; diff --git a/dist/db/redis/index.cjs b/dist/db/redis/index.cjs index 0ccac85..8bc6cfa 100644 --- a/dist/db/redis/index.cjs +++ b/dist/db/redis/index.cjs @@ -2,599 +2,594 @@ var redis = require('redis'); -/** - * @param {*} value - * @param {*} default_value - * @param {*=} merge_value - * @return {*} - */ - -/** - * @param {Map|Set} val - * @param {boolean=} stringify - * @return {Array} - */ - -function toArray(val, stringify){ - const result = []; - for(const key of val.keys()){ - result.push("" + key ); - } - return result; +/** + * @param {*} value + * @param {*} default_value + * @param {*=} merge_value + * @return {*} + */ + +/** + * @param {Map|Set} val + * @param {boolean=} stringify + * @return {Array} + */ + +function toArray(val, stringify){ + const result = []; + for(const key of val.keys()){ + result.push("" + key ); + } + return result; } -const defaults = { - host: "localhost", - port: "6379", - user: null, - pass: null -}; - -function sanitize(str) { - return str.toLowerCase().replace(/[^a-z0-9_\-]/g, ""); -} - -let DB, TRX; - -/** - * @constructor - * @implements StorageInterface - */ - -function RedisDB(name, config = {}){ - if(!this || this.constructor !== RedisDB){ - return new RedisDB(name, config); - } - if(typeof name === "object"){ - config = name; - name = name.name; - } - if(!name){ - console.info("Default storage space was used, because a name was not passed."); - } - this.id = (name ? sanitize(name) : "flexsearch") + "|"; - this.field = config.field ? "-" + sanitize(config.field) : ""; - this.type = config.type || ""; - this.fastupdate = true; - this.db = config.db || DB || null; - this.support_tag_search = true; - this.resolution = 9; - this.resolution_ctx = 9; - //this.trx = false; - Object.assign(defaults, config); - this.db && delete defaults.db; -} -// RedisDB.mount = function(flexsearch){ -// return new this().mount(flexsearch); -// }; - -RedisDB.prototype.mount = function(flexsearch){ - //if(flexsearch.constructor === Document){ - if(flexsearch.index){ - return flexsearch.mount(this); - } - flexsearch.db = this; - this.resolution = flexsearch.resolution; - this.resolution_ctx = flexsearch.resolution_ctx; - // todo support - //this.fastupdate = flexsearch.fastupdate; - return this.open(); -}; - -RedisDB.prototype.open = async function(){ - if(this.db){ - return this.db - } - if(DB){ - return this.db = DB; - } - let url = defaults.url; - if(!url){ - url = defaults.user - ? `redis://${defaults.user}:${defaults.pass}@${defaults.host}:${defaults.port}` - : `redis://${defaults.host}:${defaults.port}`; - } - return this.db = DB = - await redis.createClient(url) - .on("error", err => console.error(err)) - .connect(); -}; - -RedisDB.prototype.close = async function(){ - DB && await this.db.disconnect(); // this.db.client.disconnect(); - this.db = DB = null; - return this; -}; - -RedisDB.prototype.destroy = function(){ - return this.clear(); -}; - -RedisDB.prototype.clear = function(){ - return this.db.unlink([ - this.id + "map" + this.field, - this.id + "ctx" + this.field, - this.id + "tag" + this.field, - this.id + "cfg" + this.field, - this.id + "doc", - this.id + "reg" - ]); -}; - -function create_result(range, type, resolve, enrich, resolution){ - if(resolve){ - if(type === "number"){ - for(let i = 0, tmp, id; i < range.length; i++){ - tmp = range[i]; - id = type === "number" - ? parseInt(tmp.id || tmp, 10) - : tmp.id || tmp; - range[i] = enrich - ? { id, doc: tmp.doc } - : id; - } - } - return range; - } - else { - let result = []; - for(let i = 0, tmp, id, score; i < range.length; i++){ - tmp = range[i]; - id = type === "number" - ? parseInt(tmp.id || tmp, 10) - : tmp.id || tmp; - score = resolution - tmp.score; - result[score] || (result[score] = []); - result[score].push(id); - } - return result; - } -} - -RedisDB.prototype.get = function(key, ctx, limit = 0, offset = 0, resolve = true, enrich = false, tags){ - - if(tags){ - // flexsearch dummy - const flexsearch = { depth: !!ctx }; - const query = ctx ? [ctx, key] : [key]; // keyword first - return this.search(flexsearch, query, limit, offset, /* suggest */ false, resolve, enrich, tags); - } - - const type = this.type; - const self = this; - let result; - - if(ctx){ - result = this.db[resolve ? "zRange" : "zRangeWithScores"]( - this.id + "ctx" + this.field + ":" + ctx + ":" + key, - "" + offset, - "" + (offset + limit - 1), - { REV: true } - ); - } - else { - result = this.db[resolve ? "zRange" : "zRangeWithScores"]( - this.id + "map" + this.field + ":" + key, - "" + offset, - "" + (offset + limit - 1), - { REV: true } - ); - } - - return result.then(async function(range){ - if(!range.length) return range; - if(enrich) range = await self.enrich(range); - return create_result(range, type, resolve, enrich, ctx ? self.resolution_ctx : self.resolution); - }); -}; - -RedisDB.prototype.tag = function(tag, limit = 0, offset = 0, enrich = false){ - const self = this; - return this.db.sMembers(this.id + "tag" + this.field + ":" + tag).then(function(ids){ - if(!ids || !ids.length || offset >= ids.length) return []; - if(!limit && !offset) return ids; - const result = ids.slice(offset, offset + limit); - return enrich - ? self.enrich(result) - : result; - }); -}; - -RedisDB.prototype.enrich = function(ids){ - if(typeof ids !== "object"){ - ids = [ids]; - } - return this.db.hmGet(this.id + "doc", this.type === "number" ? ids.map(i => "" + i) : ids).then(function(res){ - for(let i = 0; i < res.length; i++){ - res[i] = { - id: ids[i], - doc: res[i] && JSON.parse(res[i]) - }; - } - return res; - }); -}; - -RedisDB.prototype.has = function(id){ - return this.db.sIsMember(this.id + "reg", "" + id); -}; - -RedisDB.prototype.search = function(flexsearch, query, limit = 100, offset = 0, suggest = false, resolve = true, enrich = false, tags){ - - const ctx = query.length > 1 && flexsearch.depth; - let result; - let params = []; - let weights = []; - - if(ctx){ - - const key = this.id + "ctx" + this.field + ":"; - let keyword = query[0]; - let term; - - for(let i = 1, swap; i < query.length; i++){ - term = query[i]; - swap = flexsearch.bidirectional && (term > keyword); - params.push(key + (swap ? term : keyword) + ":" + (swap ? keyword : term)); - weights.push(1); - keyword = term; - } - } - else { - - const key = this.id + "map" + this.field + ":"; - for(let i = 0; i < query.length; i++){ - params.push(key + query[i]); - weights.push(1); - } - } - - query = params; - - const type = this.type; - let key = this.id + "tmp:" + Math.random(); - - if(suggest){ - const multi = this.db.multi(); - // The zStore implementation lacks of ordering by match count (occurrences). - // Unfortunately, I couldn't find an elegant way to overcome this issue completely. - // For this reason it needs additionally a zInterStore to boost at least matches - // when all terms were found - multi.zInterStore(key, query, { AGGREGATE: "SUM" }); - query.push(key); - weights.push(query.length); - multi.zUnionStore(key, query, { WEIGHTS: weights, AGGREGATE: "SUM" }); - // Strict Tag Intersection: it does not put tags into union, instead it calculates the - // intersection against the term match union. This was the default behavior - // of Tag-Search. But putting everything into union will also provide suggestions derived - // from tags when no term was matched. - { - if(tags){ - // copy over zInterStore into the same destination surprisingly works fine - // const key2 = key + ":2"; - query = [key]; - for(let i = 0; i < tags.length; i += 2){ - query.push(this.id + "tag-" + sanitize(tags[i]) + ":" + tags[i + 1]); - } - multi.zInterStore(key, query, { AGGREGATE: "MAX" }); - // .unlink(key) - // key = key2; - } - } - result = multi - [(resolve ? "zRange" : "zRangeWithScores")]( - key, - "" + offset, - "" + (offset + limit - 1), - { REV: true } - ) - .unlink(key) - .exec(); - } - else { - if(tags) for(let i = 0; i < tags.length; i += 2){ - query.push(this.id + "tag-" + sanitize(tags[i]) + ":" + tags[i + 1]); - } - result = this.db.multi() - .zInterStore(key, query, { AGGREGATE: "MAX" }) - [(resolve ? "zRange" : "zRangeWithScores")]( - key, - "" + offset, - "" + (offset + limit - 1), - { REV: true } - ) - .unlink(key) - .exec(); - } - - const self = this; - return result.then(async function(range){ - range = suggest && tags - // take the 3rd result from batch return - ? range[3] - // take the 2nd result from batch return - : range[suggest ? 2 : 1]; - if(!range.length) return range; - if(enrich) range = await self.enrich(range); - return create_result(range, type, resolve, enrich, ctx ? self.resolution_ctx : self.resolution); - }); -}; - -RedisDB.prototype.info = function(){ - // todo -}; - -RedisDB.prototype.transaction = function(task, callback){ - - if(TRX){ - return task.call(this, TRX); - } - - TRX = this.db.multi(); - let promise1 = /*await*/ task.call(this, TRX); - let promise2 = TRX.exec(); - TRX = null; - return Promise.all([promise1, promise2]).then(function(){ - callback && callback(); - }); -}; - -RedisDB.prototype.commit = async function(flexsearch, _replace, _append){ - - // process cleanup tasks - if(_replace){ - await this.clear(); - // there are just removals in the task queue - flexsearch.commit_task = []; - } - else { - let tasks = flexsearch.commit_task; - flexsearch.commit_task = []; - for(let i = 0, task; i < tasks.length; i++){ - task = tasks[i]; - // there are just removals in the task queue - if(task.clear){ - await this.clear(); - _replace = true; - break; - } - else { - tasks[i] = "" + task.del; - } - } - if(!_replace){ - if(!_append){ - tasks = tasks.concat(toArray(flexsearch.reg)); - } - tasks.length && await this.remove(tasks); - } - } - - if(!flexsearch.reg.size){ - return; - } - - await this.transaction(function(trx){ - - let refs = new Map(); - for(const item of flexsearch.map){ - const key = item[0]; - const arr = item[1]; - for(let i = 0, ids; i < arr.length; i++){ - if((ids = arr[i]) && ids.length){ - - let result = []; - for(let j = 0; j < ids.length; j++){ - result.push({ - score: this.resolution - i, - value: "" + ids[j] - }); - } - if(typeof ids[0] === "number"){ - this.type = "number"; - } - - const ref = this.id + "map" + this.field + ":" + key; - trx.zAdd(ref, result); - // if(this.fastupdate) for(let j = 0; j < ids.length; j++){ - // trx.sAdd("ref" + this.field + ":" + ids[j], ref); - // } - if(this.fastupdate) for(let j = 0, id; j < ids.length; j++){ - // Map performs bad when pushing numeric-like values as key - // id = ids[j]; - // let tmp = refs.get(id); - // tmp || refs.set(id, tmp = []); - // tmp.push(ref); - id = ids[j]; - let tmp = refs.get(id); - tmp || refs.set(id, tmp = []); - tmp.push(ref); - } - } - } - } - // if(this.fastupdate) for(let item of refs){ - // const key = item[0]; - // const value = item[1]; - // trx.sAdd("ref" + this.field + ":" + key, value); - // } - if(this.fastupdate) for(const item of refs){ - const key = item[0]; - const value = item[1]; - trx.sAdd(this.id + "ref" + this.field + ":" + key, value); - } - - refs = new Map(); - for(const ctx of flexsearch.ctx){ - const ctx_key = ctx[0]; - const ctx_value = ctx[1]; - for(const item of ctx_value){ - const key = item[0]; - const arr = item[1]; - for(let i = 0, ids; i < arr.length; i++){ - if((ids = arr[i]) && ids.length){ - let result = []; - for(let j = 0; j < ids.length; j++){ - result.push({ - score: this.resolution_ctx - i, - value: "" + ids[j] - }); - } - if(typeof ids[0] === "number"){ - this.type = "number"; - } - const ref = this.id + "ctx" + this.field + ":" + ctx_key + ":" + key; - trx.zAdd(ref, result); - // if(this.fastupdate) for(let j = 0; j < ids.length; j++){ - // trx.sAdd("ref" + this.field + ":" + ids[j], ref); - // } - if(this.fastupdate) for(let j = 0, id; j < ids.length; j++){ - // Map performs bad when pushing numeric-like values as key - // id = ids[j]; - // let tmp = refs.get(id); - // tmp || refs.set(id, tmp = []); - // tmp.push(ref); - id = ids[j]; - let tmp = refs.get(id); - tmp || refs.set(id, tmp = []); - tmp.push(ref); - } - } - } - } - } - - if(this.fastupdate) for(const item of refs){ - const key = item[0]; - const value = item[1]; - trx.sAdd(this.id + "ref" + this.field + ":" + key, value); - } - - if(flexsearch.store){ - for(const item of flexsearch.store.entries()){ - const id = item[0]; - const doc = item[1]; - doc && trx.hSet(this.id + "doc", "" + id, JSON.stringify(doc)); - } - } - if(!flexsearch.bypass){ - let ids = toArray(flexsearch.reg); - if(ids.length){ - trx.sAdd(this.id + "reg", ids); - } - } - - if(flexsearch.tag){ - for(const item of flexsearch.tag){ - const tag = item[0]; - const ids = item[1]; - if(!ids.length) continue; - let result = []; - // for(let i = 0; i < ids.length; i++){ - // result.push({ - // score: 0, - // value: "" + ids[i] - // }); - // } - if(typeof ids[0] === "number"){ - for(let i = 0; i < ids.length; i++){ - result[i] = "" + ids[i]; - } - } - else { - result = ids; - } - trx.sAdd(this.id + "tag" + this.field + ":" + tag, result); - } - } - - // TODO - // trx.set(this.id + "cfg" + this.field, JSON.stringify({ - // "encode": typeof flexsearch.encode === "string" ? flexsearch.encode : "", - // "charset": typeof flexsearch.charset === "string" ? flexsearch.charset : "", - // "tokenize": flexsearch.tokenize, - // "resolution": flexsearch.resolution, - // "minlength": flexsearch.minlength, - // "optimize": flexsearch.optimize, - // "fastupdate": flexsearch.fastupdate, - // "encoder": flexsearch.encoder, - // "context": { - // "depth": flexsearch.depth, - // "bidirectional": flexsearch.bidirectional, - // "resolution": flexsearch.resolution_ctx - // } - // })); - }); - - flexsearch.map.clear(); - flexsearch.ctx.clear(); - flexsearch.tag && - flexsearch.tag.clear(); - flexsearch.store && - flexsearch.store.clear(); - flexsearch.document || - flexsearch.reg.clear(); -}; - -RedisDB.prototype.remove = function(ids){ - - if(!ids && ids !== 0){ - return; - } - if(typeof ids !== "object"){ - ids = [ids]; - } - if(!ids.length){ - return; - } - - return this.transaction(async function(trx){ - - while(ids.length){ - let next; - if(ids.length > 10000){ - next = ids.slice(10000); - ids = ids.slice(0, 10000); - } - - if(typeof ids[0] === "number"){ - for(let i = 0; i < ids.length; i++){ - ids[i] = "" + ids[i]; - } - } - - const check = await this.db.smIsMember(this.id + "reg", ids); - - for(let i = 0, id; i < ids.length; i++){ - if(!check[i]) continue; - id = "" + ids[i]; - - if(this.fastupdate){ - // const refs = new Map(); - const ref = await this.db.sMembers(this.id + "ref" + this.field + ":" + id); - if(ref){ - for(let j = 0; j < ref.length; j++){ - // let tmp = refs.get(ref[j]); - // tmp || refs.set(ref[j], tmp = []); - // tmp.push(id); - trx.zRem(ref[j], id); - } - trx.unlink(this.id + "ref" + this.field + ":" + id); - } - // for(let item of refs){ - // //console.log(item[0], item[1]) - // trx.zRem(item[0], item[1]); - // } - } - - trx.hDel(this.id + "doc", id); - trx.sRem(this.id + "reg", id); - } - - if(next) ids = next; - else break; - } - }); +const defaults = { + host: "localhost", + port: "6379", + user: null, + pass: null +}; + +function sanitize(str) { + return str.toLowerCase().replace(/[^a-z0-9_\-]/g, ""); +} + +let DB, TRX; + +/** + * @constructor + * @implements StorageInterface + */ + +function RedisDB(name, config = {}){ + if(!this || this.constructor !== RedisDB){ + return new RedisDB(name, config); + } + if(typeof name === "object"){ + config = name; + name = name.name; + } + if(!name){ + console.info("Default storage space was used, because a name was not passed."); + } + this.id = (name ? sanitize(name) : "flexsearch") + "|"; + this.field = config.field ? "-" + sanitize(config.field) : ""; + this.type = config.type || ""; + this.fastupdate = true; + this.db = config.db || DB || null; + this.support_tag_search = true; + this.resolution = 9; + this.resolution_ctx = 9; + + Object.assign(defaults, config); + this.db && delete defaults.db; +} + +RedisDB.prototype.mount = function(flexsearch){ + + if(flexsearch.index){ + return flexsearch.mount(this); + } + flexsearch.db = this; + this.resolution = flexsearch.resolution; + this.resolution_ctx = flexsearch.resolution_ctx; + + + return this.open(); +}; + +RedisDB.prototype.open = async function(){ + if(this.db){ + return this.db + } + if(DB){ + return this.db = DB; + } + let url = defaults.url; + if(!url){ + url = defaults.user + ? `redis://${defaults.user}:${defaults.pass}@${defaults.host}:${defaults.port}` + : `redis://${defaults.host}:${defaults.port}`; + } + return this.db = DB = + await redis.createClient(url) + .on("error", err => console.error(err)) + .connect(); +}; + +RedisDB.prototype.close = async function(){ + DB && await this.db.disconnect(); + this.db = DB = null; + return this; +}; + +RedisDB.prototype.destroy = function(){ + return this.clear(); +}; + +RedisDB.prototype.clear = function(){ + return this.db.unlink([ + this.id + "map" + this.field, + this.id + "ctx" + this.field, + this.id + "tag" + this.field, + this.id + "cfg" + this.field, + this.id + "doc", + this.id + "reg" + ]); +}; + +function create_result(range, type, resolve, enrich, resolution){ + if(resolve){ + for(let i = 0, tmp, id; i < range.length; i++){ + tmp = range[i]; + id = type === "number" + ? parseInt(tmp.value || tmp, 10) + : tmp.value || tmp; + range[i] = enrich + ? { id, doc: tmp.doc } + : id; + } + return range; + } + else { + let result = []; + for(let i = 0, tmp, id, score; i < range.length; i++){ + tmp = range[i]; + id = type === "number" + ? parseInt(tmp.value || tmp, 10) + : tmp.value || tmp; + score = Math.max(resolution - tmp.score, 0); + result[score] || (result[score] = []); + result[score].push(id); + } + return result; + } +} + +RedisDB.prototype.get = function(key, ctx, limit = 0, offset = 0, resolve = true, enrich = false, tags){ + + if(tags){ + + const flexsearch = { depth: !!ctx }; + const query = ctx ? [ctx, key] : [key]; + return this.search(flexsearch, query, limit, offset, false, resolve, enrich, tags); + } + + const type = this.type; + const self = this; + let result; + + if(ctx){ + result = this.db[resolve ? "zRange" : "zRangeWithScores"]( + this.id + "ctx" + this.field + ":" + ctx + ":" + key, + "" + offset, + "" + (offset + limit - 1), + { REV: true } + ); + } + else { + result = this.db[resolve ? "zRange" : "zRangeWithScores"]( + this.id + "map" + this.field + ":" + key, + "" + offset, + "" + (offset + limit - 1), + { REV: true } + ); + } + + return result.then(async function(range){ + if(!range.length) return range; + if(enrich) range = await self.enrich(range); + return create_result(range, type, resolve, enrich, ctx ? self.resolution_ctx : self.resolution); + }); +}; + +RedisDB.prototype.tag = function(tag, limit = 0, offset = 0, enrich = false){ + const self = this; + return this.db.sMembers(this.id + "tag" + this.field + ":" + tag).then(function(ids){ + if(!ids || !ids.length || offset >= ids.length) return []; + if(!limit && !offset) return ids; + const result = ids.slice(offset, offset + limit); + return enrich + ? self.enrich(result) + : result; + }); +}; + +RedisDB.prototype.enrich = function(ids){ + if(typeof ids !== "object"){ + ids = [ids]; + } + return this.db.hmGet(this.id + "doc", this.type === "number" ? ids.map(i => "" + i) : ids).then(function(res){ + for(let i = 0; i < res.length; i++){ + res[i] = { + id: ids[i], + doc: res[i] && JSON.parse(res[i]) + }; + } + return res; + }); +}; + +RedisDB.prototype.has = function(id){ + return this.db.sIsMember(this.id + "reg", "" + id); +}; + +RedisDB.prototype.search = function(flexsearch, query, limit = 100, offset = 0, suggest = false, resolve = true, enrich = false, tags){ + + const ctx = query.length > 1 && flexsearch.depth; + let result; + let params = []; + let weights = []; + + if(ctx){ + + const key = this.id + "ctx" + this.field + ":"; + let keyword = query[0]; + let term; + + for(let i = 1, swap; i < query.length; i++){ + term = query[i]; + swap = flexsearch.bidirectional && (term > keyword); + params.push(key + (swap ? term : keyword) + ":" + (swap ? keyword : term)); + weights.push(1); + keyword = term; + } + } + else { + + const key = this.id + "map" + this.field + ":"; + for(let i = 0; i < query.length; i++){ + params.push(key + query[i]); + weights.push(1); + } + } + + query = params; + + const type = this.type; + let key = this.id + "tmp:" + Math.random(); + + if(suggest){ + const multi = this.db.multi(); + + + + + multi.zInterStore(key, query, { AGGREGATE: "SUM" }); + query.push(key); + weights.push(query.length); + multi.zUnionStore(key, query, { WEIGHTS: weights, AGGREGATE: "SUM" }); + + + + + { + if(tags){ + + + query = [key]; + for(let i = 0; i < tags.length; i += 2){ + query.push(this.id + "tag-" + sanitize(tags[i]) + ":" + tags[i + 1]); + } + multi.zInterStore(key, query, { AGGREGATE: "MAX" }); + + + } + } + result = multi + [(resolve ? "zRange" : "zRangeWithScores")]( + key, + "" + offset, + "" + (offset + limit - 1), + { REV: true } + ) + .unlink(key) + .exec(); + } + else { + if(tags) for(let i = 0; i < tags.length; i += 2){ + query.push(this.id + "tag-" + sanitize(tags[i]) + ":" + tags[i + 1]); + } + result = this.db.multi() + .zInterStore(key, query, { AGGREGATE: "MAX" }) + [(resolve ? "zRange" : "zRangeWithScores")]( + key, + "" + offset, + "" + (offset + limit - 1), + { REV: true } + ) + .unlink(key) + .exec(); + } + + const self = this; + return result.then(async function(range){ + range = suggest && tags + + ? range[3] + + : range[suggest ? 2 : 1]; + if(!range.length) return range; + if(enrich) range = await self.enrich(range); + return create_result(range, type, resolve, enrich, ctx ? self.resolution_ctx : self.resolution); + }); +}; + +RedisDB.prototype.info = function(){ + +}; + +RedisDB.prototype.transaction = function(task, callback){ + + if(TRX){ + return task.call(this, TRX); + } + + TRX = this.db.multi(); + let promise1 = task.call(this, TRX); + let promise2 = TRX.exec(); + TRX = null; + return Promise.all([promise1, promise2]).then(function(){ + callback && callback(); + }); +}; + +RedisDB.prototype.commit = async function(flexsearch, _replace, _append){ + + + if(_replace){ + await this.clear(); + + flexsearch.commit_task = []; + } + else { + let tasks = flexsearch.commit_task; + flexsearch.commit_task = []; + for(let i = 0, task; i < tasks.length; i++){ + task = tasks[i]; + + if(task.clear){ + await this.clear(); + _replace = true; + break; + } + else { + tasks[i] = "" + task.del; + } + } + if(!_replace){ + if(!_append){ + tasks = tasks.concat(toArray(flexsearch.reg)); + } + tasks.length && await this.remove(tasks); + } + } + + if(!flexsearch.reg.size){ + return; + } + + await this.transaction(function(trx){ + + let refs = new Map(); + for(const item of flexsearch.map){ + const key = item[0]; + const arr = item[1]; + for(let i = 0, ids; i < arr.length; i++){ + if((ids = arr[i]) && ids.length){ + + let result = []; + for(let j = 0; j < ids.length; j++){ + result.push({ + score: this.resolution - i, + value: "" + ids[j] + }); + } + if(typeof ids[0] === "number"){ + this.type = "number"; + } + + const ref = this.id + "map" + this.field + ":" + key; + trx.zAdd(ref, result); + + + + if(this.fastupdate) for(let j = 0, id; j < ids.length; j++){ + + + + + + id = ids[j]; + let tmp = refs.get(id); + tmp || refs.set(id, tmp = []); + tmp.push(ref); + } + } + } + } + + + + + + if(this.fastupdate) for(const item of refs){ + const key = item[0]; + const value = item[1]; + trx.sAdd(this.id + "ref" + this.field + ":" + key, value); + } + + refs = new Map(); + for(const ctx of flexsearch.ctx){ + const ctx_key = ctx[0]; + const ctx_value = ctx[1]; + for(const item of ctx_value){ + const key = item[0]; + const arr = item[1]; + for(let i = 0, ids; i < arr.length; i++){ + if((ids = arr[i]) && ids.length){ + let result = []; + for(let j = 0; j < ids.length; j++){ + result.push({ + score: this.resolution_ctx - i, + value: "" + ids[j] + }); + } + if(typeof ids[0] === "number"){ + this.type = "number"; + } + const ref = this.id + "ctx" + this.field + ":" + ctx_key + ":" + key; + trx.zAdd(ref, result); + + + + if(this.fastupdate) for(let j = 0, id; j < ids.length; j++){ + + + + + + id = ids[j]; + let tmp = refs.get(id); + tmp || refs.set(id, tmp = []); + tmp.push(ref); + } + } + } + } + } + + if(this.fastupdate) for(const item of refs){ + const key = item[0]; + const value = item[1]; + trx.sAdd(this.id + "ref" + this.field + ":" + key, value); + } + + if(flexsearch.store){ + for(const item of flexsearch.store.entries()){ + const id = item[0]; + const doc = item[1]; + doc && trx.hSet(this.id + "doc", "" + id, JSON.stringify(doc)); + } + } + if(!flexsearch.bypass){ + let ids = toArray(flexsearch.reg); + if(ids.length){ + trx.sAdd(this.id + "reg", ids); + } + } + + if(flexsearch.tag){ + for(const item of flexsearch.tag){ + const tag = item[0]; + const ids = item[1]; + if(!ids.length) continue; + let result = []; + + + + + + + if(typeof ids[0] === "number"){ + for(let i = 0; i < ids.length; i++){ + result[i] = "" + ids[i]; + } + } + else { + result = ids; + } + trx.sAdd(this.id + "tag" + this.field + ":" + tag, result); + } + } + + + + + + + + + + + + + + + + + + }); + + flexsearch.map.clear(); + flexsearch.ctx.clear(); + flexsearch.tag && + flexsearch.tag.clear(); + flexsearch.store && + flexsearch.store.clear(); + flexsearch.document || + flexsearch.reg.clear(); +}; + +RedisDB.prototype.remove = function(ids){ + + if(!ids && ids !== 0){ + return; + } + if(typeof ids !== "object"){ + ids = [ids]; + } + if(!ids.length){ + return; + } + + return this.transaction(async function(trx){ + + while(ids.length){ + let next; + if(ids.length > 10000){ + next = ids.slice(10000); + ids = ids.slice(0, 10000); + } + + if(typeof ids[0] === "number"){ + for(let i = 0; i < ids.length; i++){ + ids[i] = "" + ids[i]; + } + } + + const check = await this.db.smIsMember(this.id + "reg", ids); + + for(let i = 0, id; i < ids.length; i++){ + if(!check[i]) continue; + id = "" + ids[i]; + + if(this.fastupdate){ + + const ref = await this.db.sMembers(this.id + "ref" + this.field + ":" + id); + if(ref){ + for(let j = 0; j < ref.length; j++){ + + + + trx.zRem(ref[j], id); + } + trx.unlink(this.id + "ref" + this.field + ":" + id); + } + + + + + } + + trx.hDel(this.id + "doc", id); + trx.sRem(this.id + "reg", id); + } + + if(next) ids = next; + else break; + } + }); }; module.exports = RedisDB; diff --git a/dist/db/sqlite/index.cjs b/dist/db/sqlite/index.cjs index 7c45bb5..541953e 100644 --- a/dist/db/sqlite/index.cjs +++ b/dist/db/sqlite/index.cjs @@ -3,837 +3,835 @@ var sqlite3 = require('sqlite3'); var path = require('path'); -/** - * @param {*} value - * @param {*} default_value - * @param {*=} merge_value - * @return {*} - */ - -function concat(arrays){ - return [].concat.apply([], arrays); +/** + * @param {*} value + * @param {*} default_value + * @param {*=} merge_value + * @return {*} + */ + +function concat(arrays){ + return [].concat.apply([], arrays); +} + +/** + * @param {Map|Set} val + * @param {boolean=} stringify + * @return {Array} + */ + +function toArray(val, stringify){ + const result = []; + for(const key of val.keys()){ + result.push(key); + } + return result; } -/** - * @param {Map|Set} val - * @param {boolean=} stringify - * @return {Array} - */ - -function toArray(val, stringify){ - const result = []; - for(const key of val.keys()){ - result.push(key); - } - return result; -} - -//const sqlite3 = require("sqlite3").verbose(); -const MAXIMUM_QUERY_VARS = 16000; -const fields = ["map", "ctx", "reg", "tag", "cfg"]; -const types = { - "text": "text", - "char": "text", - "varchar": "text", - "string": "text", - "number": "int", - "numeric": "int", - "integer": "int", - "smallint": "int", - "tinyint": "int", - "mediumint": "int", - "int": "int", - "int8": "int", - "uint8": "int", - "int16": "int", - "uint16": "int", - "int32": "int", - "uint32": "bigint", - "int64": "bigint", - "bigint": "bigint" -}; - -function sanitize(str) { - return str.toLowerCase().replace(/[^a-z0-9_]/g, ""); -} - -// global transaction to keep track of database lock -const TRX = Object.create(null); -const Index = Object.create(null); - -/** - * @constructor - * @implements StorageInterface - */ - -function SqliteDB(name, config = {}){ - if(!this || this.constructor !== SqliteDB){ - return new SqliteDB(name, config); - } - if(typeof name === "object"){ - config = name; - name = name.name; - } - if(!name){ - console.info("Default storage space was used, because a name was not passed."); - } - //field = "Test-456"; - this.id = config.path || ( - name === ":memory:" - ? name - : "flexsearch" + (name ? "-" + sanitize(name) : "") + ".sqlite" - ); - this.field = config.field ? "_" + sanitize(config.field) : ""; - this.support_tag_search = true; - this.db = config.db || Index[this.id] || null; - this.type = config.type ? types[config.type.toLowerCase()] : "string"; - if(!this.type) throw new Error("Unknown type of ID '" + config.type + "'"); -} -SqliteDB.prototype.mount = function(flexsearch){ - //if(flexsearch.constructor === Document){ - if(flexsearch.index){ - return flexsearch.mount(this); - } - flexsearch.db = this; - return this.open(); -}; - -SqliteDB.prototype.open = async function(){ - - if(!this.db){ - - if(!(this.db = Index[this.id])){ - - let filepath = this.id; - if(filepath !== ":memory:"){ - // skip absolute path - if(filepath[0] !== "/" && filepath[0] !== "\\"){ - // current working directory - const dir = process.cwd(); - filepath = path.join(dir, this.id); - } - } - - this.db = Index[this.id] = new sqlite3.Database(filepath); - } - } - - const db = this.db; - - for(let i = 0; i < fields.length; i++){ - const exist = await this.promisfy({ - method: "get", - stmt: "SELECT EXISTS(SELECT 1 FROM sqlite_master WHERE type = 'table' AND name = ?) as exist", - params: [fields[i] + (fields[i] === "reg" ? "" : this.field)] - }); - if(!exist || !exist.exist){ - let stmt, stmt_index; - switch(fields[i]){ - case "map": - stmt = ` - CREATE TABLE IF NOT EXISTS main.map${this.field}( - key TEXT NOT NULL, - res INTEGER NOT NULL, - id ${this.type} NOT NULL - ); - `; - stmt_index = ` - CREATE INDEX IF NOT EXISTS map_key_index${this.field} - ON map${this.field} (key); - CREATE INDEX IF NOT EXISTS map_id_index${this.field} - ON map${this.field} (id); - `; - break; - - case "ctx": - stmt = ` - CREATE TABLE IF NOT EXISTS main.ctx${this.field}( - ctx TEXT NOT NULL, - key TEXT NOT NULL, - res INTEGER NOT NULL, - id ${this.type} NOT NULL - ); - - `; - stmt_index = ` - CREATE INDEX IF NOT EXISTS ctx_key_index${this.field} - ON ctx${this.field} (ctx, key); - CREATE INDEX IF NOT EXISTS ctx_id_index${this.field} - ON ctx${this.field} (id); - `; - break; - - case "tag": - stmt = ` - CREATE TABLE IF NOT EXISTS main.tag${this.field}( - tag TEXT NOT NULL, - id ${this.type} NOT NULL - ); - `; - stmt_index = ` - CREATE INDEX IF NOT EXISTS tag_index${this.field} - ON tag${this.field} (tag); - CREATE INDEX IF NOT EXISTS tag_id_index${this.field} - ON tag${this.field} (id); - `; - break; - - case "reg": - stmt = ` - CREATE TABLE IF NOT EXISTS main.reg( - id ${this.type} NOT NULL - CONSTRAINT reg_pk${this.field} - PRIMARY KEY, - doc TEXT DEFAULT NULL - ); - `; - stmt_index = ` - CREATE INDEX IF NOT EXISTS reg_index - ON reg (id); - `; - break; - - case "cfg": - stmt = ` - CREATE TABLE IF NOT EXISTS main.cfg${this.field} ( - cfg TEXT NOT NULL - ); - `; - break; - } - - await new Promise(function(resolve, reject){ - db.exec(stmt, function(err, rows){ - if(err) return reject(err); - stmt_index - ? db.exec(stmt_index, function(err, rows){ - if(err) return reject(err); - resolve(rows); - }) - : resolve(rows); - }); - }); - } - } - - db.exec("PRAGMA optimize = 0x10002"); - - return db; -}; - -SqliteDB.prototype.close = function(){ - this.db && this.db.close(); - this.db = null; - Index[this.id] = null; - TRX[this.id] = null; - return this; -}; - -SqliteDB.prototype.destroy = function(){ - return this.transaction(function(){ - this.db.run("DROP TABLE IF EXISTS main.map" + this.field + ";"); - this.db.run("DROP TABLE IF EXISTS main.ctx" + this.field + ";"); - this.db.run("DROP TABLE IF EXISTS main.tag" + this.field + ";"); - this.db.run("DROP TABLE IF EXISTS main.cfg" + this.field + ";"); - this.db.run("DROP TABLE IF EXISTS main.reg;"); - }); -}; - -SqliteDB.prototype.clear = function(){ - return this.transaction(function(){ - this.db.run("DELETE FROM main.map" + this.field + " WHERE 1;"); - this.db.run("DELETE FROM main.ctx" + this.field + " WHERE 1;"); - this.db.run("DELETE FROM main.tag" + this.field + " WHERE 1;"); - this.db.run("DELETE FROM main.cfg" + this.field + " WHERE 1;"); - this.db.run("DELETE FROM main.reg WHERE 1;"); - }); -}; - -function create_result(rows, resolve, enrich){ - if(resolve){ - for(let i = 0; i < rows.length; i++){ - if(enrich){ - if(rows[i].doc){ - rows[i].doc = JSON.parse(rows[i].doc); - } - } - else { - rows[i] = rows[i].id; - } - } - return rows; - } - else { - const arr = []; - for(let i = 0, row; i < rows.length; i++){ - row = rows[i]; - arr[row.res] || (arr[row.res] = []); - arr[row.res].push(enrich - ? row - : row.id - ); - } - return arr; - } -} - -SqliteDB.prototype.get = function(key, ctx, limit = 0, offset = 0, resolve = true, enrich = false, tags){ - let result; - let stmt = ''; - let params = ctx ? [ctx, key] : [key]; - let table = "main." + (ctx ? "ctx" : "map") + this.field; - if(tags){ - for(let i = 0; i < tags.length; i+=2){ - stmt += ` AND ${ table }.id IN (SELECT id FROM main.tag_${ sanitize(tags[i]) } WHERE tag = ?)`; - params.push(tags[i + 1]); - } - } - if(ctx){ - result = this.promisfy({ - method: "all", - stmt: ` - SELECT ${ table }.id - ${ resolve ? "" : ", res" } - ${ enrich ? ", doc" : "" } - FROM ${ table } - ${ enrich ? ` - LEFT JOIN main.reg ON main.reg.id = ${ table }.id - ` : "" } - WHERE ctx = ? AND key = ? ${stmt} - ORDER BY res - ${ limit ? "LIMIT " + limit : "" } - ${ offset ? "OFFSET " + offset : "" } - `, - params - }); - } - else { - result = this.promisfy({ - method: "all", - stmt: ` - SELECT ${ table }.id - ${ resolve ? "" : ", res" } - ${ enrich ? ", doc" : "" } - FROM ${ table } - ${ enrich ? ` - LEFT JOIN main.reg ON main.reg.id = ${ table }.id - ` : "" } - WHERE key = ? ${stmt} - ORDER BY res - ${ limit ? "LIMIT " + limit : "" } - ${ offset ? "OFFSET " + offset : "" } - `, - params - }); - } - return result.then(function(rows){ - return create_result(rows, resolve, enrich); - }); -}; - -SqliteDB.prototype.tag = function(tag, limit = 0, offset = 0, enrich = false){ - const table = "main.tag" + this.field; - const promise = this.promisfy({ - method: "all", - stmt: ` - SELECT ${ table }.id - ${ enrich ? ", doc" : "" } - FROM ${ table } - ${ enrich ? ` - LEFT JOIN main.reg ON main.reg.id = ${ table }.id - ` : "" } - WHERE tag = ? - ${ limit ? "LIMIT " + limit : "" } - ${ offset ? "OFFSET " + offset : "" } - `, - params: [tag] - }); - enrich || promise.then(function(rows){ - return create_result(rows, true, false); - }); - return promise; -}; - -function build_params(length, single_param){ - - let stmt = single_param - ? ",(?)" - : ",?"; - for(let i = 1; i < length;){ - if(i <= (length - i)){ - stmt += stmt; - i *= 2; - } - else { - stmt += stmt.substring(0, (length - i) * (single_param ? 4 : 2)); - break; - } - } - return stmt.substring(1); -} - -SqliteDB.prototype.enrich = function(ids){ - - const result = []; - const promises = []; - if(typeof ids !== "object"){ - ids = [ids]; - } - - for(let count = 0; count < ids.length;){ - - const chunk = ids.length - count > MAXIMUM_QUERY_VARS - ? ids.slice(count, count + MAXIMUM_QUERY_VARS) - : count ? ids.slice(count) : ids; - count += chunk.length; - - // let stmt = "?"; - // for(let i = 1; i < chunk.length; i++){ - // stmt += ",?"; - // } - - // 10x faster string concatenation - let stmt = build_params(chunk.length); - - promises.push(this.promisfy({ - method: "all", - stmt: `SELECT id, doc FROM main.reg WHERE id IN (${stmt})`, - params: chunk - })); - } - - return Promise.all(promises).then(function(promises){ - - for(let i = 0, res; i < promises.length; i++){ - res = promises[i]; - if(res && res.length){ - for(let i = 0, doc; i < res.length; i++){ - if((doc = res[i].doc)){ - res[i].doc = JSON.parse(doc); - } - } - result.push(res); - } - } - - return result.length === 1 - ? result[0] - : result.length > 1 - ? concat(result) - : result; - }); -}; - -SqliteDB.prototype.has = function(id){ - return this.promisfy({ - method: "get", - stmt: `SELECT EXISTS(SELECT 1 FROM main.reg WHERE id = ?) as exist`, - params: [id] - }).then(function(result){ - return !!(result && result.exist); - }); -}; - -SqliteDB.prototype.search = function(flexsearch, query, limit = 100, offset = 0, suggest = false, resolve = true, enrich = false, tags){ - - let rows; - - if(query.length > 1 && flexsearch.depth){ - - let stmt = ""; - let params = []; - let keyword = query[0]; - let term; - - for(let i = 1; i < query.length; i++){ - term = query[i]; - const swap = flexsearch.bidirectional && (term > keyword); - stmt += (stmt ? " OR " : "") + `(ctx = ? AND key = ?)`; - params.push(swap ? term : keyword, swap ? keyword : term); - keyword = term; - } - - if(tags){ - stmt = "(" + stmt + ")"; - for(let i = 0; i < tags.length; i+=2){ - stmt += ` AND id IN (SELECT id FROM main.tag_${ sanitize(tags[i]) } WHERE tag = ?)`; - params.push(tags[i + 1]); - } - } - - rows = this.promisfy({ - method: "all", - stmt: ` - SELECT r.id - ${ resolve ? "" : ", res" } - ${ enrich ? ", doc" : "" } - FROM ( - SELECT id, count(*) as count, - ${ suggest ? "SUM" : "SUM" /*"MIN"*/ }(res) as res - FROM main.ctx${ this.field } - WHERE ${ stmt } - GROUP BY id - ) as r - ${ enrich ? ` - LEFT JOIN main.reg ON main.reg.id = r.id - ` : "" } - ${ suggest ? "" : "WHERE count = " + (query.length - 1) } - ORDER BY ${ suggest ? "count DESC, res" : "res" } - ${ limit ? "LIMIT " + limit : "" } - ${ offset ? "OFFSET " + offset : "" } - `, - params - }); - - // variant 1 - // for(let i = 1; i < query.length; i++){ - // stmt += (stmt ? " UNION ALL " : "") + ` - // SELECT id, res - // FROM main.ctx${this.field} - // WHERE ctx = ? AND key = ? - // `; - // term = query[i]; - // const swap = flexsearch.bidirectional && (term > keyword); - // params.push(swap ? term : keyword, swap ? keyword : term); - // keyword = term; - // } - // - // rows = await this.promisfy({ - // method: "all", - // stmt: ` - // SELECT id/*, res */ - // FROM ( - // SELECT id, ${suggest ? "SUM" : "MIN"}(res) as res, count(*) as count - // FROM (${stmt}) as t - // GROUP BY id - // ORDER BY ${suggest ? "count DESC, res" : "res"} - // LIMIT ${limit} - // OFFSET ${offset} - // ) as r - // ${suggest ? "" : "WHERE count = " + (query.length - 1)} - // `, - // params - // }); - } - else { - - let stmt = ""; - let query_length = query.length; - for(let i = 0; i < query_length; i++){ - stmt += (stmt ? " OR " : "") + `key = ?`; - } - - if(tags){ - stmt = "(" + stmt + ")"; - for(let i = 0; i < tags.length; i+=2){ - stmt += ` AND id IN (SELECT id FROM main.tag_${sanitize(tags[i])} WHERE tag = ?)`; - query.push(tags[i + 1]); - } - } - - rows = this.promisfy({ - method: "all", - stmt: ` - SELECT r.id - ${ resolve ? "" : ", res" } - ${ enrich ? ", doc" : "" } - FROM ( - SELECT id, count(*) as count, - ${ suggest ? "SUM" : "SUM" /*"MIN"*/ }(res) as res - FROM main.map${ this.field } - WHERE ${ stmt } - GROUP BY id - ) as r - ${ enrich ? ` - LEFT JOIN main.reg ON main.reg.id = r.id - ` : "" } - ${ suggest ? "" : "WHERE count = " + query_length } - ORDER BY ${ suggest ? "count DESC, res" : "res" } - ${ limit ? "LIMIT " + limit : "" } - ${ offset ? "OFFSET " + offset : "" } - `, - params: query - }); - - // variant 1 - // for(let i = 0; i < query.length; i++){ - // stmt += (stmt ? " UNION ALL " : "") + ` - // SELECT id, res - // FROM main.map${ this.field } - // WHERE key = ? - // `; - // } - // - // rows = await this.promisfy({ - // method: "all", - // stmt: ` - // SELECT id/*, res*/ - // FROM ( - // SELECT id, ${suggest ? "SUM" : "MIN"}(res) as res, count(*) as count - // FROM (${stmt}) as t - // GROUP BY id - // ORDER BY ${suggest ? "count DESC, res" : "res"} - // LIMIT ${limit} - // OFFSET ${offset} - // ) as r - // ${ suggest ? "" : "WHERE count = " + query.length } - // `, - // params: query - // }); - } - - return rows.then(function(rows){ - return create_result(rows, resolve, enrich); - }); -}; - -SqliteDB.prototype.info = function(){ - // todo -}; - -SqliteDB.prototype.transaction = async function(task, callback){ - - if(TRX[this.id]){ - return await task.call(this); - } - - const db = this.db; - const self = this; - - return TRX[this.id] = new Promise(function(resolve, reject){ - db.exec("PRAGMA optimize"); - db.exec('PRAGMA busy_timeout = 5000'); - db.exec("BEGIN"); - db.parallelize(function(){ - task.call(self); - }); - db.exec("COMMIT", function(err, rows){ - TRX[self.id] = null; - if(err) return reject(err); - callback && callback(rows); - resolve(rows); - db.exec("PRAGMA shrink_memory"); - }); - }); -}; - -SqliteDB.prototype.commit = async function(flexsearch, _replace, _append){ - - // process cleanup tasks - if(_replace){ - await this.clear(); - // there are just removals in the task queue - flexsearch.commit_task = []; - } - else { - let tasks = flexsearch.commit_task; - flexsearch.commit_task = []; - for(let i = 0, task; i < tasks.length; i++){ - task = tasks[i]; - // there are just removals in the task queue - if(task.clear){ - await this.clear(); - _replace = true; - break; - } - else { - tasks[i] = task.del; - } - } - if(!_replace){ - if(!_append){ - tasks = tasks.concat(toArray(flexsearch.reg)); - } - tasks.length && await this.remove(tasks); - } - } - - if(!flexsearch.reg.size){ - return; - } - - await this.transaction(function(){ - - for(const item of flexsearch.map){ - const key = item[0]; - const arr = item[1]; - - for(let i = 0, ids; i < arr.length; i++){ - if((ids = arr[i]) && ids.length){ - let stmt = ""; - let params = []; - - for(let j = 0; j < ids.length; j++){ - stmt += (stmt ? "," : "") + "(?,?,?)"; - params.push(key, i, ids[j]); - // maximum count of variables supported - if((j === ids.length - 1) || (params.length + 3 > MAXIMUM_QUERY_VARS)){ - this.db.run("INSERT INTO main.map" + this.field + " (key, res, id) VALUES " + stmt, params); - stmt = ""; - params = []; - } - //this.db.run("INSERT INTO map (key, res, id) VALUES (?, ?, ?) ON CONFLICT DO NOTHING", [key, i, ids[j]]); - } - } - } - } - - //}); - //await this.transaction(function(){ - - for(const ctx of flexsearch.ctx){ - const ctx_key = ctx[0]; - const ctx_value = ctx[1]; - - for(const item of ctx_value){ - const key = item[0]; - const arr = item[1]; - - for(let i = 0, ids; i < arr.length; i++){ - if((ids = arr[i]) && ids.length){ - let stmt = ""; - let params = []; - - for(let j = 0; j < ids.length; j++){ - stmt += (stmt ? "," : "") + "(?,?,?,?)"; - params.push(ctx_key, key, i, ids[j]); - // maximum count of variables supported - if((j === ids.length - 1) || (params.length + 4 > MAXIMUM_QUERY_VARS)){ - this.db.run("INSERT INTO main.ctx" + this.field + " (ctx, key, res, id) VALUES " + stmt, params); - stmt = ""; - params = []; - } - } - } - } - } - } - //}); - //await this.transaction(function(){ - - if(flexsearch.store){ - let stmt = ""; - let chunk = []; - for(const item of flexsearch.store.entries()){ - const id = item[0]; - const doc = item[1]; - stmt += (stmt ? "," : "") + "(?,?)"; - chunk.push(id, typeof doc === "object" - ? JSON.stringify(doc) - : doc || null - ); - if(chunk.length + 2 > MAXIMUM_QUERY_VARS){ - this.db.run("INSERT INTO main.reg (id, doc) VALUES " + stmt, chunk); - stmt = ""; - chunk = []; - } - } - if(chunk.length){ - this.db.run("INSERT INTO main.reg (id, doc) VALUES " + stmt, chunk); - } - } - else if(!flexsearch.bypass){ - let ids = toArray(flexsearch.reg); - for(let count = 0; count < ids.length;){ - const chunk = ids.length - count > MAXIMUM_QUERY_VARS - ? ids.slice(count, count + MAXIMUM_QUERY_VARS) - : count ? ids.slice(count) : ids; - count += chunk.length; - const stmt = build_params(chunk.length, /* single param */ true); - this.db.run("INSERT INTO main.reg (id) VALUES " + stmt, chunk); - } - } - //}); - //await this.transaction(function(){ - - if(flexsearch.tag){ - let stmt = ""; - let chunk = []; - for(const item of flexsearch.tag){ - const tag = item[0]; - const ids = item[1]; - if(!ids.length) continue; - for(let i = 0; i < ids.length; i++){ - stmt += (stmt ? "," : "") + "(?,?)"; - chunk.push(tag, ids[i]); - } - if(chunk.length + 2 > MAXIMUM_QUERY_VARS){ - this.db.run("INSERT INTO main.tag" + this.field + " (tag, id) VALUES " + stmt, chunk); - stmt = ""; - chunk = []; - } - } - if(chunk.length){ - this.db.run("INSERT INTO main.tag" + this.field + " (tag, id) VALUES " + stmt, chunk); - } - } - }); - - // TODO - //await this.transaction(function(){ - // this.db.run("INSERT INTO main.cfg" + this.field + " (cfg) VALUES (?)", [JSON.stringify({ - // "charset": flexsearch.charset, - // "tokenize": flexsearch.tokenize, - // "resolution": flexsearch.resolution, - // "fastupdate": flexsearch.fastupdate, - // "compress": flexsearch.compress, - // "encoder": { - // "minlength": flexsearch.encoder.minlength - // }, - // "context": { - // "depth": flexsearch.depth, - // "bidirectional": flexsearch.bidirectional, - // "resolution": flexsearch.resolution_ctx - // } - // })]); - //}); - - flexsearch.map.clear(); - flexsearch.ctx.clear(); - flexsearch.tag && - flexsearch.tag.clear(); - flexsearch.store && - flexsearch.store.clear(); - flexsearch.document || - flexsearch.reg.clear(); -}; - -SqliteDB.prototype.remove = function(ids){ - - if(typeof ids !== "object"){ - ids = [ids]; - } - - let next; - // maximum count of variables supported - if(ids.length > MAXIMUM_QUERY_VARS){ - next = ids.slice(MAXIMUM_QUERY_VARS); - ids = ids.slice(0, MAXIMUM_QUERY_VARS); - } - - const self = this; - return this.transaction(function(){ - const stmt = build_params(ids.length); - this.db.run("DELETE FROM main.map" + self.field + " WHERE id IN (" + stmt + ")", ids); - this.db.run("DELETE FROM main.ctx" + self.field + " WHERE id IN (" + stmt + ")", ids); - this.db.run("DELETE FROM main.tag" + self.field + " WHERE id IN (" + stmt + ")", ids); - this.db.run("DELETE FROM main.reg WHERE id IN (" + stmt + ")", ids); - }).then(function(result){ - return next - ? self.remove(next) - : result; - }); -}; - -SqliteDB.prototype.promisfy = function(opt){ - const db = this.db; - return new Promise(function(resolve, reject){ - db[opt.method](opt.stmt, opt.params || [], function(err, rows){ - opt.callback && opt.callback(rows); - err ? reject(err) - : resolve(rows); - }); - }); +const MAXIMUM_QUERY_VARS = 16000; +const fields = ["map", "ctx", "reg", "tag", "cfg"]; +const types = { + "text": "text", + "char": "text", + "varchar": "text", + "string": "text", + "number": "int", + "numeric": "int", + "integer": "int", + "smallint": "int", + "tinyint": "int", + "mediumint": "int", + "int": "int", + "int8": "int", + "uint8": "int", + "int16": "int", + "uint16": "int", + "int32": "int", + "uint32": "bigint", + "int64": "bigint", + "bigint": "bigint" +}; + +function sanitize(str) { + return str.toLowerCase().replace(/[^a-z0-9_]/g, ""); +} + +const TRX = Object.create(null); +const Index = Object.create(null); + +/** + * @constructor + * @implements StorageInterface + */ + +function SqliteDB(name, config = {}){ + if(!this || this.constructor !== SqliteDB){ + return new SqliteDB(name, config); + } + if(typeof name === "object"){ + config = name; + name = name.name; + } + if(!name){ + console.info("Default storage space was used, because a name was not passed."); + } + + this.id = config.path || ( + name === ":memory:" + ? name + : "flexsearch" + (name ? "-" + sanitize(name) : "") + ".sqlite" + ); + this.field = config.field ? "_" + sanitize(config.field) : ""; + this.support_tag_search = true; + this.db = config.db || Index[this.id] || null; + this.type = config.type ? types[config.type.toLowerCase()] : "string"; + if(!this.type) throw new Error("Unknown type of ID '" + config.type + "'"); +} +SqliteDB.prototype.mount = function(flexsearch){ + + if(flexsearch.index){ + return flexsearch.mount(this); + } + flexsearch.db = this; + return this.open(); +}; + +SqliteDB.prototype.open = async function(){ + + if(!this.db){ + + if(!(this.db = Index[this.id])){ + + let filepath = this.id; + if(filepath !== ":memory:"){ + + if(filepath[0] !== "/" && filepath[0] !== "\\"){ + + const dir = process.cwd(); + filepath = path.join(dir, this.id); + } + } + + this.db = Index[this.id] = new sqlite3.Database(filepath); + } + } + + const db = this.db; + + for(let i = 0; i < fields.length; i++){ + const exist = await this.promisfy({ + method: "get", + stmt: "SELECT EXISTS(SELECT 1 FROM sqlite_master WHERE type = 'table' AND name = ?) as exist", + params: [fields[i] + (fields[i] === "reg" ? "" : this.field)] + }); + if(!exist || !exist.exist){ + let stmt, stmt_index; + switch(fields[i]){ + case "map": + stmt = ` + CREATE TABLE IF NOT EXISTS main.map${this.field}( + key TEXT NOT NULL, + res INTEGER NOT NULL, + id ${this.type} NOT NULL + ); + `; + stmt_index = ` + CREATE INDEX IF NOT EXISTS map_key_index${this.field} + ON map${this.field} (key); + CREATE INDEX IF NOT EXISTS map_id_index${this.field} + ON map${this.field} (id); + `; + break; + + case "ctx": + stmt = ` + CREATE TABLE IF NOT EXISTS main.ctx${this.field}( + ctx TEXT NOT NULL, + key TEXT NOT NULL, + res INTEGER NOT NULL, + id ${this.type} NOT NULL + ); + + `; + stmt_index = ` + CREATE INDEX IF NOT EXISTS ctx_key_index${this.field} + ON ctx${this.field} (ctx, key); + CREATE INDEX IF NOT EXISTS ctx_id_index${this.field} + ON ctx${this.field} (id); + `; + break; + + case "tag": + stmt = ` + CREATE TABLE IF NOT EXISTS main.tag${this.field}( + tag TEXT NOT NULL, + id ${this.type} NOT NULL + ); + `; + stmt_index = ` + CREATE INDEX IF NOT EXISTS tag_index${this.field} + ON tag${this.field} (tag); + CREATE INDEX IF NOT EXISTS tag_id_index${this.field} + ON tag${this.field} (id); + `; + break; + + case "reg": + stmt = ` + CREATE TABLE IF NOT EXISTS main.reg( + id ${this.type} NOT NULL + CONSTRAINT reg_pk${this.field} + PRIMARY KEY, + doc TEXT DEFAULT NULL + ); + `; + stmt_index = ` + CREATE INDEX IF NOT EXISTS reg_index + ON reg (id); + `; + break; + + case "cfg": + stmt = ` + CREATE TABLE IF NOT EXISTS main.cfg${this.field} ( + cfg TEXT NOT NULL + ); + `; + break; + } + + await new Promise(function(resolve, reject){ + db.exec(stmt, function(err, rows){ + if(err) return reject(err); + stmt_index + ? db.exec(stmt_index, function(err, rows){ + if(err) return reject(err); + resolve(rows); + }) + : resolve(rows); + }); + }); + } + } + + db.exec("PRAGMA optimize = 0x10002"); + + return db; +}; + +SqliteDB.prototype.close = function(){ + this.db && this.db.close(); + this.db = null; + Index[this.id] = null; + TRX[this.id] = null; + return this; +}; + +SqliteDB.prototype.destroy = function(){ + return this.transaction(function(){ + this.db.run("DROP TABLE IF EXISTS main.map" + this.field + ";"); + this.db.run("DROP TABLE IF EXISTS main.ctx" + this.field + ";"); + this.db.run("DROP TABLE IF EXISTS main.tag" + this.field + ";"); + this.db.run("DROP TABLE IF EXISTS main.cfg" + this.field + ";"); + this.db.run("DROP TABLE IF EXISTS main.reg;"); + }); +}; + +SqliteDB.prototype.clear = function(){ + return this.transaction(function(){ + this.db.run("DELETE FROM main.map" + this.field + " WHERE 1;"); + this.db.run("DELETE FROM main.ctx" + this.field + " WHERE 1;"); + this.db.run("DELETE FROM main.tag" + this.field + " WHERE 1;"); + this.db.run("DELETE FROM main.cfg" + this.field + " WHERE 1;"); + this.db.run("DELETE FROM main.reg WHERE 1;"); + }); +}; + +function create_result(rows, resolve, enrich){ + if(resolve){ + for(let i = 0; i < rows.length; i++){ + if(enrich){ + if(rows[i].doc){ + rows[i].doc = JSON.parse(rows[i].doc); + } + } + else { + rows[i] = rows[i].id; + } + } + return rows; + } + else { + const arr = []; + for(let i = 0, row; i < rows.length; i++){ + row = rows[i]; + arr[row.res] || (arr[row.res] = []); + arr[row.res].push(enrich + ? row + : row.id + ); + } + return arr; + } +} + +SqliteDB.prototype.get = function(key, ctx, limit = 0, offset = 0, resolve = true, enrich = false, tags){ + let result; + let stmt = ''; + let params = ctx ? [ctx, key] : [key]; + let table = "main." + (ctx ? "ctx" : "map") + this.field; + if(tags){ + for(let i = 0; i < tags.length; i+=2){ + stmt += ` AND ${ table }.id IN (SELECT id FROM main.tag_${ sanitize(tags[i]) } WHERE tag = ?)`; + params.push(tags[i + 1]); + } + } + if(ctx){ + result = this.promisfy({ + method: "all", + stmt: ` + SELECT ${ table }.id + ${ resolve ? "" : ", res" } + ${ enrich ? ", doc" : "" } + FROM ${ table } + ${ enrich ? ` + LEFT JOIN main.reg ON main.reg.id = ${ table }.id + ` : "" } + WHERE ctx = ? AND key = ? ${stmt} + ORDER BY res + ${ limit ? "LIMIT " + limit : "" } + ${ offset ? "OFFSET " + offset : "" } + `, + params + }); + } + else { + result = this.promisfy({ + method: "all", + stmt: ` + SELECT ${ table }.id + ${ resolve ? "" : ", res" } + ${ enrich ? ", doc" : "" } + FROM ${ table } + ${ enrich ? ` + LEFT JOIN main.reg ON main.reg.id = ${ table }.id + ` : "" } + WHERE key = ? ${stmt} + ORDER BY res + ${ limit ? "LIMIT " + limit : "" } + ${ offset ? "OFFSET " + offset : "" } + `, + params + }); + } + return result.then(function(rows){ + return create_result(rows, resolve, enrich); + }); +}; + +SqliteDB.prototype.tag = function(tag, limit = 0, offset = 0, enrich = false){ + const table = "main.tag" + this.field; + const promise = this.promisfy({ + method: "all", + stmt: ` + SELECT ${ table }.id + ${ enrich ? ", doc" : "" } + FROM ${ table } + ${ enrich ? ` + LEFT JOIN main.reg ON main.reg.id = ${ table }.id + ` : "" } + WHERE tag = ? + ${ limit ? "LIMIT " + limit : "" } + ${ offset ? "OFFSET " + offset : "" } + `, + params: [tag] + }); + enrich || promise.then(function(rows){ + return create_result(rows, true, false); + }); + return promise; +}; + +function build_params(length, single_param){ + + let stmt = single_param + ? ",(?)" + : ",?"; + for(let i = 1; i < length;){ + if(i <= (length - i)){ + stmt += stmt; + i *= 2; + } + else { + stmt += stmt.substring(0, (length - i) * (single_param ? 4 : 2)); + break; + } + } + return stmt.substring(1); +} + +SqliteDB.prototype.enrich = function(ids){ + + const result = []; + const promises = []; + if(typeof ids !== "object"){ + ids = [ids]; + } + + for(let count = 0; count < ids.length;){ + + const chunk = ids.length - count > MAXIMUM_QUERY_VARS + ? ids.slice(count, count + MAXIMUM_QUERY_VARS) + : count ? ids.slice(count) : ids; + count += chunk.length; + + + + + + + + let stmt = build_params(chunk.length); + + promises.push(this.promisfy({ + method: "all", + stmt: `SELECT id, doc FROM main.reg WHERE id IN (${stmt})`, + params: chunk + })); + } + + return Promise.all(promises).then(function(promises){ + + for(let i = 0, res; i < promises.length; i++){ + res = promises[i]; + if(res && res.length){ + for(let i = 0, doc; i < res.length; i++){ + if((doc = res[i].doc)){ + res[i].doc = JSON.parse(doc); + } + } + result.push(res); + } + } + + return result.length === 1 + ? result[0] + : result.length > 1 + ? concat(result) + : result; + }); +}; + +SqliteDB.prototype.has = function(id){ + return this.promisfy({ + method: "get", + stmt: `SELECT EXISTS(SELECT 1 FROM main.reg WHERE id = ?) as exist`, + params: [id] + }).then(function(result){ + return !!(result && result.exist); + }); +}; + +SqliteDB.prototype.search = function(flexsearch, query, limit = 100, offset = 0, suggest = false, resolve = true, enrich = false, tags){ + + let rows; + + if(query.length > 1 && flexsearch.depth){ + + let stmt = ""; + let params = []; + let keyword = query[0]; + let term; + + for(let i = 1; i < query.length; i++){ + term = query[i]; + const swap = flexsearch.bidirectional && (term > keyword); + stmt += (stmt ? " OR " : "") + `(ctx = ? AND key = ?)`; + params.push(swap ? term : keyword, swap ? keyword : term); + keyword = term; + } + + if(tags){ + stmt = "(" + stmt + ")"; + for(let i = 0; i < tags.length; i+=2){ + stmt += ` AND id IN (SELECT id FROM main.tag_${ sanitize(tags[i]) } WHERE tag = ?)`; + params.push(tags[i + 1]); + } + } + + rows = this.promisfy({ + method: "all", + stmt: ` + SELECT r.id + ${ resolve ? "" : ", res" } + ${ enrich ? ", doc" : "" } + FROM ( + SELECT id, count(*) as count, + ${ suggest ? "SUM" : "SUM" }(res) as res + FROM main.ctx${ this.field } + WHERE ${ stmt } + GROUP BY id + ) as r + ${ enrich ? ` + LEFT JOIN main.reg ON main.reg.id = r.id + ` : "" } + ${ suggest ? "" : "WHERE count = " + (query.length - 1) } + ORDER BY ${ suggest ? "count DESC, res" : "res" } + ${ limit ? "LIMIT " + limit : "" } + ${ offset ? "OFFSET " + offset : "" } + `, + params + }); + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + } + else { + + let stmt = ""; + let query_length = query.length; + for(let i = 0; i < query_length; i++){ + stmt += (stmt ? " OR " : "") + `key = ?`; + } + + if(tags){ + stmt = "(" + stmt + ")"; + for(let i = 0; i < tags.length; i+=2){ + stmt += ` AND id IN (SELECT id FROM main.tag_${sanitize(tags[i])} WHERE tag = ?)`; + query.push(tags[i + 1]); + } + } + + rows = this.promisfy({ + method: "all", + stmt: ` + SELECT r.id + ${ resolve ? "" : ", res" } + ${ enrich ? ", doc" : "" } + FROM ( + SELECT id, count(*) as count, + ${ suggest ? "SUM" : "SUM" }(res) as res + FROM main.map${ this.field } + WHERE ${ stmt } + GROUP BY id + ) as r + ${ enrich ? ` + LEFT JOIN main.reg ON main.reg.id = r.id + ` : "" } + ${ suggest ? "" : "WHERE count = " + query_length } + ORDER BY ${ suggest ? "count DESC, res" : "res" } + ${ limit ? "LIMIT " + limit : "" } + ${ offset ? "OFFSET " + offset : "" } + `, + params: query + }); + + + + + + + + + + + + + + + + + + + + + + + + + + + } + + return rows.then(function(rows){ + return create_result(rows, resolve, enrich); + }); +}; + +SqliteDB.prototype.info = function(){ + +}; + +SqliteDB.prototype.transaction = async function(task, callback){ + + if(TRX[this.id]){ + return await task.call(this); + } + + const db = this.db; + const self = this; + + return TRX[this.id] = new Promise(function(resolve, reject){ + db.exec("PRAGMA optimize"); + db.exec('PRAGMA busy_timeout = 5000'); + db.exec("BEGIN"); + db.parallelize(function(){ + task.call(self); + }); + db.exec("COMMIT", function(err, rows){ + TRX[self.id] = null; + if(err) return reject(err); + callback && callback(rows); + resolve(rows); + db.exec("PRAGMA shrink_memory"); + }); + }); +}; + +SqliteDB.prototype.commit = async function(flexsearch, _replace, _append){ + + + if(_replace){ + await this.clear(); + + flexsearch.commit_task = []; + } + else { + let tasks = flexsearch.commit_task; + flexsearch.commit_task = []; + for(let i = 0, task; i < tasks.length; i++){ + task = tasks[i]; + + if(task.clear){ + await this.clear(); + _replace = true; + break; + } + else { + tasks[i] = task.del; + } + } + if(!_replace){ + if(!_append){ + tasks = tasks.concat(toArray(flexsearch.reg)); + } + tasks.length && await this.remove(tasks); + } + } + + if(!flexsearch.reg.size){ + return; + } + + await this.transaction(function(){ + + for(const item of flexsearch.map){ + const key = item[0]; + const arr = item[1]; + + for(let i = 0, ids; i < arr.length; i++){ + if((ids = arr[i]) && ids.length){ + let stmt = ""; + let params = []; + + for(let j = 0; j < ids.length; j++){ + stmt += (stmt ? "," : "") + "(?,?,?)"; + params.push(key, i, ids[j]); + + if((j === ids.length - 1) || (params.length + 3 > MAXIMUM_QUERY_VARS)){ + this.db.run("INSERT INTO main.map" + this.field + " (key, res, id) VALUES " + stmt, params); + stmt = ""; + params = []; + } + + } + } + } + } + + + + + for(const ctx of flexsearch.ctx){ + const ctx_key = ctx[0]; + const ctx_value = ctx[1]; + + for(const item of ctx_value){ + const key = item[0]; + const arr = item[1]; + + for(let i = 0, ids; i < arr.length; i++){ + if((ids = arr[i]) && ids.length){ + let stmt = ""; + let params = []; + + for(let j = 0; j < ids.length; j++){ + stmt += (stmt ? "," : "") + "(?,?,?,?)"; + params.push(ctx_key, key, i, ids[j]); + + if((j === ids.length - 1) || (params.length + 4 > MAXIMUM_QUERY_VARS)){ + this.db.run("INSERT INTO main.ctx" + this.field + " (ctx, key, res, id) VALUES " + stmt, params); + stmt = ""; + params = []; + } + } + } + } + } + } + + + + if(flexsearch.store){ + let stmt = ""; + let chunk = []; + for(const item of flexsearch.store.entries()){ + const id = item[0]; + const doc = item[1]; + stmt += (stmt ? "," : "") + "(?,?)"; + chunk.push(id, typeof doc === "object" + ? JSON.stringify(doc) + : doc || null + ); + if(chunk.length + 2 > MAXIMUM_QUERY_VARS){ + this.db.run("INSERT INTO main.reg (id, doc) VALUES " + stmt, chunk); + stmt = ""; + chunk = []; + } + } + if(chunk.length){ + this.db.run("INSERT INTO main.reg (id, doc) VALUES " + stmt, chunk); + } + } + else if(!flexsearch.bypass){ + let ids = toArray(flexsearch.reg); + for(let count = 0; count < ids.length;){ + const chunk = ids.length - count > MAXIMUM_QUERY_VARS + ? ids.slice(count, count + MAXIMUM_QUERY_VARS) + : count ? ids.slice(count) : ids; + count += chunk.length; + const stmt = build_params(chunk.length, true); + this.db.run("INSERT INTO main.reg (id) VALUES " + stmt, chunk); + } + } + + + + if(flexsearch.tag){ + let stmt = ""; + let chunk = []; + for(const item of flexsearch.tag){ + const tag = item[0]; + const ids = item[1]; + if(!ids.length) continue; + for(let i = 0; i < ids.length; i++){ + stmt += (stmt ? "," : "") + "(?,?)"; + chunk.push(tag, ids[i]); + } + if(chunk.length + 2 > MAXIMUM_QUERY_VARS){ + this.db.run("INSERT INTO main.tag" + this.field + " (tag, id) VALUES " + stmt, chunk); + stmt = ""; + chunk = []; + } + } + if(chunk.length){ + this.db.run("INSERT INTO main.tag" + this.field + " (tag, id) VALUES " + stmt, chunk); + } + } + }); + + + + + + + + + + + + + + + + + + + + + flexsearch.map.clear(); + flexsearch.ctx.clear(); + flexsearch.tag && + flexsearch.tag.clear(); + flexsearch.store && + flexsearch.store.clear(); + flexsearch.document || + flexsearch.reg.clear(); +}; + +SqliteDB.prototype.remove = function(ids){ + + if(typeof ids !== "object"){ + ids = [ids]; + } + + let next; + + if(ids.length > MAXIMUM_QUERY_VARS){ + next = ids.slice(MAXIMUM_QUERY_VARS); + ids = ids.slice(0, MAXIMUM_QUERY_VARS); + } + + const self = this; + return this.transaction(function(){ + const stmt = build_params(ids.length); + this.db.run("DELETE FROM main.map" + self.field + " WHERE id IN (" + stmt + ")", ids); + this.db.run("DELETE FROM main.ctx" + self.field + " WHERE id IN (" + stmt + ")", ids); + this.db.run("DELETE FROM main.tag" + self.field + " WHERE id IN (" + stmt + ")", ids); + this.db.run("DELETE FROM main.reg WHERE id IN (" + stmt + ")", ids); + }).then(function(result){ + return next + ? self.remove(next) + : result; + }); +}; + +SqliteDB.prototype.promisfy = function(opt){ + const db = this.db; + return new Promise(function(resolve, reject){ + db[opt.method](opt.stmt, opt.params || [], function(err, rows){ + opt.callback && opt.callback(rows); + err ? reject(err) + : resolve(rows); + }); + }); }; module.exports = SqliteDB; diff --git a/dist/flexsearch.bundle.debug.js b/dist/flexsearch.bundle.debug.js index fb3ad32..1e9d38f 100644 --- a/dist/flexsearch.bundle.debug.js +++ b/dist/flexsearch.bundle.debug.js @@ -1,5 +1,5 @@ /**! - * FlexSearch.js v0.8.167 (Bundle/Debug) + * FlexSearch.js v0.8.200 (Bundle/Debug) * Author and Copyright: Thomas Wilkerling * Licence: Apache-2.0 * Hosted by Nextapps GmbH @@ -7,30 +7,30 @@ */ (function _f(self){'use strict';if(typeof module!=='undefined')self=module;else if(typeof process !== 'undefined')self=process;self._factory=_f; var w; -function E(a, b, c) { - const d = typeof c, e = typeof a; - if ("undefined" !== d) { - if ("undefined" !== e) { - if (c) { - if ("function" === e && d === e) { +function H(a, c, b) { + const e = typeof b, d = typeof a; + if ("undefined" !== e) { + if ("undefined" !== d) { + if (b) { + if ("function" === d && e === d) { return function(h) { - return a(c(h)); + return a(b(h)); }; } - b = a.constructor; - if (b === c.constructor) { - if (b === Array) { - return c.concat(a); + c = a.constructor; + if (c === b.constructor) { + if (c === Array) { + return b.concat(a); } - if (b === Map) { - var f = new Map(c); + if (c === Map) { + var f = new Map(b); for (var g of a) { f.set(g[0], g[1]); } return f; } - if (b === Set) { - g = new Set(c); + if (c === Set) { + g = new Set(b); for (f of a.values()) { g.add(f); } @@ -40,50 +40,43 @@ function E(a, b, c) { } return a; } - return c; + return b; } - return "undefined" === e ? b : a; + return "undefined" === d ? c : a; } -function aa(a, b) { - return "undefined" === typeof a ? b : a; +function aa(a, c) { + return "undefined" === typeof a ? c : a; } -function G() { +function I() { return Object.create(null); } -function M(a) { +function N(a) { return "string" === typeof a; } -function da(a) { +function ba(a) { return "object" === typeof a; } -function ea(a) { - const b = []; - for (const c of a.keys()) { - b.push(c); +function ca(a) { + const c = []; + for (const b of a.keys()) { + c.push(b); } - return b; + return c; } -function fa(a, b) { - if (M(b)) { - a = a[b]; +function da(a, c) { + if (N(c)) { + a = a[c]; } else { - for (let c = 0; a && c < b.length; c++) { - a = a[b[c]]; + for (let b = 0; a && b < c.length; b++) { + a = a[c[b]]; } } return a; } -function ha(a) { - let b = 0; - for (let c = 0, d; c < a.length; c++) { - (d = a[c]) && b < d.length && (b = d.length); - } - return b; -} -;const ia = /[^\p{L}\p{N}]+/u, ja = /(\d{3})/g, ka = /(\D)(\d{3})/g, la = /(\d{3})(\D)/g, ma = /[\u0300-\u036f]/g; -function na(a = {}) { - if (!this || this.constructor !== na) { - return new na(...arguments); +;const fa = /[^\p{L}\p{N}]+/u, ha = /(\d{3})/g, ia = /(\D)(\d{3})/g, ja = /(\d{3})(\D)/g, ka = /[\u0300-\u036f]/g; +function la(a = {}) { + if (!this || this.constructor !== la) { + return new la(...arguments); } if (arguments.length) { for (a = 0; a < arguments.length; a++) { @@ -93,468 +86,313 @@ function na(a = {}) { this.assign(a); } } -w = na.prototype; +w = la.prototype; w.assign = function(a) { - this.normalize = E(a.normalize, !0, this.normalize); - let b = a.include, c = b || a.exclude || a.split, d; - if (c || "" === c) { - if ("object" === typeof c && c.constructor !== RegExp) { - let e = ""; - d = !b; - b || (e += "\\p{Z}"); - c.letter && (e += "\\p{L}"); - c.number && (e += "\\p{N}", d = !!b); - c.symbol && (e += "\\p{S}"); - c.punctuation && (e += "\\p{P}"); - c.control && (e += "\\p{C}"); - if (c = c.char) { - e += "object" === typeof c ? c.join("") : c; + this.normalize = H(a.normalize, !0, this.normalize); + let c = a.include, b = c || a.exclude || a.split, e; + if (b || "" === b) { + if ("object" === typeof b && b.constructor !== RegExp) { + let d = ""; + e = !c; + c || (d += "\\p{Z}"); + b.letter && (d += "\\p{L}"); + b.number && (d += "\\p{N}", e = !!c); + b.symbol && (d += "\\p{S}"); + b.punctuation && (d += "\\p{P}"); + b.control && (d += "\\p{C}"); + if (b = b.char) { + d += "object" === typeof b ? b.join("") : b; } try { - this.split = new RegExp("[" + (b ? "^" : "") + e + "]+", "u"); + this.split = new RegExp("[" + (c ? "^" : "") + d + "]+", "u"); } catch (f) { - console.error("Your split configuration:", c, "is not supported on this platform. It falls back to using simple whitespace splitter instead: /s+/."), this.split = /\s+/; + console.error("Your split configuration:", b, "is not supported on this platform. It falls back to using simple whitespace splitter instead: /s+/."), this.split = /\s+/; } } else { - this.split = c, d = !1 === c || 2 > "a1a".split(c).length; + this.split = b, e = !1 === b || 2 > "a1a".split(b).length; } - this.numeric = E(a.numeric, d); + this.numeric = H(a.numeric, e); } else { try { - this.split = E(this.split, ia); - } catch (e) { + this.split = H(this.split, fa); + } catch (d) { console.warn("This platform does not support unicode regex. It falls back to using simple whitespace splitter instead: /s+/."), this.split = /\s+/; } - this.numeric = E(a.numeric, E(this.numeric, !0)); + this.numeric = H(a.numeric, H(this.numeric, !0)); } - this.prepare = E(a.prepare, null, this.prepare); - this.finalize = E(a.finalize, null, this.finalize); - c = a.filter; - this.filter = "function" === typeof c ? c : E(c && new Set(c), null, this.filter); - this.dedupe = E(a.dedupe, !0, this.dedupe); - this.matcher = E((c = a.matcher) && new Map(c), null, this.matcher); - this.mapper = E((c = a.mapper) && new Map(c), null, this.mapper); - this.stemmer = E((c = a.stemmer) && new Map(c), null, this.stemmer); - this.replacer = E(a.replacer, null, this.replacer); - this.minlength = E(a.minlength, 1, this.minlength); - this.maxlength = E(a.maxlength, 1024, this.maxlength); - this.rtl = E(a.rtl, !1, this.rtl); - if (this.cache = c = E(a.cache, !0, this.cache)) { - this.H = null, this.S = "number" === typeof c ? c : 2e5, this.B = new Map(), this.G = new Map(), this.L = this.K = 128; + this.prepare = H(a.prepare, null, this.prepare); + this.finalize = H(a.finalize, null, this.finalize); + b = a.filter; + this.filter = "function" === typeof b ? b : H(b && new Set(b), null, this.filter); + this.dedupe = H(a.dedupe, !0, this.dedupe); + this.matcher = H((b = a.matcher) && new Map(b), null, this.matcher); + this.mapper = H((b = a.mapper) && new Map(b), null, this.mapper); + this.stemmer = H((b = a.stemmer) && new Map(b), null, this.stemmer); + this.replacer = H(a.replacer, null, this.replacer); + this.minlength = H(a.minlength, 1, this.minlength); + this.maxlength = H(a.maxlength, 1024, this.maxlength); + this.rtl = H(a.rtl, !1, this.rtl); + if (this.cache = b = H(a.cache, !0, this.cache)) { + this.I = null, this.R = "number" === typeof b ? b : 2e5, this.B = new Map(), this.H = new Map(), this.M = this.L = 128; } this.h = ""; - this.M = null; - this.A = ""; this.N = null; + this.A = ""; + this.O = null; if (this.matcher) { - for (const e of this.matcher.keys()) { - this.h += (this.h ? "|" : "") + e; + for (const d of this.matcher.keys()) { + this.h += (this.h ? "|" : "") + d; } } if (this.stemmer) { - for (const e of this.stemmer.keys()) { - this.A += (this.A ? "|" : "") + e; + for (const d of this.stemmer.keys()) { + this.A += (this.A ? "|" : "") + d; } } return this; }; -w.addStemmer = function(a, b) { +w.addStemmer = function(a, c) { this.stemmer || (this.stemmer = new Map()); - this.stemmer.set(a, b); + this.stemmer.set(a, c); this.A += (this.A ? "|" : "") + a; - this.N = null; - this.cache && N(this); + this.O = null; + this.cache && Q(this); return this; }; w.addFilter = function(a) { "function" === typeof a ? this.filter = a : (this.filter || (this.filter = new Set()), this.filter.add(a)); - this.cache && N(this); + this.cache && Q(this); return this; }; -w.addMapper = function(a, b) { +w.addMapper = function(a, c) { if ("object" === typeof a) { - return this.addReplacer(a, b); + return this.addReplacer(a, c); } if (1 < a.length) { - return this.addMatcher(a, b); + return this.addMatcher(a, c); } this.mapper || (this.mapper = new Map()); - this.mapper.set(a, b); - this.cache && N(this); + this.mapper.set(a, c); + this.cache && Q(this); return this; }; -w.addMatcher = function(a, b) { +w.addMatcher = function(a, c) { if ("object" === typeof a) { - return this.addReplacer(a, b); + return this.addReplacer(a, c); } if (2 > a.length && (this.dedupe || this.mapper)) { - return this.addMapper(a, b); + return this.addMapper(a, c); } this.matcher || (this.matcher = new Map()); - this.matcher.set(a, b); + this.matcher.set(a, c); this.h += (this.h ? "|" : "") + a; - this.M = null; - this.cache && N(this); + this.N = null; + this.cache && Q(this); return this; }; -w.addReplacer = function(a, b) { +w.addReplacer = function(a, c) { if ("string" === typeof a) { - return this.addMatcher(a, b); + return this.addMatcher(a, c); } this.replacer || (this.replacer = []); - this.replacer.push(a, b); - this.cache && N(this); + this.replacer.push(a, c); + this.cache && Q(this); return this; }; -w.encode = function(a, b) { - if (this.cache && a.length <= this.K) { - if (this.H) { +w.encode = function(a, c) { + if (this.cache && a.length <= this.L) { + if (this.I) { if (this.B.has(a)) { return this.B.get(a); } } else { - this.H = setTimeout(N, 50, this); + this.I = setTimeout(Q, 50, this); } } - this.normalize && ("function" === typeof this.normalize ? a = this.normalize(a) : a = ma ? a.normalize("NFKD").replace(ma, "").toLowerCase() : a.toLowerCase()); + this.normalize && ("function" === typeof this.normalize ? a = this.normalize(a) : a = ka ? a.normalize("NFKD").replace(ka, "").toLowerCase() : a.toLowerCase()); this.prepare && (a = this.prepare(a)); - this.numeric && 3 < a.length && (a = a.replace(ka, "$1 $2").replace(la, "$1 $2").replace(ja, "$1 ")); - const c = !(this.dedupe || this.mapper || this.filter || this.matcher || this.stemmer || this.replacer); - let d = [], e = G(), f, g, h = this.split || "" === this.split ? a.split(this.split) : [a]; - for (let l = 0, m, p; l < h.length; l++) { - if ((m = p = h[l]) && !(m.length < this.minlength || m.length > this.maxlength)) { - if (b) { - if (e[m]) { - continue; - } - e[m] = 1; - } else { - if (f === m) { - continue; - } - f = m; - } + this.numeric && 3 < a.length && (a = a.replace(ia, "$1 $2").replace(ja, "$1 $2").replace(ha, "$1 ")); + const b = !(this.dedupe || this.mapper || this.filter || this.matcher || this.stemmer || this.replacer); + let e = [], d = I(), f, g, h = this.split || "" === this.split ? a.split(this.split) : [a]; + for (let m = 0, l, n; m < h.length; m++) { + if ((l = n = h[m]) && !(l.length < this.minlength || l.length > this.maxlength)) { if (c) { - d.push(m); + if (d[l]) { + continue; + } + d[l] = 1; } else { - if (!this.filter || ("function" === typeof this.filter ? this.filter(m) : !this.filter.has(m))) { - if (this.cache && m.length <= this.L) { - if (this.H) { - var k = this.G.get(m); + if (f === l) { + continue; + } + f = l; + } + if (b) { + e.push(l); + } else { + if (!this.filter || ("function" === typeof this.filter ? this.filter(l) : !this.filter.has(l))) { + if (this.cache && l.length <= this.M) { + if (this.I) { + var k = this.H.get(l); if (k || "" === k) { - k && d.push(k); + k && e.push(k); continue; } } else { - this.H = setTimeout(N, 50, this); + this.I = setTimeout(Q, 50, this); } } if (this.stemmer) { - this.N || (this.N = new RegExp("(?!^)(" + this.A + ")$")); + this.O || (this.O = new RegExp("(?!^)(" + this.A + ")$")); let u; - for (; u !== m && 2 < m.length;) { - u = m, m = m.replace(this.N, q => this.stemmer.get(q)); + for (; u !== l && 2 < l.length;) { + u = l, l = l.replace(this.O, p => this.stemmer.get(p)); } } - if (m && (this.mapper || this.dedupe && 1 < m.length)) { + if (l && (this.mapper || this.dedupe && 1 < l.length)) { k = ""; - for (let u = 0, q = "", r, n; u < m.length; u++) { - r = m.charAt(u), r === q && this.dedupe || ((n = this.mapper && this.mapper.get(r)) || "" === n ? n === q && this.dedupe || !(q = n) || (k += n) : k += q = r); + for (let u = 0, p = "", t, r; u < l.length; u++) { + t = l.charAt(u), t === p && this.dedupe || ((r = this.mapper && this.mapper.get(t)) || "" === r ? r === p && this.dedupe || !(p = r) || (k += r) : k += p = t); } - m = k; + l = k; } - this.matcher && 1 < m.length && (this.M || (this.M = new RegExp("(" + this.h + ")", "g")), m = m.replace(this.M, u => this.matcher.get(u))); - if (m && this.replacer) { - for (k = 0; m && k < this.replacer.length; k += 2) { - m = m.replace(this.replacer[k], this.replacer[k + 1]); + this.matcher && 1 < l.length && (this.N || (this.N = new RegExp("(" + this.h + ")", "g")), l = l.replace(this.N, u => this.matcher.get(u))); + if (l && this.replacer) { + for (k = 0; l && k < this.replacer.length; k += 2) { + l = l.replace(this.replacer[k], this.replacer[k + 1]); } } - this.cache && p.length <= this.L && (this.G.set(p, m), this.G.size > this.S && (this.G.clear(), this.L = this.L / 1.1 | 0)); - if (m) { - if (m !== p) { - if (b) { - if (e[m]) { + this.cache && n.length <= this.M && (this.H.set(n, l), this.H.size > this.R && (this.H.clear(), this.M = this.M / 1.1 | 0)); + if (l) { + if (l !== n) { + if (c) { + if (d[l]) { continue; } - e[m] = 1; + d[l] = 1; } else { - if (g === m) { + if (g === l) { continue; } - g = m; + g = l; } } - d.push(m); + e.push(l); } } } } } - this.finalize && (d = this.finalize(d) || d); - this.cache && a.length <= this.K && (this.B.set(a, d), this.B.size > this.S && (this.B.clear(), this.K = this.K / 1.1 | 0)); - return d; + this.finalize && (e = this.finalize(e) || e); + this.cache && a.length <= this.L && (this.B.set(a, e), this.B.size > this.R && (this.B.clear(), this.L = this.L / 1.1 | 0)); + return e; }; -function N(a) { - a.H = null; +function Q(a) { + a.I = null; a.B.clear(); - a.G.clear(); + a.H.clear(); } -;let qa, O; -async function ra(a) { - a = a.data; - var b = a.task; - const c = a.id; - let d = a.args; - switch(b) { - case "init": - O = a.options || {}; - (b = a.factory) ? (Function("return " + b)()(self), qa = new self.FlexSearch.Index(O), delete self.FlexSearch) : qa = new P(O); - postMessage({id:c}); - break; - default: - let e; - if ("export" === b) { - if (!O.export || "function" !== typeof O.export) { - throw Error('Either no extern configuration provided for the Worker-Index or no method was defined on the config property "export".'); - } - d[1] ? (d[0] = O.export, d[2] = 0, d[3] = 1) : d = null; - } - if ("import" === b) { - if (!O.import || "function" !== typeof O.import) { - throw Error('Either no extern configuration provided for the Worker-Index or no method was defined on the config property "import".'); - } - d[0] && (a = await O.import.call(qa, d[0]), qa.import(d[0], a)); - } else { - (e = d && qa[b].apply(qa, d)) && e.then && (e = await e); - } - postMessage("search" === b ? {id:c, msg:e} : {id:c}); +;function ma(a, c, b) { + b || (c || "object" !== typeof a ? "object" === typeof c && (b = c, c = 0) : b = a); + b && (a = b.query || a, c = b.limit || c); + let e = "" + (c || 0); + b && (e += (b.offset || 0) + !!b.context + !!b.suggest + (!1 !== b.resolve) + (b.resolution || this.resolution) + (b.boost || 0)); + a = ("" + a).toLowerCase(); + this.cache || (this.cache = new na()); + let d = this.cache.get(a + e); + if (!d) { + const f = b && b.cache; + f && (b.cache = !1); + d = this.search(a, c, b); + f && (b.cache = f); + this.cache.set(a + e, d); } + return d; } -;function sa(a) { - ta.call(a, "add"); - ta.call(a, "append"); - ta.call(a, "search"); - ta.call(a, "update"); - ta.call(a, "remove"); - ta.call(a, "searchCache"); +function na(a) { + this.limit = a && !0 !== a ? a : 1000; + this.cache = new Map(); + this.h = ""; } -let ua, va, ya; -function za() { - ua = ya = 0; -} -function ta(a) { - this[a + "Async"] = function() { - const b = arguments; - var c = b[b.length - 1]; - let d; - "function" === typeof c && (d = c, delete b[b.length - 1]); - ua ? ya || (ya = Date.now() - va >= this.priority * this.priority * 3) : (ua = setTimeout(za, 0), va = Date.now()); - if (ya) { - const f = this; - return new Promise(g => { - setTimeout(function() { - g(f[a + "Async"].apply(f, b)); - }, 0); - }); - } - const e = this[a].apply(this, b); - c = e.then ? e : new Promise(f => f(e)); - d && c.then(d); - return c; - }; -} -;let Aa = 0; -function Ba(a = {}, b) { - function c(h) { - function k(l) { - l = l.data || l; - const m = l.id, p = m && f.h[m]; - p && (p(l.msg), delete f.h[m]); - } - this.worker = h; - this.h = G(); - if (this.worker) { - e ? this.worker.on("message", k) : this.worker.onmessage = k; - if (a.config) { - return new Promise(function(l) { - f.h[++Aa] = function() { - l(f); - 1e9 < Aa && (Aa = 0); - }; - f.worker.postMessage({id:Aa, task:"init", factory:d, options:a}); - }); - } - this.priority = a.priority || 4; - this.encoder = b || null; - this.worker.postMessage({task:"init", factory:d, options:a}); - return this; - } - } - if (!this || this.constructor !== Ba) { - return new Ba(a); - } - let d = "undefined" !== typeof self ? self._factory : "undefined" !== typeof window ? window._factory : null; - d && (d = d.toString()); - const e = "undefined" === typeof window, f = this, g = Ca(d, e, a.worker); - return g.then ? g.then(function(h) { - return c.call(f, h); - }) : c.call(this, g); -} -R("add"); -R("append"); -R("search"); -R("searchCache"); -R("update"); -R("remove"); -R("clear"); -R("export"); -R("import"); -sa(Ba.prototype); -function R(a) { - Ba.prototype[a] = function() { - const b = this, c = [].slice.call(arguments); - var d = c[c.length - 1]; - let e; - "function" === typeof d && (e = d, c.pop()); - d = new Promise(function(f) { - "export" === a && "function" === typeof c[0] && (c[0] = null); - b.h[++Aa] = f; - b.worker.postMessage({task:a, id:Aa, args:c}); - }); - return e ? (d.then(e), this) : d; - }; -} -function Ca(a, b, c) { - return b ? "undefined" !== typeof module ? new(require("worker_threads")["Worker"])(__dirname+"/node/node.js") : import("worker_threads").then(function(worker){return new worker["Worker"]((1,eval)("import.meta.dirname")+"/node/node.mjs")}) : a ? new window.Worker(URL.createObjectURL(new Blob(["onmessage=" + ra.toString()], {type:"text/javascript"}))) : new window.Worker("string" === typeof c ? c : (0,eval)("import.meta.url").replace("/worker.js", "/worker/worker.js").replace("flexsearch.bundle.module.min.js", - "module/worker/worker.js"), {type:"module"}); -} -;function Da(a, b = 0) { - let c = [], d = []; - b && (b = 250000 / b * 5000 | 0); - for (const e of a.entries()) { - d.push(e), d.length === b && (c.push(d), d = []); - } - d.length && c.push(d); +na.prototype.set = function(a, c) { + this.cache.set(this.h = a, c); + this.cache.size > this.limit && this.cache.delete(this.cache.keys().next().value); +}; +na.prototype.get = function(a) { + const c = this.cache.get(a); + c && this.h !== a && (this.cache.delete(a), this.cache.set(this.h = a, c)); return c; -} -function Ea(a, b) { - b || (b = new Map()); - for (let c = 0, d; c < a.length; c++) { - d = a[c], b.set(d[0], d[1]); +}; +na.prototype.remove = function(a) { + for (const c of this.cache) { + const b = c[0]; + c[1].includes(a) && this.cache.delete(b); } - return b; -} -function Fa(a, b = 0) { - let c = [], d = []; - b && (b = 250000 / b * 1000 | 0); - for (const e of a.entries()) { - d.push([e[0], Da(e[1])[0]]), d.length === b && (c.push(d), d = []); - } - d.length && c.push(d); - return c; -} -function Ga(a, b) { - b || (b = new Map()); - for (let c = 0, d, e; c < a.length; c++) { - d = a[c], e = b.get(d[0]), b.set(d[0], Ea(d[1], e)); - } - return b; -} -function Ha(a) { - let b = [], c = []; - for (const d of a.keys()) { - c.push(d), 250000 === c.length && (b.push(c), c = []); - } - c.length && b.push(c); - return b; -} -function Ia(a, b) { - b || (b = new Set()); - for (let c = 0; c < a.length; c++) { - b.add(a[c]); - } - return b; -} -function Ja(a, b, c, d, e, f, g = 0) { - const h = d && d.constructor === Array; - var k = h ? d.shift() : d; - if (!k) { - return this.export(a, b, e, f + 1); - } - if ((k = a((b ? b + "." : "") + (g + 1) + "." + c, JSON.stringify(k))) && k.then) { - const l = this; - return k.then(function() { - return Ja.call(l, a, b, c, h ? d : null, e, f, g + 1); - }); - } - return Ja.call(this, a, b, c, h ? d : null, e, f, g + 1); -} -function Ka(a, b) { - let c = ""; - for (const d of a.entries()) { - a = d[0]; - const e = d[1]; - let f = ""; - for (let g = 0, h; g < e.length; g++) { - h = e[g] || [""]; - let k = ""; - for (let l = 0; l < h.length; l++) { - k += (k ? "," : "") + ("string" === b ? '"' + h[l] + '"' : h[l]); - } - k = "[" + k + "]"; - f += (f ? "," : "") + k; +}; +na.prototype.clear = function() { + this.cache.clear(); + this.h = ""; +}; +const qa = {normalize:!1, numeric:!1, dedupe:!1}; +const ra = {}; +const sa = new Map([["b", "p"], ["v", "f"], ["w", "f"], ["z", "s"], ["x", "s"], ["d", "t"], ["n", "m"], ["c", "k"], ["g", "k"], ["j", "k"], ["q", "k"], ["i", "e"], ["y", "e"], ["u", "o"]]); +const ta = new Map([["ae", "a"], ["oe", "o"], ["sh", "s"], ["kh", "k"], ["th", "t"], ["ph", "f"], ["pf", "f"]]), ua = [/([^aeo])h(.)/g, "$1$2", /([aeo])h([^aeo]|$)/g, "$1$2", /(.)\1+/g, "$1"]; +const va = {a:"", e:"", i:"", o:"", u:"", y:"", b:1, f:1, p:1, v:1, c:2, g:2, j:2, k:2, q:2, s:2, x:2, z:2, "\u00df":2, d:3, t:3, l:4, m:5, n:5, r:6}; +var wa = {Exact:qa, Default:ra, Normalize:ra, LatinBalance:{mapper:sa}, LatinAdvanced:{mapper:sa, matcher:ta, replacer:ua}, LatinExtra:{mapper:sa, replacer:ua.concat([/(?!^)[aeo]/g, ""]), matcher:ta}, LatinSoundex:{dedupe:!1, include:{letter:!0}, finalize:function(a) { + for (let b = 0; b < a.length; b++) { + var c = a[b]; + let e = c.charAt(0), d = va[e]; + for (let f = 1, g; f < c.length && (g = c.charAt(f), "h" === g || "w" === g || !(g = va[g]) || g === d || (e += g, d = g, 4 !== e.length)); f++) { } - f = '["' + a + '",[' + f + "]]"; - c += (c ? "," : "") + f; + a[b] = e; } - return c; -} -;function La(a, b, c, d) { - let e = []; +}}, CJK:{split:""}, LatinExact:qa, LatinDefault:ra, LatinSimple:ra}; +function za(a, c, b, e) { + let d = []; for (let f = 0, g; f < a.index.length; f++) { - if (g = a.index[f], b >= g.length) { - b -= g.length; + if (g = a.index[f], c >= g.length) { + c -= g.length; } else { - b = g[d ? "splice" : "slice"](b, c); - const h = b.length; - if (h && (e = e.length ? e.concat(b) : b, c -= h, d && (a.length -= h), !c)) { + c = g[e ? "splice" : "slice"](c, b); + const h = c.length; + if (h && (d = d.length ? d.concat(c) : c, b -= h, e && (a.length -= h), !b)) { break; } - b = 0; + c = 0; } } - return e; + return d; } -function U(a) { - if (!this || this.constructor !== U) { - return new U(a); +function Aa(a) { + if (!this || this.constructor !== Aa) { + return new Aa(a); } this.index = a ? [a] : []; this.length = a ? a.length : 0; - const b = this; - return new Proxy([], {get(c, d) { - if ("length" === d) { - return b.length; + const c = this; + return new Proxy([], {get(b, e) { + if ("length" === e) { + return c.length; } - if ("push" === d) { - return function(e) { - b.index[b.index.length - 1].push(e); - b.length++; + if ("push" === e) { + return function(d) { + c.index[c.index.length - 1].push(d); + c.length++; }; } - if ("pop" === d) { + if ("pop" === e) { return function() { - if (b.length) { - return b.length--, b.index[b.index.length - 1].pop(); + if (c.length) { + return c.length--, c.index[c.index.length - 1].pop(); } }; } - if ("indexOf" === d) { - return function(e) { + if ("indexOf" === e) { + return function(d) { let f = 0; - for (let g = 0, h, k; g < b.index.length; g++) { - h = b.index[g]; - k = h.indexOf(e); + for (let g = 0, h, k; g < c.index.length; g++) { + h = c.index[g]; + k = h.indexOf(d); if (0 <= k) { return f + k; } @@ -563,812 +401,767 @@ function U(a) { return -1; }; } - if ("includes" === d) { - return function(e) { - for (let f = 0; f < b.index.length; f++) { - if (b.index[f].includes(e)) { + if ("includes" === e) { + return function(d) { + for (let f = 0; f < c.index.length; f++) { + if (c.index[f].includes(d)) { return !0; } } return !1; }; } - if ("slice" === d) { - return function(e, f) { - return La(b, e || 0, f || b.length, !1); + if ("slice" === e) { + return function(d, f) { + return za(c, d || 0, f || c.length, !1); }; } - if ("splice" === d) { - return function(e, f) { - return La(b, e || 0, f || b.length, !0); + if ("splice" === e) { + return function(d, f) { + return za(c, d || 0, f || c.length, !0); }; } - if ("constructor" === d) { + if ("constructor" === e) { return Array; } - if ("symbol" !== typeof d) { - return (c = b.index[d / 2 ** 31 | 0]) && c[d]; + if ("symbol" !== typeof e) { + return (b = c.index[e / 2 ** 31 | 0]) && b[e]; } - }, set(c, d, e) { - c = d / 2 ** 31 | 0; - (b.index[c] || (b.index[c] = []))[d] = e; - b.length++; + }, set(b, e, d) { + b = e / 2 ** 31 | 0; + (c.index[b] || (c.index[b] = []))[e] = d; + c.length++; return !0; }}); } -U.prototype.clear = function() { +Aa.prototype.clear = function() { this.index.length = 0; }; -U.prototype.destroy = function() { +Aa.prototype.destroy = function() { this.proxy = this.index = null; }; -U.prototype.push = function() { +Aa.prototype.push = function() { }; -function V(a = 8) { - if (!this || this.constructor !== V) { - return new V(a); +function R(a = 8) { + if (!this || this.constructor !== R) { + return new R(a); } - this.index = G(); + this.index = I(); this.h = []; this.size = 0; - 32 < a ? (this.B = Oa, this.A = BigInt(a)) : (this.B = Pa, this.A = a); + 32 < a ? (this.B = Ba, this.A = BigInt(a)) : (this.B = Ca, this.A = a); } -V.prototype.get = function(a) { - const b = this.index[this.B(a)]; - return b && b.get(a); +R.prototype.get = function(a) { + const c = this.index[this.B(a)]; + return c && c.get(a); }; -V.prototype.set = function(a, b) { - var c = this.B(a); - let d = this.index[c]; - d ? (c = d.size, d.set(a, b), (c -= d.size) && this.size++) : (this.index[c] = d = new Map([[a, b]]), this.h.push(d), this.size++); -}; -function W(a = 8) { - if (!this || this.constructor !== W) { - return new W(a); - } - this.index = G(); - this.h = []; - this.size = 0; - 32 < a ? (this.B = Oa, this.A = BigInt(a)) : (this.B = Pa, this.A = a); -} -W.prototype.add = function(a) { +R.prototype.set = function(a, c) { var b = this.B(a); - let c = this.index[b]; - c ? (b = c.size, c.add(a), (b -= c.size) && this.size++) : (this.index[b] = c = new Set([a]), this.h.push(c), this.size++); + let e = this.index[b]; + e ? (b = e.size, e.set(a, c), (b -= e.size) && this.size++) : (this.index[b] = e = new Map([[a, c]]), this.h.push(e), this.size++); }; -w = V.prototype; -w.has = W.prototype.has = function(a) { - const b = this.index[this.B(a)]; - return b && b.has(a); +function S(a = 8) { + if (!this || this.constructor !== S) { + return new S(a); + } + this.index = I(); + this.h = []; + this.size = 0; + 32 < a ? (this.B = Ba, this.A = BigInt(a)) : (this.B = Ca, this.A = a); +} +S.prototype.add = function(a) { + var c = this.B(a); + let b = this.index[c]; + b ? (c = b.size, b.add(a), (c -= b.size) && this.size++) : (this.index[c] = b = new Set([a]), this.h.push(b), this.size++); }; -w.delete = W.prototype.delete = function(a) { - const b = this.index[this.B(a)]; - b && b.delete(a) && this.size--; +w = R.prototype; +w.has = S.prototype.has = function(a) { + const c = this.index[this.B(a)]; + return c && c.has(a); }; -w.clear = W.prototype.clear = function() { - this.index = G(); +w.delete = S.prototype.delete = function(a) { + const c = this.index[this.B(a)]; + c && c.delete(a) && this.size--; +}; +w.clear = S.prototype.clear = function() { + this.index = I(); this.h = []; this.size = 0; }; -w.values = W.prototype.values = function*() { +w.values = S.prototype.values = function*() { for (let a = 0; a < this.h.length; a++) { - for (let b of this.h[a].values()) { - yield b; + for (let c of this.h[a].values()) { + yield c; } } }; -w.keys = W.prototype.keys = function*() { +w.keys = S.prototype.keys = function*() { for (let a = 0; a < this.h.length; a++) { - for (let b of this.h[a].keys()) { - yield b; + for (let c of this.h[a].keys()) { + yield c; } } }; -w.entries = W.prototype.entries = function*() { +w.entries = S.prototype.entries = function*() { for (let a = 0; a < this.h.length; a++) { - for (let b of this.h[a].entries()) { - yield b; + for (let c of this.h[a].entries()) { + yield c; } } }; -function Pa(a) { - let b = 2 ** this.A - 1; +function Ca(a) { + let c = 2 ** this.A - 1; if ("number" == typeof a) { - return a & b; + return a & c; } - let c = 0, d = this.A + 1; - for (let e = 0; e < a.length; e++) { - c = (c * d ^ a.charCodeAt(e)) & b; + let b = 0, e = this.A + 1; + for (let d = 0; d < a.length; d++) { + b = (b * e ^ a.charCodeAt(d)) & c; } - return 32 === this.A ? c + 2 ** 31 : c; + return 32 === this.A ? b + 2 ** 31 : b; } -function Oa(a) { - let b = BigInt(2) ** this.A - BigInt(1); - var c = typeof a; - if ("bigint" === c) { - return a & b; +function Ba(a) { + let c = BigInt(2) ** this.A - BigInt(1); + var b = typeof a; + if ("bigint" === b) { + return a & c; } - if ("number" === c) { - return BigInt(a) & b; + if ("number" === b) { + return BigInt(a) & c; } - c = BigInt(0); - let d = this.A + BigInt(1); - for (let e = 0; e < a.length; e++) { - c = (c * d ^ BigInt(a.charCodeAt(e))) & b; + b = BigInt(0); + let e = this.A + BigInt(1); + for (let d = 0; d < a.length; d++) { + b = (b * e ^ BigInt(a.charCodeAt(d))) & c; } - return c; + return b; } -;Qa.prototype.add = function(a, b, c) { - da(a) && (b = a, a = fa(b, this.key)); - if (b && (a || 0 === a)) { - if (!c && this.reg.has(a)) { - return this.update(a, b); +;let Da, T; +async function Ea(a) { + a = a.data; + var c = a.task; + const b = a.id; + let e = a.args; + switch(c) { + case "init": + T = a.options || {}; + (c = a.factory) ? (Function("return " + c)()(self), Da = new self.FlexSearch.Index(T), delete self.FlexSearch) : Da = new V(T); + postMessage({id:b}); + break; + default: + let d; + if ("export" === c) { + if (!T.export || "function" !== typeof T.export) { + throw Error('Either no extern configuration provided for the Worker-Index or no method was defined on the config property "export".'); + } + e[1] ? (e[0] = T.export, e[2] = 0, e[3] = 1) : e = null; + } + if ("import" === c) { + if (!T.import || "function" !== typeof T.import) { + throw Error('Either no extern configuration provided for the Worker-Index or no method was defined on the config property "import".'); + } + e[0] && (a = await T.import.call(Da, e[0]), Da.import(e[0], a)); + } else { + (d = e && Da[c].apply(Da, e)) && d.then && (d = await d), d && d.await && (d = await d.await), "search" === c && d.result && (d = d.result); + } + postMessage("search" === c ? {id:b, msg:d} : {id:b}); + } +} +;function Fa(a) { + Ga.call(a, "add"); + Ga.call(a, "append"); + Ga.call(a, "search"); + Ga.call(a, "update"); + Ga.call(a, "remove"); + Ga.call(a, "searchCache"); +} +let Ha, Ia, Ja; +function Ka() { + Ha = Ja = 0; +} +function Ga(a) { + this[a + "Async"] = function() { + const c = arguments; + var b = c[c.length - 1]; + let e; + "function" === typeof b && (e = b, delete c[c.length - 1]); + Ha ? Ja || (Ja = Date.now() - Ia >= this.priority * this.priority * 3) : (Ha = setTimeout(Ka, 0), Ia = Date.now()); + if (Ja) { + const f = this; + return new Promise(g => { + setTimeout(function() { + g(f[a + "Async"].apply(f, c)); + }, 0); + }); + } + const d = this[a].apply(this, c); + b = d.then ? d : new Promise(f => f(d)); + e && b.then(e); + return b; + }; +} +;let W = 0; +function La(a = {}, c) { + function b(h) { + function k(m) { + m = m.data || m; + const l = m.id, n = l && f.h[l]; + n && (n(m.msg), delete f.h[l]); + } + this.worker = h; + this.h = I(); + if (this.worker) { + d ? this.worker.on("message", k) : this.worker.onmessage = k; + if (a.config) { + return new Promise(function(m) { + 1e9 < W && (W = 0); + f.h[++W] = function() { + m(f); + }; + f.worker.postMessage({id:W, task:"init", factory:e, options:a}); + }); + } + this.priority = a.priority || 4; + this.encoder = c || null; + this.worker.postMessage({task:"init", factory:e, options:a}); + return this; + } + console.warn("Worker is not available on this platform. Please report on Github: https://github.com/nextapps-de/flexsearch/issues"); + } + if (!this || this.constructor !== La) { + return new La(a); + } + let e = "undefined" !== typeof self ? self._factory : "undefined" !== typeof window ? window._factory : null; + e && (e = e.toString()); + const d = "undefined" === typeof window, f = this, g = Ma(e, d, a.worker); + return g.then ? g.then(function(h) { + return b.call(f, h); + }) : b.call(this, g); +} +X("add"); +X("append"); +X("search"); +X("update"); +X("remove"); +X("clear"); +X("export"); +X("import"); +La.prototype.searchCache = ma; +Fa(La.prototype); +function X(a) { + La.prototype[a] = function() { + const c = this, b = [].slice.call(arguments); + var e = b[b.length - 1]; + let d; + "function" === typeof e && (d = e, b.pop()); + e = new Promise(function(f) { + "export" === a && "function" === typeof b[0] && (b[0] = null); + 1e9 < W && (W = 0); + c.h[++W] = f; + c.worker.postMessage({task:a, id:W, args:b}); + }); + return d ? (e.then(d), this) : e; + }; +} +function Ma(a, c, b) { + return c ? "undefined" !== typeof module ? new(require("worker_threads")["Worker"])(__dirname+"/node/node.js") : import("worker_threads").then(function(worker){return new worker["Worker"]((1,eval)("import.meta.dirname")+"/node/node.mjs")}) : a ? new window.Worker(URL.createObjectURL(new Blob(["onmessage=" + Ea.toString()], {type:"text/javascript"}))) : new window.Worker("string" === typeof b ? b : (0,eval)("import.meta.url").replace("/worker.js", "/worker/worker.js").replace("flexsearch.bundle.module.min.js", + "module/worker/worker.js"), {type:"module"}); +} +;Na.prototype.add = function(a, c, b) { + ba(a) && (c = a, a = da(c, this.key)); + if (c && (a || 0 === a)) { + if (!b && this.reg.has(a)) { + return this.update(a, c); } for (let h = 0, k; h < this.field.length; h++) { - k = this.F[h]; - var d = this.index.get(this.field[h]); + k = this.G[h]; + var e = this.index.get(this.field[h]); if ("function" === typeof k) { - var e = k(b); - e && d.add(a, e, !1, !0); + var d = k(c); + d && e.add(a, d, !1, !0); } else { - if (e = k.I, !e || e(b)) { - k.constructor === String ? k = ["" + k] : M(k) && (k = [k]), Ra(b, k, this.J, 0, d, a, k[0], c); + if (d = k.J, !d || d(c)) { + k.constructor === String ? k = ["" + k] : N(k) && (k = [k]), Oa(c, k, this.K, 0, e, a, k[0], b); } } } if (this.tag) { - for (d = 0; d < this.D.length; d++) { - var f = this.D[d], g = this.R[d]; - e = this.tag.get(g); - let h = G(); + for (e = 0; e < this.F.length; e++) { + var f = this.F[e], g = this.P[e]; + d = this.tag.get(g); + let h = I(); if ("function" === typeof f) { - if (f = f(b), !f) { + if (f = f(c), !f) { continue; } } else { - const k = f.I; - if (k && !k(b)) { + const k = f.J; + if (k && !k(c)) { continue; } f.constructor === String && (f = "" + f); - f = fa(b, f); + f = da(c, f); } - if (e && f) { - M(f) && (f = [f]); - for (let k = 0, l, m; k < f.length; k++) { - if (l = f[k], !h[l] && (h[l] = 1, (g = e.get(l)) ? m = g : e.set(l, m = []), !c || !m.includes(a))) { - if (m.length === 2 ** 31 - 1) { - g = new U(m); + if (d && f) { + N(f) && (f = [f]); + for (let k = 0, m, l; k < f.length; k++) { + if (m = f[k], !h[m] && (h[m] = 1, (g = d.get(m)) ? l = g : d.set(m, l = []), !b || !l.includes(a))) { + if (l.length === 2 ** 31 - 1) { + g = new Aa(l); if (this.fastupdate) { - for (let p of this.reg.values()) { - p.includes(m) && (p[p.indexOf(m)] = g); + for (let n of this.reg.values()) { + n.includes(l) && (n[n.indexOf(l)] = g); } } - e.set(l, m = g); + d.set(m, l = g); } - m.push(a); - this.fastupdate && ((g = this.reg.get(a)) ? g.push(m) : this.reg.set(a, [m])); + l.push(a); + this.fastupdate && ((g = this.reg.get(a)) ? g.push(l) : this.reg.set(a, [l])); } } } else { - e || console.warn("Tag '" + g + "' was not found"); + d || console.warn("Tag '" + g + "' was not found"); } } } - if (this.store && (!c || !this.store.has(a))) { + if (this.store && (!b || !this.store.has(a))) { let h; - if (this.C) { - h = G(); - for (let k = 0, l; k < this.C.length; k++) { - l = this.C[k]; - if ((c = l.I) && !c(b)) { + if (this.D) { + h = I(); + for (let k = 0, m; k < this.D.length; k++) { + m = this.D[k]; + if ((b = m.J) && !b(c)) { continue; } - let m; - if ("function" === typeof l) { - m = l(b); - if (!m) { + let l; + if ("function" === typeof m) { + l = m(c); + if (!l) { continue; } - l = [l.V]; - } else if (M(l) || l.constructor === String) { - h[l] = b[l]; + m = [m.U]; + } else if (N(m) || m.constructor === String) { + h[m] = c[m]; continue; } - Sa(b, h, l, 0, l[0], m); + Ra(c, h, m, 0, m[0], l); } } - this.store.set(a, h || b); + this.store.set(a, h || c); } this.worker && (this.fastupdate || this.reg.add(a)); } return this; }; -function Sa(a, b, c, d, e, f) { - a = a[e]; - if (d === c.length - 1) { - b[e] = f || a; +function Ra(a, c, b, e, d, f) { + a = a[d]; + if (e === b.length - 1) { + c[d] = f || a; } else if (a) { if (a.constructor === Array) { - for (b = b[e] = Array(a.length), e = 0; e < a.length; e++) { - Sa(a, b, c, d, e); + for (c = c[d] = Array(a.length), d = 0; d < a.length; d++) { + Ra(a, c, b, e, d); } } else { - b = b[e] || (b[e] = G()), e = c[++d], Sa(a, b, c, d, e); + c = c[d] || (c[d] = I()), d = b[++e], Ra(a, c, b, e, d); } } } -function Ra(a, b, c, d, e, f, g, h) { +function Oa(a, c, b, e, d, f, g, h) { if (a = a[g]) { - if (d === b.length - 1) { + if (e === c.length - 1) { if (a.constructor === Array) { - if (c[d]) { - for (b = 0; b < a.length; b++) { - e.add(f, a[b], !0, !0); + if (b[e]) { + for (c = 0; c < a.length; c++) { + d.add(f, a[c], !0, !0); } return; } a = a.join(" "); } - e.add(f, a, h, !0); + d.add(f, a, h, !0); } else { if (a.constructor === Array) { for (g = 0; g < a.length; g++) { - Ra(a, b, c, d, e, f, g, h); + Oa(a, c, b, e, d, f, g, h); } } else { - g = b[++d], Ra(a, b, c, d, e, f, g, h); + g = c[++e], Oa(a, c, b, e, d, f, g, h); } } } else { - e.db && e.remove(f); + d.db && d.remove(f); } } -;function Ta(a, b, c, d) { +;function Sa(a, c, b, e) { if (!a.length) { return a; } if (1 === a.length) { - return a = a[0], a = c || a.length > b ? b ? a.slice(c, c + b) : a.slice(c) : a, d ? X.call(this, a) : a; + return a = a[0], a = b || a.length > c ? a.slice(b, b + c) : a, e ? Ta.call(this, a) : a; } - let e = []; + let d = []; for (let f = 0, g, h; f < a.length; f++) { if ((g = a[f]) && (h = g.length)) { - if (c) { - if (c >= h) { - c -= h; + if (b) { + if (b >= h) { + b -= h; continue; } - c < h && (g = b ? g.slice(c, c + b) : g.slice(c), h = g.length, c = 0); + g = g.slice(b, b + c); + h = g.length; + b = 0; } - h > b && (g = g.slice(0, b), h = b); - if (!e.length && h >= b) { - return d ? X.call(this, g) : g; + h > c && (g = g.slice(0, c), h = c); + if (!d.length && h >= c) { + return e ? Ta.call(this, g) : g; } - e.push(g); - b -= h; - if (!b) { + d.push(g); + c -= h; + if (!c) { break; } } } - e = 1 < e.length ? [].concat.apply([], e) : e[0]; - return d ? X.call(this, e) : e; + d = 1 < d.length ? [].concat.apply([], d) : d[0]; + return e ? Ta.call(this, d) : d; } -;function Ua(a, b, c) { - var d = c[0]; - if (d.then) { - return Promise.all(c).then(function(q) { - return a[b].apply(a, q); - }); +;function Ua(a, c, b, e) { + var d = e[0]; + if (d[0] && d[0].query) { + return a[c].apply(a, d); } - if (d[0] && d[0].index) { - return a[b].apply(a, d); + if (!("and" !== c && "not" !== c || a.result.length || a.await || d.suggest)) { + return 1 < e.length && (d = e[e.length - 1]), (e = d.resolve) ? a.await || a.result : a; } - d = []; - let e = [], f = 0, g = 0, h, k, l, m, p; - for (let q = 0, r; q < c.length; q++) { - if (r = c[q]) { - var u = void 0; - if (r.constructor === Y) { - u = r.result; - } else if (r.constructor === Array) { - u = r; + let f = [], g = 0, h = 0, k, m, l; + var n; + let u; + for (c = 0; c < e.length; c++) { + if (d = e[c]) { + var p = void 0; + if (d.constructor === Y) { + p = d.await || d.result; + } else if (d.then || d.constructor === Array) { + p = d; } else { - f = r.limit || 0; - g = r.offset || 0; - l = r.suggest; - k = r.resolve; - h = (m = r.highlight && k) || r.enrich && k; - let n; - r.index ? a.index = n = r.index : n = a.index; - if (r.query || r.tag) { + g = d.limit || 0; + h = d.offset || 0; + l = d.suggest; + m = d.resolve; + k = (n = d.highlight && m) || d.enrich && m; + p = d.queue; + let t = d.async || p; + (n = d.index) ? a.index || (a.index = n) : n = a.index; + if (d.query || d.tag) { if (!a.index) { throw Error("Resolver can't apply because the corresponding Index was never specified"); } - if (u = r.field || r.pluck) { + const r = d.field || d.pluck; + if (r) { if (!a.index.index) { throw Error("Resolver can't apply because the corresponding Document Index was not specified"); } - n = a.index.index.get(u); + n = a.index.index.get(r); if (!n) { - throw Error("Resolver can't apply because the specified Document Field '" + u + "' was not found"); + throw Error("Resolver can't apply because the specified Document Field '" + r + "' was not found"); } } - r.resolve = !1; - u = n.search(r).result; - r.resolve = k; - m && (p = r.query); - } else if (r.and) { - u = a.and(r.and); - } else if (r.or) { - u = a.or(r.or); - } else if (r.xor) { - u = a.xor(r.xor); - } else if (r.not) { - u = a.not(r.not); + if (p && (u || a.await)) { + u = 1; + let q; + const y = a.C.length, v = new Promise(function(A) { + q = A; + }); + (function(A, B) { + v.h = function() { + B.index = null; + B.resolve = !1; + let E = t ? A.searchAsync(B) : A.search(B); + if (E.then) { + return E.then(function(C) { + a.C[y] = C = C.result || C; + q(C); + return C; + }); + } + E = E.result || E; + q(E); + return E; + }; + })(n, Object.assign({}, d)); + a.C.push(v); + f[c] = v; + continue; + } else { + d.resolve = !1, d.index = null, p = t ? n.searchAsync(d) : n.search(d), d.resolve = m, d.index = n; + } + } else if (d.and) { + p = Va(d, "and", n); + } else if (d.or) { + p = Va(d, "or", n); + } else if (d.not) { + p = Va(d, "not", n); + } else if (d.xor) { + p = Va(d, "xor", n); } else { continue; } } - if (u.then) { - e.push(u); - } else if (u.length) { - d[q] = u; - } else if (!l && ("and" === b || "xor" === b)) { - d = []; - break; - } + p.await ? (u = 1, p = p.await) : p.then ? (u = 1, p = p.then(function(t) { + return t.result || t; + })) : p = p.result || p; + f[c] = p; } } - return {O:d, P:e, limit:f, offset:g, enrich:h, resolve:k, suggest:l, highlight:m, W:p}; -} -;Y.prototype.or = function() { - const {O:a, P:b, limit:c, offset:d, enrich:e, resolve:f} = Ua(this, "or", arguments); - return Va.call(this, a, b, c, d, e, f); -}; -function Va(a, b, c, d, e, f) { - if (b.length) { - const g = this; - return Promise.all(b).then(function(h) { - a = []; - for (let k = 0, l; k < h.length; k++) { - (l = h[k]).length && (a[k] = l); - } - return Va.call(g, a, [], c, d, e, f); - }); - } - a.length && (this.result.length && a.push(this.result), 2 > a.length ? this.result = a[0] : (this.result = Wa(a, c, d, !1, this.h), d = 0)); - return f ? this.resolve(c, d, e) : this; -} -;Y.prototype.and = function() { - let a = this.result.length, b, c, d, e; - if (!a) { - const f = arguments[0]; - f && (a = !!f.suggest, e = f.resolve, b = f.limit, c = f.offset, d = f.enrich && e); - } - if (a) { - const {O:f, P:g, limit:h, offset:k, enrich:l, resolve:m, suggest:p} = Ua(this, "and", arguments); - return Xa.call(this, f, g, h, k, l, m, p); - } - return e ? this.resolve(b, c, d) : this; -}; -function Xa(a, b, c, d, e, f, g) { - if (b.length) { - const h = this; - return Promise.all(b).then(function(k) { - a = []; - for (let l = 0, m; l < k.length; l++) { - (m = k[l]).length && (a[l] = m); - } - return Xa.call(h, a, [], c, d, e, f, g); - }); - } - if (a.length) { - if (this.result.length && a.unshift(this.result), 2 > a.length) { - this.result = a[0]; - } else { - if (b = ha(a)) { - return this.result = Ya(a, b, c, d, g, this.h, f), f ? e ? X.call(this.index, this.result) : this.result : this; - } - this.result = []; - } - } else { - g || (this.result = a); - } - return f ? this.resolve(c, d, e) : this; -} -;Y.prototype.xor = function() { - const {O:a, P:b, limit:c, offset:d, enrich:e, resolve:f, suggest:g} = Ua(this, "xor", arguments); - return Za.call(this, a, b, c, d, e, f, g); -}; -function Za(a, b, c, d, e, f, g) { - if (b.length) { - const h = this; - return Promise.all(b).then(function(k) { - a = []; - for (let l = 0, m; l < k.length; l++) { - (m = k[l]).length && (a[l] = m); - } - return Za.call(h, a, [], c, d, e, f, g); - }); - } - if (a.length) { - if (this.result.length && a.unshift(this.result), 2 > a.length) { - this.result = a[0]; - } else { - return this.result = $a.call(this, a, c, d, f, this.h), f ? e ? X.call(this.index, this.result) : this.result : this; - } - } else { - g || (this.result = a); - } - return f ? this.resolve(c, d, e) : this; -} -function $a(a, b, c, d, e) { - const f = [], g = G(); - let h = 0; - for (let k = 0, l; k < a.length; k++) { - if (l = a[k]) { - h < l.length && (h = l.length); - for (let m = 0, p; m < l.length; m++) { - if (p = l[m]) { - for (let u = 0, q; u < p.length; u++) { - q = p[u], g[q] = g[q] ? 2 : 1; - } - } - } - } - } - for (let k = 0, l, m = 0; k < h; k++) { - for (let p = 0, u; p < a.length; p++) { - if (u = a[p]) { - if (l = u[k]) { - for (let q = 0, r; q < l.length; q++) { - if (r = l[q], 1 === g[r]) { - if (c) { - c--; - } else { - if (d) { - if (f.push(r), f.length === b) { - return f; - } - } else { - const n = k + (p ? e : 0); - f[n] || (f[n] = []); - f[n].push(r); - if (++m === b) { - return f; - } - } - } - } - } - } - } - } - } - return f; -} -;Y.prototype.not = function() { - const {O:a, P:b, limit:c, offset:d, enrich:e, resolve:f, suggest:g} = Ua(this, "not", arguments); - return ab.call(this, a, b, c, d, e, f, g); -}; -function ab(a, b, c, d, e, f, g) { - if (b.length) { - const h = this; - return Promise.all(b).then(function(k) { - a = []; - for (let l = 0, m; l < k.length; l++) { - (m = k[l]).length && (a[l] = m); - } - return ab.call(h, a, [], c, d, e, f, g); - }); - } - if (a.length && this.result.length) { - this.result = bb.call(this, a, c, d, f); - } else if (f) { - return this.resolve(c, d, e); - } - return f ? e ? X.call(this.index, this.result) : this.result : this; -} -function bb(a, b, c, d) { - const e = []; - a = new Set(a.flat().flat()); - for (let f = 0, g, h = 0; f < this.result.length; f++) { - if (g = this.result[f]) { - for (let k = 0, l; k < g.length; k++) { - if (l = g[k], !a.has(l)) { - if (c) { - c--; - } else { - if (d) { - if (e.push(l), e.length === b) { - return e; - } - } else { - if (e[f] || (e[f] = []), e[f].push(l), ++h === b) { - return e; - } - } - } - } - } - } - } - return e; -} -;function cb(a, b, c, d, e) { - let f, g, h; - "string" === typeof e ? (f = e, e = "") : f = e.template; - if (!f) { - throw Error('No template pattern was specified by the search option "highlight"'); - } - g = f.indexOf("$1"); - if (-1 === g) { - throw Error('Invalid highlight template. The replacement pattern "$1" was not found in template: ' + f); - } - h = f.substring(g + 2); - g = f.substring(0, g); - let k = e && e.boundary, l = !e || !1 !== e.clip, m = e && e.merge && h && g && new RegExp(h + " " + g, "g"); - e = e && e.ellipsis; - var p = 0; - if ("object" === typeof e) { - var u = e.template; - p = u.length - 2; - e = e.pattern; - } - "string" !== typeof e && (e = !1 === e ? "" : "..."); - p && (e = u.replace("$1", e)); - u = e.length - p; - let q, r; - "object" === typeof k && (q = k.before, 0 === q && (q = -1), r = k.after, 0 === r && (r = -1), k = k.total || 9e5); - p = new Map(); - for (let Ma = 0, ba, ib, oa; Ma < b.length; Ma++) { - let pa; - if (d) { - pa = b, oa = d; - } else { - var n = b[Ma]; - oa = n.field; - if (!oa) { - continue; - } - pa = n.result; - } - ib = c.get(oa); - ba = ib.encoder; - n = p.get(ba); - "string" !== typeof n && (n = ba.encode(a), p.set(ba, n)); - for (let wa = 0; wa < pa.length; wa++) { - var t = pa[wa].doc; - if (!t) { - continue; - } - t = fa(t, oa); - if (!t) { - continue; - } - var x = t.trim().split(/\s+/); - if (!x.length) { - continue; - } - t = ""; - var y = []; - let xa = []; - var D = -1, H = -1, B = 0; - for (var v = 0; v < x.length; v++) { - var A = x[v], I = ba.encode(A); - I = 1 < I.length ? I.join(" ") : I[0]; - let z; - if (I && A) { - var C = A.length, J = (ba.split ? A.replace(ba.split, "") : A).length - I.length, F = "", S = 0; - for (var ca = 0; ca < n.length; ca++) { - var Q = n[ca]; - if (Q) { - var L = Q.length; - L += J; - S && L <= S || (Q = I.indexOf(Q), -1 < Q && (F = (Q ? A.substring(0, Q) : "") + g + A.substring(Q, Q + L) + h + (Q + L < C ? A.substring(Q + L) : ""), S = L, z = !0)); - } - } - F && (k && (0 > D && (D = t.length + (t ? 1 : 0)), H = t.length + (t ? 1 : 0) + F.length, B += C, xa.push(y.length), y.push({match:F})), t += (t ? " " : "") + F); - } - if (!z) { - A = x[v], t += (t ? " " : "") + A, k && y.push({text:A}); - } else if (k && B >= k) { + u && !a.await && (a.await = new Promise(function(t) { + a.return = t; + })); + if (u) { + const t = Promise.all(f).then(function(r) { + for (let q = 0; q < a.C.length; q++) { + if (a.C[q] === t) { + a.C[q] = function() { + return b.call(a, r, g, h, k, m, l); + }; break; } } - B = xa.length * (f.length - 2); - if (q || r || k && t.length - B > k) { - if (B = k + B - 2 * u, v = H - D, 0 < q && (v += q), 0 < r && (v += r), v <= B) { - x = q ? D - (0 < q ? q : 0) : D - ((B - v) / 2 | 0), y = r ? H + (0 < r ? r : 0) : x + B, l || (0 < x && " " !== t.charAt(x) && " " !== t.charAt(x - 1) && (x = t.indexOf(" ", x), 0 > x && (x = 0)), y < t.length && " " !== t.charAt(y - 1) && " " !== t.charAt(y) && (y = t.lastIndexOf(" ", y), y < H ? y = H : ++y)), t = (x ? e : "") + t.substring(x, y) + (y < t.length ? e : ""); - } else { - H = []; - D = {}; - B = {}; - v = {}; - A = {}; - I = {}; - F = J = C = 0; - for (ca = S = 1;;) { - var T = void 0; - for (let z = 0, K; z < xa.length; z++) { - K = xa[z]; - if (F) { - if (J !== F) { - if (v[z + 1]) { - continue; - } - K += F; - if (D[K]) { - C -= u; - B[z + 1] = 1; - v[z + 1] = 1; - continue; - } - if (K >= y.length - 1) { - if (K >= y.length) { - v[z + 1] = 1; - K >= x.length && (B[z + 1] = 1); - continue; - } - C -= u; - } - t = y[K].text; - if (L = r && I[z]) { - if (0 < L) { - if (t.length > L) { - if (v[z + 1] = 1, l) { - t = t.substring(0, L); - } else { - continue; - } - } - (L -= t.length) || (L = -1); - I[z] = L; - } else { - v[z + 1] = 1; - continue; - } - } - if (C + t.length + 1 <= k) { - t = " " + t, H[z] += t; - } else if (l) { - T = k - C - 1, 0 < T && (t = " " + t.substring(0, T), H[z] += t), v[z + 1] = 1; - } else { - v[z + 1] = 1; - continue; - } - } else { - if (v[z]) { - continue; - } - K -= J; - if (D[K]) { - C -= u; - v[z] = 1; - B[z] = 1; - continue; - } - if (0 >= K) { - if (0 > K) { - v[z] = 1; - B[z] = 1; - continue; - } - C -= u; - } - t = y[K].text; - if (L = q && A[z]) { - if (0 < L) { - if (t.length > L) { - if (v[z] = 1, l) { - t = t.substring(t.length - L); - } else { - continue; - } - } - (L -= t.length) || (L = -1); - A[z] = L; - } else { - v[z] = 1; - continue; - } - } - if (C + t.length + 1 <= k) { - t += " ", H[z] = t + H[z]; - } else if (l) { - T = t.length + 1 - (k - C), 0 <= T && T < t.length && (t = t.substring(T) + " ", H[z] = t + H[z]), v[z] = 1; - } else { - v[z] = 1; - continue; - } - } - } else { - t = y[K].match; - q && (A[z] = q); - r && (I[z] = r); - z && C++; - let Na; - K ? !z && u && (C += u) : (B[z] = 1, v[z] = 1); - K >= x.length - 1 ? Na = 1 : K < y.length - 1 && y[K + 1].match ? Na = 1 : u && (C += u); - C -= f.length - 2; - if (!z || C + t.length <= k) { - H[z] = t; - } else { - T = S = ca = B[z] = 0; - break; - } - Na && (B[z + 1] = 1, v[z + 1] = 1); - } - C += t.length; - T = D[K] = 1; - } - if (T) { - J === F ? F++ : J++; - } else { - J === F ? S = 0 : ca = 0; - if (!S && !ca) { - break; - } - S ? (J++, F = J) : F++; - } - } - t = ""; - for (let z = 0, K; z < H.length; z++) { - K = (z && B[z] ? " " : (z && !e ? " " : "") + e) + H[z], t += K; - } - e && !B[H.length] && (t += e); - } - } - m && (t = t.replace(m, " ")); - pa[wa].highlight = t; - } - if (d) { - break; - } + Wa(a); + }); + a.C.push(t); + } else if (a.await) { + a.C.push(function() { + return b.call(a, f, g, h, k, m, l); + }); + } else { + return b.call(a, f, g, h, k, m, l); } + return m ? a.await || a.result : a; +} +function Va(a, c, b) { + a = a[c]; + const e = a[0] || a; + e.index || (e.index = b); + b = new Y(e); + 1 < a.length && (b = b[c].apply(b, a.slice(1))); return b; } -;function Y(a, b) { +;Y.prototype.or = function() { + return Ua(this, "or", Xa, arguments); +}; +function Xa(a, c, b, e, d) { + a.length && (this.result.length && a.push(this.result), 2 > a.length ? this.result = a[0] : (this.result = Ya(a, c, b, !1, this.h), b = 0)); + d && (this.await = null); + return d ? this.resolve(c, b, e) : this; +} +;Y.prototype.and = function() { + return Ua(this, "and", Za, arguments); +}; +function Za(a, c, b, e, d, f) { + if (!f && !this.result.length) { + return d ? this.result : this; + } + let g; + if (a.length) { + if (this.result.length && a.unshift(this.result), 2 > a.length) { + this.result = a[0]; + } else { + let h = 0; + for (let k = 0, m, l; k < a.length; k++) { + if ((m = a[k]) && (l = m.length)) { + h < l && (h = l); + } else if (!f) { + h = 0; + break; + } + } + h ? (this.result = $a(a, h, c, b, f, this.h, d), g = !0) : this.result = []; + } + } else { + f || (this.result = a); + } + d && (this.await = null); + return d ? this.resolve(c, b, e, g) : this; +} +;Y.prototype.xor = function() { + return Ua(this, "xor", ab, arguments); +}; +function ab(a, c, b, e, d, f) { + if (a.length) { + if (this.result.length && a.unshift(this.result), 2 > a.length) { + this.result = a[0]; + } else { + a: { + f = b; + var g = this.h; + const h = [], k = I(); + let m = 0; + for (let l = 0, n; l < a.length; l++) { + if (n = a[l]) { + m < n.length && (m = n.length); + for (let u = 0, p; u < n.length; u++) { + if (p = n[u]) { + for (let t = 0, r; t < p.length; t++) { + r = p[t], k[r] = k[r] ? 2 : 1; + } + } + } + } + } + for (let l = 0, n, u = 0; l < m; l++) { + for (let p = 0, t; p < a.length; p++) { + if (t = a[p]) { + if (n = t[l]) { + for (let r = 0, q; r < n.length; r++) { + if (q = n[r], 1 === k[q]) { + if (f) { + f--; + } else { + if (d) { + if (h.push(q), h.length === c) { + a = h; + break a; + } + } else { + const y = l + (p ? g : 0); + h[y] || (h[y] = []); + h[y].push(q); + if (++u === c) { + a = h; + break a; + } + } + } + } + } + } + } + } + } + a = h; + } + this.result = a; + g = !0; + } + } else { + f || (this.result = a); + } + d && (this.await = null); + return d ? this.resolve(c, b, e, g) : this; +} +;Y.prototype.not = function() { + return Ua(this, "not", bb, arguments); +}; +function bb(a, c, b, e, d, f) { + if (!f && !this.result.length) { + return d ? this.result : this; + } + if (a.length && this.result.length) { + a: { + f = b; + var g = []; + a = new Set(a.flat().flat()); + for (let h = 0, k, m = 0; h < this.result.length; h++) { + if (k = this.result[h]) { + for (let l = 0, n; l < k.length; l++) { + if (n = k[l], !a.has(n)) { + if (f) { + f--; + } else { + if (d) { + if (g.push(n), g.length === c) { + a = g; + break a; + } + } else { + if (g[h] || (g[h] = []), g[h].push(n), ++m === c) { + a = g; + break a; + } + } + } + } + } + } + } + a = g; + } + this.result = a; + g = !0; + } + d && (this.await = null); + return d ? this.resolve(c, b, e, g) : this; +} +;function Y(a, c) { if (!this || this.constructor !== Y) { - return new Y(a, b); + return new Y(a, c); } + let b = 0, e, d, f; if (a && a.index) { - return a.resolve = !1, this.index = a.index, this.h = a.boost || 0, this.result = this.index.search(a).result, this; + const g = a; + c = g.index; + b = g.boost || 0; + if (g.query) { + const h = g.resolve; + a = g.async || g.queue; + g.resolve = !1; + g.index = null; + a = a ? c.searchAsync(g) : c.search(g); + g.resolve = h; + g.index = c; + a = a.result || a; + } else { + a = []; + } } - this.index = b || null; + if (a && a.then) { + const g = this; + a = a.then(function(h) { + g.C[0] = g.result = h.result || h; + Wa(g); + }); + e = [a]; + a = []; + d = new Promise(function(h) { + f = h; + }); + } + this.index = c || null; this.result = a || []; - this.h = 0; + this.h = b; + this.C = e || []; + this.await = d || null; + this.return = f || null; } Y.prototype.limit = function(a) { if (this.result.length) { - const b = []; - for (let c = 0, d; c < this.result.length; c++) { - if (d = this.result[c]) { - if (d.length <= a) { - if (b[c] = d, a -= d.length, !a) { + const c = []; + for (let b = 0, e; b < this.result.length; b++) { + if (e = this.result[b]) { + if (e.length <= a) { + if (c[b] = e, a -= e.length, !a) { break; } } else { - b[c] = d.slice(0, a); + c[b] = e.slice(0, a); break; } } } - this.result = b; + this.result = c; } return this; }; Y.prototype.offset = function(a) { if (this.result.length) { - const b = []; - for (let c = 0, d; c < this.result.length; c++) { - if (d = this.result[c]) { - d.length <= a ? a -= d.length : (b[c] = d.slice(a), a = 0); + const c = []; + for (let b = 0, e; b < this.result.length; b++) { + if (e = this.result[b]) { + e.length <= a ? a -= e.length : (c[b] = e.slice(a), a = 0); } } - this.result = b; + this.result = c; } return this; }; @@ -1376,87 +1169,115 @@ Y.prototype.boost = function(a) { this.h += a; return this; }; -Y.prototype.resolve = function(a, b, c) { - const d = this.index; - let e = this.result; - this.result = this.index = null; - e.length && ("object" === typeof a && (c = a.enrich, b = a.offset, a = a.limit), e = Ta.call(d, e, a || 100, b, c)); - return e; +function Wa(a, c) { + let b = a.result; + for (let d = 0, f; d < a.C.length; d++) { + if (f = a.C[d]) { + if ("function" === typeof f) { + b = f(), a.C[d] = b = b.result || b, d--; + } else if (f.h) { + b = f.h(), a.C[d] = b = b.result || b, d--; + } else if (f.then) { + return a.await; + } + } + } + const e = a.return; + a.result = b; + a.C = []; + a.await = null; + a.return = null; + c || e(b); + return b; +} +Y.prototype.resolve = function(a, c, b, e) { + let d = this.await ? Wa(this, !0) : this.result; + if (d.then) { + const f = this; + return d.then(function() { + return f.resolve(a, c, b); + }); + } + d.length && ("object" === typeof a && (b = a.enrich, c = a.offset, a.highlight && console.warn('Highlighting results is not supported within the resolve() method. Instead pass highlight options within the last resolver stage like { query: "...", resolve: true, highlight: ... }.'), a = a.limit), d = e ? b ? Ta.call(this.index, d) : d : Sa.call(this.index, d, a || 100, c, b)); + e = this.return; + this.return = this.await = this.C = this.result = this.index = null; + e && e(d); + return d; }; -function Ya(a, b, c, d, e, f, g) { +function $a(a, c, b, e, d, f, g) { const h = a.length; - let k = [], l, m; - l = G(); - for (let p = 0, u, q, r, n; p < b; p++) { - for (let t = 0; t < h; t++) { - if (r = a[t], p < r.length && (u = r[p])) { - for (let x = 0; x < u.length; x++) { - q = u[x]; - (m = l[q]) ? l[q]++ : (m = 0, l[q] = 1); - n = k[m] || (k[m] = []); + let k = [], m, l; + m = I(); + for (let n = 0, u, p, t, r; n < c; n++) { + for (let q = 0; q < h; q++) { + if (t = a[q], n < t.length && (u = t[n])) { + for (let y = 0; y < u.length; y++) { + p = u[y]; + (l = m[p]) ? m[p]++ : (l = 0, m[p] = 1); + r = k[l] || (k[l] = []); if (!g) { - let y = p + (t || !e ? 0 : f || 0); - n = n[y] || (n[y] = []); + let v = n + (q || !d ? 0 : f || 0); + r = r[v] || (r[v] = []); } - n.push(q); - if (g && c && m === h - 1 && n.length - d === c) { - return d ? n.slice(d) : n; + r.push(p); + if (g && b && l === h - 1 && r.length - e === b) { + return e ? r.slice(e) : r; } } } } } if (a = k.length) { - if (e) { - k = 1 < k.length ? Wa(k, c, d, g, f) : (k = k[0]).length > c || d ? k.slice(d, c + d) : k; + if (d) { + k = 1 < k.length ? Ya(k, b, e, g, f) : (k = k[0]) && b && k.length > b || e ? k.slice(e, b + e) : k; } else { if (a < h) { return []; } k = k[a - 1]; - if (c || d) { + if (b || e) { if (g) { - if (k.length > c || d) { - k = k.slice(d, c + d); + if (k.length > b || e) { + k = k.slice(e, b + e); } } else { - e = []; - for (let p = 0, u; p < k.length; p++) { - if (u = k[p]) { - if (d && u.length > d) { - d -= u.length; + d = []; + for (let n = 0, u; n < k.length; n++) { + if (u = k[n]) { + if (e && u.length > e) { + e -= u.length; } else { - if (c && u.length > c || d) { - u = u.slice(d, c + d), c -= u.length, d && (d -= u.length); + if (b && u.length > b || e) { + u = u.slice(e, b + e), b -= u.length, e && (e -= u.length); } - e.push(u); - if (!c) { + d.push(u); + if (!b) { break; } } } } - k = e; + k = d; } } } } return k; } -function Wa(a, b, c, d, e) { - const f = [], g = G(); +function Ya(a, c, b, e, d) { + const f = [], g = I(); let h; var k = a.length; - let l; - if (d) { - for (e = k - 1; 0 <= e; e--) { - if (l = (d = a[e]) && d.length) { - for (k = 0; k < l; k++) { - if (h = d[k], !g[h]) { - if (g[h] = 1, c) { - c--; + let m; + if (e) { + for (d = k - 1; 0 <= d; d--) { + if (m = (e = a[d]) && e.length) { + for (k = 0; k < m; k++) { + if (h = e[k], !g[h]) { + if (g[h] = 1, b) { + b--; } else { - if (f.push(h), f.length === b) { + if (f.push(h), f.length === c) { return f; } } @@ -1465,18 +1286,18 @@ function Wa(a, b, c, d, e) { } } } else { - for (let m = k - 1, p, u = 0; 0 <= m; m--) { - p = a[m]; - for (let q = 0; q < p.length; q++) { - if (l = (d = p[q]) && d.length) { - for (let r = 0; r < l; r++) { - if (h = d[r], !g[h]) { - if (g[h] = 1, c) { - c--; + for (let l = k - 1, n, u = 0; 0 <= l; l--) { + n = a[l]; + for (let p = 0; p < n.length; p++) { + if (m = (e = n[p]) && e.length) { + for (let t = 0; t < m; t++) { + if (h = e[t], !g[h]) { + if (g[h] = 1, b) { + b--; } else { - let n = (q + (m < k - 1 ? e || 0 : 0)) / (m + 1) | 0; - (f[n] || (f[n] = [])).push(h); - if (++u === b) { + let r = (p + (l < k - 1 ? d || 0 : 0)) / (l + 1) | 0; + (f[r] || (f[r] = [])).push(h); + if (++u === c) { return f; } } @@ -1488,334 +1309,557 @@ function Wa(a, b, c, d, e) { } return f; } -function db(a, b, c) { - const d = G(), e = []; - for (let f = 0, g; f < b.length; f++) { - g = b[f]; +function cb(a, c, b) { + const e = I(), d = []; + for (let f = 0, g; f < c.length; f++) { + g = c[f]; for (let h = 0; h < g.length; h++) { - d[g[h]] = 1; + e[g[h]] = 1; } } - if (c) { + if (b) { for (let f = 0, g; f < a.length; f++) { - g = a[f], d[g] && (e.push(g), d[g] = 0); + g = a[f], e[g] && (d.push(g), e[g] = 0); } } else { for (let f = 0, g, h; f < a.result.length; f++) { - for (g = a.result[f], b = 0; b < g.length; b++) { - h = g[b], d[h] && ((e[f] || (e[f] = [])).push(h), d[h] = 0); + for (g = a.result[f], c = 0; c < g.length; c++) { + h = g[c], e[h] && ((d[f] || (d[f] = [])).push(h), e[h] = 0); } } } - return e; + return d; } -;G(); -Qa.prototype.search = function(a, b, c, d) { - c || (!b && da(a) ? (c = a, a = "") : da(b) && (c = b, b = 0)); - if (c && c.cache) { - c.cache = !1; - var e = this.searchCache(a, b, c); - c.cache = !0; - return e; +;I(); +function db(a, c, b, e, d) { + let f, g, h; + "string" === typeof d ? (f = d, d = "") : f = d.template; + if (!f) { + throw Error('No template pattern was specified by the search option "highlight"'); } - let f = []; - var g = []; - let h, k, l, m, p; - let u = 0, q = !0, r; - if (c) { - c.constructor === Array && (c = {index:c}); - a = c.query || a; - h = c.pluck; - k = c.merge; - m = c.boost; - p = h || c.field || (p = c.index) && (p.index ? null : p); - var n = this.tag && c.tag; - l = c.suggest; - q = !1 !== c.resolve; - this.store && c.highlight && !q ? console.warn("Highlighting results can only be done on a final resolver task or when calling .resolve({ highlight: ... })") : this.store && c.enrich && !q && console.warn("Enrich results can only be done on a final resolver task or when calling .resolve({ enrich: true })"); - r = q && this.store && c.highlight; - e = !!r || q && this.store && c.enrich; - b = c.limit || b; - var t = c.offset || 0; - b || (b = q ? 100 : 0); - if (n && (!this.db || !d)) { - n.constructor !== Array && (n = [n]); - var x = []; - for (let B = 0, v; B < n.length; B++) { - v = n[B]; - if (M(v)) { + g = f.indexOf("$1"); + if (-1 === g) { + throw Error('Invalid highlight template. The replacement pattern "$1" was not found in template: ' + f); + } + h = f.substring(g + 2); + g = f.substring(0, g); + let k = d && d.boundary, m = !d || !1 !== d.clip, l = d && d.merge && h && g && new RegExp(h + " " + g, "g"); + d = d && d.ellipsis; + var n = 0; + if ("object" === typeof d) { + var u = d.template; + n = u.length - 2; + d = d.pattern; + } + "string" !== typeof d && (d = !1 === d ? "" : "..."); + n && (d = u.replace("$1", d)); + u = d.length - n; + let p, t; + "object" === typeof k && (p = k.before, 0 === p && (p = -1), t = k.after, 0 === t && (t = -1), k = k.total || 9e5); + n = new Map(); + for (let Pa = 0, ea, gb, oa; Pa < c.length; Pa++) { + let pa; + if (e) { + pa = c, oa = e; + } else { + var r = c[Pa]; + oa = r.field; + if (!oa) { + continue; + } + pa = r.result; + } + gb = b.get(oa); + ea = gb.encoder; + r = n.get(ea); + "string" !== typeof r && (r = ea.encode(a), n.set(ea, r)); + for (let xa = 0; xa < pa.length; xa++) { + var q = pa[xa].doc; + if (!q) { + continue; + } + q = da(q, oa); + if (!q) { + continue; + } + var y = q.trim().split(/\s+/); + if (!y.length) { + continue; + } + q = ""; + var v = []; + let ya = []; + var A = -1, B = -1, E = 0; + for (var C = 0; C < y.length; C++) { + var F = y[C], z = ea.encode(F); + z = 1 < z.length ? z.join(" ") : z[0]; + let x; + if (z && F) { + var D = F.length, K = (ea.split ? F.replace(ea.split, "") : F).length - z.length, G = "", L = 0; + for (var O = 0; O < r.length; O++) { + var P = r[O]; + if (P) { + var M = P.length; + M += K; + L && M <= L || (P = z.indexOf(P), -1 < P && (G = (P ? F.substring(0, P) : "") + g + F.substring(P, P + M) + h + (P + M < D ? F.substring(P + M) : ""), L = M, x = !0)); + } + } + G && (k && (0 > A && (A = q.length + (q ? 1 : 0)), B = q.length + (q ? 1 : 0) + G.length, E += D, ya.push(v.length), v.push({match:G})), q += (q ? " " : "") + G); + } + if (!x) { + F = y[C], q += (q ? " " : "") + F, k && v.push({text:F}); + } else if (k && E >= k) { + break; + } + } + E = ya.length * (f.length - 2); + if (p || t || k && q.length - E > k) { + if (E = k + E - 2 * u, C = B - A, 0 < p && (C += p), 0 < t && (C += t), C <= E) { + y = p ? A - (0 < p ? p : 0) : A - ((E - C) / 2 | 0), v = t ? B + (0 < t ? t : 0) : y + E, m || (0 < y && " " !== q.charAt(y) && " " !== q.charAt(y - 1) && (y = q.indexOf(" ", y), 0 > y && (y = 0)), v < q.length && " " !== q.charAt(v - 1) && " " !== q.charAt(v) && (v = q.lastIndexOf(" ", v), v < B ? v = B : ++v)), q = (y ? d : "") + q.substring(y, v) + (v < q.length ? d : ""); + } else { + B = []; + A = {}; + E = {}; + C = {}; + F = {}; + z = {}; + G = K = D = 0; + for (O = L = 1;;) { + var U = void 0; + for (let x = 0, J; x < ya.length; x++) { + J = ya[x]; + if (G) { + if (K !== G) { + if (C[x + 1]) { + continue; + } + J += G; + if (A[J]) { + D -= u; + E[x + 1] = 1; + C[x + 1] = 1; + continue; + } + if (J >= v.length - 1) { + if (J >= v.length) { + C[x + 1] = 1; + J >= y.length && (E[x + 1] = 1); + continue; + } + D -= u; + } + q = v[J].text; + if (M = t && z[x]) { + if (0 < M) { + if (q.length > M) { + if (C[x + 1] = 1, m) { + q = q.substring(0, M); + } else { + continue; + } + } + (M -= q.length) || (M = -1); + z[x] = M; + } else { + C[x + 1] = 1; + continue; + } + } + if (D + q.length + 1 <= k) { + q = " " + q, B[x] += q; + } else if (m) { + U = k - D - 1, 0 < U && (q = " " + q.substring(0, U), B[x] += q), C[x + 1] = 1; + } else { + C[x + 1] = 1; + continue; + } + } else { + if (C[x]) { + continue; + } + J -= K; + if (A[J]) { + D -= u; + C[x] = 1; + E[x] = 1; + continue; + } + if (0 >= J) { + if (0 > J) { + C[x] = 1; + E[x] = 1; + continue; + } + D -= u; + } + q = v[J].text; + if (M = p && F[x]) { + if (0 < M) { + if (q.length > M) { + if (C[x] = 1, m) { + q = q.substring(q.length - M); + } else { + continue; + } + } + (M -= q.length) || (M = -1); + F[x] = M; + } else { + C[x] = 1; + continue; + } + } + if (D + q.length + 1 <= k) { + q += " ", B[x] = q + B[x]; + } else if (m) { + U = q.length + 1 - (k - D), 0 <= U && U < q.length && (q = q.substring(U) + " ", B[x] = q + B[x]), C[x] = 1; + } else { + C[x] = 1; + continue; + } + } + } else { + q = v[J].match; + p && (F[x] = p); + t && (z[x] = t); + x && D++; + let Qa; + J ? !x && u && (D += u) : (E[x] = 1, C[x] = 1); + J >= y.length - 1 ? Qa = 1 : J < v.length - 1 && v[J + 1].match ? Qa = 1 : u && (D += u); + D -= f.length - 2; + if (!x || D + q.length <= k) { + B[x] = q; + } else { + U = L = O = E[x] = 0; + break; + } + Qa && (E[x + 1] = 1, C[x + 1] = 1); + } + D += q.length; + U = A[J] = 1; + } + if (U) { + K === G ? G++ : K++; + } else { + K === G ? L = 0 : O = 0; + if (!L && !O) { + break; + } + L ? (K++, G = K) : G++; + } + } + q = ""; + for (let x = 0, J; x < B.length; x++) { + J = (x && E[x] ? " " : (x && !d ? " " : "") + d) + B[x], q += J; + } + d && !E[B.length] && (q += d); + } + } + l && (q = q.replace(l, " ")); + pa[xa].highlight = q; + } + if (e) { + break; + } + } + return c; +} +;Na.prototype.search = function(a, c, b, e) { + b || (!c && ba(a) ? (b = a, a = "") : ba(c) && (b = c, c = 0)); + let d = []; + var f = []; + let g; + let h, k, m, l, n; + let u = 0, p = !0, t; + if (b) { + b.constructor === Array && (b = {index:b}); + a = b.query || a; + g = b.pluck; + h = b.merge; + m = b.boost; + n = g || b.field || (n = b.index) && (n.index ? null : n); + var r = this.tag && b.tag; + k = b.suggest; + p = !1 !== b.resolve; + l = b.cache; + this.store && b.highlight && !p ? console.warn("Highlighting results can only be done on a final resolver task or when calling .resolve({ highlight: ... })") : this.store && b.enrich && !p && console.warn("Enrich results can only be done on a final resolver task or when calling .resolve({ enrich: true })"); + t = p && this.store && b.highlight; + var q = !!t || p && this.store && b.enrich; + c = b.limit || c; + var y = b.offset || 0; + c || (c = p ? 100 : 0); + if (r && (!this.db || !e)) { + r.constructor !== Array && (r = [r]); + var v = []; + for (let F = 0, z; F < r.length; F++) { + z = r[F]; + if (N(z)) { throw Error("A tag option can't be a string, instead it needs a { field: tag } format."); } - if (v.field && v.tag) { - var y = v.tag; - if (y.constructor === Array) { - for (var D = 0; D < y.length; D++) { - x.push(v.field, y[D]); + if (z.field && z.tag) { + var A = z.tag; + if (A.constructor === Array) { + for (var B = 0; B < A.length; B++) { + v.push(z.field, A[B]); } } else { - x.push(v.field, y); + v.push(z.field, A); } } else { - y = Object.keys(v); - for (let A = 0, I, C; A < y.length; A++) { - if (I = y[A], C = v[I], C.constructor === Array) { - for (D = 0; D < C.length; D++) { - x.push(I, C[D]); + A = Object.keys(z); + for (let D = 0, K, G; D < A.length; D++) { + if (K = A[D], G = z[K], G.constructor === Array) { + for (B = 0; B < G.length; B++) { + v.push(K, G[B]); } } else { - x.push(I, C); + v.push(K, G); } } } } - if (!x.length) { + if (!v.length) { throw Error("Your tag definition within the search options is probably wrong. No valid tags found."); } - n = x; + r = v; if (!a) { - g = []; - if (x.length) { - for (n = 0; n < x.length; n += 2) { + f = []; + if (v.length) { + for (r = 0; r < v.length; r += 2) { if (this.db) { - d = this.index.get(x[n]); - if (!d) { - console.warn("Tag '" + x[n] + ":" + x[n + 1] + "' will be skipped because there is no field '" + x[n] + "'."); + e = this.index.get(v[r]); + if (!e) { + console.warn("Tag '" + v[r] + ":" + v[r + 1] + "' will be skipped because there is no field '" + v[r] + "'."); continue; } - g.push(d = d.db.tag(x[n + 1], b, t, e)); + f.push(e = e.db.tag(v[r + 1], c, y, q)); } else { - d = eb.call(this, x[n], x[n + 1], b, t, e); + e = eb.call(this, v[r], v[r + 1], c, y, q); } - f.push(q ? {field:x[n], tag:x[n + 1], result:d} : [d]); + d.push(p ? {field:v[r], tag:v[r + 1], result:e} : [e]); } } - if (g.length) { - const B = this; - return Promise.all(g).then(function(v) { - for (let A = 0; A < v.length; A++) { - q ? f[A].result = v[A] : f[A] = v[A]; + if (f.length) { + const F = this; + return Promise.all(f).then(function(z) { + for (let D = 0; D < z.length; D++) { + p ? d[D].result = z[D] : d[D] = z[D]; } - return q ? f : new Y(1 < f.length ? Ya(f, 1, 0, 0, l, m) : f[0], B); + return p ? d : new Y(1 < d.length ? $a(d, 1, 0, 0, k, m) : d[0], F); }); } - return q ? f : new Y(1 < f.length ? Ya(f, 1, 0, 0, l, m) : f[0], this); + return p ? d : new Y(1 < d.length ? $a(d, 1, 0, 0, k, m) : d[0], this); } } - if (!q && !h) { - if (p = p || this.field) { - M(p) ? h = p : (p.constructor === Array && 1 === p.length && (p = p[0]), h = p.field || p.index); + if (!p && !g) { + if (n = n || this.field) { + N(n) ? g = n : (n.constructor === Array && 1 === n.length && (n = n[0]), g = n.field || n.index); } - if (!h) { + if (!g) { throw Error("Apply resolver on document search requires either the option 'pluck' to be set or just select a single field name in your query."); } } - p && p.constructor !== Array && (p = [p]); + n && n.constructor !== Array && (n = [n]); } - p || (p = this.field); - let H; - x = (this.worker || this.db) && !d && []; - for (let B = 0, v, A, I; B < p.length; B++) { - A = p[B]; - if (this.db && this.tag && !this.F[B]) { + n || (n = this.field); + let E; + v = (this.worker || this.db) && !e && []; + for (let F = 0, z, D, K; F < n.length; F++) { + D = n[F]; + if (this.db && this.tag && !this.G[F]) { continue; } - let C; - M(A) || (C = A, A = C.field, a = C.query || a, b = aa(C.limit, b), t = aa(C.offset, t), l = aa(C.suggest, l), r = q && this.store && aa(C.highlight, r), e = !!r || q && this.store && aa(C.enrich, e)); - if (d) { - v = d[B]; + let G; + N(D) || (G = D, D = G.field, a = G.query || a, c = aa(G.limit, c), y = aa(G.offset, y), k = aa(G.suggest, k), t = p && this.store && aa(G.highlight, t), q = !!t || p && this.store && aa(G.enrich, q), l = aa(G.cache, l)); + if (e) { + z = e[F]; } else { - if (y = C || c, D = this.index.get(A), n && (this.db && (y.tag = n, H = D.db.support_tag_search, y.field = p), H || (y.enrich = !1)), x) { - x[B] = D.search(a, b, y); - y && e && (y.enrich = e); + A = G || b || {}; + B = A.enrich; + var C = this.index.get(D); + r && (this.db && (A.tag = r, E = C.db.support_tag_search, A.field = n), !E && B && (A.enrich = !1)); + z = l ? C.searchCache(a, c, A) : C.search(a, c, A); + B && (A.enrich = B); + if (v) { + v[F] = z; continue; - } else { - v = D.search(a, b, y), y && e && (y.enrich = e); } } - I = v && (q ? v.length : v.result.length); - if (n && I) { - y = []; - D = 0; - if (this.db && d) { - if (!H) { - for (let J = p.length; J < d.length; J++) { - let F = d[J]; - if (F && F.length) { - D++, y.push(F); - } else if (!l) { - return q ? f : new Y(f, this); + K = (z = z.result || z) && z.length; + if (r && K) { + A = []; + B = 0; + if (this.db && e) { + if (!E) { + for (C = n.length; C < e.length; C++) { + let L = e[C]; + if (L && L.length) { + B++, A.push(L); + } else if (!k) { + return p ? d : new Y(d, this); } } } } else { - for (let J = 0, F, S; J < n.length; J += 2) { - F = this.tag.get(n[J]); - if (!F) { - if (console.warn("Tag '" + n[J] + ":" + n[J + 1] + "' will be skipped because there is no field '" + n[J] + "'."), l) { + for (let L = 0, O, P; L < r.length; L += 2) { + O = this.tag.get(r[L]); + if (!O) { + if (console.warn("Tag '" + r[L] + ":" + r[L + 1] + "' will be skipped because there is no field '" + r[L] + "'."), k) { continue; } else { - return q ? f : new Y(f, this); + return p ? d : new Y(d, this); } } - if (S = (F = F && F.get(n[J + 1])) && F.length) { - D++, y.push(F); - } else if (!l) { - return q ? f : new Y(f, this); + if (P = (O = O && O.get(r[L + 1])) && O.length) { + B++, A.push(O); + } else if (!k) { + return p ? d : new Y(d, this); } } } - if (D) { - v = db(v, y, q); - I = v.length; - if (!I && !l) { - return q ? v : new Y(v, this); + if (B) { + z = cb(z, A, p); + K = z.length; + if (!K && !k) { + return p ? z : new Y(z, this); } - D--; + B--; } } - if (I) { - g[u] = A, f.push(v), u++; - } else if (1 === p.length) { - return q ? f : new Y(f, this); + if (K) { + f[u] = D, d.push(z), u++; + } else if (1 === n.length) { + return p ? d : new Y(d, this); } } - if (x) { - if (this.db && n && n.length && !H) { - for (e = 0; e < n.length; e += 2) { - g = this.index.get(n[e]); - if (!g) { - if (console.warn("Tag '" + n[e] + ":" + n[e + 1] + "' was not found because there is no field '" + n[e] + "'."), l) { + if (v) { + if (this.db && r && r.length && !E) { + for (q = 0; q < r.length; q += 2) { + f = this.index.get(r[q]); + if (!f) { + if (console.warn("Tag '" + r[q] + ":" + r[q + 1] + "' was not found because there is no field '" + r[q] + "'."), k) { continue; } else { - return q ? f : new Y(f, this); + return p ? d : new Y(d, this); } } - x.push(g.db.tag(n[e + 1], b, t, !1)); + v.push(f.db.tag(r[q + 1], c, y, !1)); } } - const B = this; - return Promise.all(x).then(function(v) { - return v.length ? B.search(a, b, c, v) : v; + const F = this; + return Promise.all(v).then(function(z) { + b && (b.resolve = p); + z.length && (z = F.search(a, c, b, z)); + return z; }); } if (!u) { - return q ? f : new Y(f, this); + return p ? d : new Y(d, this); } - if (h && (!e || !this.store)) { - return f = f[0], q || (f.index = this), f; + if (g && (!q || !this.store)) { + return d = d[0], p ? d : new Y(d, this); } - x = []; - for (t = 0; t < g.length; t++) { - n = f[t]; - e && n.length && "undefined" === typeof n[0].doc && (this.db ? x.push(n = this.index.get(this.field[0]).db.enrich(n)) : n = X.call(this, n)); - if (h) { - return q ? r ? cb(a, n, this.index, h, r) : n : new Y(n, this); + v = []; + for (y = 0; y < f.length; y++) { + r = d[y]; + q && r.length && "undefined" === typeof r[0].doc && (this.db ? v.push(r = this.index.get(this.field[0]).db.enrich(r)) : r = Ta.call(this, r)); + if (g) { + return p ? t ? db(a, r, this.index, g, t) : r : new Y(r, this); } - f[t] = {field:g[t], result:n}; + d[y] = {field:f[y], result:r}; } - if (e && this.db && x.length) { - const B = this; - return Promise.all(x).then(function(v) { - for (let A = 0; A < v.length; A++) { - f[A].result = v[A]; + if (q && this.db && v.length) { + const F = this; + return Promise.all(v).then(function(z) { + for (let D = 0; D < z.length; D++) { + d[D].result = z[D]; } - r && (f = cb(a, f, B.index, h, r)); - return k ? fb(f) : f; + t && (d = db(a, d, F.index, g, t)); + return h ? fb(d) : d; }); } - r && (f = cb(a, f, this.index, h, r)); - return k ? fb(f) : f; + t && (d = db(a, d, this.index, g, t)); + return h ? fb(d) : d; }; function fb(a) { - const b = [], c = G(), d = G(); - for (let e = 0, f, g, h, k, l, m, p; e < a.length; e++) { - f = a[e]; + const c = [], b = I(), e = I(); + for (let d = 0, f, g, h, k, m, l, n; d < a.length; d++) { + f = a[d]; g = f.field; h = f.result; for (let u = 0; u < h.length; u++) { - if (l = h[u], "object" !== typeof l ? l = {id:k = l} : k = l.id, (m = c[k]) ? m.push(g) : (l.field = c[k] = [g], b.push(l)), p = l.highlight) { - m = d[k], m || (d[k] = m = {}, l.highlight = m), m[g] = p; + if (m = h[u], "object" !== typeof m ? m = {id:k = m} : k = m.id, (l = b[k]) ? l.push(g) : (m.field = b[k] = [g], c.push(m)), n = m.highlight) { + l = e[k], l || (e[k] = l = {}, m.highlight = l), l[g] = n; } } } - return b; + return c; } -function eb(a, b, c, d, e) { +function eb(a, c, b, e, d) { a = this.tag.get(a); if (!a) { return []; } - a = a.get(b); + a = a.get(c); if (!a) { return []; } - b = a.length - d; - if (0 < b) { - if (c && b > c || d) { - a = a.slice(d, d + c); + c = a.length - e; + if (0 < c) { + if (b && c > b || e) { + a = a.slice(e, e + b); } - e && (a = X.call(this, a)); + d && (a = Ta.call(this, a)); } return a; } -function X(a) { +function Ta(a) { if (!this || !this.store) { return a; } - const b = Array(a.length); - for (let c = 0, d; c < a.length; c++) { - d = a[c], b[c] = {id:d, doc:this.store.get(d)}; + if (this.db) { + return this.index.get(this.field[0]).db.enrich(a); } - return b; + const c = Array(a.length); + for (let b = 0, e; b < a.length; b++) { + e = a[b], c[b] = {id:e, doc:this.store.get(e)}; + } + return c; } -;const gb = {normalize:!1, numeric:!1, dedupe:!1}; -const hb = {}; -const jb = new Map([["b", "p"], ["v", "f"], ["w", "f"], ["z", "s"], ["x", "s"], ["d", "t"], ["n", "m"], ["c", "k"], ["g", "k"], ["j", "k"], ["q", "k"], ["i", "e"], ["y", "e"], ["u", "o"]]); -const kb = new Map([["ae", "a"], ["oe", "o"], ["sh", "s"], ["kh", "k"], ["th", "t"], ["ph", "f"], ["pf", "f"]]), lb = [/([^aeo])h(.)/g, "$1$2", /([aeo])h([^aeo]|$)/g, "$1$2", /(.)\1+/g, "$1"]; -const mb = {a:"", e:"", i:"", o:"", u:"", y:"", b:1, f:1, p:1, v:1, c:2, g:2, j:2, k:2, q:2, s:2, x:2, z:2, "\u00df":2, d:3, t:3, l:4, m:5, n:5, r:6}; -var nb = {Exact:gb, Default:hb, Normalize:hb, LatinBalance:{mapper:jb}, LatinAdvanced:{mapper:jb, matcher:kb, replacer:lb}, LatinExtra:{mapper:jb, replacer:lb.concat([/(?!^)[aeo]/g, ""]), matcher:kb}, LatinSoundex:{dedupe:!1, include:{letter:!0}, finalize:function(a) { - for (let c = 0; c < a.length; c++) { - var b = a[c]; - let d = b.charAt(0), e = mb[d]; - for (let f = 1, g; f < b.length && (g = b.charAt(f), "h" === g || "w" === g || !(g = mb[g]) || g === e || (d += g, e = g, 4 !== d.length)); f++) { - } - a[c] = d; +;function Na(a) { + if (!this || this.constructor !== Na) { + return new Na(a); } -}}, CJK:{split:""}, LatinExact:gb, LatinDefault:hb, LatinSimple:hb}; -function Qa(a) { - if (!this || this.constructor !== Qa) { - return new Qa(a); - } - const b = a.document || a.doc || a; - let c, d; - this.F = []; + const c = a.document || a.doc || a; + let b, e; + this.G = []; this.field = []; - this.J = []; - this.key = (c = b.key || b.id) && ob(c, this.J) || "id"; - (d = a.keystore || 0) && (this.keystore = d); + this.K = []; + this.key = (b = c.key || c.id) && hb(b, this.K) || "id"; + (e = a.keystore || 0) && (this.keystore = e); this.fastupdate = !!a.fastupdate; - this.reg = !this.fastupdate || a.worker || a.db ? d ? new W(d) : new Set() : d ? new V(d) : new Map(); - this.C = (c = b.store || null) && c && !0 !== c && []; - this.store = c && (d ? new V(d) : new Map()); - this.cache = (c = a.cache || null) && new pb(c); + this.reg = !this.fastupdate || a.worker || a.db ? e ? new S(e) : new Set() : e ? new R(e) : new Map(); + this.D = (b = c.store || null) && b && !0 !== b && []; + this.store = b && (e ? new R(e) : new Map()); + this.cache = (b = a.cache || null) && new na(b); a.cache = !1; this.worker = a.worker || !1; this.priority = a.priority || 4; - this.index = qb.call(this, a, b); + this.index = ib.call(this, a, c); this.tag = null; - if (c = b.tag) { - if ("string" === typeof c && (c = [c]), c.length) { + if (b = c.tag) { + if ("string" === typeof b && (b = [b]), b.length) { this.tag = new Map(); - this.D = []; - this.R = []; - for (let e = 0, f, g; e < c.length; e++) { - f = c[e]; + this.F = []; + this.P = []; + for (let d = 0, f, g; d < b.length; d++) { + f = b[d]; g = f.field || f; if (!g) { throw Error("The tag field from the document descriptor is undefined."); } - f.custom ? this.D[e] = f.custom : (this.D[e] = ob(g, this.J), f.filter && ("string" === typeof this.D[e] && (this.D[e] = new String(this.D[e])), this.D[e].I = f.filter)); - this.R[e] = g; + f.custom ? this.F[d] = f.custom : (this.F[d] = hb(g, this.K), f.filter && ("string" === typeof this.F[d] && (this.F[d] = new String(this.F[d])), this.F[d].J = f.filter)); + this.P[d] = g; this.tag.set(g, new Map()); } } @@ -1823,126 +1867,126 @@ function Qa(a) { if (this.worker) { this.fastupdate = !1; a = []; - for (const e of this.index.values()) { - e.then && a.push(e); + for (const d of this.index.values()) { + d.then && a.push(d); } if (a.length) { - const e = this; + const d = this; return Promise.all(a).then(function(f) { let g = 0; - for (const h of e.index.entries()) { + for (const h of d.index.entries()) { const k = h[0]; - let l = h[1]; - l.then && (l = f[g], e.index.set(k, l), g++); + let m = h[1]; + m.then && (m = f[g], d.index.set(k, m), g++); } - return e; + return d; }); } } else { a.db && (this.fastupdate = !1, this.mount(a.db)); } } -w = Qa.prototype; +w = Na.prototype; w.mount = function(a) { if (this.worker) { throw Error("You can't use Worker-Indexes on a persistent model. That would be useless, since each of the persistent model acts like Worker-Index by default (Master/Slave)."); } - let b = this.field; + let c = this.field; if (this.tag) { - for (let f = 0, g; f < this.R.length; f++) { - g = this.R[f]; - var c = void 0; - this.index.set(g, c = new P({}, this.reg)); - b === this.field && (b = b.slice(0)); - b.push(g); - c.tag = this.tag.get(g); + for (let f = 0, g; f < this.P.length; f++) { + g = this.P[f]; + var b = void 0; + this.index.set(g, b = new V({}, this.reg)); + c === this.field && (c = c.slice(0)); + c.push(g); + b.tag = this.tag.get(g); } } - c = []; - const d = {db:a.db, type:a.type, fastupdate:a.fastupdate}; - for (let f = 0, g, h; f < b.length; f++) { - d.field = h = b[f]; + b = []; + const e = {db:a.db, type:a.type, fastupdate:a.fastupdate}; + for (let f = 0, g, h; f < c.length; f++) { + e.field = h = c[f]; g = this.index.get(h); - const k = new a.constructor(a.id, d); + const k = new a.constructor(a.id, e); k.id = a.id; - c[f] = k.mount(g); + b[f] = k.mount(g); g.document = !0; f ? g.bypass = !0 : g.store = this.store; } - const e = this; - return this.db = Promise.all(c).then(function() { - e.db = !0; + const d = this; + return this.db = Promise.all(b).then(function() { + d.db = !0; }); }; -w.commit = async function(a, b) { - const c = []; - for (const d of this.index.values()) { - c.push(d.commit(a, b)); +w.commit = async function(a, c) { + const b = []; + for (const e of this.index.values()) { + b.push(e.commit(a, c)); } - await Promise.all(c); + await Promise.all(b); this.reg.clear(); }; w.destroy = function() { const a = []; - for (const b of this.index.values()) { - a.push(b.destroy()); + for (const c of this.index.values()) { + a.push(c.destroy()); } return Promise.all(a); }; -function qb(a, b) { - const c = new Map(); - let d = b.index || b.field || b; - M(d) && (d = [d]); - for (let f = 0, g, h; f < d.length; f++) { - g = d[f]; - M(g) || (h = g, g = g.field); - h = da(h) ? Object.assign({}, a, h) : a; +function ib(a, c) { + const b = new Map(); + let e = c.index || c.field || c; + N(e) && (e = [e]); + for (let f = 0, g, h; f < e.length; f++) { + g = e[f]; + N(g) || (h = g, g = g.field); + h = ba(h) ? Object.assign({}, a, h) : a; if (this.worker) { - var e = void 0; - e = (e = h.encoder) && e.encode ? e : new na("string" === typeof e ? nb[e] : e || {}); - e = new Ba(h, e); - c.set(g, e); + var d = void 0; + d = (d = h.encoder) && d.encode ? d : new la("string" === typeof d ? wa[d] : d || {}); + d = new La(h, d); + b.set(g, d); } - this.worker || c.set(g, new P(h, this.reg)); - h.custom ? this.F[f] = h.custom : (this.F[f] = ob(g, this.J), h.filter && ("string" === typeof this.F[f] && (this.F[f] = new String(this.F[f])), this.F[f].I = h.filter)); + this.worker || b.set(g, new V(h, this.reg)); + h.custom ? this.G[f] = h.custom : (this.G[f] = hb(g, this.K), h.filter && ("string" === typeof this.G[f] && (this.G[f] = new String(this.G[f])), this.G[f].J = h.filter)); this.field[f] = g; } - if (this.C) { - a = b.store; - M(a) && (a = [a]); + if (this.D) { + a = c.store; + N(a) && (a = [a]); for (let f = 0, g, h; f < a.length; f++) { - g = a[f], h = g.field || g, g.custom ? (this.C[f] = g.custom, g.custom.V = h) : (this.C[f] = ob(h, this.J), g.filter && ("string" === typeof this.C[f] && (this.C[f] = new String(this.C[f])), this.C[f].I = g.filter)); + g = a[f], h = g.field || g, g.custom ? (this.D[f] = g.custom, g.custom.U = h) : (this.D[f] = hb(h, this.K), g.filter && ("string" === typeof this.D[f] && (this.D[f] = new String(this.D[f])), this.D[f].J = g.filter)); } } - return c; + return b; } -function ob(a, b) { - const c = a.split(":"); - let d = 0; - for (let e = 0; e < c.length; e++) { - a = c[e], "]" === a[a.length - 1] && (a = a.substring(0, a.length - 2)) && (b[d] = !0), a && (c[d++] = a); +function hb(a, c) { + const b = a.split(":"); + let e = 0; + for (let d = 0; d < b.length; d++) { + a = b[d], "]" === a[a.length - 1] && (a = a.substring(0, a.length - 2)) && (c[e] = !0), a && (b[e++] = a); } - d < c.length && (c.length = d); - return 1 < d ? c : c[0]; + e < b.length && (b.length = e); + return 1 < e ? b : b[0]; } -w.append = function(a, b) { - return this.add(a, b, !0); +w.append = function(a, c) { + return this.add(a, c, !0); }; -w.update = function(a, b) { - return this.remove(a).add(a, b); +w.update = function(a, c) { + return this.remove(a).add(a, c); }; w.remove = function(a) { - da(a) && (a = fa(a, this.key)); - for (var b of this.index.values()) { - b.remove(a, !0); + ba(a) && (a = da(a, this.key)); + for (var c of this.index.values()) { + c.remove(a, !0); } if (this.reg.has(a)) { if (this.tag && !this.fastupdate) { - for (let c of this.tag.values()) { - for (let d of c) { - b = d[0]; - const e = d[1], f = e.indexOf(a); - -1 < f && (1 < e.length ? e.splice(f, 1) : c.delete(b)); + for (let b of this.tag.values()) { + for (let e of b) { + c = e[0]; + const d = e[1], f = d.indexOf(a); + -1 < f && (1 < d.length ? d.splice(f, 1) : b.delete(c)); } } } @@ -1954,13 +1998,13 @@ w.remove = function(a) { }; w.clear = function() { const a = []; - for (const b of this.index.values()) { - const c = b.clear(); - c.then && a.push(c); + for (const c of this.index.values()) { + const b = c.clear(); + b.then && a.push(b); } if (this.tag) { - for (const b of this.tag.values()) { - b.clear(); + for (const c of this.tag.values()) { + c.clear(); } } this.store && this.store.clear(); @@ -1977,233 +2021,279 @@ w.cleanup = function() { return this; }; w.get = function(a) { - return this.db ? this.index.get(this.field[0]).db.enrich(a).then(function(b) { - return b[0] && b[0].doc || null; + return this.db ? this.index.get(this.field[0]).db.enrich(a).then(function(c) { + return c[0] && c[0].doc || null; }) : this.store.get(a) || null; }; -w.set = function(a, b) { - "object" === typeof a && (b = a, a = fa(b, this.key)); - this.store.set(a, b); +w.set = function(a, c) { + "object" === typeof a && (c = a, a = da(c, this.key)); + this.store.set(a, c); return this; }; -w.searchCache = rb; -w.export = function(a, b, c = 0, d = 0) { - if (c < this.field.length) { - const g = this.field[c]; - if ((b = this.index.get(g).export(a, g, c, d = 1)) && b.then) { +w.searchCache = ma; +w.export = jb; +w.import = kb; +Fa(Na.prototype); +function lb(a, c = 0) { + let b = [], e = []; + c && (c = 250000 / c * 5000 | 0); + for (const d of a.entries()) { + e.push(d), e.length === c && (b.push(e), e = []); + } + e.length && b.push(e); + return b; +} +function mb(a, c) { + c || (c = new Map()); + for (let b = 0, e; b < a.length; b++) { + e = a[b], c.set(e[0], e[1]); + } + return c; +} +function nb(a, c = 0) { + let b = [], e = []; + c && (c = 250000 / c * 1000 | 0); + for (const d of a.entries()) { + e.push([d[0], lb(d[1])[0]]), e.length === c && (b.push(e), e = []); + } + e.length && b.push(e); + return b; +} +function ob(a, c) { + c || (c = new Map()); + for (let b = 0, e, d; b < a.length; b++) { + e = a[b], d = c.get(e[0]), c.set(e[0], mb(e[1], d)); + } + return c; +} +function pb(a) { + let c = [], b = []; + for (const e of a.keys()) { + b.push(e), 250000 === b.length && (c.push(b), b = []); + } + b.length && c.push(b); + return c; +} +function qb(a, c) { + c || (c = new Set()); + for (let b = 0; b < a.length; b++) { + c.add(a[b]); + } + return c; +} +function rb(a, c, b, e, d, f, g = 0) { + const h = e && e.constructor === Array; + var k = h ? e.shift() : e; + if (!k) { + return this.export(a, c, d, f + 1); + } + if ((k = a((c ? c + "." : "") + (g + 1) + "." + b, JSON.stringify(k))) && k.then) { + const m = this; + return k.then(function() { + return rb.call(m, a, c, b, h ? e : null, d, f, g + 1); + }); + } + return rb.call(this, a, c, b, h ? e : null, d, f, g + 1); +} +function jb(a, c, b = 0, e = 0) { + if (b < this.field.length) { + const g = this.field[b]; + if ((c = this.index.get(g).export(a, g, b, e = 1)) && c.then) { const h = this; - return b.then(function() { - return h.export(a, g, c + 1); + return c.then(function() { + return h.export(a, g, b + 1); }); } - return this.export(a, g, c + 1); + return this.export(a, g, b + 1); } - let e, f; - switch(d) { + let d, f; + switch(e) { case 0: - e = "reg"; - f = Ha(this.reg); - b = null; + d = "reg"; + f = pb(this.reg); + c = null; break; case 1: - e = "tag"; - f = this.tag && Fa(this.tag, this.reg.size); - b = null; + d = "tag"; + f = this.tag && nb(this.tag, this.reg.size); + c = null; break; case 2: - e = "doc"; - f = this.store && Da(this.store); - b = null; + d = "doc"; + f = this.store && lb(this.store); + c = null; break; default: return; } - return Ja.call(this, a, b, e, f, c, d); -}; -w.import = function(a, b) { - var c = a.split("."); - "json" === c[c.length - 1] && c.pop(); - const d = 2 < c.length ? c[0] : ""; - c = 2 < c.length ? c[2] : c[1]; - if (this.worker && d) { - return this.index.get(d).import(a); + return rb.call(this, a, c, d, f, b, e); +} +function kb(a, c) { + var b = a.split("."); + "json" === b[b.length - 1] && b.pop(); + const e = 2 < b.length ? b[0] : ""; + b = 2 < b.length ? b[2] : b[1]; + if (this.worker && e) { + return this.index.get(e).import(a); } - if (b) { - "string" === typeof b && (b = JSON.parse(b)); - if (d) { - return this.index.get(d).import(c, b); + if (c) { + "string" === typeof c && (c = JSON.parse(c)); + if (e) { + return this.index.get(e).import(b, c); } - switch(c) { + switch(b) { case "reg": this.fastupdate = !1; - this.reg = Ia(b, this.reg); - for (let e = 0, f; e < this.field.length; e++) { - f = this.index.get(this.field[e]), f.fastupdate = !1, f.reg = this.reg; + this.reg = qb(c, this.reg); + for (let d = 0, f; d < this.field.length; d++) { + f = this.index.get(this.field[d]), f.fastupdate = !1, f.reg = this.reg; } if (this.worker) { - b = []; - for (const e of this.index.values()) { - b.push(e.import(a)); + c = []; + for (const d of this.index.values()) { + c.push(d.import(a)); } - return Promise.all(b); + return Promise.all(c); } break; case "tag": - this.tag = Ga(b, this.tag); + this.tag = ob(c, this.tag); break; case "doc": - this.store = Ea(b, this.store); + this.store = mb(c, this.store); } } -}; -sa(Qa.prototype); -function rb(a, b, c) { - const d = (b ? "" + a : "object" === typeof a ? "" + a.query : a).toLowerCase(); - this.cache || (this.cache = new pb()); - let e = this.cache.get(d); - if (!e) { - e = this.search(a, b, c); - if (e.then) { - const f = this; - e.then(function(g) { - f.cache.set(d, g); - return g; - }); +} +function sb(a, c) { + let b = ""; + for (const e of a.entries()) { + a = e[0]; + const d = e[1]; + let f = ""; + for (let g = 0, h; g < d.length; g++) { + h = d[g] || [""]; + let k = ""; + for (let m = 0; m < h.length; m++) { + k += (k ? "," : "") + ("string" === c ? '"' + h[m] + '"' : h[m]); + } + k = "[" + k + "]"; + f += (f ? "," : "") + k; } - this.cache.set(d, e); + f = '["' + a + '",[' + f + "]]"; + b += (b ? "," : "") + f; } - return e; -} -function pb(a) { - this.limit = a && !0 !== a ? a : 1000; - this.cache = new Map(); - this.h = ""; -} -pb.prototype.set = function(a, b) { - this.cache.set(this.h = a, b); - this.cache.size > this.limit && this.cache.delete(this.cache.keys().next().value); -}; -pb.prototype.get = function(a) { - const b = this.cache.get(a); - b && this.h !== a && (this.cache.delete(a), this.cache.set(this.h = a, b)); return b; -}; -pb.prototype.remove = function(a) { - for (const b of this.cache) { - const c = b[0]; - b[1].includes(a) && this.cache.delete(c); - } -}; -pb.prototype.clear = function() { - this.cache.clear(); - this.h = ""; -}; -P.prototype.remove = function(a, b) { - const c = this.reg.size && (this.fastupdate ? this.reg.get(a) : this.reg.has(a)); - if (c) { +} +;V.prototype.remove = function(a, c) { + const b = this.reg.size && (this.fastupdate ? this.reg.get(a) : this.reg.has(a)); + if (b) { if (this.fastupdate) { - for (let d = 0, e, f; d < c.length; d++) { - if ((e = c[d]) && (f = e.length)) { - if (e[f - 1] === a) { - e.pop(); + for (let e = 0, d, f; e < b.length; e++) { + if ((d = b[e]) && (f = d.length)) { + if (d[f - 1] === a) { + d.pop(); } else { - const g = e.indexOf(a); - 0 <= g && e.splice(g, 1); + const g = d.indexOf(a); + 0 <= g && d.splice(g, 1); } } } } else { - sb(this.map, a), this.depth && sb(this.ctx, a); + tb(this.map, a), this.depth && tb(this.ctx, a); } - b || this.reg.delete(a); + c || this.reg.delete(a); } - this.db && (this.commit_task.push({del:a}), this.T && tb(this)); + this.db && (this.commit_task.push({del:a}), this.S && ub(this)); this.cache && this.cache.remove(a); return this; }; -function sb(a, b) { - let c = 0; - var d = "undefined" === typeof b; +function tb(a, c) { + let b = 0; + var e = "undefined" === typeof c; if (a.constructor === Array) { - for (let e = 0, f, g; e < a.length; e++) { - if ((f = a[e]) && f.length) { - if (d) { - c++; - } else { - if (g = f.indexOf(b), 0 <= g) { - if (1 < f.length) { - f.splice(g, 1); - c++; - break; - } else { - delete a[e]; - } - } else { - c++; + for (let d = 0, f, g, h; d < a.length; d++) { + if ((f = a[d]) && f.length) { + if (e) { + return 1; + } + g = f.indexOf(c); + if (0 <= g) { + if (1 < f.length) { + return f.splice(g, 1), 1; } + delete a[d]; + if (b) { + return 1; + } + h = 1; + } else { + if (h) { + return 1; + } + b++; } } } } else { - for (let e of a.entries()) { - d = e[0], sb(e[1], b) ? c++ : a.delete(d); + for (let d of a.entries()) { + e = d[0], tb(d[1], c) ? b++ : a.delete(e); } } - return c; + return b; } -;const ub = {memory:{resolution:1}, performance:{resolution:3, fastupdate:!0, context:{depth:1, resolution:1}}, match:{tokenize:"forward"}, score:{resolution:9, context:{depth:2, resolution:3}}}; -P.prototype.add = function(a, b, c, d) { - if (b && (a || 0 === a)) { - if (!d && !c && this.reg.has(a)) { - return this.update(a, b); +;const vb = {memory:{resolution:1}, performance:{resolution:3, fastupdate:!0, context:{depth:1, resolution:1}}, match:{tokenize:"forward"}, score:{resolution:9, context:{depth:2, resolution:3}}}; +V.prototype.add = function(a, c, b, e) { + if (c && (a || 0 === a)) { + if (!e && !b && this.reg.has(a)) { + return this.update(a, c); } - d = this.depth; - b = this.encoder.encode(b, !d); - const l = b.length; - if (l) { - const m = G(), p = G(), u = this.resolution; - for (let q = 0; q < l; q++) { - let r = b[this.rtl ? l - 1 - q : q]; - var e = r.length; - if (e && (d || !p[r])) { - var f = this.score ? this.score(b, r, q, null, 0) : vb(u, l, q), g = ""; + e = this.depth; + c = this.encoder.encode(c, !e); + const m = c.length; + if (m) { + const l = I(), n = I(), u = this.resolution; + for (let p = 0; p < m; p++) { + let t = c[this.rtl ? m - 1 - p : p]; + var d = t.length; + if (d && (e || !n[t])) { + var f = this.score ? this.score(c, t, p, null, 0) : wb(u, m, p), g = ""; switch(this.tokenize) { case "full": - if (2 < e) { - for (let n = 0, t; n < e; n++) { - for (f = e; f > n; f--) { - g = r.substring(n, f); - t = this.rtl ? e - 1 - n : n; - var h = this.score ? this.score(b, r, q, g, t) : vb(u, l, q, e, t); - wb(this, p, g, h, a, c); + if (2 < d) { + for (let r = 0, q; r < d; r++) { + for (f = d; f > r; f--) { + g = t.substring(r, f); + q = this.rtl ? d - 1 - r : r; + var h = this.score ? this.score(c, t, p, g, q) : wb(u, m, p, d, q); + xb(this, n, g, h, a, b); } } break; } case "bidirectional": case "reverse": - if (1 < e) { - for (h = e - 1; 0 < h; h--) { - g = r[this.rtl ? e - 1 - h : h] + g; - var k = this.score ? this.score(b, r, q, g, h) : vb(u, l, q, e, h); - wb(this, p, g, k, a, c); + if (1 < d) { + for (h = d - 1; 0 < h; h--) { + g = t[this.rtl ? d - 1 - h : h] + g; + var k = this.score ? this.score(c, t, p, g, h) : wb(u, m, p, d, h); + xb(this, n, g, k, a, b); } g = ""; } case "forward": - if (1 < e) { - for (h = 0; h < e; h++) { - g += r[this.rtl ? e - 1 - h : h], wb(this, p, g, f, a, c); + if (1 < d) { + for (h = 0; h < d; h++) { + g += t[this.rtl ? d - 1 - h : h], xb(this, n, g, f, a, b); } break; } default: - if (wb(this, p, r, f, a, c), d && 1 < l && q < l - 1) { - for (e = G(), g = this.U, f = r, h = Math.min(d + 1, this.rtl ? q + 1 : l - q), e[f] = 1, k = 1; k < h; k++) { - if ((r = b[this.rtl ? l - 1 - q - k : q + k]) && !e[r]) { - e[r] = 1; - const n = this.score ? this.score(b, f, q, r, k - 1) : vb(g + (l / 2 > g ? 0 : 1), l, q, h - 1, k - 1), t = this.bidirectional && r > f; - wb(this, m, t ? f : r, n, a, c, t ? r : f); + if (xb(this, n, t, f, a, b), e && 1 < m && p < m - 1) { + for (d = I(), g = this.T, f = t, h = Math.min(e + 1, this.rtl ? p + 1 : m - p), d[f] = 1, k = 1; k < h; k++) { + if ((t = c[this.rtl ? m - 1 - p - k : p + k]) && !d[t]) { + d[t] = 1; + const r = this.score ? this.score(c, f, p, t, k - 1) : wb(g + (m / 2 > g ? 0 : 1), m, p, h - 1, k - 1), q = this.bidirectional && t > f; + xb(this, l, q ? f : t, r, a, b, q ? t : f); } } } @@ -2212,184 +2302,184 @@ P.prototype.add = function(a, b, c, d) { } this.fastupdate || this.reg.add(a); } else { - b = ""; + c = ""; } } - this.db && (b || this.commit_task.push({del:a}), this.T && tb(this)); + this.db && (c || this.commit_task.push({del:a}), this.S && ub(this)); return this; }; -function wb(a, b, c, d, e, f, g) { +function xb(a, c, b, e, d, f, g) { let h = g ? a.ctx : a.map, k; - if (!b[c] || g && !(k = b[c])[g]) { - if (g ? (b = k || (b[c] = G()), b[g] = 1, (k = h.get(g)) ? h = k : h.set(g, h = new Map())) : b[c] = 1, (k = h.get(c)) ? h = k : h.set(c, h = k = []), h = h[d] || (h[d] = []), !f || !h.includes(e)) { + if (!c[b] || g && !(k = c[b])[g]) { + if (g ? (c = k || (c[b] = I()), c[g] = 1, (k = h.get(g)) ? h = k : h.set(g, h = new Map())) : c[b] = 1, (k = h.get(b)) ? h = k : h.set(b, h = k = []), h = h[e] || (h[e] = []), !f || !h.includes(d)) { if (h.length === 2 ** 31 - 1) { - b = new U(h); + c = new Aa(h); if (a.fastupdate) { - for (let l of a.reg.values()) { - l.includes(h) && (l[l.indexOf(h)] = b); + for (let m of a.reg.values()) { + m.includes(h) && (m[m.indexOf(h)] = c); } } - k[d] = h = b; + k[e] = h = c; } - h.push(e); - a.fastupdate && ((d = a.reg.get(e)) ? d.push(h) : a.reg.set(e, [h])); + h.push(d); + a.fastupdate && ((e = a.reg.get(d)) ? e.push(h) : a.reg.set(d, [h])); } } } -function vb(a, b, c, d, e) { - return c && 1 < a ? b + (d || 0) <= a ? c + (e || 0) : (a - 1) / (b + (d || 0)) * (c + (e || 0)) + 1 | 0 : 0; +function wb(a, c, b, e, d) { + return b && 1 < a ? c + (e || 0) <= a ? b + (d || 0) : (a - 1) / (c + (e || 0)) * (b + (d || 0)) + 1 | 0 : 0; } -;P.prototype.search = function(a, b, c) { - c || (b || "object" !== typeof a ? "object" === typeof b && (c = b, b = 0) : (c = a, a = "")); - if (c && c.cache) { - return c.cache = !1, a = this.searchCache(a, b, c), c.cache = !0, a; +;V.prototype.search = function(a, c, b) { + b || (c || "object" !== typeof a ? "object" === typeof c && (b = c, c = 0) : (b = a, a = "")); + if (b && b.cache) { + return b.cache = !1, a = this.searchCache(a, c, b), b.cache = !0, a; } - let d = [], e, f, g, h = 0, k, l, m, p, u; - c && (a = c.query || a, b = c.limit || b, h = c.offset || 0, f = c.context, g = c.suggest, u = (k = c.resolve) && c.enrich, m = c.boost, p = c.resolution, l = this.db && c.tag); + let e = [], d, f, g, h = 0, k, m, l, n, u; + b && (a = b.query || a, c = b.limit || c, h = b.offset || 0, f = b.context, g = b.suggest, u = (k = b.resolve) && b.enrich, l = b.boost, n = b.resolution, m = this.db && b.tag); "undefined" === typeof k && (k = this.resolve); f = this.depth && !1 !== f; - let q = this.encoder.encode(a, !f); - e = q.length; - b = b || (k ? 100 : 0); - if (1 === e) { - return xb.call(this, q[0], "", b, h, k, u, l); + let p = this.encoder.encode(a, !f); + d = p.length; + c = c || (k ? 100 : 0); + if (1 === d) { + return yb.call(this, p[0], "", c, h, k, u, m); } - if (2 === e && f && !g) { - return xb.call(this, q[1], q[0], b, h, k, u, l); + if (2 === d && f && !g) { + return yb.call(this, p[1], p[0], c, h, k, u, m); } - let r = G(), n = 0, t; - f && (t = q[0], n = 1); - p || 0 === p || (p = t ? this.U : this.resolution); + let t = I(), r = 0, q; + f && (q = p[0], r = 1); + n || 0 === n || (n = q ? this.T : this.resolution); if (this.db) { - if (this.db.search && (c = this.db.search(this, q, b, h, g, k, u, l), !1 !== c)) { - return c; + if (this.db.search && (b = this.db.search(this, p, c, h, g, k, u, m), !1 !== b)) { + return b; } - const x = this; + const y = this; return async function() { - for (let y, D; n < e; n++) { - if ((D = q[n]) && !r[D]) { - r[D] = 1; - y = await yb(x, D, t, 0, 0, !1, !1); - if (y = zb(y, d, g, p)) { - d = y; + for (let v, A; r < d; r++) { + if ((A = p[r]) && !t[A]) { + t[A] = 1; + v = await zb(y, A, q, 0, 0, !1, !1); + if (v = Ab(v, e, g, n)) { + e = v; break; } - t && (g && y && d.length || (t = D)); + q && (g && v && e.length || (q = A)); } - g && t && n === e - 1 && !d.length && (p = x.resolution, t = "", n = -1, r = G()); + g && q && r === d - 1 && !e.length && (n = y.resolution, q = "", r = -1, t = I()); } - return Ab(d, p, b, h, g, m, k); + return Bb(e, n, c, h, g, l, k); }(); } - for (let x, y; n < e; n++) { - if ((y = q[n]) && !r[y]) { - r[y] = 1; - x = yb(this, y, t, 0, 0, !1, !1); - if (x = zb(x, d, g, p)) { - d = x; + for (let y, v; r < d; r++) { + if ((v = p[r]) && !t[v]) { + t[v] = 1; + y = zb(this, v, q, 0, 0, !1, !1); + if (y = Ab(y, e, g, n)) { + e = y; break; } - t && (g && x && d.length || (t = y)); + q && (g && y && e.length || (q = v)); } - g && t && n === e - 1 && !d.length && (p = this.resolution, t = "", n = -1, r = G()); + g && q && r === d - 1 && !e.length && (n = this.resolution, q = "", r = -1, t = I()); } - return Ab(d, p, b, h, g, m, k); + return Bb(e, n, c, h, g, l, k); }; -function Ab(a, b, c, d, e, f, g) { +function Bb(a, c, b, e, d, f, g) { let h = a.length, k = a; if (1 < h) { - k = Ya(a, b, c, d, e, f, g); + k = $a(a, c, b, e, d, f, g); } else if (1 === h) { - return g ? Ta.call(null, a[0], c, d) : new Y(a[0], this); + return g ? Sa.call(null, a[0], b, e) : new Y(a[0], this); } return g ? k : new Y(k, this); } -function xb(a, b, c, d, e, f, g) { - a = yb(this, a, b, c, d, e, f, g); +function yb(a, c, b, e, d, f, g) { + a = zb(this, a, c, b, e, d, f, g); return this.db ? a.then(function(h) { - return e ? h || [] : new Y(h, this); - }) : a && a.length ? e ? Ta.call(this, a, c, d) : new Y(a, this) : e ? [] : new Y([], this); + return d ? h || [] : new Y(h, this); + }) : a && a.length ? d ? Sa.call(this, a, b, e) : new Y(a, this) : d ? [] : new Y([], this); } -function zb(a, b, c, d) { - let e = []; +function Ab(a, c, b, e) { + let d = []; if (a && a.length) { - if (a.length <= d) { - b.push(a); + if (a.length <= e) { + c.push(a); return; } - for (let f = 0, g; f < d; f++) { + for (let f = 0, g; f < e; f++) { if (g = a[f]) { - e[f] = g; + d[f] = g; } } - if (e.length) { - b.push(e); + if (d.length) { + c.push(d); return; } } - if (!c) { - return e; + if (!b) { + return d; } } -function yb(a, b, c, d, e, f, g, h) { +function zb(a, c, b, e, d, f, g, h) { let k; - c && (k = a.bidirectional && b > c) && (k = c, c = b, b = k); + b && (k = a.bidirectional && c > b) && (k = b, b = c, c = k); if (a.db) { - return a.db.get(b, c, d, e, f, g, h); + return a.db.get(c, b, e, d, f, g, h); } - a = c ? (a = a.ctx.get(c)) && a.get(b) : a.map.get(b); + a = b ? (a = a.ctx.get(b)) && a.get(c) : a.map.get(c); return a; } -;function P(a, b) { - if (!this || this.constructor !== P) { - return new P(a); +;function V(a, c) { + if (!this || this.constructor !== V) { + return new V(a); } if (a) { - var c = M(a) ? a : a.preset; - c && (ub[c] || console.warn("Preset not found: " + c), a = Object.assign({}, ub[c], a)); + var b = N(a) ? a : a.preset; + b && (vb[b] || console.warn("Preset not found: " + b), a = Object.assign({}, vb[b], a)); } else { a = {}; } - c = a.context; - const d = !0 === c ? {depth:1} : c || {}, e = M(a.encoder) ? nb[a.encoder] : a.encode || a.encoder || {}; - this.encoder = e.encode ? e : "object" === typeof e ? new na(e) : {encode:e}; + b = a.context; + const e = !0 === b ? {depth:1} : b || {}, d = N(a.encoder) ? wa[a.encoder] : a.encode || a.encoder || {}; + this.encoder = d.encode ? d : "object" === typeof d ? new la(d) : {encode:d}; this.resolution = a.resolution || 9; - this.tokenize = c = (c = a.tokenize) && "default" !== c && "exact" !== c && c || "strict"; - this.depth = "strict" === c && d.depth || 0; - this.bidirectional = !1 !== d.bidirectional; + this.tokenize = b = (b = a.tokenize) && "default" !== b && "exact" !== b && b || "strict"; + this.depth = "strict" === b && e.depth || 0; + this.bidirectional = !1 !== e.bidirectional; this.fastupdate = !!a.fastupdate; this.score = a.score || null; - d && d.depth && "strict" !== this.tokenize && console.warn('Context-Search could not applied, because it is just supported when using the tokenizer "strict".'); - (c = a.keystore || 0) && (this.keystore = c); - this.map = c ? new V(c) : new Map(); - this.ctx = c ? new V(c) : new Map(); - this.reg = b || (this.fastupdate ? c ? new V(c) : new Map() : c ? new W(c) : new Set()); - this.U = d.resolution || 3; - this.rtl = e.rtl || a.rtl || !1; - this.cache = (c = a.cache || null) && new pb(c); + e && e.depth && "strict" !== this.tokenize && console.warn('Context-Search could not applied, because it is just supported when using the tokenizer "strict".'); + (b = a.keystore || 0) && (this.keystore = b); + this.map = b ? new R(b) : new Map(); + this.ctx = b ? new R(b) : new Map(); + this.reg = c || (this.fastupdate ? b ? new R(b) : new Map() : b ? new S(b) : new Set()); + this.T = e.resolution || 3; + this.rtl = d.rtl || a.rtl || !1; + this.cache = (b = a.cache || null) && new na(b); this.resolve = !1 !== a.resolve; - if (c = a.db) { - this.db = this.mount(c); + if (b = a.db) { + this.db = this.mount(b); } - this.T = !1 !== a.commit; + this.S = !1 !== a.commit; this.commit_task = []; this.commit_timer = null; this.priority = a.priority || 4; } -w = P.prototype; +w = V.prototype; w.mount = function(a) { this.commit_timer && (clearTimeout(this.commit_timer), this.commit_timer = null); return a.mount(this); }; -w.commit = function(a, b) { +w.commit = function(a, c) { this.commit_timer && (clearTimeout(this.commit_timer), this.commit_timer = null); - return this.db.commit(this, a, b); + return this.db.commit(this, a, c); }; w.destroy = function() { this.commit_timer && (clearTimeout(this.commit_timer), this.commit_timer = null); return this.db.destroy(); }; -function tb(a) { +function ub(a) { a.commit_timer || (a.commit_timer = setTimeout(function() { a.commit_timer = null; a.db.commit(a, void 0, void 0); @@ -2403,101 +2493,101 @@ w.clear = function() { this.db && (this.commit_timer && clearTimeout(this.commit_timer), this.commit_timer = null, this.commit_task = [{clear:!0}]); return this; }; -w.append = function(a, b) { - return this.add(a, b, !0); +w.append = function(a, c) { + return this.add(a, c, !0); }; w.contain = function(a) { return this.db ? this.db.has(a) : this.reg.has(a); }; -w.update = function(a, b) { - const c = this, d = this.remove(a); - return d && d.then ? d.then(() => c.add(a, b)) : this.add(a, b); +w.update = function(a, c) { + const b = this, e = this.remove(a); + return e && e.then ? e.then(() => b.add(a, c)) : this.add(a, c); }; w.cleanup = function() { if (!this.fastupdate) { return console.info('Cleanup the index isn\'t required when not using "fastupdate".'), this; } - sb(this.map); - this.depth && sb(this.ctx); + tb(this.map); + this.depth && tb(this.ctx); return this; }; -w.searchCache = rb; -w.export = function(a, b, c = 0, d = 0) { - let e, f; - switch(d) { +w.searchCache = ma; +w.export = function(a, c, b = 0, e = 0) { + let d, f; + switch(e) { case 0: - e = "reg"; - f = Ha(this.reg); + d = "reg"; + f = pb(this.reg); break; case 1: - e = "cfg"; + d = "cfg"; f = null; break; case 2: - e = "map"; - f = Da(this.map, this.reg.size); + d = "map"; + f = lb(this.map, this.reg.size); break; case 3: - e = "ctx"; - f = Fa(this.ctx, this.reg.size); + d = "ctx"; + f = nb(this.ctx, this.reg.size); break; default: return; } - return Ja.call(this, a, b, e, f, c, d); + return rb.call(this, a, c, d, f, b, e); }; -w.import = function(a, b) { - if (b) { - switch("string" === typeof b && (b = JSON.parse(b)), a = a.split("."), "json" === a[a.length - 1] && a.pop(), 3 === a.length && a.shift(), a = 1 < a.length ? a[1] : a[0], a) { +w.import = function(a, c) { + if (c) { + switch("string" === typeof c && (c = JSON.parse(c)), a = a.split("."), "json" === a[a.length - 1] && a.pop(), 3 === a.length && a.shift(), a = 1 < a.length ? a[1] : a[0], a) { case "reg": this.fastupdate = !1; - this.reg = Ia(b, this.reg); + this.reg = qb(c, this.reg); break; case "map": - this.map = Ea(b, this.map); + this.map = mb(c, this.map); break; case "ctx": - this.ctx = Ga(b, this.ctx); + this.ctx = ob(c, this.ctx); } } }; w.serialize = function(a = !0) { - let b = "", c = "", d = ""; + let c = "", b = "", e = ""; if (this.reg.size) { let f; - for (var e of this.reg.keys()) { - f || (f = typeof e), b += (b ? "," : "") + ("string" === f ? '"' + e + '"' : e); + for (var d of this.reg.keys()) { + f || (f = typeof d), c += (c ? "," : "") + ("string" === f ? '"' + d + '"' : d); } - b = "index.reg=new Set([" + b + "]);"; - c = Ka(this.map, f); - c = "index.map=new Map([" + c + "]);"; + c = "index.reg=new Set([" + c + "]);"; + b = sb(this.map, f); + b = "index.map=new Map([" + b + "]);"; for (const g of this.ctx.entries()) { - e = g[0]; - let h = Ka(g[1], f); + d = g[0]; + let h = sb(g[1], f); h = "new Map([" + h + "])"; - h = '["' + e + '",' + h + "]"; - d += (d ? "," : "") + h; + h = '["' + d + '",' + h + "]"; + e += (e ? "," : "") + h; } - d = "index.ctx=new Map([" + d + "]);"; + e = "index.ctx=new Map([" + e + "]);"; } - return a ? "function inject(index){" + b + c + d + "}" : b + c + d; + return a ? "function inject(index){" + c + b + e + "}" : c + b + e; }; -sa(P.prototype); -const Bb = "undefined" !== typeof window && (window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB), Cb = ["map", "ctx", "tag", "reg", "cfg"], Db = G(); -function Eb(a, b = {}) { - if (!this || this.constructor !== Eb) { - return new Eb(a, b); +Fa(V.prototype); +const Cb = "undefined" !== typeof window && (window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB), Db = ["map", "ctx", "tag", "reg", "cfg"], Eb = I(); +function Fb(a, c = {}) { + if (!this || this.constructor !== Fb) { + return new Fb(a, c); } - "object" === typeof a && (b = a, a = a.name); + "object" === typeof a && (c = a, a = a.name); a || console.info("Default storage space was used, because a name was not passed."); this.id = "flexsearch" + (a ? ":" + a.toLowerCase().replace(/[^a-z0-9_\-]/g, "") : ""); - this.field = b.field ? b.field.toLowerCase().replace(/[^a-z0-9_\-]/g, "") : ""; - this.type = b.type; + this.field = c.field ? c.field.toLowerCase().replace(/[^a-z0-9_\-]/g, "") : ""; + this.type = c.type; this.fastupdate = this.support_tag_search = !1; this.db = null; this.h = {}; } -w = Eb.prototype; +w = Fb.prototype; w.mount = function(a) { if (a.index) { return a.mount(this); @@ -2511,20 +2601,20 @@ w.open = function() { } let a = this; navigator.storage && navigator.storage.persist(); - Db[a.id] || (Db[a.id] = []); - Db[a.id].push(a.field); - const b = Bb.open(a.id, 1); - b.onupgradeneeded = function() { - const c = a.db = this.result; - for (let d = 0, e; d < Cb.length; d++) { - e = Cb[d]; - for (let f = 0, g; f < Db[a.id].length; f++) { - g = Db[a.id][f], c.objectStoreNames.contains(e + ("reg" !== e ? g ? ":" + g : "" : "")) || c.createObjectStore(e + ("reg" !== e ? g ? ":" + g : "" : "")); + Eb[a.id] || (Eb[a.id] = []); + Eb[a.id].push(a.field); + const c = Cb.open(a.id, 1); + c.onupgradeneeded = function() { + const b = a.db = this.result; + for (let e = 0, d; e < Db.length; e++) { + d = Db[e]; + for (let f = 0, g; f < Eb[a.id].length; f++) { + g = Eb[a.id][f], b.objectStoreNames.contains(d + ("reg" !== d ? g ? ":" + g : "" : "")) || b.createObjectStore(d + ("reg" !== d ? g ? ":" + g : "" : "")); } } }; - return a.db = Z(b, function(c) { - a.db = c; + return a.db = Z(c, function(b) { + a.db = b; a.db.onversionchange = function() { a.close(); }; @@ -2535,47 +2625,47 @@ w.close = function() { this.db = null; }; w.destroy = function() { - const a = Bb.deleteDatabase(this.id); + const a = Cb.deleteDatabase(this.id); return Z(a); }; w.clear = function() { const a = []; - for (let c = 0, d; c < Cb.length; c++) { - d = Cb[c]; - for (let e = 0, f; e < Db[this.id].length; e++) { - f = Db[this.id][e], a.push(d + ("reg" !== d ? f ? ":" + f : "" : "")); + for (let b = 0, e; b < Db.length; b++) { + e = Db[b]; + for (let d = 0, f; d < Eb[this.id].length; d++) { + f = Eb[this.id][d], a.push(e + ("reg" !== e ? f ? ":" + f : "" : "")); } } - const b = this.db.transaction(a, "readwrite"); - for (let c = 0; c < a.length; c++) { - b.objectStore(a[c]).clear(); + const c = this.db.transaction(a, "readwrite"); + for (let b = 0; b < a.length; b++) { + c.objectStore(a[b]).clear(); } - return Z(b); + return Z(c); }; -w.get = function(a, b, c = 0, d = 0, e = !0, f = !1) { - a = this.db.transaction((b ? "ctx" : "map") + (this.field ? ":" + this.field : ""), "readonly").objectStore((b ? "ctx" : "map") + (this.field ? ":" + this.field : "")).get(b ? b + ":" + a : a); +w.get = function(a, c, b = 0, e = 0, d = !0, f = !1) { + a = this.db.transaction((c ? "ctx" : "map") + (this.field ? ":" + this.field : ""), "readonly").objectStore((c ? "ctx" : "map") + (this.field ? ":" + this.field : "")).get(c ? c + ":" + a : a); const g = this; return Z(a).then(function(h) { let k = []; if (!h || !h.length) { return k; } - if (e) { - if (!c && !d && 1 === h.length) { + if (d) { + if (!b && !e && 1 === h.length) { return h[0]; } - for (let l = 0, m; l < h.length; l++) { - if ((m = h[l]) && m.length) { - if (d >= m.length) { - d -= m.length; + for (let m = 0, l; m < h.length; m++) { + if ((l = h[m]) && l.length) { + if (e >= l.length) { + e -= l.length; continue; } - const p = c ? d + Math.min(m.length - d, c) : m.length; - for (let u = d; u < p; u++) { - k.push(m[u]); + const n = b ? e + Math.min(l.length - e, b) : l.length; + for (let u = e; u < n; u++) { + k.push(l[u]); } - d = 0; - if (k.length === c) { + e = 0; + if (k.length === b) { break; } } @@ -2585,90 +2675,90 @@ w.get = function(a, b, c = 0, d = 0, e = !0, f = !1) { return h; }); }; -w.tag = function(a, b = 0, c = 0, d = !1) { +w.tag = function(a, c = 0, b = 0, e = !1) { a = this.db.transaction("tag" + (this.field ? ":" + this.field : ""), "readonly").objectStore("tag" + (this.field ? ":" + this.field : "")).get(a); - const e = this; + const d = this; return Z(a).then(function(f) { - if (!f || !f.length || c >= f.length) { + if (!f || !f.length || b >= f.length) { return []; } - if (!b && !c) { + if (!c && !b) { return f; } - f = f.slice(c, c + b); - return d ? e.enrich(f) : f; + f = f.slice(b, b + c); + return e ? d.enrich(f) : f; }); }; w.enrich = function(a) { "object" !== typeof a && (a = [a]); - const b = this.db.transaction("reg", "readonly").objectStore("reg"), c = []; - for (let d = 0; d < a.length; d++) { - c[d] = Z(b.get(a[d])); + const c = this.db.transaction("reg", "readonly").objectStore("reg"), b = []; + for (let e = 0; e < a.length; e++) { + b[e] = Z(c.get(a[e])); } - return Promise.all(c).then(function(d) { - for (let e = 0; e < d.length; e++) { - d[e] = {id:a[e], doc:d[e] ? JSON.parse(d[e]) : null}; + return Promise.all(b).then(function(e) { + for (let d = 0; d < e.length; d++) { + e[d] = {id:a[d], doc:e[d] ? JSON.parse(e[d]) : null}; } - return d; + return e; }); }; w.has = function(a) { a = this.db.transaction("reg", "readonly").objectStore("reg").getKey(a); - return Z(a).then(function(b) { - return !!b; + return Z(a).then(function(c) { + return !!c; }); }; w.search = null; w.info = function() { }; -w.transaction = function(a, b, c) { +w.transaction = function(a, c, b) { a += "reg" !== a ? this.field ? ":" + this.field : "" : ""; - let d = this.h[a + ":" + b]; - if (d) { - return c.call(this, d); + let e = this.h[a + ":" + c]; + if (e) { + return b.call(this, e); } - let e = this.db.transaction(a, b); - this.h[a + ":" + b] = d = e.objectStore(a); - const f = c.call(this, d); - this.h[a + ":" + b] = null; - return Z(e).finally(function() { - e = d = null; + let d = this.db.transaction(a, c); + this.h[a + ":" + c] = e = d.objectStore(a); + const f = b.call(this, e); + this.h[a + ":" + c] = null; + return Z(d).finally(function() { + d = e = null; return f; }); }; -w.commit = async function(a, b, c) { - if (b) { +w.commit = async function(a, c, b) { + if (c) { await this.clear(), a.commit_task = []; } else { - let d = a.commit_task; + let e = a.commit_task; a.commit_task = []; - for (let e = 0, f; e < d.length; e++) { - if (f = d[e], f.clear) { + for (let d = 0, f; d < e.length; d++) { + if (f = e[d], f.clear) { await this.clear(); - b = !0; + c = !0; break; } else { - d[e] = f.del; + e[d] = f.del; } } - b || (c || (d = d.concat(ea(a.reg))), d.length && await this.remove(d)); + c || (b || (e = e.concat(ca(a.reg))), e.length && await this.remove(e)); } - a.reg.size && (await this.transaction("map", "readwrite", function(d) { - for (const e of a.map) { - const f = e[0], g = e[1]; - g.length && (b ? d.put(g, f) : d.get(f).onsuccess = function() { + a.reg.size && (await this.transaction("map", "readwrite", function(e) { + for (const d of a.map) { + const f = d[0], g = d[1]; + g.length && (c ? e.put(g, f) : e.get(f).onsuccess = function() { let h = this.result; var k; if (h && h.length) { - const l = Math.max(h.length, g.length); - for (let m = 0, p, u; m < l; m++) { - if ((u = g[m]) && u.length) { - if ((p = h[m]) && p.length) { + const m = Math.max(h.length, g.length); + for (let l = 0, n, u; l < m; l++) { + if ((u = g[l]) && u.length) { + if ((n = h[l]) && n.length) { for (k = 0; k < u.length; k++) { - p.push(u[k]); + n.push(u[k]); } } else { - h[m] = u; + h[l] = u; } k = 1; } @@ -2676,119 +2766,119 @@ w.commit = async function(a, b, c) { } else { h = g, k = 1; } - k && d.put(h, f); + k && e.put(h, f); }); } - }), await this.transaction("ctx", "readwrite", function(d) { - for (const e of a.ctx) { - const f = e[0], g = e[1]; + }), await this.transaction("ctx", "readwrite", function(e) { + for (const d of a.ctx) { + const f = d[0], g = d[1]; for (const h of g) { - const k = h[0], l = h[1]; - l.length && (b ? d.put(l, f + ":" + k) : d.get(f + ":" + k).onsuccess = function() { - let m = this.result; - var p; - if (m && m.length) { - const u = Math.max(m.length, l.length); - for (let q = 0, r, n; q < u; q++) { - if ((n = l[q]) && n.length) { - if ((r = m[q]) && r.length) { - for (p = 0; p < n.length; p++) { - r.push(n[p]); + const k = h[0], m = h[1]; + m.length && (c ? e.put(m, f + ":" + k) : e.get(f + ":" + k).onsuccess = function() { + let l = this.result; + var n; + if (l && l.length) { + const u = Math.max(l.length, m.length); + for (let p = 0, t, r; p < u; p++) { + if ((r = m[p]) && r.length) { + if ((t = l[p]) && t.length) { + for (n = 0; n < r.length; n++) { + t.push(r[n]); } } else { - m[q] = n; + l[p] = r; } - p = 1; + n = 1; } } } else { - m = l, p = 1; + l = m, n = 1; } - p && d.put(m, f + ":" + k); + n && e.put(l, f + ":" + k); }); } } - }), a.store ? await this.transaction("reg", "readwrite", function(d) { - for (const e of a.store) { - const f = e[0], g = e[1]; - d.put("object" === typeof g ? JSON.stringify(g) : 1, f); + }), a.store ? await this.transaction("reg", "readwrite", function(e) { + for (const d of a.store) { + const f = d[0], g = d[1]; + e.put("object" === typeof g ? JSON.stringify(g) : 1, f); } - }) : a.bypass || await this.transaction("reg", "readwrite", function(d) { - for (const e of a.reg.keys()) { - d.put(1, e); + }) : a.bypass || await this.transaction("reg", "readwrite", function(e) { + for (const d of a.reg.keys()) { + e.put(1, d); } - }), a.tag && await this.transaction("tag", "readwrite", function(d) { - for (const e of a.tag) { - const f = e[0], g = e[1]; - g.length && (d.get(f).onsuccess = function() { + }), a.tag && await this.transaction("tag", "readwrite", function(e) { + for (const d of a.tag) { + const f = d[0], g = d[1]; + g.length && (e.get(f).onsuccess = function() { let h = this.result; h = h && h.length ? h.concat(g) : g; - d.put(h, f); + e.put(h, f); }); } }), a.map.clear(), a.ctx.clear(), a.tag && a.tag.clear(), a.store && a.store.clear(), a.document || a.reg.clear()); }; -function Fb(a, b, c) { - const d = a.value; - let e, f = 0; - for (let g = 0, h; g < d.length; g++) { - if (h = c ? d : d[g]) { - for (let k = 0, l, m; k < b.length; k++) { - if (m = b[k], l = h.indexOf(m), 0 <= l) { - if (e = 1, 1 < h.length) { - h.splice(l, 1); +function Gb(a, c, b) { + const e = a.value; + let d, f = 0; + for (let g = 0, h; g < e.length; g++) { + if (h = b ? e : e[g]) { + for (let k = 0, m, l; k < c.length; k++) { + if (l = c[k], m = h.indexOf(l), 0 <= m) { + if (d = 1, 1 < h.length) { + h.splice(m, 1); } else { - d[g] = []; + e[g] = []; break; } } } f += h.length; } - if (c) { + if (b) { break; } } - f ? e && a.update(d) : a.delete(); + f ? d && a.update(e) : a.delete(); a.continue(); } w.remove = function(a) { "object" !== typeof a && (a = [a]); - return Promise.all([this.transaction("map", "readwrite", function(b) { - b.openCursor().onsuccess = function() { - const c = this.result; - c && Fb(c, a); + return Promise.all([this.transaction("map", "readwrite", function(c) { + c.openCursor().onsuccess = function() { + const b = this.result; + b && Gb(b, a); }; - }), this.transaction("ctx", "readwrite", function(b) { - b.openCursor().onsuccess = function() { - const c = this.result; - c && Fb(c, a); + }), this.transaction("ctx", "readwrite", function(c) { + c.openCursor().onsuccess = function() { + const b = this.result; + b && Gb(b, a); }; - }), this.transaction("tag", "readwrite", function(b) { - b.openCursor().onsuccess = function() { - const c = this.result; - c && Fb(c, a, !0); + }), this.transaction("tag", "readwrite", function(c) { + c.openCursor().onsuccess = function() { + const b = this.result; + b && Gb(b, a, !0); }; - }), this.transaction("reg", "readwrite", function(b) { - for (let c = 0; c < a.length; c++) { - b.delete(a[c]); + }), this.transaction("reg", "readwrite", function(c) { + for (let b = 0; b < a.length; b++) { + c.delete(a[b]); } })]); }; -function Z(a, b) { - return new Promise((c, d) => { +function Z(a, c) { + return new Promise((b, e) => { a.onsuccess = a.oncomplete = function() { - b && b(this.result); - b = null; - c(this.result); + c && c(this.result); + c = null; + b(this.result); }; - a.onerror = a.onblocked = d; + a.onerror = a.onblocked = e; a = null; }); } -;const Gb = {Index:P, Charset:nb, Encoder:na, Document:Qa, Worker:Ba, Resolver:Y, IndexedDB:Eb, Language:{}}, Hb = "undefined" !== typeof self ? self : "undefined" !== typeof global ? global : self; -let Ib; -(Ib = Hb.define) && Ib.amd ? Ib([], function() { - return Gb; -}) : "object" === typeof Hb.exports ? Hb.exports = Gb : Hb.FlexSearch = Gb; +;const Hb = {Index:V, Charset:wa, Encoder:la, Document:Na, Worker:La, Resolver:Y, IndexedDB:Fb, Language:{}}, Ib = "undefined" !== typeof self ? self : "undefined" !== typeof global ? global : self; +let Jb; +(Jb = Ib.define) && Jb.amd ? Jb([], function() { + return Hb; +}) : "object" === typeof Ib.exports ? Ib.exports = Hb : Ib.FlexSearch = Hb; }(this||self)); diff --git a/dist/flexsearch.bundle.min.js b/dist/flexsearch.bundle.min.js index f89fd77..b2680c7 100644 --- a/dist/flexsearch.bundle.min.js +++ b/dist/flexsearch.bundle.min.js @@ -1,101 +1,105 @@ /**! - * FlexSearch.js v0.8.167 (Bundle) + * FlexSearch.js v0.8.200 (Bundle) * Author and Copyright: Thomas Wilkerling * Licence: Apache-2.0 * Hosted by Nextapps GmbH * https://github.com/nextapps-de/flexsearch */ -(function _f(self){'use strict';if(typeof module!=='undefined')self=module;else if(typeof process !== 'undefined')self=process;self._factory=_f;var v;function E(a,b,c){const d=typeof c,e=typeof a;if("undefined"!==d){if("undefined"!==e){if(c){if("function"===e&&d===e)return function(h){return a(c(h))};b=a.constructor;if(b===c.constructor){if(b===Array)return c.concat(a);if(b===Map){var f=new Map(c);for(var g of a)f.set(g[0],g[1]);return f}if(b===Set){g=new Set(c);for(f of a.values())g.add(f);return g}}}return a}return c}return"undefined"===e?b:a}function ca(a,b){return"undefined"===typeof a?b:a}function G(){return Object.create(null)} -function M(a){return"string"===typeof a}function da(a){return"object"===typeof a}function ea(a){const b=[];for(const c of a.keys())b.push(c);return b}function fa(a,b){if(M(b))a=a[b];else for(let c=0;a&&c"a1a".split(c).length; -this.numeric=E(a.numeric,d)}else{try{this.split=E(this.split,ia)}catch(e){this.split=/\s+/}this.numeric=E(a.numeric,E(this.numeric,!0))}this.prepare=E(a.prepare,null,this.prepare);this.finalize=E(a.finalize,null,this.finalize);c=a.filter;this.filter="function"===typeof c?c:E(c&&new Set(c),null,this.filter);this.dedupe=E(a.dedupe,!0,this.dedupe);this.matcher=E((c=a.matcher)&&new Map(c),null,this.matcher);this.mapper=E((c=a.mapper)&&new Map(c),null,this.mapper);this.stemmer=E((c=a.stemmer)&&new Map(c), -null,this.stemmer);this.replacer=E(a.replacer,null,this.replacer);this.minlength=E(a.minlength,1,this.minlength);this.maxlength=E(a.maxlength,1024,this.maxlength);this.rtl=E(a.rtl,!1,this.rtl);if(this.cache=c=E(a.cache,!0,this.cache))this.H=null,this.S="number"===typeof c?c:2E5,this.B=new Map,this.G=new Map,this.L=this.K=128;this.h="";this.M=null;this.A="";this.N=null;if(this.matcher)for(const e of this.matcher.keys())this.h+=(this.h?"|":"")+e;if(this.stemmer)for(const e of this.stemmer.keys())this.A+= -(this.A?"|":"")+e;return this};v.addStemmer=function(a,b){this.stemmer||(this.stemmer=new Map);this.stemmer.set(a,b);this.A+=(this.A?"|":"")+a;this.N=null;this.cache&&N(this);return this};v.addFilter=function(a){"function"===typeof a?this.filter=a:(this.filter||(this.filter=new Set),this.filter.add(a));this.cache&&N(this);return this}; -v.addMapper=function(a,b){if("object"===typeof a)return this.addReplacer(a,b);if(1a.length&&(this.dedupe||this.mapper))return this.addMapper(a,b);this.matcher||(this.matcher=new Map);this.matcher.set(a,b);this.h+=(this.h?"|":"")+a;this.M=null;this.cache&&N(this);return this}; -v.addReplacer=function(a,b){if("string"===typeof a)return this.addMatcher(a,b);this.replacer||(this.replacer=[]);this.replacer.push(a,b);this.cache&&N(this);return this}; -v.encode=function(a,b){if(this.cache&&a.length<=this.K)if(this.H){if(this.B.has(a))return this.B.get(a)}else this.H=setTimeout(N,50,this);this.normalize&&("function"===typeof this.normalize?a=this.normalize(a):a=ma?a.normalize("NFKD").replace(ma,"").toLowerCase():a.toLowerCase());this.prepare&&(a=this.prepare(a));this.numeric&&3this.maxlength)){if(b){if(e[m])continue;e[m]=1}else{if(f===m)continue;f=m}if(c)d.push(m);else if(!this.filter||("function"===typeof this.filter?this.filter(m):!this.filter.has(m))){if(this.cache&&m.length<=this.L)if(this.H){var k=this.G.get(m);if(k||""===k){k&&d.push(k);continue}}else this.H=setTimeout(N,50,this);if(this.stemmer){this.N||(this.N=new RegExp("(?!^)("+ -this.A+")$"));let u;for(;u!==m&&2this.stemmer.get(r))}if(m&&(this.mapper||this.dedupe&&1this.matcher.get(u)));if(m&&this.replacer)for(k=0;m&&kthis.S&&(this.G.clear(),this.L=this.L/1.1|0));if(m){if(m!==n)if(b){if(e[m])continue;e[m]=1}else{if(g===m)continue;g=m}d.push(m)}}}this.finalize&&(d=this.finalize(d)||d);this.cache&&a.length<=this.K&&(this.B.set(a,d),this.B.size>this.S&&(this.B.clear(),this.K=this.K/1.1|0));return d};function N(a){a.H=null;a.B.clear();a.G.clear()};let qa,ra;async function sa(a){a=a.data;var b=a.task;const c=a.id;let d=a.args;switch(b){case "init":ra=a.options||{};(b=a.factory)?(Function("return "+b)()(self),qa=new self.FlexSearch.Index(ra),delete self.FlexSearch):qa=new O(ra);postMessage({id:c});break;default:let e;"export"===b&&(d[1]?(d[0]=ra.export,d[2]=0,d[3]=1):d=null);"import"===b?d[0]&&(a=await ra.import.call(qa,d[0]),qa.import(d[0],a)):(e=d&&qa[b].apply(qa,d))&&e.then&&(e=await e);postMessage("search"===b?{id:c,msg:e}:{id:c})}};function ta(a){ua.call(a,"add");ua.call(a,"append");ua.call(a,"search");ua.call(a,"update");ua.call(a,"remove");ua.call(a,"searchCache")}let va,ya,za;function Aa(){va=za=0} -function ua(a){this[a+"Async"]=function(){const b=arguments;var c=b[b.length-1];let d;"function"===typeof c&&(d=c,delete b[b.length-1]);va?za||(za=Date.now()-ya>=this.priority*this.priority*3):(va=setTimeout(Aa,0),ya=Date.now());if(za){const f=this;return new Promise(g=>{setTimeout(function(){g(f[a+"Async"].apply(f,b))},0)})}const e=this[a].apply(this,b);c=e.then?e:new Promise(f=>f(e));d&&c.then(d);return c}};let Ba=0; -function Ca(a={},b){function c(h){function k(l){l=l.data||l;const m=l.id,n=m&&f.h[m];n&&(n(l.msg),delete f.h[m])}this.worker=h;this.h=G();if(this.worker){e?this.worker.on("message",k):this.worker.onmessage=k;if(a.config)return new Promise(function(l){f.h[++Ba]=function(){l(f);1E9=g.length)b-=g.length;else{b=g[d?"splice":"slice"](b,c);const h=b.length;if(h&&(e=e.length?e.concat(b):b,c-=h,d&&(a.length-=h),!c))break;b=0}return e} -function R(a){if(!this||this.constructor!==R)return new R(a);this.index=a?[a]:[];this.length=a?a.length:0;const b=this;return new Proxy([],{get(c,d){if("length"===d)return b.length;if("push"===d)return function(e){b.index[b.index.length-1].push(e);b.length++};if("pop"===d)return function(){if(b.length)return b.length--,b.index[b.index.length-1].pop()};if("indexOf"===d)return function(e){let f=0;for(let g=0,h,k;gb?b?a.slice(c,c+b):a.slice(c):a,d?X.call(this,a):a;let e=[];for(let f=0,g,h;f=h){c-=h;continue}cb&&(g=g.slice(0,b),h=b);if(!e.length&&h>=b)return d?X.call(this,g):g;e.push(g);b-=h;if(!b)break}e=1a.length?this.result=a[0]:(this.result=Wa(a,c,d,!1,this.h),d=0));return f?this.resolve(c,d,e):this};Y.prototype.and=function(){let a=this.result.length,b,c,d,e;if(!a){const f=arguments[0];f&&(a=!!f.suggest,e=f.resolve,b=f.limit,c=f.offset,d=f.enrich&&e)}if(a){const {O:f,P:g,limit:h,offset:k,enrich:l,resolve:m,suggest:n}=Ua(this,"and",arguments);return Xa.call(this,f,g,h,k,l,m,n)}return e?this.resolve(b,c,d):this}; -function Xa(a,b,c,d,e,f,g){if(b.length){const h=this;return Promise.all(b).then(function(k){a=[];for(let l=0,m;la.length)this.result=a[0];else{if(b=ha(a))return this.result=Ya(a,b,c,d,g,this.h,f),f?e?X.call(this.index,this.result):this.result:this;this.result=[]}else g||(this.result=a);return f?this.resolve(c,d,e):this};Y.prototype.xor=function(){const {O:a,P:b,limit:c,offset:d,enrich:e,resolve:f,suggest:g}=Ua(this,"xor",arguments);return Za.call(this,a,b,c,d,e,f,g)}; -function Za(a,b,c,d,e,f,g){if(b.length){const h=this;return Promise.all(b).then(function(k){a=[];for(let l=0,m;la.length)this.result=a[0];else return this.result=$a.call(this,a,c,d,f,this.h),f?e?X.call(this.index,this.result):this.result:this;else g||(this.result=a);return f?this.resolve(c,d,e):this} -function $a(a,b,c,d,e){const f=[],g=G();let h=0;for(let k=0,l;kD&&(D=t.length+(t?1:0)),H=t.length+(t?1:0)+F.length,B+=C,xa.push(w.length),w.push({match:F})),t+=(t?" ":"")+F)}if(!z)A=y[x],t+=(t?" ":"")+A,k&&w.push({text:A});else if(k&&B>=k)break}B=xa.length*(f.length-2);if(r||p||k&&t.length-B>k)if(B=k+B-2*u,x=H-D, -0y&&(y=0)),w=w.length-1){if(J>= -w.length){x[z+1]=1;J>=y.length&&(B[z+1]=1);continue}C-=u}t=w[J].text;if(K=p&&I[z])if(0K)if(x[z+1]=1,l)t=t.substring(0,K);else continue;(K-=t.length)||(K=-1);I[z]=K}else{x[z+1]=1;continue}if(C+t.length+1<=k)t=" "+t,H[z]+=t;else if(l)T=k-C-1,0=J){if(0>J){x[z]=1;B[z]=1;continue}C-=u}t=w[J].text;if(K=r&&A[z])if(0K)if(x[z]=1,l)t=t.substring(t.length- -K);else continue;(K-=t.length)||(K=-1);A[z]=K}else{x[z]=1;continue}if(C+t.length+1<=k)t+=" ",H[z]=t+H[z];else if(l)T=t.length+1-(k-C),0<=T&&T=y.length-1?Na=1:Jc||d?k.slice(d,c+d):k;else{if(ac||d)k=k.slice(d,c+d)}else{e= -[];for(let n=0,u;nd)d-=u.length;else{if(c&&u.length>c||d)u=u.slice(d,c+d),c-=u.length,d&&(d-=u.length);e.push(u);if(!c)break}k=e}}return k} -function Wa(a,b,c,d,e){const f=[],g=G();let h;var k=a.length;let l;if(d)for(e=k-1;0<=e;e--){if(l=(d=a[e])&&d.length)for(k=0;kc||d)a=a.slice(d,d+c);e&&(a=X.call(this,a))}return a} -function X(a){if(!this||!this.store)return a;const b=Array(a.length);for(let c=0,d;cthis.limit&&this.cache.delete(this.cache.keys().next().value)}; -pb.prototype.get=function(a){const b=this.cache.get(a);b&&this.h!==a&&(this.cache.delete(a),this.cache.set(this.h=a,b));return b};pb.prototype.remove=function(a){for(const b of this.cache){const c=b[0];b[1].includes(a)&&this.cache.delete(c)}};pb.prototype.clear=function(){this.cache.clear();this.h=""};O.prototype.remove=function(a,b){const c=this.reg.size&&(this.fastupdate?this.reg.get(a):this.reg.has(a));if(c){if(this.fastupdate)for(let d=0,e,f;dq;f--){g=p.substring(q,f);t=this.rtl?e-1-q:q;var h=this.score?this.score(b,p,r,g,t):vb(u, -l,r,e,t);wb(this,n,g,h,a,c)}break}case "bidirectional":case "reverse":if(1g?0:1),l,r,h-1,k-1),t=this.bidirectional&&p>f;wb(this,m,t?f:p,q,a,c,t?p:f)}}}}this.fastupdate||this.reg.add(a)}else b=""}this.db&&(b||this.commit_task.push({del:a}),this.T&&tb(this));return this}; -function wb(a,b,c,d,e,f,g){let h=g?a.ctx:a.map,k;if(!b[c]||g&&!(k=b[c])[g])if(g?(b=k||(b[c]=G()),b[g]=1,(k=h.get(g))?h=k:h.set(g,h=new Map)):b[c]=1,(k=h.get(c))?h=k:h.set(c,h=k=[]),h=h[d]||(h[d]=[]),!f||!h.includes(e)){if(h.length===2**31-1){b=new R(h);if(a.fastupdate)for(let l of a.reg.values())l.includes(h)&&(l[l.indexOf(h)]=b);k[d]=h=b}h.push(e);a.fastupdate&&((d=a.reg.get(e))?d.push(h):a.reg.set(e,[h]))}} -function vb(a,b,c,d,e){return c&&1c)&&(k=c,c=b,b=k);if(a.db)return a.db.get(b,c,d,e,f,g,h);a=c?(a=a.ctx.get(c))&&a.get(b):a.map.get(b);return a};function O(a,b){if(!this||this.constructor!==O)return new O(a);if(a){var c=M(a)?a:a.preset;c&&(a=Object.assign({},ub[c],a))}else a={};c=a.context;const d=!0===c?{depth:1}:c||{},e=M(a.encoder)?nb[a.encoder]:a.encode||a.encoder||{};this.encoder=e.encode?e:"object"===typeof e?new na(e):{encode:e};this.resolution=a.resolution||9;this.tokenize=c=(c=a.tokenize)&&"default"!==c&&"exact"!==c&&c||"strict";this.depth="strict"===c&&d.depth||0;this.bidirectional=!1!==d.bidirectional;this.fastupdate=!!a.fastupdate; -this.score=a.score||null;(c=a.keystore||0)&&(this.keystore=c);this.map=c?new U(c):new Map;this.ctx=c?new U(c):new Map;this.reg=b||(this.fastupdate?c?new U(c):new Map:c?new V(c):new Set);this.U=d.resolution||3;this.rtl=e.rtl||a.rtl||!1;this.cache=(c=a.cache||null)&&new pb(c);this.resolve=!1!==a.resolve;if(c=a.db)this.db=this.mount(c);this.T=!1!==a.commit;this.commit_task=[];this.commit_timer=null;this.priority=a.priority||4}v=O.prototype; -v.mount=function(a){this.commit_timer&&(clearTimeout(this.commit_timer),this.commit_timer=null);return a.mount(this)};v.commit=function(a,b){this.commit_timer&&(clearTimeout(this.commit_timer),this.commit_timer=null);return this.db.commit(this,a,b)};v.destroy=function(){this.commit_timer&&(clearTimeout(this.commit_timer),this.commit_timer=null);return this.db.destroy()};function tb(a){a.commit_timer||(a.commit_timer=setTimeout(function(){a.commit_timer=null;a.db.commit(a,void 0,void 0)},1))} -v.clear=function(){this.map.clear();this.ctx.clear();this.reg.clear();this.cache&&this.cache.clear();this.db&&(this.commit_timer&&clearTimeout(this.commit_timer),this.commit_timer=null,this.commit_task=[{clear:!0}]);return this};v.append=function(a,b){return this.add(a,b,!0)};v.contain=function(a){return this.db?this.db.has(a):this.reg.has(a)};v.update=function(a,b){const c=this,d=this.remove(a);return d&&d.then?d.then(()=>c.add(a,b)):this.add(a,b)}; -v.cleanup=function(){if(!this.fastupdate)return this;sb(this.map);this.depth&&sb(this.ctx);return this};v.searchCache=rb;v.export=function(a,b,c=0,d=0){let e,f;switch(d){case 0:e="reg";f=Ia(this.reg);break;case 1:e="cfg";f=null;break;case 2:e="map";f=Ea(this.map,this.reg.size);break;case 3:e="ctx";f=Ga(this.ctx,this.reg.size);break;default:return}return Ka.call(this,a,b,e,f,c,d)}; -v.import=function(a,b){if(b)switch("string"===typeof b&&(b=JSON.parse(b)),a=a.split("."),"json"===a[a.length-1]&&a.pop(),3===a.length&&a.shift(),a=1=m.length){d-=m.length;continue}const n=c?d+Math.min(m.length-d,c):m.length;for(let u=d;u=f.length)return[];if(!b&&!c)return f;f=f.slice(c,c+b);return d?e.enrich(f):f})}; -v.enrich=function(a){"object"!==typeof a&&(a=[a]);const b=this.db.transaction("reg","readonly").objectStore("reg"),c=[];for(let d=0;d{a.onsuccess=a.oncomplete=function(){b&&b(this.result);b=null;c(this.result)};a.onerror=a.onblocked=d;a=null})};const Gb={Index:O,Charset:nb,Encoder:na,Document:W,Worker:Ca,Resolver:Y,IndexedDB:Eb,Language:{}},Hb="undefined"!==typeof self?self:"undefined"!==typeof global?global:self;let Ib;(Ib=Hb.define)&&Ib.amd?Ib([],function(){return Gb}):"object"===typeof Hb.exports?Hb.exports=Gb:Hb.FlexSearch=Gb;}(this||self)); +(function _f(self){'use strict';if(typeof module!=='undefined')self=module;else if(typeof process !== 'undefined')self=process;self._factory=_f;var w;function H(a,c,b){const e=typeof b,d=typeof a;if("undefined"!==e){if("undefined"!==d){if(b){if("function"===d&&e===d)return function(h){return a(b(h))};c=a.constructor;if(c===b.constructor){if(c===Array)return b.concat(a);if(c===Map){var f=new Map(b);for(var g of a)f.set(g[0],g[1]);return f}if(c===Set){g=new Set(b);for(f of a.values())g.add(f);return g}}}return a}return b}return"undefined"===d?c:a}function aa(a,c){return"undefined"===typeof a?c:a}function I(){return Object.create(null)} +function M(a){return"string"===typeof a}function ba(a){return"object"===typeof a}function ca(a){const c=[];for(const b of a.keys())c.push(b);return c}function ea(a,c){if(M(c))a=a[c];else for(let b=0;a&&b"a1a".split(b).length; +this.numeric=H(a.numeric,e)}else{try{this.split=H(this.split,fa)}catch(d){this.split=/\s+/}this.numeric=H(a.numeric,H(this.numeric,!0))}this.prepare=H(a.prepare,null,this.prepare);this.finalize=H(a.finalize,null,this.finalize);b=a.filter;this.filter="function"===typeof b?b:H(b&&new Set(b),null,this.filter);this.dedupe=H(a.dedupe,!0,this.dedupe);this.matcher=H((b=a.matcher)&&new Map(b),null,this.matcher);this.mapper=H((b=a.mapper)&&new Map(b),null,this.mapper);this.stemmer=H((b=a.stemmer)&&new Map(b), +null,this.stemmer);this.replacer=H(a.replacer,null,this.replacer);this.minlength=H(a.minlength,1,this.minlength);this.maxlength=H(a.maxlength,1024,this.maxlength);this.rtl=H(a.rtl,!1,this.rtl);if(this.cache=b=H(a.cache,!0,this.cache))this.I=null,this.R="number"===typeof b?b:2E5,this.B=new Map,this.H=new Map,this.M=this.L=128;this.h="";this.N=null;this.A="";this.O=null;if(this.matcher)for(const d of this.matcher.keys())this.h+=(this.h?"|":"")+d;if(this.stemmer)for(const d of this.stemmer.keys())this.A+= +(this.A?"|":"")+d;return this};w.addStemmer=function(a,c){this.stemmer||(this.stemmer=new Map);this.stemmer.set(a,c);this.A+=(this.A?"|":"")+a;this.O=null;this.cache&&Q(this);return this};w.addFilter=function(a){"function"===typeof a?this.filter=a:(this.filter||(this.filter=new Set),this.filter.add(a));this.cache&&Q(this);return this}; +w.addMapper=function(a,c){if("object"===typeof a)return this.addReplacer(a,c);if(1a.length&&(this.dedupe||this.mapper))return this.addMapper(a,c);this.matcher||(this.matcher=new Map);this.matcher.set(a,c);this.h+=(this.h?"|":"")+a;this.N=null;this.cache&&Q(this);return this}; +w.addReplacer=function(a,c){if("string"===typeof a)return this.addMatcher(a,c);this.replacer||(this.replacer=[]);this.replacer.push(a,c);this.cache&&Q(this);return this}; +w.encode=function(a,c){if(this.cache&&a.length<=this.L)if(this.I){if(this.B.has(a))return this.B.get(a)}else this.I=setTimeout(Q,50,this);this.normalize&&("function"===typeof this.normalize?a=this.normalize(a):a=ka?a.normalize("NFKD").replace(ka,"").toLowerCase():a.toLowerCase());this.prepare&&(a=this.prepare(a));this.numeric&&3this.maxlength)){if(c){if(d[l])continue;d[l]=1}else{if(f===l)continue;f=l}if(b)e.push(l);else if(!this.filter||("function"===typeof this.filter?this.filter(l):!this.filter.has(l))){if(this.cache&&l.length<=this.M)if(this.I){var k=this.H.get(l);if(k||""===k){k&&e.push(k);continue}}else this.I=setTimeout(Q,50,this);if(this.stemmer){this.O||(this.O=new RegExp("(?!^)("+ +this.A+")$"));let u;for(;u!==l&&2this.stemmer.get(p))}if(l&&(this.mapper||this.dedupe&&1this.matcher.get(u)));if(l&&this.replacer)for(k=0;l&&kthis.R&&(this.H.clear(),this.M=this.M/1.1|0));if(l){if(l!==n)if(c){if(d[l])continue;d[l]=1}else{if(g===l)continue;g=l}e.push(l)}}}this.finalize&&(e=this.finalize(e)||e);this.cache&&a.length<=this.L&&(this.B.set(a,e),this.B.size>this.R&&(this.B.clear(),this.L=this.L/1.1|0));return e};function Q(a){a.I=null;a.B.clear();a.H.clear()};function ma(a,c,b){b||(c||"object"!==typeof a?"object"===typeof c&&(b=c,c=0):b=a);b&&(a=b.query||a,c=b.limit||c);let e=""+(c||0);b&&(e+=(b.offset||0)+!!b.context+!!b.suggest+(!1!==b.resolve)+(b.resolution||this.resolution)+(b.boost||0));a=(""+a).toLowerCase();this.cache||(this.cache=new R);let d=this.cache.get(a+e);if(!d){const f=b&&b.cache;f&&(b.cache=!1);d=this.search(a,c,b);f&&(b.cache=f);this.cache.set(a+e,d)}return d}function R(a){this.limit=a&&!0!==a?a:1E3;this.cache=new Map;this.h=""} +R.prototype.set=function(a,c){this.cache.set(this.h=a,c);this.cache.size>this.limit&&this.cache.delete(this.cache.keys().next().value)};R.prototype.get=function(a){const c=this.cache.get(a);c&&this.h!==a&&(this.cache.delete(a),this.cache.set(this.h=a,c));return c};R.prototype.remove=function(a){for(const c of this.cache){const b=c[0];c[1].includes(a)&&this.cache.delete(b)}};R.prototype.clear=function(){this.cache.clear();this.h=""};const na={normalize:!1,numeric:!1,dedupe:!1};const qa={};const ra=new Map([["b","p"],["v","f"],["w","f"],["z","s"],["x","s"],["d","t"],["n","m"],["c","k"],["g","k"],["j","k"],["q","k"],["i","e"],["y","e"],["u","o"]]);const sa=new Map([["ae","a"],["oe","o"],["sh","s"],["kh","k"],["th","t"],["ph","f"],["pf","f"]]),ta=[/([^aeo])h(.)/g,"$1$2",/([aeo])h([^aeo]|$)/g,"$1$2",/(.)\1+/g,"$1"];const ua={a:"",e:"",i:"",o:"",u:"",y:"",b:1,f:1,p:1,v:1,c:2,g:2,j:2,k:2,q:2,s:2,x:2,z:2,"\u00df":2,d:3,t:3,l:4,m:5,n:5,r:6};var va={Exact:na,Default:qa,Normalize:qa,LatinBalance:{mapper:ra},LatinAdvanced:{mapper:ra,matcher:sa,replacer:ta},LatinExtra:{mapper:ra,replacer:ta.concat([/(?!^)[aeo]/g,""]),matcher:sa},LatinSoundex:{dedupe:!1,include:{letter:!0},finalize:function(a){for(let b=0;b=g.length)c-=g.length;else{c=g[e?"splice":"slice"](c,b);const h=c.length;if(h&&(d=d.length?d.concat(c):c,b-=h,e&&(a.length-=h),!b))break;c=0}return d} +function za(a){if(!this||this.constructor!==za)return new za(a);this.index=a?[a]:[];this.length=a?a.length:0;const c=this;return new Proxy([],{get(b,e){if("length"===e)return c.length;if("push"===e)return function(d){c.index[c.index.length-1].push(d);c.length++};if("pop"===e)return function(){if(c.length)return c.length--,c.index[c.index.length-1].pop()};if("indexOf"===e)return function(d){let f=0;for(let g=0,h,k;g=this.priority*this.priority*3):(Ha=setTimeout(Ka,0),Ia=Date.now());if(Ja){const f=this;return new Promise(g=>{setTimeout(function(){g(f[a+"Async"].apply(f,c))},0)})}const d=this[a].apply(this,c);b=d.then?d:new Promise(f=>f(d));e&&b.then(e);return b}};let W=0; +function La(a={},c){function b(h){function k(m){m=m.data||m;const l=m.id,n=l&&f.h[l];n&&(n(m.msg),delete f.h[l])}this.worker=h;this.h=I();if(this.worker){d?this.worker.on("message",k):this.worker.onmessage=k;if(a.config)return new Promise(function(m){1E9c?a.slice(b,b+c):a,e?Ta.call(this,a):a;let d=[];for(let f=0,g,h;f=h){b-=h;continue}g=g.slice(b,b+c);h=g.length;b=0}h>c&&(g=g.slice(0,c),h=c);if(!d.length&&h>=c)return e?Ta.call(this,g):g;d.push(g);c-=h;if(!c)break}d=1a.length?this.result=a[0]:(this.result=Ya(a,c,b,!1,this.h),b=0));d&&(this.await=null);return d?this.resolve(c,b,e):this};Y.prototype.and=function(){return Ua(this,"and",Za,arguments)};function Za(a,c,b,e,d,f){if(!f&&!this.result.length)return d?this.result:this;let g;if(a.length)if(this.result.length&&a.unshift(this.result),2>a.length)this.result=a[0];else{let h=0;for(let k=0,m,l;ka.length)this.result=a[0];else{a:{f=b;var g=this.h;const h=[],k=I();let m=0;for(let l=0,n;lb||e?k.slice(e,b+e):k;else{if(ab||e)k=k.slice(e,b+ +e)}else{d=[];for(let n=0,u;ne)e-=u.length;else{if(b&&u.length>b||e)u=u.slice(e,b+e),b-=u.length,e&&(e-=u.length);d.push(u);if(!b)break}k=d}}return k} +function Ya(a,c,b,e,d){const f=[],g=I();let h;var k=a.length;let m;if(e)for(d=k-1;0<=d;d--){if(m=(e=a[d])&&e.length)for(k=0;kA&&(A=q.length+(q?1:0)),B=q.length+(q?1:0)+G.length,E+=D,ya.push(v.length),v.push({match:G})),q+=(q?" ":"")+G)}if(!x)F=y[C],q+=(q?" ":"")+F,k&&v.push({text:F});else if(k&&E>=k)break}E=ya.length*(f.length-2);if(p||t||k&&q.length-E>k)if(E=k+E-2*u,C=B-A,0y&&(y=0)),v=v.length-1){if(J>= +v.length){C[x+1]=1;J>=y.length&&(E[x+1]=1);continue}D-=u}q=v[J].text;if(L=t&&z[x])if(0L)if(C[x+1]=1,m)q=q.substring(0,L);else continue;(L-=q.length)||(L=-1);z[x]=L}else{C[x+1]=1;continue}if(D+q.length+1<=k)q=" "+q,B[x]+=q;else if(m)U=k-D-1,0=J){if(0>J){C[x]=1;E[x]=1;continue}D-=u}q=v[J].text;if(L=p&&F[x])if(0L)if(C[x]=1,m)q=q.substring(q.length- +L);else continue;(L-=q.length)||(L=-1);F[x]=L}else{C[x]=1;continue}if(D+q.length+1<=k)q+=" ",B[x]=q+B[x];else if(m)U=q.length+1-(k-D),0<=U&&U=y.length-1?Qa=1:Jb||e)a=a.slice(e,e+b);d&&(a=Ta.call(this,a))}return a} +function Ta(a){if(!this||!this.store)return a;if(this.db)return this.index.get(this.field[0]).db.enrich(a);const c=Array(a.length);for(let b=0,e;br;f--){g=t.substring(r,f);q=this.rtl?d-1-r:r;var h=this.score?this.score(c,t,p,g,q):wb(u, +m,p,d,q);xb(this,n,g,h,a,b)}break}case "bidirectional":case "reverse":if(1g?0:1),m,p,h-1,k-1),q=this.bidirectional&&t>f;xb(this,l,q?f:t,r,a,b,q?t:f)}}}}this.fastupdate||this.reg.add(a)}else c=""}this.db&&(c||this.commit_task.push({del:a}),this.S&&ub(this));return this}; +function xb(a,c,b,e,d,f,g){let h=g?a.ctx:a.map,k;if(!c[b]||g&&!(k=c[b])[g])if(g?(c=k||(c[b]=I()),c[g]=1,(k=h.get(g))?h=k:h.set(g,h=new Map)):c[b]=1,(k=h.get(b))?h=k:h.set(b,h=k=[]),h=h[e]||(h[e]=[]),!f||!h.includes(d)){if(h.length===2**31-1){c=new za(h);if(a.fastupdate)for(let m of a.reg.values())m.includes(h)&&(m[m.indexOf(h)]=c);k[e]=h=c}h.push(d);a.fastupdate&&((e=a.reg.get(d))?e.push(h):a.reg.set(d,[h]))}} +function wb(a,c,b,e,d){return b&&1b)&&(k=b,b=c,c=k);if(a.db)return a.db.get(c,b,e,d,f,g,h);a=b?(a=a.ctx.get(b))&&a.get(c):a.map.get(c);return a};function V(a,c){if(!this||this.constructor!==V)return new V(a);if(a){var b=M(a)?a:a.preset;b&&(a=Object.assign({},vb[b],a))}else a={};b=a.context;const e=!0===b?{depth:1}:b||{},d=M(a.encoder)?va[a.encoder]:a.encode||a.encoder||{};this.encoder=d.encode?d:"object"===typeof d?new la(d):{encode:d};this.resolution=a.resolution||9;this.tokenize=b=(b=a.tokenize)&&"default"!==b&&"exact"!==b&&b||"strict";this.depth="strict"===b&&e.depth||0;this.bidirectional=!1!==e.bidirectional;this.fastupdate=!!a.fastupdate; +this.score=a.score||null;(b=a.keystore||0)&&(this.keystore=b);this.map=b?new S(b):new Map;this.ctx=b?new S(b):new Map;this.reg=c||(this.fastupdate?b?new S(b):new Map:b?new T(b):new Set);this.T=e.resolution||3;this.rtl=d.rtl||a.rtl||!1;this.cache=(b=a.cache||null)&&new R(b);this.resolve=!1!==a.resolve;if(b=a.db)this.db=this.mount(b);this.S=!1!==a.commit;this.commit_task=[];this.commit_timer=null;this.priority=a.priority||4}w=V.prototype; +w.mount=function(a){this.commit_timer&&(clearTimeout(this.commit_timer),this.commit_timer=null);return a.mount(this)};w.commit=function(a,c){this.commit_timer&&(clearTimeout(this.commit_timer),this.commit_timer=null);return this.db.commit(this,a,c)};w.destroy=function(){this.commit_timer&&(clearTimeout(this.commit_timer),this.commit_timer=null);return this.db.destroy()};function ub(a){a.commit_timer||(a.commit_timer=setTimeout(function(){a.commit_timer=null;a.db.commit(a,void 0,void 0)},1))} +w.clear=function(){this.map.clear();this.ctx.clear();this.reg.clear();this.cache&&this.cache.clear();this.db&&(this.commit_timer&&clearTimeout(this.commit_timer),this.commit_timer=null,this.commit_task=[{clear:!0}]);return this};w.append=function(a,c){return this.add(a,c,!0)};w.contain=function(a){return this.db?this.db.has(a):this.reg.has(a)};w.update=function(a,c){const b=this,e=this.remove(a);return e&&e.then?e.then(()=>b.add(a,c)):this.add(a,c)}; +w.cleanup=function(){if(!this.fastupdate)return this;tb(this.map);this.depth&&tb(this.ctx);return this};w.searchCache=ma;w.export=function(a,c,b=0,e=0){let d,f;switch(e){case 0:d="reg";f=pb(this.reg);break;case 1:d="cfg";f=null;break;case 2:d="map";f=lb(this.map,this.reg.size);break;case 3:d="ctx";f=nb(this.ctx,this.reg.size);break;default:return}return rb.call(this,a,c,d,f,b,e)}; +w.import=function(a,c){if(c)switch("string"===typeof c&&(c=JSON.parse(c)),a=a.split("."),"json"===a[a.length-1]&&a.pop(),3===a.length&&a.shift(),a=1=l.length){e-=l.length;continue}const n=b?e+Math.min(l.length-e,b):l.length;for(let u=e;u=f.length)return[];if(!c&&!b)return f;f=f.slice(b,b+c);return e?d.enrich(f):f})}; +w.enrich=function(a){"object"!==typeof a&&(a=[a]);const c=this.db.transaction("reg","readonly").objectStore("reg"),b=[];for(let e=0;e{a.onsuccess=a.oncomplete=function(){c&&c(this.result);c=null;b(this.result)};a.onerror=a.onblocked=e;a=null})};const Hb={Index:V,Charset:va,Encoder:la,Document:Na,Worker:La,Resolver:Y,IndexedDB:Fb,Language:{}},Ib="undefined"!==typeof self?self:"undefined"!==typeof global?global:self;let Jb;(Jb=Ib.define)&&Jb.amd?Jb([],function(){return Hb}):"object"===typeof Ib.exports?Ib.exports=Hb:Ib.FlexSearch=Hb;}(this||self)); diff --git a/dist/flexsearch.bundle.module.debug.js b/dist/flexsearch.bundle.module.debug.js index 7cf2293..44a6f0c 100644 --- a/dist/flexsearch.bundle.module.debug.js +++ b/dist/flexsearch.bundle.module.debug.js @@ -1,35 +1,35 @@ /**! - * FlexSearch.js v0.8.167 (Bundle/Module/Debug) + * FlexSearch.js v0.8.200 (Bundle/Module/Debug) * Author and Copyright: Thomas Wilkerling * Licence: Apache-2.0 * Hosted by Nextapps GmbH * https://github.com/nextapps-de/flexsearch */ var w; -function E(a, b, c) { - const d = typeof c, e = typeof a; - if ("undefined" !== d) { - if ("undefined" !== e) { - if (c) { - if ("function" === e && d === e) { +function H(a, c, b) { + const e = typeof b, d = typeof a; + if ("undefined" !== e) { + if ("undefined" !== d) { + if (b) { + if ("function" === d && e === d) { return function(h) { - return a(c(h)); + return a(b(h)); }; } - b = a.constructor; - if (b === c.constructor) { - if (b === Array) { - return c.concat(a); + c = a.constructor; + if (c === b.constructor) { + if (c === Array) { + return b.concat(a); } - if (b === Map) { - var f = new Map(c); + if (c === Map) { + var f = new Map(b); for (var g of a) { f.set(g[0], g[1]); } return f; } - if (b === Set) { - g = new Set(c); + if (c === Set) { + g = new Set(b); for (f of a.values()) { g.add(f); } @@ -39,50 +39,43 @@ function E(a, b, c) { } return a; } - return c; + return b; } - return "undefined" === e ? b : a; + return "undefined" === d ? c : a; } -function aa(a, b) { - return "undefined" === typeof a ? b : a; +function aa(a, c) { + return "undefined" === typeof a ? c : a; } -function G() { +function I() { return Object.create(null); } -function M(a) { +function N(a) { return "string" === typeof a; } -function da(a) { +function ba(a) { return "object" === typeof a; } -function ea(a) { - const b = []; - for (const c of a.keys()) { - b.push(c); +function ca(a) { + const c = []; + for (const b of a.keys()) { + c.push(b); } - return b; + return c; } -function fa(a, b) { - if (M(b)) { - a = a[b]; +function da(a, c) { + if (N(c)) { + a = a[c]; } else { - for (let c = 0; a && c < b.length; c++) { - a = a[b[c]]; + for (let b = 0; a && b < c.length; b++) { + a = a[c[b]]; } } return a; } -function ha(a) { - let b = 0; - for (let c = 0, d; c < a.length; c++) { - (d = a[c]) && b < d.length && (b = d.length); - } - return b; -} -;const ia = /[^\p{L}\p{N}]+/u, ja = /(\d{3})/g, ka = /(\D)(\d{3})/g, la = /(\d{3})(\D)/g, ma = /[\u0300-\u036f]/g; -function na(a = {}) { - if (!this || this.constructor !== na) { - return new na(...arguments); +;const fa = /[^\p{L}\p{N}]+/u, ha = /(\d{3})/g, ia = /(\D)(\d{3})/g, ja = /(\d{3})(\D)/g, ka = /[\u0300-\u036f]/g; +function la(a = {}) { + if (!this || this.constructor !== la) { + return new la(...arguments); } if (arguments.length) { for (a = 0; a < arguments.length; a++) { @@ -92,468 +85,313 @@ function na(a = {}) { this.assign(a); } } -w = na.prototype; +w = la.prototype; w.assign = function(a) { - this.normalize = E(a.normalize, !0, this.normalize); - let b = a.include, c = b || a.exclude || a.split, d; - if (c || "" === c) { - if ("object" === typeof c && c.constructor !== RegExp) { - let e = ""; - d = !b; - b || (e += "\\p{Z}"); - c.letter && (e += "\\p{L}"); - c.number && (e += "\\p{N}", d = !!b); - c.symbol && (e += "\\p{S}"); - c.punctuation && (e += "\\p{P}"); - c.control && (e += "\\p{C}"); - if (c = c.char) { - e += "object" === typeof c ? c.join("") : c; + this.normalize = H(a.normalize, !0, this.normalize); + let c = a.include, b = c || a.exclude || a.split, e; + if (b || "" === b) { + if ("object" === typeof b && b.constructor !== RegExp) { + let d = ""; + e = !c; + c || (d += "\\p{Z}"); + b.letter && (d += "\\p{L}"); + b.number && (d += "\\p{N}", e = !!c); + b.symbol && (d += "\\p{S}"); + b.punctuation && (d += "\\p{P}"); + b.control && (d += "\\p{C}"); + if (b = b.char) { + d += "object" === typeof b ? b.join("") : b; } try { - this.split = new RegExp("[" + (b ? "^" : "") + e + "]+", "u"); + this.split = new RegExp("[" + (c ? "^" : "") + d + "]+", "u"); } catch (f) { - console.error("Your split configuration:", c, "is not supported on this platform. It falls back to using simple whitespace splitter instead: /s+/."), this.split = /\s+/; + console.error("Your split configuration:", b, "is not supported on this platform. It falls back to using simple whitespace splitter instead: /s+/."), this.split = /\s+/; } } else { - this.split = c, d = !1 === c || 2 > "a1a".split(c).length; + this.split = b, e = !1 === b || 2 > "a1a".split(b).length; } - this.numeric = E(a.numeric, d); + this.numeric = H(a.numeric, e); } else { try { - this.split = E(this.split, ia); - } catch (e) { + this.split = H(this.split, fa); + } catch (d) { console.warn("This platform does not support unicode regex. It falls back to using simple whitespace splitter instead: /s+/."), this.split = /\s+/; } - this.numeric = E(a.numeric, E(this.numeric, !0)); + this.numeric = H(a.numeric, H(this.numeric, !0)); } - this.prepare = E(a.prepare, null, this.prepare); - this.finalize = E(a.finalize, null, this.finalize); - c = a.filter; - this.filter = "function" === typeof c ? c : E(c && new Set(c), null, this.filter); - this.dedupe = E(a.dedupe, !0, this.dedupe); - this.matcher = E((c = a.matcher) && new Map(c), null, this.matcher); - this.mapper = E((c = a.mapper) && new Map(c), null, this.mapper); - this.stemmer = E((c = a.stemmer) && new Map(c), null, this.stemmer); - this.replacer = E(a.replacer, null, this.replacer); - this.minlength = E(a.minlength, 1, this.minlength); - this.maxlength = E(a.maxlength, 1024, this.maxlength); - this.rtl = E(a.rtl, !1, this.rtl); - if (this.cache = c = E(a.cache, !0, this.cache)) { - this.H = null, this.S = "number" === typeof c ? c : 2e5, this.B = new Map(), this.G = new Map(), this.L = this.K = 128; + this.prepare = H(a.prepare, null, this.prepare); + this.finalize = H(a.finalize, null, this.finalize); + b = a.filter; + this.filter = "function" === typeof b ? b : H(b && new Set(b), null, this.filter); + this.dedupe = H(a.dedupe, !0, this.dedupe); + this.matcher = H((b = a.matcher) && new Map(b), null, this.matcher); + this.mapper = H((b = a.mapper) && new Map(b), null, this.mapper); + this.stemmer = H((b = a.stemmer) && new Map(b), null, this.stemmer); + this.replacer = H(a.replacer, null, this.replacer); + this.minlength = H(a.minlength, 1, this.minlength); + this.maxlength = H(a.maxlength, 1024, this.maxlength); + this.rtl = H(a.rtl, !1, this.rtl); + if (this.cache = b = H(a.cache, !0, this.cache)) { + this.I = null, this.R = "number" === typeof b ? b : 2e5, this.B = new Map(), this.H = new Map(), this.M = this.L = 128; } this.h = ""; - this.M = null; - this.A = ""; this.N = null; + this.A = ""; + this.O = null; if (this.matcher) { - for (const e of this.matcher.keys()) { - this.h += (this.h ? "|" : "") + e; + for (const d of this.matcher.keys()) { + this.h += (this.h ? "|" : "") + d; } } if (this.stemmer) { - for (const e of this.stemmer.keys()) { - this.A += (this.A ? "|" : "") + e; + for (const d of this.stemmer.keys()) { + this.A += (this.A ? "|" : "") + d; } } return this; }; -w.addStemmer = function(a, b) { +w.addStemmer = function(a, c) { this.stemmer || (this.stemmer = new Map()); - this.stemmer.set(a, b); + this.stemmer.set(a, c); this.A += (this.A ? "|" : "") + a; - this.N = null; - this.cache && N(this); + this.O = null; + this.cache && Q(this); return this; }; w.addFilter = function(a) { "function" === typeof a ? this.filter = a : (this.filter || (this.filter = new Set()), this.filter.add(a)); - this.cache && N(this); + this.cache && Q(this); return this; }; -w.addMapper = function(a, b) { +w.addMapper = function(a, c) { if ("object" === typeof a) { - return this.addReplacer(a, b); + return this.addReplacer(a, c); } if (1 < a.length) { - return this.addMatcher(a, b); + return this.addMatcher(a, c); } this.mapper || (this.mapper = new Map()); - this.mapper.set(a, b); - this.cache && N(this); + this.mapper.set(a, c); + this.cache && Q(this); return this; }; -w.addMatcher = function(a, b) { +w.addMatcher = function(a, c) { if ("object" === typeof a) { - return this.addReplacer(a, b); + return this.addReplacer(a, c); } if (2 > a.length && (this.dedupe || this.mapper)) { - return this.addMapper(a, b); + return this.addMapper(a, c); } this.matcher || (this.matcher = new Map()); - this.matcher.set(a, b); + this.matcher.set(a, c); this.h += (this.h ? "|" : "") + a; - this.M = null; - this.cache && N(this); + this.N = null; + this.cache && Q(this); return this; }; -w.addReplacer = function(a, b) { +w.addReplacer = function(a, c) { if ("string" === typeof a) { - return this.addMatcher(a, b); + return this.addMatcher(a, c); } this.replacer || (this.replacer = []); - this.replacer.push(a, b); - this.cache && N(this); + this.replacer.push(a, c); + this.cache && Q(this); return this; }; -w.encode = function(a, b) { - if (this.cache && a.length <= this.K) { - if (this.H) { +w.encode = function(a, c) { + if (this.cache && a.length <= this.L) { + if (this.I) { if (this.B.has(a)) { return this.B.get(a); } } else { - this.H = setTimeout(N, 50, this); + this.I = setTimeout(Q, 50, this); } } - this.normalize && ("function" === typeof this.normalize ? a = this.normalize(a) : a = ma ? a.normalize("NFKD").replace(ma, "").toLowerCase() : a.toLowerCase()); + this.normalize && ("function" === typeof this.normalize ? a = this.normalize(a) : a = ka ? a.normalize("NFKD").replace(ka, "").toLowerCase() : a.toLowerCase()); this.prepare && (a = this.prepare(a)); - this.numeric && 3 < a.length && (a = a.replace(ka, "$1 $2").replace(la, "$1 $2").replace(ja, "$1 ")); - const c = !(this.dedupe || this.mapper || this.filter || this.matcher || this.stemmer || this.replacer); - let d = [], e = G(), f, g, h = this.split || "" === this.split ? a.split(this.split) : [a]; - for (let l = 0, m, p; l < h.length; l++) { - if ((m = p = h[l]) && !(m.length < this.minlength || m.length > this.maxlength)) { - if (b) { - if (e[m]) { - continue; - } - e[m] = 1; - } else { - if (f === m) { - continue; - } - f = m; - } + this.numeric && 3 < a.length && (a = a.replace(ia, "$1 $2").replace(ja, "$1 $2").replace(ha, "$1 ")); + const b = !(this.dedupe || this.mapper || this.filter || this.matcher || this.stemmer || this.replacer); + let e = [], d = I(), f, g, h = this.split || "" === this.split ? a.split(this.split) : [a]; + for (let m = 0, l, n; m < h.length; m++) { + if ((l = n = h[m]) && !(l.length < this.minlength || l.length > this.maxlength)) { if (c) { - d.push(m); + if (d[l]) { + continue; + } + d[l] = 1; } else { - if (!this.filter || ("function" === typeof this.filter ? this.filter(m) : !this.filter.has(m))) { - if (this.cache && m.length <= this.L) { - if (this.H) { - var k = this.G.get(m); + if (f === l) { + continue; + } + f = l; + } + if (b) { + e.push(l); + } else { + if (!this.filter || ("function" === typeof this.filter ? this.filter(l) : !this.filter.has(l))) { + if (this.cache && l.length <= this.M) { + if (this.I) { + var k = this.H.get(l); if (k || "" === k) { - k && d.push(k); + k && e.push(k); continue; } } else { - this.H = setTimeout(N, 50, this); + this.I = setTimeout(Q, 50, this); } } if (this.stemmer) { - this.N || (this.N = new RegExp("(?!^)(" + this.A + ")$")); + this.O || (this.O = new RegExp("(?!^)(" + this.A + ")$")); let u; - for (; u !== m && 2 < m.length;) { - u = m, m = m.replace(this.N, q => this.stemmer.get(q)); + for (; u !== l && 2 < l.length;) { + u = l, l = l.replace(this.O, p => this.stemmer.get(p)); } } - if (m && (this.mapper || this.dedupe && 1 < m.length)) { + if (l && (this.mapper || this.dedupe && 1 < l.length)) { k = ""; - for (let u = 0, q = "", r, n; u < m.length; u++) { - r = m.charAt(u), r === q && this.dedupe || ((n = this.mapper && this.mapper.get(r)) || "" === n ? n === q && this.dedupe || !(q = n) || (k += n) : k += q = r); + for (let u = 0, p = "", t, r; u < l.length; u++) { + t = l.charAt(u), t === p && this.dedupe || ((r = this.mapper && this.mapper.get(t)) || "" === r ? r === p && this.dedupe || !(p = r) || (k += r) : k += p = t); } - m = k; + l = k; } - this.matcher && 1 < m.length && (this.M || (this.M = new RegExp("(" + this.h + ")", "g")), m = m.replace(this.M, u => this.matcher.get(u))); - if (m && this.replacer) { - for (k = 0; m && k < this.replacer.length; k += 2) { - m = m.replace(this.replacer[k], this.replacer[k + 1]); + this.matcher && 1 < l.length && (this.N || (this.N = new RegExp("(" + this.h + ")", "g")), l = l.replace(this.N, u => this.matcher.get(u))); + if (l && this.replacer) { + for (k = 0; l && k < this.replacer.length; k += 2) { + l = l.replace(this.replacer[k], this.replacer[k + 1]); } } - this.cache && p.length <= this.L && (this.G.set(p, m), this.G.size > this.S && (this.G.clear(), this.L = this.L / 1.1 | 0)); - if (m) { - if (m !== p) { - if (b) { - if (e[m]) { + this.cache && n.length <= this.M && (this.H.set(n, l), this.H.size > this.R && (this.H.clear(), this.M = this.M / 1.1 | 0)); + if (l) { + if (l !== n) { + if (c) { + if (d[l]) { continue; } - e[m] = 1; + d[l] = 1; } else { - if (g === m) { + if (g === l) { continue; } - g = m; + g = l; } } - d.push(m); + e.push(l); } } } } } - this.finalize && (d = this.finalize(d) || d); - this.cache && a.length <= this.K && (this.B.set(a, d), this.B.size > this.S && (this.B.clear(), this.K = this.K / 1.1 | 0)); - return d; + this.finalize && (e = this.finalize(e) || e); + this.cache && a.length <= this.L && (this.B.set(a, e), this.B.size > this.R && (this.B.clear(), this.L = this.L / 1.1 | 0)); + return e; }; -function N(a) { - a.H = null; +function Q(a) { + a.I = null; a.B.clear(); - a.G.clear(); + a.H.clear(); } -;let qa, O; -async function ra(a) { - a = a.data; - var b = a.task; - const c = a.id; - let d = a.args; - switch(b) { - case "init": - O = a.options || {}; - (b = a.factory) ? (Function("return " + b)()(self), qa = new self.FlexSearch.Index(O), delete self.FlexSearch) : qa = new P(O); - postMessage({id:c}); - break; - default: - let e; - if ("export" === b) { - if (!O.export || "function" !== typeof O.export) { - throw Error('Either no extern configuration provided for the Worker-Index or no method was defined on the config property "export".'); - } - d[1] ? (d[0] = O.export, d[2] = 0, d[3] = 1) : d = null; - } - if ("import" === b) { - if (!O.import || "function" !== typeof O.import) { - throw Error('Either no extern configuration provided for the Worker-Index or no method was defined on the config property "import".'); - } - d[0] && (a = await O.import.call(qa, d[0]), qa.import(d[0], a)); - } else { - (e = d && qa[b].apply(qa, d)) && e.then && (e = await e); - } - postMessage("search" === b ? {id:c, msg:e} : {id:c}); +;function ma(a, c, b) { + b || (c || "object" !== typeof a ? "object" === typeof c && (b = c, c = 0) : b = a); + b && (a = b.query || a, c = b.limit || c); + let e = "" + (c || 0); + b && (e += (b.offset || 0) + !!b.context + !!b.suggest + (!1 !== b.resolve) + (b.resolution || this.resolution) + (b.boost || 0)); + a = ("" + a).toLowerCase(); + this.cache || (this.cache = new na()); + let d = this.cache.get(a + e); + if (!d) { + const f = b && b.cache; + f && (b.cache = !1); + d = this.search(a, c, b); + f && (b.cache = f); + this.cache.set(a + e, d); } + return d; } -;function sa(a) { - ta.call(a, "add"); - ta.call(a, "append"); - ta.call(a, "search"); - ta.call(a, "update"); - ta.call(a, "remove"); - ta.call(a, "searchCache"); +function na(a) { + this.limit = a && !0 !== a ? a : 1000; + this.cache = new Map(); + this.h = ""; } -let ua, va, ya; -function za() { - ua = ya = 0; -} -function ta(a) { - this[a + "Async"] = function() { - const b = arguments; - var c = b[b.length - 1]; - let d; - "function" === typeof c && (d = c, delete b[b.length - 1]); - ua ? ya || (ya = Date.now() - va >= this.priority * this.priority * 3) : (ua = setTimeout(za, 0), va = Date.now()); - if (ya) { - const f = this; - return new Promise(g => { - setTimeout(function() { - g(f[a + "Async"].apply(f, b)); - }, 0); - }); - } - const e = this[a].apply(this, b); - c = e.then ? e : new Promise(f => f(e)); - d && c.then(d); - return c; - }; -} -;let Aa = 0; -function Ba(a = {}, b) { - function c(h) { - function k(l) { - l = l.data || l; - const m = l.id, p = m && f.h[m]; - p && (p(l.msg), delete f.h[m]); - } - this.worker = h; - this.h = G(); - if (this.worker) { - e ? this.worker.on("message", k) : this.worker.onmessage = k; - if (a.config) { - return new Promise(function(l) { - f.h[++Aa] = function() { - l(f); - 1e9 < Aa && (Aa = 0); - }; - f.worker.postMessage({id:Aa, task:"init", factory:d, options:a}); - }); - } - this.priority = a.priority || 4; - this.encoder = b || null; - this.worker.postMessage({task:"init", factory:d, options:a}); - return this; - } - } - if (!this || this.constructor !== Ba) { - return new Ba(a); - } - let d = "undefined" !== typeof self ? self._factory : "undefined" !== typeof window ? window._factory : null; - d && (d = d.toString()); - const e = "undefined" === typeof window, f = this, g = Ca(d, e, a.worker); - return g.then ? g.then(function(h) { - return c.call(f, h); - }) : c.call(this, g); -} -R("add"); -R("append"); -R("search"); -R("searchCache"); -R("update"); -R("remove"); -R("clear"); -R("export"); -R("import"); -sa(Ba.prototype); -function R(a) { - Ba.prototype[a] = function() { - const b = this, c = [].slice.call(arguments); - var d = c[c.length - 1]; - let e; - "function" === typeof d && (e = d, c.pop()); - d = new Promise(function(f) { - "export" === a && "function" === typeof c[0] && (c[0] = null); - b.h[++Aa] = f; - b.worker.postMessage({task:a, id:Aa, args:c}); - }); - return e ? (d.then(e), this) : d; - }; -} -function Ca(a, b, c) { - return b ? "undefined" !== typeof module ? new(require("worker_threads")["Worker"])(__dirname+"/worker/node.js") : import("worker_threads").then(function(worker){return new worker["Worker"](import.meta.dirname+"/node/node.mjs")}) : a ? new window.Worker(URL.createObjectURL(new Blob(["onmessage=" + ra.toString()], {type:"text/javascript"}))) : new window.Worker("string" === typeof c ? c : import.meta.url.replace("/worker.js", "/worker/worker.js").replace("flexsearch.bundle.module.min.js", - "module/worker/worker.js"), {type:"module"}); -} -;function Da(a, b = 0) { - let c = [], d = []; - b && (b = 250000 / b * 5000 | 0); - for (const e of a.entries()) { - d.push(e), d.length === b && (c.push(d), d = []); - } - d.length && c.push(d); +na.prototype.set = function(a, c) { + this.cache.set(this.h = a, c); + this.cache.size > this.limit && this.cache.delete(this.cache.keys().next().value); +}; +na.prototype.get = function(a) { + const c = this.cache.get(a); + c && this.h !== a && (this.cache.delete(a), this.cache.set(this.h = a, c)); return c; -} -function Ea(a, b) { - b || (b = new Map()); - for (let c = 0, d; c < a.length; c++) { - d = a[c], b.set(d[0], d[1]); +}; +na.prototype.remove = function(a) { + for (const c of this.cache) { + const b = c[0]; + c[1].includes(a) && this.cache.delete(b); } - return b; -} -function Fa(a, b = 0) { - let c = [], d = []; - b && (b = 250000 / b * 1000 | 0); - for (const e of a.entries()) { - d.push([e[0], Da(e[1])[0]]), d.length === b && (c.push(d), d = []); - } - d.length && c.push(d); - return c; -} -function Ga(a, b) { - b || (b = new Map()); - for (let c = 0, d, e; c < a.length; c++) { - d = a[c], e = b.get(d[0]), b.set(d[0], Ea(d[1], e)); - } - return b; -} -function Ha(a) { - let b = [], c = []; - for (const d of a.keys()) { - c.push(d), 250000 === c.length && (b.push(c), c = []); - } - c.length && b.push(c); - return b; -} -function Ia(a, b) { - b || (b = new Set()); - for (let c = 0; c < a.length; c++) { - b.add(a[c]); - } - return b; -} -function Ja(a, b, c, d, e, f, g = 0) { - const h = d && d.constructor === Array; - var k = h ? d.shift() : d; - if (!k) { - return this.export(a, b, e, f + 1); - } - if ((k = a((b ? b + "." : "") + (g + 1) + "." + c, JSON.stringify(k))) && k.then) { - const l = this; - return k.then(function() { - return Ja.call(l, a, b, c, h ? d : null, e, f, g + 1); - }); - } - return Ja.call(this, a, b, c, h ? d : null, e, f, g + 1); -} -function Ka(a, b) { - let c = ""; - for (const d of a.entries()) { - a = d[0]; - const e = d[1]; - let f = ""; - for (let g = 0, h; g < e.length; g++) { - h = e[g] || [""]; - let k = ""; - for (let l = 0; l < h.length; l++) { - k += (k ? "," : "") + ("string" === b ? '"' + h[l] + '"' : h[l]); - } - k = "[" + k + "]"; - f += (f ? "," : "") + k; +}; +na.prototype.clear = function() { + this.cache.clear(); + this.h = ""; +}; +const qa = {normalize:!1, numeric:!1, dedupe:!1}; +const ra = {}; +const sa = new Map([["b", "p"], ["v", "f"], ["w", "f"], ["z", "s"], ["x", "s"], ["d", "t"], ["n", "m"], ["c", "k"], ["g", "k"], ["j", "k"], ["q", "k"], ["i", "e"], ["y", "e"], ["u", "o"]]); +const ta = new Map([["ae", "a"], ["oe", "o"], ["sh", "s"], ["kh", "k"], ["th", "t"], ["ph", "f"], ["pf", "f"]]), ua = [/([^aeo])h(.)/g, "$1$2", /([aeo])h([^aeo]|$)/g, "$1$2", /(.)\1+/g, "$1"]; +const va = {a:"", e:"", i:"", o:"", u:"", y:"", b:1, f:1, p:1, v:1, c:2, g:2, j:2, k:2, q:2, s:2, x:2, z:2, "\u00df":2, d:3, t:3, l:4, m:5, n:5, r:6}; +var wa = {Exact:qa, Default:ra, Normalize:ra, LatinBalance:{mapper:sa}, LatinAdvanced:{mapper:sa, matcher:ta, replacer:ua}, LatinExtra:{mapper:sa, replacer:ua.concat([/(?!^)[aeo]/g, ""]), matcher:ta}, LatinSoundex:{dedupe:!1, include:{letter:!0}, finalize:function(a) { + for (let b = 0; b < a.length; b++) { + var c = a[b]; + let e = c.charAt(0), d = va[e]; + for (let f = 1, g; f < c.length && (g = c.charAt(f), "h" === g || "w" === g || !(g = va[g]) || g === d || (e += g, d = g, 4 !== e.length)); f++) { } - f = '["' + a + '",[' + f + "]]"; - c += (c ? "," : "") + f; + a[b] = e; } - return c; -} -;function Na(a, b, c, d) { - let e = []; +}}, CJK:{split:""}, LatinExact:qa, LatinDefault:ra, LatinSimple:ra}; +function za(a, c, b, e) { + let d = []; for (let f = 0, g; f < a.index.length; f++) { - if (g = a.index[f], b >= g.length) { - b -= g.length; + if (g = a.index[f], c >= g.length) { + c -= g.length; } else { - b = g[d ? "splice" : "slice"](b, c); - const h = b.length; - if (h && (e = e.length ? e.concat(b) : b, c -= h, d && (a.length -= h), !c)) { + c = g[e ? "splice" : "slice"](c, b); + const h = c.length; + if (h && (d = d.length ? d.concat(c) : c, b -= h, e && (a.length -= h), !b)) { break; } - b = 0; + c = 0; } } - return e; + return d; } -function U(a) { - if (!this || this.constructor !== U) { - return new U(a); +function Aa(a) { + if (!this || this.constructor !== Aa) { + return new Aa(a); } this.index = a ? [a] : []; this.length = a ? a.length : 0; - const b = this; - return new Proxy([], {get(c, d) { - if ("length" === d) { - return b.length; + const c = this; + return new Proxy([], {get(b, e) { + if ("length" === e) { + return c.length; } - if ("push" === d) { - return function(e) { - b.index[b.index.length - 1].push(e); - b.length++; + if ("push" === e) { + return function(d) { + c.index[c.index.length - 1].push(d); + c.length++; }; } - if ("pop" === d) { + if ("pop" === e) { return function() { - if (b.length) { - return b.length--, b.index[b.index.length - 1].pop(); + if (c.length) { + return c.length--, c.index[c.index.length - 1].pop(); } }; } - if ("indexOf" === d) { - return function(e) { + if ("indexOf" === e) { + return function(d) { let f = 0; - for (let g = 0, h, k; g < b.index.length; g++) { - h = b.index[g]; - k = h.indexOf(e); + for (let g = 0, h, k; g < c.index.length; g++) { + h = c.index[g]; + k = h.indexOf(d); if (0 <= k) { return f + k; } @@ -562,812 +400,767 @@ function U(a) { return -1; }; } - if ("includes" === d) { - return function(e) { - for (let f = 0; f < b.index.length; f++) { - if (b.index[f].includes(e)) { + if ("includes" === e) { + return function(d) { + for (let f = 0; f < c.index.length; f++) { + if (c.index[f].includes(d)) { return !0; } } return !1; }; } - if ("slice" === d) { - return function(e, f) { - return Na(b, e || 0, f || b.length, !1); + if ("slice" === e) { + return function(d, f) { + return za(c, d || 0, f || c.length, !1); }; } - if ("splice" === d) { - return function(e, f) { - return Na(b, e || 0, f || b.length, !0); + if ("splice" === e) { + return function(d, f) { + return za(c, d || 0, f || c.length, !0); }; } - if ("constructor" === d) { + if ("constructor" === e) { return Array; } - if ("symbol" !== typeof d) { - return (c = b.index[d / 2 ** 31 | 0]) && c[d]; + if ("symbol" !== typeof e) { + return (b = c.index[e / 2 ** 31 | 0]) && b[e]; } - }, set(c, d, e) { - c = d / 2 ** 31 | 0; - (b.index[c] || (b.index[c] = []))[d] = e; - b.length++; + }, set(b, e, d) { + b = e / 2 ** 31 | 0; + (c.index[b] || (c.index[b] = []))[e] = d; + c.length++; return !0; }}); } -U.prototype.clear = function() { +Aa.prototype.clear = function() { this.index.length = 0; }; -U.prototype.destroy = function() { +Aa.prototype.destroy = function() { this.proxy = this.index = null; }; -U.prototype.push = function() { +Aa.prototype.push = function() { }; -function V(a = 8) { - if (!this || this.constructor !== V) { - return new V(a); +function R(a = 8) { + if (!this || this.constructor !== R) { + return new R(a); } - this.index = G(); + this.index = I(); this.h = []; this.size = 0; - 32 < a ? (this.B = Oa, this.A = BigInt(a)) : (this.B = Pa, this.A = a); + 32 < a ? (this.B = Ba, this.A = BigInt(a)) : (this.B = Ca, this.A = a); } -V.prototype.get = function(a) { - const b = this.index[this.B(a)]; - return b && b.get(a); +R.prototype.get = function(a) { + const c = this.index[this.B(a)]; + return c && c.get(a); }; -V.prototype.set = function(a, b) { - var c = this.B(a); - let d = this.index[c]; - d ? (c = d.size, d.set(a, b), (c -= d.size) && this.size++) : (this.index[c] = d = new Map([[a, b]]), this.h.push(d), this.size++); -}; -function W(a = 8) { - if (!this || this.constructor !== W) { - return new W(a); - } - this.index = G(); - this.h = []; - this.size = 0; - 32 < a ? (this.B = Oa, this.A = BigInt(a)) : (this.B = Pa, this.A = a); -} -W.prototype.add = function(a) { +R.prototype.set = function(a, c) { var b = this.B(a); - let c = this.index[b]; - c ? (b = c.size, c.add(a), (b -= c.size) && this.size++) : (this.index[b] = c = new Set([a]), this.h.push(c), this.size++); + let e = this.index[b]; + e ? (b = e.size, e.set(a, c), (b -= e.size) && this.size++) : (this.index[b] = e = new Map([[a, c]]), this.h.push(e), this.size++); }; -w = V.prototype; -w.has = W.prototype.has = function(a) { - const b = this.index[this.B(a)]; - return b && b.has(a); +function S(a = 8) { + if (!this || this.constructor !== S) { + return new S(a); + } + this.index = I(); + this.h = []; + this.size = 0; + 32 < a ? (this.B = Ba, this.A = BigInt(a)) : (this.B = Ca, this.A = a); +} +S.prototype.add = function(a) { + var c = this.B(a); + let b = this.index[c]; + b ? (c = b.size, b.add(a), (c -= b.size) && this.size++) : (this.index[c] = b = new Set([a]), this.h.push(b), this.size++); }; -w.delete = W.prototype.delete = function(a) { - const b = this.index[this.B(a)]; - b && b.delete(a) && this.size--; +w = R.prototype; +w.has = S.prototype.has = function(a) { + const c = this.index[this.B(a)]; + return c && c.has(a); }; -w.clear = W.prototype.clear = function() { - this.index = G(); +w.delete = S.prototype.delete = function(a) { + const c = this.index[this.B(a)]; + c && c.delete(a) && this.size--; +}; +w.clear = S.prototype.clear = function() { + this.index = I(); this.h = []; this.size = 0; }; -w.values = W.prototype.values = function*() { +w.values = S.prototype.values = function*() { for (let a = 0; a < this.h.length; a++) { - for (let b of this.h[a].values()) { - yield b; + for (let c of this.h[a].values()) { + yield c; } } }; -w.keys = W.prototype.keys = function*() { +w.keys = S.prototype.keys = function*() { for (let a = 0; a < this.h.length; a++) { - for (let b of this.h[a].keys()) { - yield b; + for (let c of this.h[a].keys()) { + yield c; } } }; -w.entries = W.prototype.entries = function*() { +w.entries = S.prototype.entries = function*() { for (let a = 0; a < this.h.length; a++) { - for (let b of this.h[a].entries()) { - yield b; + for (let c of this.h[a].entries()) { + yield c; } } }; -function Pa(a) { - let b = 2 ** this.A - 1; +function Ca(a) { + let c = 2 ** this.A - 1; if ("number" == typeof a) { - return a & b; + return a & c; } - let c = 0, d = this.A + 1; - for (let e = 0; e < a.length; e++) { - c = (c * d ^ a.charCodeAt(e)) & b; + let b = 0, e = this.A + 1; + for (let d = 0; d < a.length; d++) { + b = (b * e ^ a.charCodeAt(d)) & c; } - return 32 === this.A ? c + 2 ** 31 : c; + return 32 === this.A ? b + 2 ** 31 : b; } -function Oa(a) { - let b = BigInt(2) ** this.A - BigInt(1); - var c = typeof a; - if ("bigint" === c) { - return a & b; +function Ba(a) { + let c = BigInt(2) ** this.A - BigInt(1); + var b = typeof a; + if ("bigint" === b) { + return a & c; } - if ("number" === c) { - return BigInt(a) & b; + if ("number" === b) { + return BigInt(a) & c; } - c = BigInt(0); - let d = this.A + BigInt(1); - for (let e = 0; e < a.length; e++) { - c = (c * d ^ BigInt(a.charCodeAt(e))) & b; + b = BigInt(0); + let e = this.A + BigInt(1); + for (let d = 0; d < a.length; d++) { + b = (b * e ^ BigInt(a.charCodeAt(d))) & c; } - return c; + return b; } -;Qa.prototype.add = function(a, b, c) { - da(a) && (b = a, a = fa(b, this.key)); - if (b && (a || 0 === a)) { - if (!c && this.reg.has(a)) { - return this.update(a, b); +;let Da, T; +async function Ea(a) { + a = a.data; + var c = a.task; + const b = a.id; + let e = a.args; + switch(c) { + case "init": + T = a.options || {}; + (c = a.factory) ? (Function("return " + c)()(self), Da = new self.FlexSearch.Index(T), delete self.FlexSearch) : Da = new V(T); + postMessage({id:b}); + break; + default: + let d; + if ("export" === c) { + if (!T.export || "function" !== typeof T.export) { + throw Error('Either no extern configuration provided for the Worker-Index or no method was defined on the config property "export".'); + } + e[1] ? (e[0] = T.export, e[2] = 0, e[3] = 1) : e = null; + } + if ("import" === c) { + if (!T.import || "function" !== typeof T.import) { + throw Error('Either no extern configuration provided for the Worker-Index or no method was defined on the config property "import".'); + } + e[0] && (a = await T.import.call(Da, e[0]), Da.import(e[0], a)); + } else { + (d = e && Da[c].apply(Da, e)) && d.then && (d = await d), d && d.await && (d = await d.await), "search" === c && d.result && (d = d.result); + } + postMessage("search" === c ? {id:b, msg:d} : {id:b}); + } +} +;function Fa(a) { + Ga.call(a, "add"); + Ga.call(a, "append"); + Ga.call(a, "search"); + Ga.call(a, "update"); + Ga.call(a, "remove"); + Ga.call(a, "searchCache"); +} +let Ha, Ia, Ja; +function Ka() { + Ha = Ja = 0; +} +function Ga(a) { + this[a + "Async"] = function() { + const c = arguments; + var b = c[c.length - 1]; + let e; + "function" === typeof b && (e = b, delete c[c.length - 1]); + Ha ? Ja || (Ja = Date.now() - Ia >= this.priority * this.priority * 3) : (Ha = setTimeout(Ka, 0), Ia = Date.now()); + if (Ja) { + const f = this; + return new Promise(g => { + setTimeout(function() { + g(f[a + "Async"].apply(f, c)); + }, 0); + }); + } + const d = this[a].apply(this, c); + b = d.then ? d : new Promise(f => f(d)); + e && b.then(e); + return b; + }; +} +;let W = 0; +function La(a = {}, c) { + function b(h) { + function k(m) { + m = m.data || m; + const l = m.id, n = l && f.h[l]; + n && (n(m.msg), delete f.h[l]); + } + this.worker = h; + this.h = I(); + if (this.worker) { + d ? this.worker.on("message", k) : this.worker.onmessage = k; + if (a.config) { + return new Promise(function(m) { + 1e9 < W && (W = 0); + f.h[++W] = function() { + m(f); + }; + f.worker.postMessage({id:W, task:"init", factory:e, options:a}); + }); + } + this.priority = a.priority || 4; + this.encoder = c || null; + this.worker.postMessage({task:"init", factory:e, options:a}); + return this; + } + console.warn("Worker is not available on this platform. Please report on Github: https://github.com/nextapps-de/flexsearch/issues"); + } + if (!this || this.constructor !== La) { + return new La(a); + } + let e = "undefined" !== typeof self ? self._factory : "undefined" !== typeof window ? window._factory : null; + e && (e = e.toString()); + const d = "undefined" === typeof window, f = this, g = Ma(e, d, a.worker); + return g.then ? g.then(function(h) { + return b.call(f, h); + }) : b.call(this, g); +} +X("add"); +X("append"); +X("search"); +X("update"); +X("remove"); +X("clear"); +X("export"); +X("import"); +La.prototype.searchCache = ma; +Fa(La.prototype); +function X(a) { + La.prototype[a] = function() { + const c = this, b = [].slice.call(arguments); + var e = b[b.length - 1]; + let d; + "function" === typeof e && (d = e, b.pop()); + e = new Promise(function(f) { + "export" === a && "function" === typeof b[0] && (b[0] = null); + 1e9 < W && (W = 0); + c.h[++W] = f; + c.worker.postMessage({task:a, id:W, args:b}); + }); + return d ? (e.then(d), this) : e; + }; +} +function Ma(a, c, b) { + return c ? "undefined" !== typeof module ? new(require("worker_threads")["Worker"])(__dirname+"/worker/node.js") : import("worker_threads").then(function(worker){return new worker["Worker"](import.meta.dirname+"/node/node.mjs")}) : a ? new window.Worker(URL.createObjectURL(new Blob(["onmessage=" + Ea.toString()], {type:"text/javascript"}))) : new window.Worker("string" === typeof b ? b : import.meta.url.replace("/worker.js", "/worker/worker.js").replace("flexsearch.bundle.module.min.js", + "module/worker/worker.js"), {type:"module"}); +} +;Na.prototype.add = function(a, c, b) { + ba(a) && (c = a, a = da(c, this.key)); + if (c && (a || 0 === a)) { + if (!b && this.reg.has(a)) { + return this.update(a, c); } for (let h = 0, k; h < this.field.length; h++) { - k = this.F[h]; - var d = this.index.get(this.field[h]); + k = this.G[h]; + var e = this.index.get(this.field[h]); if ("function" === typeof k) { - var e = k(b); - e && d.add(a, e, !1, !0); + var d = k(c); + d && e.add(a, d, !1, !0); } else { - if (e = k.I, !e || e(b)) { - k.constructor === String ? k = ["" + k] : M(k) && (k = [k]), Ra(b, k, this.J, 0, d, a, k[0], c); + if (d = k.J, !d || d(c)) { + k.constructor === String ? k = ["" + k] : N(k) && (k = [k]), Qa(c, k, this.K, 0, e, a, k[0], b); } } } if (this.tag) { - for (d = 0; d < this.D.length; d++) { - var f = this.D[d], g = this.R[d]; - e = this.tag.get(g); - let h = G(); + for (e = 0; e < this.F.length; e++) { + var f = this.F[e], g = this.P[e]; + d = this.tag.get(g); + let h = I(); if ("function" === typeof f) { - if (f = f(b), !f) { + if (f = f(c), !f) { continue; } } else { - const k = f.I; - if (k && !k(b)) { + const k = f.J; + if (k && !k(c)) { continue; } f.constructor === String && (f = "" + f); - f = fa(b, f); + f = da(c, f); } - if (e && f) { - M(f) && (f = [f]); - for (let k = 0, l, m; k < f.length; k++) { - if (l = f[k], !h[l] && (h[l] = 1, (g = e.get(l)) ? m = g : e.set(l, m = []), !c || !m.includes(a))) { - if (m.length === 2 ** 31 - 1) { - g = new U(m); + if (d && f) { + N(f) && (f = [f]); + for (let k = 0, m, l; k < f.length; k++) { + if (m = f[k], !h[m] && (h[m] = 1, (g = d.get(m)) ? l = g : d.set(m, l = []), !b || !l.includes(a))) { + if (l.length === 2 ** 31 - 1) { + g = new Aa(l); if (this.fastupdate) { - for (let p of this.reg.values()) { - p.includes(m) && (p[p.indexOf(m)] = g); + for (let n of this.reg.values()) { + n.includes(l) && (n[n.indexOf(l)] = g); } } - e.set(l, m = g); + d.set(m, l = g); } - m.push(a); - this.fastupdate && ((g = this.reg.get(a)) ? g.push(m) : this.reg.set(a, [m])); + l.push(a); + this.fastupdate && ((g = this.reg.get(a)) ? g.push(l) : this.reg.set(a, [l])); } } } else { - e || console.warn("Tag '" + g + "' was not found"); + d || console.warn("Tag '" + g + "' was not found"); } } } - if (this.store && (!c || !this.store.has(a))) { + if (this.store && (!b || !this.store.has(a))) { let h; - if (this.C) { - h = G(); - for (let k = 0, l; k < this.C.length; k++) { - l = this.C[k]; - if ((c = l.I) && !c(b)) { + if (this.D) { + h = I(); + for (let k = 0, m; k < this.D.length; k++) { + m = this.D[k]; + if ((b = m.J) && !b(c)) { continue; } - let m; - if ("function" === typeof l) { - m = l(b); - if (!m) { + let l; + if ("function" === typeof m) { + l = m(c); + if (!l) { continue; } - l = [l.V]; - } else if (M(l) || l.constructor === String) { - h[l] = b[l]; + m = [m.U]; + } else if (N(m) || m.constructor === String) { + h[m] = c[m]; continue; } - Sa(b, h, l, 0, l[0], m); + Ra(c, h, m, 0, m[0], l); } } - this.store.set(a, h || b); + this.store.set(a, h || c); } this.worker && (this.fastupdate || this.reg.add(a)); } return this; }; -function Sa(a, b, c, d, e, f) { - a = a[e]; - if (d === c.length - 1) { - b[e] = f || a; +function Ra(a, c, b, e, d, f) { + a = a[d]; + if (e === b.length - 1) { + c[d] = f || a; } else if (a) { if (a.constructor === Array) { - for (b = b[e] = Array(a.length), e = 0; e < a.length; e++) { - Sa(a, b, c, d, e); + for (c = c[d] = Array(a.length), d = 0; d < a.length; d++) { + Ra(a, c, b, e, d); } } else { - b = b[e] || (b[e] = G()), e = c[++d], Sa(a, b, c, d, e); + c = c[d] || (c[d] = I()), d = b[++e], Ra(a, c, b, e, d); } } } -function Ra(a, b, c, d, e, f, g, h) { +function Qa(a, c, b, e, d, f, g, h) { if (a = a[g]) { - if (d === b.length - 1) { + if (e === c.length - 1) { if (a.constructor === Array) { - if (c[d]) { - for (b = 0; b < a.length; b++) { - e.add(f, a[b], !0, !0); + if (b[e]) { + for (c = 0; c < a.length; c++) { + d.add(f, a[c], !0, !0); } return; } a = a.join(" "); } - e.add(f, a, h, !0); + d.add(f, a, h, !0); } else { if (a.constructor === Array) { for (g = 0; g < a.length; g++) { - Ra(a, b, c, d, e, f, g, h); + Qa(a, c, b, e, d, f, g, h); } } else { - g = b[++d], Ra(a, b, c, d, e, f, g, h); + g = c[++e], Qa(a, c, b, e, d, f, g, h); } } } else { - e.db && e.remove(f); + d.db && d.remove(f); } } -;function Ta(a, b, c, d) { +;function Sa(a, c, b, e) { if (!a.length) { return a; } if (1 === a.length) { - return a = a[0], a = c || a.length > b ? b ? a.slice(c, c + b) : a.slice(c) : a, d ? X.call(this, a) : a; + return a = a[0], a = b || a.length > c ? a.slice(b, b + c) : a, e ? Ta.call(this, a) : a; } - let e = []; + let d = []; for (let f = 0, g, h; f < a.length; f++) { if ((g = a[f]) && (h = g.length)) { - if (c) { - if (c >= h) { - c -= h; + if (b) { + if (b >= h) { + b -= h; continue; } - c < h && (g = b ? g.slice(c, c + b) : g.slice(c), h = g.length, c = 0); + g = g.slice(b, b + c); + h = g.length; + b = 0; } - h > b && (g = g.slice(0, b), h = b); - if (!e.length && h >= b) { - return d ? X.call(this, g) : g; + h > c && (g = g.slice(0, c), h = c); + if (!d.length && h >= c) { + return e ? Ta.call(this, g) : g; } - e.push(g); - b -= h; - if (!b) { + d.push(g); + c -= h; + if (!c) { break; } } } - e = 1 < e.length ? [].concat.apply([], e) : e[0]; - return d ? X.call(this, e) : e; + d = 1 < d.length ? [].concat.apply([], d) : d[0]; + return e ? Ta.call(this, d) : d; } -;function Ua(a, b, c) { - var d = c[0]; - if (d.then) { - return Promise.all(c).then(function(q) { - return a[b].apply(a, q); - }); +;function Ua(a, c, b, e) { + var d = e[0]; + if (d[0] && d[0].query) { + return a[c].apply(a, d); } - if (d[0] && d[0].index) { - return a[b].apply(a, d); + if (!("and" !== c && "not" !== c || a.result.length || a.await || d.suggest)) { + return 1 < e.length && (d = e[e.length - 1]), (e = d.resolve) ? a.await || a.result : a; } - d = []; - let e = [], f = 0, g = 0, h, k, l, m, p; - for (let q = 0, r; q < c.length; q++) { - if (r = c[q]) { - var u = void 0; - if (r.constructor === Y) { - u = r.result; - } else if (r.constructor === Array) { - u = r; + let f = [], g = 0, h = 0, k, m, l; + var n; + let u; + for (c = 0; c < e.length; c++) { + if (d = e[c]) { + var p = void 0; + if (d.constructor === Y) { + p = d.await || d.result; + } else if (d.then || d.constructor === Array) { + p = d; } else { - f = r.limit || 0; - g = r.offset || 0; - l = r.suggest; - k = r.resolve; - h = (m = r.highlight && k) || r.enrich && k; - let n; - r.index ? a.index = n = r.index : n = a.index; - if (r.query || r.tag) { + g = d.limit || 0; + h = d.offset || 0; + l = d.suggest; + m = d.resolve; + k = (n = d.highlight && m) || d.enrich && m; + p = d.queue; + let t = d.async || p; + (n = d.index) ? a.index || (a.index = n) : n = a.index; + if (d.query || d.tag) { if (!a.index) { throw Error("Resolver can't apply because the corresponding Index was never specified"); } - if (u = r.field || r.pluck) { + const r = d.field || d.pluck; + if (r) { if (!a.index.index) { throw Error("Resolver can't apply because the corresponding Document Index was not specified"); } - n = a.index.index.get(u); + n = a.index.index.get(r); if (!n) { - throw Error("Resolver can't apply because the specified Document Field '" + u + "' was not found"); + throw Error("Resolver can't apply because the specified Document Field '" + r + "' was not found"); } } - r.resolve = !1; - u = n.search(r).result; - r.resolve = k; - m && (p = r.query); - } else if (r.and) { - u = a.and(r.and); - } else if (r.or) { - u = a.or(r.or); - } else if (r.xor) { - u = a.xor(r.xor); - } else if (r.not) { - u = a.not(r.not); + if (p && (u || a.await)) { + u = 1; + let q; + const y = a.C.length, v = new Promise(function(A) { + q = A; + }); + (function(A, B) { + v.h = function() { + B.index = null; + B.resolve = !1; + let E = t ? A.searchAsync(B) : A.search(B); + if (E.then) { + return E.then(function(C) { + a.C[y] = C = C.result || C; + q(C); + return C; + }); + } + E = E.result || E; + q(E); + return E; + }; + })(n, Object.assign({}, d)); + a.C.push(v); + f[c] = v; + continue; + } else { + d.resolve = !1, d.index = null, p = t ? n.searchAsync(d) : n.search(d), d.resolve = m, d.index = n; + } + } else if (d.and) { + p = Va(d, "and", n); + } else if (d.or) { + p = Va(d, "or", n); + } else if (d.not) { + p = Va(d, "not", n); + } else if (d.xor) { + p = Va(d, "xor", n); } else { continue; } } - if (u.then) { - e.push(u); - } else if (u.length) { - d[q] = u; - } else if (!l && ("and" === b || "xor" === b)) { - d = []; - break; - } + p.await ? (u = 1, p = p.await) : p.then ? (u = 1, p = p.then(function(t) { + return t.result || t; + })) : p = p.result || p; + f[c] = p; } } - return {O:d, P:e, limit:f, offset:g, enrich:h, resolve:k, suggest:l, highlight:m, W:p}; -} -;Y.prototype.or = function() { - const {O:a, P:b, limit:c, offset:d, enrich:e, resolve:f} = Ua(this, "or", arguments); - return Va.call(this, a, b, c, d, e, f); -}; -function Va(a, b, c, d, e, f) { - if (b.length) { - const g = this; - return Promise.all(b).then(function(h) { - a = []; - for (let k = 0, l; k < h.length; k++) { - (l = h[k]).length && (a[k] = l); - } - return Va.call(g, a, [], c, d, e, f); - }); - } - a.length && (this.result.length && a.push(this.result), 2 > a.length ? this.result = a[0] : (this.result = Wa(a, c, d, !1, this.h), d = 0)); - return f ? this.resolve(c, d, e) : this; -} -;Y.prototype.and = function() { - let a = this.result.length, b, c, d, e; - if (!a) { - const f = arguments[0]; - f && (a = !!f.suggest, e = f.resolve, b = f.limit, c = f.offset, d = f.enrich && e); - } - if (a) { - const {O:f, P:g, limit:h, offset:k, enrich:l, resolve:m, suggest:p} = Ua(this, "and", arguments); - return Xa.call(this, f, g, h, k, l, m, p); - } - return e ? this.resolve(b, c, d) : this; -}; -function Xa(a, b, c, d, e, f, g) { - if (b.length) { - const h = this; - return Promise.all(b).then(function(k) { - a = []; - for (let l = 0, m; l < k.length; l++) { - (m = k[l]).length && (a[l] = m); - } - return Xa.call(h, a, [], c, d, e, f, g); - }); - } - if (a.length) { - if (this.result.length && a.unshift(this.result), 2 > a.length) { - this.result = a[0]; - } else { - if (b = ha(a)) { - return this.result = Ya(a, b, c, d, g, this.h, f), f ? e ? X.call(this.index, this.result) : this.result : this; - } - this.result = []; - } - } else { - g || (this.result = a); - } - return f ? this.resolve(c, d, e) : this; -} -;Y.prototype.xor = function() { - const {O:a, P:b, limit:c, offset:d, enrich:e, resolve:f, suggest:g} = Ua(this, "xor", arguments); - return Za.call(this, a, b, c, d, e, f, g); -}; -function Za(a, b, c, d, e, f, g) { - if (b.length) { - const h = this; - return Promise.all(b).then(function(k) { - a = []; - for (let l = 0, m; l < k.length; l++) { - (m = k[l]).length && (a[l] = m); - } - return Za.call(h, a, [], c, d, e, f, g); - }); - } - if (a.length) { - if (this.result.length && a.unshift(this.result), 2 > a.length) { - this.result = a[0]; - } else { - return this.result = $a.call(this, a, c, d, f, this.h), f ? e ? X.call(this.index, this.result) : this.result : this; - } - } else { - g || (this.result = a); - } - return f ? this.resolve(c, d, e) : this; -} -function $a(a, b, c, d, e) { - const f = [], g = G(); - let h = 0; - for (let k = 0, l; k < a.length; k++) { - if (l = a[k]) { - h < l.length && (h = l.length); - for (let m = 0, p; m < l.length; m++) { - if (p = l[m]) { - for (let u = 0, q; u < p.length; u++) { - q = p[u], g[q] = g[q] ? 2 : 1; - } - } - } - } - } - for (let k = 0, l, m = 0; k < h; k++) { - for (let p = 0, u; p < a.length; p++) { - if (u = a[p]) { - if (l = u[k]) { - for (let q = 0, r; q < l.length; q++) { - if (r = l[q], 1 === g[r]) { - if (c) { - c--; - } else { - if (d) { - if (f.push(r), f.length === b) { - return f; - } - } else { - const n = k + (p ? e : 0); - f[n] || (f[n] = []); - f[n].push(r); - if (++m === b) { - return f; - } - } - } - } - } - } - } - } - } - return f; -} -;Y.prototype.not = function() { - const {O:a, P:b, limit:c, offset:d, enrich:e, resolve:f, suggest:g} = Ua(this, "not", arguments); - return ab.call(this, a, b, c, d, e, f, g); -}; -function ab(a, b, c, d, e, f, g) { - if (b.length) { - const h = this; - return Promise.all(b).then(function(k) { - a = []; - for (let l = 0, m; l < k.length; l++) { - (m = k[l]).length && (a[l] = m); - } - return ab.call(h, a, [], c, d, e, f, g); - }); - } - if (a.length && this.result.length) { - this.result = bb.call(this, a, c, d, f); - } else if (f) { - return this.resolve(c, d, e); - } - return f ? e ? X.call(this.index, this.result) : this.result : this; -} -function bb(a, b, c, d) { - const e = []; - a = new Set(a.flat().flat()); - for (let f = 0, g, h = 0; f < this.result.length; f++) { - if (g = this.result[f]) { - for (let k = 0, l; k < g.length; k++) { - if (l = g[k], !a.has(l)) { - if (c) { - c--; - } else { - if (d) { - if (e.push(l), e.length === b) { - return e; - } - } else { - if (e[f] || (e[f] = []), e[f].push(l), ++h === b) { - return e; - } - } - } - } - } - } - } - return e; -} -;function cb(a, b, c, d, e) { - let f, g, h; - "string" === typeof e ? (f = e, e = "") : f = e.template; - if (!f) { - throw Error('No template pattern was specified by the search option "highlight"'); - } - g = f.indexOf("$1"); - if (-1 === g) { - throw Error('Invalid highlight template. The replacement pattern "$1" was not found in template: ' + f); - } - h = f.substring(g + 2); - g = f.substring(0, g); - let k = e && e.boundary, l = !e || !1 !== e.clip, m = e && e.merge && h && g && new RegExp(h + " " + g, "g"); - e = e && e.ellipsis; - var p = 0; - if ("object" === typeof e) { - var u = e.template; - p = u.length - 2; - e = e.pattern; - } - "string" !== typeof e && (e = !1 === e ? "" : "..."); - p && (e = u.replace("$1", e)); - u = e.length - p; - let q, r; - "object" === typeof k && (q = k.before, 0 === q && (q = -1), r = k.after, 0 === r && (r = -1), k = k.total || 9e5); - p = new Map(); - for (let La = 0, ba, fb, oa; La < b.length; La++) { - let pa; - if (d) { - pa = b, oa = d; - } else { - var n = b[La]; - oa = n.field; - if (!oa) { - continue; - } - pa = n.result; - } - fb = c.get(oa); - ba = fb.encoder; - n = p.get(ba); - "string" !== typeof n && (n = ba.encode(a), p.set(ba, n)); - for (let wa = 0; wa < pa.length; wa++) { - var t = pa[wa].doc; - if (!t) { - continue; - } - t = fa(t, oa); - if (!t) { - continue; - } - var x = t.trim().split(/\s+/); - if (!x.length) { - continue; - } - t = ""; - var y = []; - let xa = []; - var D = -1, H = -1, B = 0; - for (var v = 0; v < x.length; v++) { - var A = x[v], I = ba.encode(A); - I = 1 < I.length ? I.join(" ") : I[0]; - let z; - if (I && A) { - var C = A.length, J = (ba.split ? A.replace(ba.split, "") : A).length - I.length, F = "", S = 0; - for (var ca = 0; ca < n.length; ca++) { - var Q = n[ca]; - if (Q) { - var L = Q.length; - L += J; - S && L <= S || (Q = I.indexOf(Q), -1 < Q && (F = (Q ? A.substring(0, Q) : "") + g + A.substring(Q, Q + L) + h + (Q + L < C ? A.substring(Q + L) : ""), S = L, z = !0)); - } - } - F && (k && (0 > D && (D = t.length + (t ? 1 : 0)), H = t.length + (t ? 1 : 0) + F.length, B += C, xa.push(y.length), y.push({match:F})), t += (t ? " " : "") + F); - } - if (!z) { - A = x[v], t += (t ? " " : "") + A, k && y.push({text:A}); - } else if (k && B >= k) { + u && !a.await && (a.await = new Promise(function(t) { + a.return = t; + })); + if (u) { + const t = Promise.all(f).then(function(r) { + for (let q = 0; q < a.C.length; q++) { + if (a.C[q] === t) { + a.C[q] = function() { + return b.call(a, r, g, h, k, m, l); + }; break; } } - B = xa.length * (f.length - 2); - if (q || r || k && t.length - B > k) { - if (B = k + B - 2 * u, v = H - D, 0 < q && (v += q), 0 < r && (v += r), v <= B) { - x = q ? D - (0 < q ? q : 0) : D - ((B - v) / 2 | 0), y = r ? H + (0 < r ? r : 0) : x + B, l || (0 < x && " " !== t.charAt(x) && " " !== t.charAt(x - 1) && (x = t.indexOf(" ", x), 0 > x && (x = 0)), y < t.length && " " !== t.charAt(y - 1) && " " !== t.charAt(y) && (y = t.lastIndexOf(" ", y), y < H ? y = H : ++y)), t = (x ? e : "") + t.substring(x, y) + (y < t.length ? e : ""); - } else { - H = []; - D = {}; - B = {}; - v = {}; - A = {}; - I = {}; - F = J = C = 0; - for (ca = S = 1;;) { - var T = void 0; - for (let z = 0, K; z < xa.length; z++) { - K = xa[z]; - if (F) { - if (J !== F) { - if (v[z + 1]) { - continue; - } - K += F; - if (D[K]) { - C -= u; - B[z + 1] = 1; - v[z + 1] = 1; - continue; - } - if (K >= y.length - 1) { - if (K >= y.length) { - v[z + 1] = 1; - K >= x.length && (B[z + 1] = 1); - continue; - } - C -= u; - } - t = y[K].text; - if (L = r && I[z]) { - if (0 < L) { - if (t.length > L) { - if (v[z + 1] = 1, l) { - t = t.substring(0, L); - } else { - continue; - } - } - (L -= t.length) || (L = -1); - I[z] = L; - } else { - v[z + 1] = 1; - continue; - } - } - if (C + t.length + 1 <= k) { - t = " " + t, H[z] += t; - } else if (l) { - T = k - C - 1, 0 < T && (t = " " + t.substring(0, T), H[z] += t), v[z + 1] = 1; - } else { - v[z + 1] = 1; - continue; - } - } else { - if (v[z]) { - continue; - } - K -= J; - if (D[K]) { - C -= u; - v[z] = 1; - B[z] = 1; - continue; - } - if (0 >= K) { - if (0 > K) { - v[z] = 1; - B[z] = 1; - continue; - } - C -= u; - } - t = y[K].text; - if (L = q && A[z]) { - if (0 < L) { - if (t.length > L) { - if (v[z] = 1, l) { - t = t.substring(t.length - L); - } else { - continue; - } - } - (L -= t.length) || (L = -1); - A[z] = L; - } else { - v[z] = 1; - continue; - } - } - if (C + t.length + 1 <= k) { - t += " ", H[z] = t + H[z]; - } else if (l) { - T = t.length + 1 - (k - C), 0 <= T && T < t.length && (t = t.substring(T) + " ", H[z] = t + H[z]), v[z] = 1; - } else { - v[z] = 1; - continue; - } - } - } else { - t = y[K].match; - q && (A[z] = q); - r && (I[z] = r); - z && C++; - let Ma; - K ? !z && u && (C += u) : (B[z] = 1, v[z] = 1); - K >= x.length - 1 ? Ma = 1 : K < y.length - 1 && y[K + 1].match ? Ma = 1 : u && (C += u); - C -= f.length - 2; - if (!z || C + t.length <= k) { - H[z] = t; - } else { - T = S = ca = B[z] = 0; - break; - } - Ma && (B[z + 1] = 1, v[z + 1] = 1); - } - C += t.length; - T = D[K] = 1; - } - if (T) { - J === F ? F++ : J++; - } else { - J === F ? S = 0 : ca = 0; - if (!S && !ca) { - break; - } - S ? (J++, F = J) : F++; - } - } - t = ""; - for (let z = 0, K; z < H.length; z++) { - K = (z && B[z] ? " " : (z && !e ? " " : "") + e) + H[z], t += K; - } - e && !B[H.length] && (t += e); - } - } - m && (t = t.replace(m, " ")); - pa[wa].highlight = t; - } - if (d) { - break; - } + Wa(a); + }); + a.C.push(t); + } else if (a.await) { + a.C.push(function() { + return b.call(a, f, g, h, k, m, l); + }); + } else { + return b.call(a, f, g, h, k, m, l); } + return m ? a.await || a.result : a; +} +function Va(a, c, b) { + a = a[c]; + const e = a[0] || a; + e.index || (e.index = b); + b = new Y(e); + 1 < a.length && (b = b[c].apply(b, a.slice(1))); return b; } -;function Y(a, b) { +;Y.prototype.or = function() { + return Ua(this, "or", Xa, arguments); +}; +function Xa(a, c, b, e, d) { + a.length && (this.result.length && a.push(this.result), 2 > a.length ? this.result = a[0] : (this.result = Ya(a, c, b, !1, this.h), b = 0)); + d && (this.await = null); + return d ? this.resolve(c, b, e) : this; +} +;Y.prototype.and = function() { + return Ua(this, "and", Za, arguments); +}; +function Za(a, c, b, e, d, f) { + if (!f && !this.result.length) { + return d ? this.result : this; + } + let g; + if (a.length) { + if (this.result.length && a.unshift(this.result), 2 > a.length) { + this.result = a[0]; + } else { + let h = 0; + for (let k = 0, m, l; k < a.length; k++) { + if ((m = a[k]) && (l = m.length)) { + h < l && (h = l); + } else if (!f) { + h = 0; + break; + } + } + h ? (this.result = $a(a, h, c, b, f, this.h, d), g = !0) : this.result = []; + } + } else { + f || (this.result = a); + } + d && (this.await = null); + return d ? this.resolve(c, b, e, g) : this; +} +;Y.prototype.xor = function() { + return Ua(this, "xor", ab, arguments); +}; +function ab(a, c, b, e, d, f) { + if (a.length) { + if (this.result.length && a.unshift(this.result), 2 > a.length) { + this.result = a[0]; + } else { + a: { + f = b; + var g = this.h; + const h = [], k = I(); + let m = 0; + for (let l = 0, n; l < a.length; l++) { + if (n = a[l]) { + m < n.length && (m = n.length); + for (let u = 0, p; u < n.length; u++) { + if (p = n[u]) { + for (let t = 0, r; t < p.length; t++) { + r = p[t], k[r] = k[r] ? 2 : 1; + } + } + } + } + } + for (let l = 0, n, u = 0; l < m; l++) { + for (let p = 0, t; p < a.length; p++) { + if (t = a[p]) { + if (n = t[l]) { + for (let r = 0, q; r < n.length; r++) { + if (q = n[r], 1 === k[q]) { + if (f) { + f--; + } else { + if (d) { + if (h.push(q), h.length === c) { + a = h; + break a; + } + } else { + const y = l + (p ? g : 0); + h[y] || (h[y] = []); + h[y].push(q); + if (++u === c) { + a = h; + break a; + } + } + } + } + } + } + } + } + } + a = h; + } + this.result = a; + g = !0; + } + } else { + f || (this.result = a); + } + d && (this.await = null); + return d ? this.resolve(c, b, e, g) : this; +} +;Y.prototype.not = function() { + return Ua(this, "not", bb, arguments); +}; +function bb(a, c, b, e, d, f) { + if (!f && !this.result.length) { + return d ? this.result : this; + } + if (a.length && this.result.length) { + a: { + f = b; + var g = []; + a = new Set(a.flat().flat()); + for (let h = 0, k, m = 0; h < this.result.length; h++) { + if (k = this.result[h]) { + for (let l = 0, n; l < k.length; l++) { + if (n = k[l], !a.has(n)) { + if (f) { + f--; + } else { + if (d) { + if (g.push(n), g.length === c) { + a = g; + break a; + } + } else { + if (g[h] || (g[h] = []), g[h].push(n), ++m === c) { + a = g; + break a; + } + } + } + } + } + } + } + a = g; + } + this.result = a; + g = !0; + } + d && (this.await = null); + return d ? this.resolve(c, b, e, g) : this; +} +;function Y(a, c) { if (!this || this.constructor !== Y) { - return new Y(a, b); + return new Y(a, c); } + let b = 0, e, d, f; if (a && a.index) { - return a.resolve = !1, this.index = a.index, this.h = a.boost || 0, this.result = this.index.search(a).result, this; + const g = a; + c = g.index; + b = g.boost || 0; + if (g.query) { + const h = g.resolve; + a = g.async || g.queue; + g.resolve = !1; + g.index = null; + a = a ? c.searchAsync(g) : c.search(g); + g.resolve = h; + g.index = c; + a = a.result || a; + } else { + a = []; + } } - this.index = b || null; + if (a && a.then) { + const g = this; + a = a.then(function(h) { + g.C[0] = g.result = h.result || h; + Wa(g); + }); + e = [a]; + a = []; + d = new Promise(function(h) { + f = h; + }); + } + this.index = c || null; this.result = a || []; - this.h = 0; + this.h = b; + this.C = e || []; + this.await = d || null; + this.return = f || null; } Y.prototype.limit = function(a) { if (this.result.length) { - const b = []; - for (let c = 0, d; c < this.result.length; c++) { - if (d = this.result[c]) { - if (d.length <= a) { - if (b[c] = d, a -= d.length, !a) { + const c = []; + for (let b = 0, e; b < this.result.length; b++) { + if (e = this.result[b]) { + if (e.length <= a) { + if (c[b] = e, a -= e.length, !a) { break; } } else { - b[c] = d.slice(0, a); + c[b] = e.slice(0, a); break; } } } - this.result = b; + this.result = c; } return this; }; Y.prototype.offset = function(a) { if (this.result.length) { - const b = []; - for (let c = 0, d; c < this.result.length; c++) { - if (d = this.result[c]) { - d.length <= a ? a -= d.length : (b[c] = d.slice(a), a = 0); + const c = []; + for (let b = 0, e; b < this.result.length; b++) { + if (e = this.result[b]) { + e.length <= a ? a -= e.length : (c[b] = e.slice(a), a = 0); } } - this.result = b; + this.result = c; } return this; }; @@ -1375,87 +1168,115 @@ Y.prototype.boost = function(a) { this.h += a; return this; }; -Y.prototype.resolve = function(a, b, c) { - const d = this.index; - let e = this.result; - this.result = this.index = null; - e.length && ("object" === typeof a && (c = a.enrich, b = a.offset, a = a.limit), e = Ta.call(d, e, a || 100, b, c)); - return e; +function Wa(a, c) { + let b = a.result; + for (let d = 0, f; d < a.C.length; d++) { + if (f = a.C[d]) { + if ("function" === typeof f) { + b = f(), a.C[d] = b = b.result || b, d--; + } else if (f.h) { + b = f.h(), a.C[d] = b = b.result || b, d--; + } else if (f.then) { + return a.await; + } + } + } + const e = a.return; + a.result = b; + a.C = []; + a.await = null; + a.return = null; + c || e(b); + return b; +} +Y.prototype.resolve = function(a, c, b, e) { + let d = this.await ? Wa(this, !0) : this.result; + if (d.then) { + const f = this; + return d.then(function() { + return f.resolve(a, c, b); + }); + } + d.length && ("object" === typeof a && (b = a.enrich, c = a.offset, a.highlight && console.warn('Highlighting results is not supported within the resolve() method. Instead pass highlight options within the last resolver stage like { query: "...", resolve: true, highlight: ... }.'), a = a.limit), d = e ? b ? Ta.call(this.index, d) : d : Sa.call(this.index, d, a || 100, c, b)); + e = this.return; + this.return = this.await = this.C = this.result = this.index = null; + e && e(d); + return d; }; -function Ya(a, b, c, d, e, f, g) { +function $a(a, c, b, e, d, f, g) { const h = a.length; - let k = [], l, m; - l = G(); - for (let p = 0, u, q, r, n; p < b; p++) { - for (let t = 0; t < h; t++) { - if (r = a[t], p < r.length && (u = r[p])) { - for (let x = 0; x < u.length; x++) { - q = u[x]; - (m = l[q]) ? l[q]++ : (m = 0, l[q] = 1); - n = k[m] || (k[m] = []); + let k = [], m, l; + m = I(); + for (let n = 0, u, p, t, r; n < c; n++) { + for (let q = 0; q < h; q++) { + if (t = a[q], n < t.length && (u = t[n])) { + for (let y = 0; y < u.length; y++) { + p = u[y]; + (l = m[p]) ? m[p]++ : (l = 0, m[p] = 1); + r = k[l] || (k[l] = []); if (!g) { - let y = p + (t || !e ? 0 : f || 0); - n = n[y] || (n[y] = []); + let v = n + (q || !d ? 0 : f || 0); + r = r[v] || (r[v] = []); } - n.push(q); - if (g && c && m === h - 1 && n.length - d === c) { - return d ? n.slice(d) : n; + r.push(p); + if (g && b && l === h - 1 && r.length - e === b) { + return e ? r.slice(e) : r; } } } } } if (a = k.length) { - if (e) { - k = 1 < k.length ? Wa(k, c, d, g, f) : (k = k[0]).length > c || d ? k.slice(d, c + d) : k; + if (d) { + k = 1 < k.length ? Ya(k, b, e, g, f) : (k = k[0]) && b && k.length > b || e ? k.slice(e, b + e) : k; } else { if (a < h) { return []; } k = k[a - 1]; - if (c || d) { + if (b || e) { if (g) { - if (k.length > c || d) { - k = k.slice(d, c + d); + if (k.length > b || e) { + k = k.slice(e, b + e); } } else { - e = []; - for (let p = 0, u; p < k.length; p++) { - if (u = k[p]) { - if (d && u.length > d) { - d -= u.length; + d = []; + for (let n = 0, u; n < k.length; n++) { + if (u = k[n]) { + if (e && u.length > e) { + e -= u.length; } else { - if (c && u.length > c || d) { - u = u.slice(d, c + d), c -= u.length, d && (d -= u.length); + if (b && u.length > b || e) { + u = u.slice(e, b + e), b -= u.length, e && (e -= u.length); } - e.push(u); - if (!c) { + d.push(u); + if (!b) { break; } } } } - k = e; + k = d; } } } } return k; } -function Wa(a, b, c, d, e) { - const f = [], g = G(); +function Ya(a, c, b, e, d) { + const f = [], g = I(); let h; var k = a.length; - let l; - if (d) { - for (e = k - 1; 0 <= e; e--) { - if (l = (d = a[e]) && d.length) { - for (k = 0; k < l; k++) { - if (h = d[k], !g[h]) { - if (g[h] = 1, c) { - c--; + let m; + if (e) { + for (d = k - 1; 0 <= d; d--) { + if (m = (e = a[d]) && e.length) { + for (k = 0; k < m; k++) { + if (h = e[k], !g[h]) { + if (g[h] = 1, b) { + b--; } else { - if (f.push(h), f.length === b) { + if (f.push(h), f.length === c) { return f; } } @@ -1464,18 +1285,18 @@ function Wa(a, b, c, d, e) { } } } else { - for (let m = k - 1, p, u = 0; 0 <= m; m--) { - p = a[m]; - for (let q = 0; q < p.length; q++) { - if (l = (d = p[q]) && d.length) { - for (let r = 0; r < l; r++) { - if (h = d[r], !g[h]) { - if (g[h] = 1, c) { - c--; + for (let l = k - 1, n, u = 0; 0 <= l; l--) { + n = a[l]; + for (let p = 0; p < n.length; p++) { + if (m = (e = n[p]) && e.length) { + for (let t = 0; t < m; t++) { + if (h = e[t], !g[h]) { + if (g[h] = 1, b) { + b--; } else { - let n = (q + (m < k - 1 ? e || 0 : 0)) / (m + 1) | 0; - (f[n] || (f[n] = [])).push(h); - if (++u === b) { + let r = (p + (l < k - 1 ? d || 0 : 0)) / (l + 1) | 0; + (f[r] || (f[r] = [])).push(h); + if (++u === c) { return f; } } @@ -1487,334 +1308,557 @@ function Wa(a, b, c, d, e) { } return f; } -function db(a, b, c) { - const d = G(), e = []; - for (let f = 0, g; f < b.length; f++) { - g = b[f]; +function cb(a, c, b) { + const e = I(), d = []; + for (let f = 0, g; f < c.length; f++) { + g = c[f]; for (let h = 0; h < g.length; h++) { - d[g[h]] = 1; + e[g[h]] = 1; } } - if (c) { + if (b) { for (let f = 0, g; f < a.length; f++) { - g = a[f], d[g] && (e.push(g), d[g] = 0); + g = a[f], e[g] && (d.push(g), e[g] = 0); } } else { for (let f = 0, g, h; f < a.result.length; f++) { - for (g = a.result[f], b = 0; b < g.length; b++) { - h = g[b], d[h] && ((e[f] || (e[f] = [])).push(h), d[h] = 0); + for (g = a.result[f], c = 0; c < g.length; c++) { + h = g[c], e[h] && ((d[f] || (d[f] = [])).push(h), e[h] = 0); } } } - return e; + return d; } -;G(); -Qa.prototype.search = function(a, b, c, d) { - c || (!b && da(a) ? (c = a, a = "") : da(b) && (c = b, b = 0)); - if (c && c.cache) { - c.cache = !1; - var e = this.searchCache(a, b, c); - c.cache = !0; - return e; +;I(); +function eb(a, c, b, e, d) { + let f, g, h; + "string" === typeof d ? (f = d, d = "") : f = d.template; + if (!f) { + throw Error('No template pattern was specified by the search option "highlight"'); } - let f = []; - var g = []; - let h, k, l, m, p; - let u = 0, q = !0, r; - if (c) { - c.constructor === Array && (c = {index:c}); - a = c.query || a; - h = c.pluck; - k = c.merge; - m = c.boost; - p = h || c.field || (p = c.index) && (p.index ? null : p); - var n = this.tag && c.tag; - l = c.suggest; - q = !1 !== c.resolve; - this.store && c.highlight && !q ? console.warn("Highlighting results can only be done on a final resolver task or when calling .resolve({ highlight: ... })") : this.store && c.enrich && !q && console.warn("Enrich results can only be done on a final resolver task or when calling .resolve({ enrich: true })"); - r = q && this.store && c.highlight; - e = !!r || q && this.store && c.enrich; - b = c.limit || b; - var t = c.offset || 0; - b || (b = q ? 100 : 0); - if (n && (!this.db || !d)) { - n.constructor !== Array && (n = [n]); - var x = []; - for (let B = 0, v; B < n.length; B++) { - v = n[B]; - if (M(v)) { + g = f.indexOf("$1"); + if (-1 === g) { + throw Error('Invalid highlight template. The replacement pattern "$1" was not found in template: ' + f); + } + h = f.substring(g + 2); + g = f.substring(0, g); + let k = d && d.boundary, m = !d || !1 !== d.clip, l = d && d.merge && h && g && new RegExp(h + " " + g, "g"); + d = d && d.ellipsis; + var n = 0; + if ("object" === typeof d) { + var u = d.template; + n = u.length - 2; + d = d.pattern; + } + "string" !== typeof d && (d = !1 === d ? "" : "..."); + n && (d = u.replace("$1", d)); + u = d.length - n; + let p, t; + "object" === typeof k && (p = k.before, 0 === p && (p = -1), t = k.after, 0 === t && (t = -1), k = k.total || 9e5); + n = new Map(); + for (let Oa = 0, ea, db, oa; Oa < c.length; Oa++) { + let pa; + if (e) { + pa = c, oa = e; + } else { + var r = c[Oa]; + oa = r.field; + if (!oa) { + continue; + } + pa = r.result; + } + db = b.get(oa); + ea = db.encoder; + r = n.get(ea); + "string" !== typeof r && (r = ea.encode(a), n.set(ea, r)); + for (let xa = 0; xa < pa.length; xa++) { + var q = pa[xa].doc; + if (!q) { + continue; + } + q = da(q, oa); + if (!q) { + continue; + } + var y = q.trim().split(/\s+/); + if (!y.length) { + continue; + } + q = ""; + var v = []; + let ya = []; + var A = -1, B = -1, E = 0; + for (var C = 0; C < y.length; C++) { + var F = y[C], z = ea.encode(F); + z = 1 < z.length ? z.join(" ") : z[0]; + let x; + if (z && F) { + var D = F.length, K = (ea.split ? F.replace(ea.split, "") : F).length - z.length, G = "", L = 0; + for (var O = 0; O < r.length; O++) { + var P = r[O]; + if (P) { + var M = P.length; + M += K; + L && M <= L || (P = z.indexOf(P), -1 < P && (G = (P ? F.substring(0, P) : "") + g + F.substring(P, P + M) + h + (P + M < D ? F.substring(P + M) : ""), L = M, x = !0)); + } + } + G && (k && (0 > A && (A = q.length + (q ? 1 : 0)), B = q.length + (q ? 1 : 0) + G.length, E += D, ya.push(v.length), v.push({match:G})), q += (q ? " " : "") + G); + } + if (!x) { + F = y[C], q += (q ? " " : "") + F, k && v.push({text:F}); + } else if (k && E >= k) { + break; + } + } + E = ya.length * (f.length - 2); + if (p || t || k && q.length - E > k) { + if (E = k + E - 2 * u, C = B - A, 0 < p && (C += p), 0 < t && (C += t), C <= E) { + y = p ? A - (0 < p ? p : 0) : A - ((E - C) / 2 | 0), v = t ? B + (0 < t ? t : 0) : y + E, m || (0 < y && " " !== q.charAt(y) && " " !== q.charAt(y - 1) && (y = q.indexOf(" ", y), 0 > y && (y = 0)), v < q.length && " " !== q.charAt(v - 1) && " " !== q.charAt(v) && (v = q.lastIndexOf(" ", v), v < B ? v = B : ++v)), q = (y ? d : "") + q.substring(y, v) + (v < q.length ? d : ""); + } else { + B = []; + A = {}; + E = {}; + C = {}; + F = {}; + z = {}; + G = K = D = 0; + for (O = L = 1;;) { + var U = void 0; + for (let x = 0, J; x < ya.length; x++) { + J = ya[x]; + if (G) { + if (K !== G) { + if (C[x + 1]) { + continue; + } + J += G; + if (A[J]) { + D -= u; + E[x + 1] = 1; + C[x + 1] = 1; + continue; + } + if (J >= v.length - 1) { + if (J >= v.length) { + C[x + 1] = 1; + J >= y.length && (E[x + 1] = 1); + continue; + } + D -= u; + } + q = v[J].text; + if (M = t && z[x]) { + if (0 < M) { + if (q.length > M) { + if (C[x + 1] = 1, m) { + q = q.substring(0, M); + } else { + continue; + } + } + (M -= q.length) || (M = -1); + z[x] = M; + } else { + C[x + 1] = 1; + continue; + } + } + if (D + q.length + 1 <= k) { + q = " " + q, B[x] += q; + } else if (m) { + U = k - D - 1, 0 < U && (q = " " + q.substring(0, U), B[x] += q), C[x + 1] = 1; + } else { + C[x + 1] = 1; + continue; + } + } else { + if (C[x]) { + continue; + } + J -= K; + if (A[J]) { + D -= u; + C[x] = 1; + E[x] = 1; + continue; + } + if (0 >= J) { + if (0 > J) { + C[x] = 1; + E[x] = 1; + continue; + } + D -= u; + } + q = v[J].text; + if (M = p && F[x]) { + if (0 < M) { + if (q.length > M) { + if (C[x] = 1, m) { + q = q.substring(q.length - M); + } else { + continue; + } + } + (M -= q.length) || (M = -1); + F[x] = M; + } else { + C[x] = 1; + continue; + } + } + if (D + q.length + 1 <= k) { + q += " ", B[x] = q + B[x]; + } else if (m) { + U = q.length + 1 - (k - D), 0 <= U && U < q.length && (q = q.substring(U) + " ", B[x] = q + B[x]), C[x] = 1; + } else { + C[x] = 1; + continue; + } + } + } else { + q = v[J].match; + p && (F[x] = p); + t && (z[x] = t); + x && D++; + let Pa; + J ? !x && u && (D += u) : (E[x] = 1, C[x] = 1); + J >= y.length - 1 ? Pa = 1 : J < v.length - 1 && v[J + 1].match ? Pa = 1 : u && (D += u); + D -= f.length - 2; + if (!x || D + q.length <= k) { + B[x] = q; + } else { + U = L = O = E[x] = 0; + break; + } + Pa && (E[x + 1] = 1, C[x + 1] = 1); + } + D += q.length; + U = A[J] = 1; + } + if (U) { + K === G ? G++ : K++; + } else { + K === G ? L = 0 : O = 0; + if (!L && !O) { + break; + } + L ? (K++, G = K) : G++; + } + } + q = ""; + for (let x = 0, J; x < B.length; x++) { + J = (x && E[x] ? " " : (x && !d ? " " : "") + d) + B[x], q += J; + } + d && !E[B.length] && (q += d); + } + } + l && (q = q.replace(l, " ")); + pa[xa].highlight = q; + } + if (e) { + break; + } + } + return c; +} +;Na.prototype.search = function(a, c, b, e) { + b || (!c && ba(a) ? (b = a, a = "") : ba(c) && (b = c, c = 0)); + let d = []; + var f = []; + let g; + let h, k, m, l, n; + let u = 0, p = !0, t; + if (b) { + b.constructor === Array && (b = {index:b}); + a = b.query || a; + g = b.pluck; + h = b.merge; + m = b.boost; + n = g || b.field || (n = b.index) && (n.index ? null : n); + var r = this.tag && b.tag; + k = b.suggest; + p = !1 !== b.resolve; + l = b.cache; + this.store && b.highlight && !p ? console.warn("Highlighting results can only be done on a final resolver task or when calling .resolve({ highlight: ... })") : this.store && b.enrich && !p && console.warn("Enrich results can only be done on a final resolver task or when calling .resolve({ enrich: true })"); + t = p && this.store && b.highlight; + var q = !!t || p && this.store && b.enrich; + c = b.limit || c; + var y = b.offset || 0; + c || (c = p ? 100 : 0); + if (r && (!this.db || !e)) { + r.constructor !== Array && (r = [r]); + var v = []; + for (let F = 0, z; F < r.length; F++) { + z = r[F]; + if (N(z)) { throw Error("A tag option can't be a string, instead it needs a { field: tag } format."); } - if (v.field && v.tag) { - var y = v.tag; - if (y.constructor === Array) { - for (var D = 0; D < y.length; D++) { - x.push(v.field, y[D]); + if (z.field && z.tag) { + var A = z.tag; + if (A.constructor === Array) { + for (var B = 0; B < A.length; B++) { + v.push(z.field, A[B]); } } else { - x.push(v.field, y); + v.push(z.field, A); } } else { - y = Object.keys(v); - for (let A = 0, I, C; A < y.length; A++) { - if (I = y[A], C = v[I], C.constructor === Array) { - for (D = 0; D < C.length; D++) { - x.push(I, C[D]); + A = Object.keys(z); + for (let D = 0, K, G; D < A.length; D++) { + if (K = A[D], G = z[K], G.constructor === Array) { + for (B = 0; B < G.length; B++) { + v.push(K, G[B]); } } else { - x.push(I, C); + v.push(K, G); } } } } - if (!x.length) { + if (!v.length) { throw Error("Your tag definition within the search options is probably wrong. No valid tags found."); } - n = x; + r = v; if (!a) { - g = []; - if (x.length) { - for (n = 0; n < x.length; n += 2) { + f = []; + if (v.length) { + for (r = 0; r < v.length; r += 2) { if (this.db) { - d = this.index.get(x[n]); - if (!d) { - console.warn("Tag '" + x[n] + ":" + x[n + 1] + "' will be skipped because there is no field '" + x[n] + "'."); + e = this.index.get(v[r]); + if (!e) { + console.warn("Tag '" + v[r] + ":" + v[r + 1] + "' will be skipped because there is no field '" + v[r] + "'."); continue; } - g.push(d = d.db.tag(x[n + 1], b, t, e)); + f.push(e = e.db.tag(v[r + 1], c, y, q)); } else { - d = eb.call(this, x[n], x[n + 1], b, t, e); + e = fb.call(this, v[r], v[r + 1], c, y, q); } - f.push(q ? {field:x[n], tag:x[n + 1], result:d} : [d]); + d.push(p ? {field:v[r], tag:v[r + 1], result:e} : [e]); } } - if (g.length) { - const B = this; - return Promise.all(g).then(function(v) { - for (let A = 0; A < v.length; A++) { - q ? f[A].result = v[A] : f[A] = v[A]; + if (f.length) { + const F = this; + return Promise.all(f).then(function(z) { + for (let D = 0; D < z.length; D++) { + p ? d[D].result = z[D] : d[D] = z[D]; } - return q ? f : new Y(1 < f.length ? Ya(f, 1, 0, 0, l, m) : f[0], B); + return p ? d : new Y(1 < d.length ? $a(d, 1, 0, 0, k, m) : d[0], F); }); } - return q ? f : new Y(1 < f.length ? Ya(f, 1, 0, 0, l, m) : f[0], this); + return p ? d : new Y(1 < d.length ? $a(d, 1, 0, 0, k, m) : d[0], this); } } - if (!q && !h) { - if (p = p || this.field) { - M(p) ? h = p : (p.constructor === Array && 1 === p.length && (p = p[0]), h = p.field || p.index); + if (!p && !g) { + if (n = n || this.field) { + N(n) ? g = n : (n.constructor === Array && 1 === n.length && (n = n[0]), g = n.field || n.index); } - if (!h) { + if (!g) { throw Error("Apply resolver on document search requires either the option 'pluck' to be set or just select a single field name in your query."); } } - p && p.constructor !== Array && (p = [p]); + n && n.constructor !== Array && (n = [n]); } - p || (p = this.field); - let H; - x = (this.worker || this.db) && !d && []; - for (let B = 0, v, A, I; B < p.length; B++) { - A = p[B]; - if (this.db && this.tag && !this.F[B]) { + n || (n = this.field); + let E; + v = (this.worker || this.db) && !e && []; + for (let F = 0, z, D, K; F < n.length; F++) { + D = n[F]; + if (this.db && this.tag && !this.G[F]) { continue; } - let C; - M(A) || (C = A, A = C.field, a = C.query || a, b = aa(C.limit, b), t = aa(C.offset, t), l = aa(C.suggest, l), r = q && this.store && aa(C.highlight, r), e = !!r || q && this.store && aa(C.enrich, e)); - if (d) { - v = d[B]; + let G; + N(D) || (G = D, D = G.field, a = G.query || a, c = aa(G.limit, c), y = aa(G.offset, y), k = aa(G.suggest, k), t = p && this.store && aa(G.highlight, t), q = !!t || p && this.store && aa(G.enrich, q), l = aa(G.cache, l)); + if (e) { + z = e[F]; } else { - if (y = C || c, D = this.index.get(A), n && (this.db && (y.tag = n, H = D.db.support_tag_search, y.field = p), H || (y.enrich = !1)), x) { - x[B] = D.search(a, b, y); - y && e && (y.enrich = e); + A = G || b || {}; + B = A.enrich; + var C = this.index.get(D); + r && (this.db && (A.tag = r, E = C.db.support_tag_search, A.field = n), !E && B && (A.enrich = !1)); + z = l ? C.searchCache(a, c, A) : C.search(a, c, A); + B && (A.enrich = B); + if (v) { + v[F] = z; continue; - } else { - v = D.search(a, b, y), y && e && (y.enrich = e); } } - I = v && (q ? v.length : v.result.length); - if (n && I) { - y = []; - D = 0; - if (this.db && d) { - if (!H) { - for (let J = p.length; J < d.length; J++) { - let F = d[J]; - if (F && F.length) { - D++, y.push(F); - } else if (!l) { - return q ? f : new Y(f, this); + K = (z = z.result || z) && z.length; + if (r && K) { + A = []; + B = 0; + if (this.db && e) { + if (!E) { + for (C = n.length; C < e.length; C++) { + let L = e[C]; + if (L && L.length) { + B++, A.push(L); + } else if (!k) { + return p ? d : new Y(d, this); } } } } else { - for (let J = 0, F, S; J < n.length; J += 2) { - F = this.tag.get(n[J]); - if (!F) { - if (console.warn("Tag '" + n[J] + ":" + n[J + 1] + "' will be skipped because there is no field '" + n[J] + "'."), l) { + for (let L = 0, O, P; L < r.length; L += 2) { + O = this.tag.get(r[L]); + if (!O) { + if (console.warn("Tag '" + r[L] + ":" + r[L + 1] + "' will be skipped because there is no field '" + r[L] + "'."), k) { continue; } else { - return q ? f : new Y(f, this); + return p ? d : new Y(d, this); } } - if (S = (F = F && F.get(n[J + 1])) && F.length) { - D++, y.push(F); - } else if (!l) { - return q ? f : new Y(f, this); + if (P = (O = O && O.get(r[L + 1])) && O.length) { + B++, A.push(O); + } else if (!k) { + return p ? d : new Y(d, this); } } } - if (D) { - v = db(v, y, q); - I = v.length; - if (!I && !l) { - return q ? v : new Y(v, this); + if (B) { + z = cb(z, A, p); + K = z.length; + if (!K && !k) { + return p ? z : new Y(z, this); } - D--; + B--; } } - if (I) { - g[u] = A, f.push(v), u++; - } else if (1 === p.length) { - return q ? f : new Y(f, this); + if (K) { + f[u] = D, d.push(z), u++; + } else if (1 === n.length) { + return p ? d : new Y(d, this); } } - if (x) { - if (this.db && n && n.length && !H) { - for (e = 0; e < n.length; e += 2) { - g = this.index.get(n[e]); - if (!g) { - if (console.warn("Tag '" + n[e] + ":" + n[e + 1] + "' was not found because there is no field '" + n[e] + "'."), l) { + if (v) { + if (this.db && r && r.length && !E) { + for (q = 0; q < r.length; q += 2) { + f = this.index.get(r[q]); + if (!f) { + if (console.warn("Tag '" + r[q] + ":" + r[q + 1] + "' was not found because there is no field '" + r[q] + "'."), k) { continue; } else { - return q ? f : new Y(f, this); + return p ? d : new Y(d, this); } } - x.push(g.db.tag(n[e + 1], b, t, !1)); + v.push(f.db.tag(r[q + 1], c, y, !1)); } } - const B = this; - return Promise.all(x).then(function(v) { - return v.length ? B.search(a, b, c, v) : v; + const F = this; + return Promise.all(v).then(function(z) { + b && (b.resolve = p); + z.length && (z = F.search(a, c, b, z)); + return z; }); } if (!u) { - return q ? f : new Y(f, this); + return p ? d : new Y(d, this); } - if (h && (!e || !this.store)) { - return f = f[0], q || (f.index = this), f; + if (g && (!q || !this.store)) { + return d = d[0], p ? d : new Y(d, this); } - x = []; - for (t = 0; t < g.length; t++) { - n = f[t]; - e && n.length && "undefined" === typeof n[0].doc && (this.db ? x.push(n = this.index.get(this.field[0]).db.enrich(n)) : n = X.call(this, n)); - if (h) { - return q ? r ? cb(a, n, this.index, h, r) : n : new Y(n, this); + v = []; + for (y = 0; y < f.length; y++) { + r = d[y]; + q && r.length && "undefined" === typeof r[0].doc && (this.db ? v.push(r = this.index.get(this.field[0]).db.enrich(r)) : r = Ta.call(this, r)); + if (g) { + return p ? t ? eb(a, r, this.index, g, t) : r : new Y(r, this); } - f[t] = {field:g[t], result:n}; + d[y] = {field:f[y], result:r}; } - if (e && this.db && x.length) { - const B = this; - return Promise.all(x).then(function(v) { - for (let A = 0; A < v.length; A++) { - f[A].result = v[A]; + if (q && this.db && v.length) { + const F = this; + return Promise.all(v).then(function(z) { + for (let D = 0; D < z.length; D++) { + d[D].result = z[D]; } - r && (f = cb(a, f, B.index, h, r)); - return k ? gb(f) : f; + t && (d = eb(a, d, F.index, g, t)); + return h ? gb(d) : d; }); } - r && (f = cb(a, f, this.index, h, r)); - return k ? gb(f) : f; + t && (d = eb(a, d, this.index, g, t)); + return h ? gb(d) : d; }; function gb(a) { - const b = [], c = G(), d = G(); - for (let e = 0, f, g, h, k, l, m, p; e < a.length; e++) { - f = a[e]; + const c = [], b = I(), e = I(); + for (let d = 0, f, g, h, k, m, l, n; d < a.length; d++) { + f = a[d]; g = f.field; h = f.result; for (let u = 0; u < h.length; u++) { - if (l = h[u], "object" !== typeof l ? l = {id:k = l} : k = l.id, (m = c[k]) ? m.push(g) : (l.field = c[k] = [g], b.push(l)), p = l.highlight) { - m = d[k], m || (d[k] = m = {}, l.highlight = m), m[g] = p; + if (m = h[u], "object" !== typeof m ? m = {id:k = m} : k = m.id, (l = b[k]) ? l.push(g) : (m.field = b[k] = [g], c.push(m)), n = m.highlight) { + l = e[k], l || (e[k] = l = {}, m.highlight = l), l[g] = n; } } } - return b; + return c; } -function eb(a, b, c, d, e) { +function fb(a, c, b, e, d) { a = this.tag.get(a); if (!a) { return []; } - a = a.get(b); + a = a.get(c); if (!a) { return []; } - b = a.length - d; - if (0 < b) { - if (c && b > c || d) { - a = a.slice(d, d + c); + c = a.length - e; + if (0 < c) { + if (b && c > b || e) { + a = a.slice(e, e + b); } - e && (a = X.call(this, a)); + d && (a = Ta.call(this, a)); } return a; } -function X(a) { +function Ta(a) { if (!this || !this.store) { return a; } - const b = Array(a.length); - for (let c = 0, d; c < a.length; c++) { - d = a[c], b[c] = {id:d, doc:this.store.get(d)}; + if (this.db) { + return this.index.get(this.field[0]).db.enrich(a); } - return b; + const c = Array(a.length); + for (let b = 0, e; b < a.length; b++) { + e = a[b], c[b] = {id:e, doc:this.store.get(e)}; + } + return c; } -;const hb = {normalize:!1, numeric:!1, dedupe:!1}; -const ib = {}; -const jb = new Map([["b", "p"], ["v", "f"], ["w", "f"], ["z", "s"], ["x", "s"], ["d", "t"], ["n", "m"], ["c", "k"], ["g", "k"], ["j", "k"], ["q", "k"], ["i", "e"], ["y", "e"], ["u", "o"]]); -const kb = new Map([["ae", "a"], ["oe", "o"], ["sh", "s"], ["kh", "k"], ["th", "t"], ["ph", "f"], ["pf", "f"]]), lb = [/([^aeo])h(.)/g, "$1$2", /([aeo])h([^aeo]|$)/g, "$1$2", /(.)\1+/g, "$1"]; -const mb = {a:"", e:"", i:"", o:"", u:"", y:"", b:1, f:1, p:1, v:1, c:2, g:2, j:2, k:2, q:2, s:2, x:2, z:2, "\u00df":2, d:3, t:3, l:4, m:5, n:5, r:6}; -var nb = {Exact:hb, Default:ib, Normalize:ib, LatinBalance:{mapper:jb}, LatinAdvanced:{mapper:jb, matcher:kb, replacer:lb}, LatinExtra:{mapper:jb, replacer:lb.concat([/(?!^)[aeo]/g, ""]), matcher:kb}, LatinSoundex:{dedupe:!1, include:{letter:!0}, finalize:function(a) { - for (let c = 0; c < a.length; c++) { - var b = a[c]; - let d = b.charAt(0), e = mb[d]; - for (let f = 1, g; f < b.length && (g = b.charAt(f), "h" === g || "w" === g || !(g = mb[g]) || g === e || (d += g, e = g, 4 !== d.length)); f++) { - } - a[c] = d; +;function Na(a) { + if (!this || this.constructor !== Na) { + return new Na(a); } -}}, CJK:{split:""}, LatinExact:hb, LatinDefault:ib, LatinSimple:ib}; -function Qa(a) { - if (!this || this.constructor !== Qa) { - return new Qa(a); - } - const b = a.document || a.doc || a; - let c, d; - this.F = []; + const c = a.document || a.doc || a; + let b, e; + this.G = []; this.field = []; - this.J = []; - this.key = (c = b.key || b.id) && ob(c, this.J) || "id"; - (d = a.keystore || 0) && (this.keystore = d); + this.K = []; + this.key = (b = c.key || c.id) && hb(b, this.K) || "id"; + (e = a.keystore || 0) && (this.keystore = e); this.fastupdate = !!a.fastupdate; - this.reg = !this.fastupdate || a.worker || a.db ? d ? new W(d) : new Set() : d ? new V(d) : new Map(); - this.C = (c = b.store || null) && c && !0 !== c && []; - this.store = c && (d ? new V(d) : new Map()); - this.cache = (c = a.cache || null) && new pb(c); + this.reg = !this.fastupdate || a.worker || a.db ? e ? new S(e) : new Set() : e ? new R(e) : new Map(); + this.D = (b = c.store || null) && b && !0 !== b && []; + this.store = b && (e ? new R(e) : new Map()); + this.cache = (b = a.cache || null) && new na(b); a.cache = !1; this.worker = a.worker || !1; this.priority = a.priority || 4; - this.index = qb.call(this, a, b); + this.index = ib.call(this, a, c); this.tag = null; - if (c = b.tag) { - if ("string" === typeof c && (c = [c]), c.length) { + if (b = c.tag) { + if ("string" === typeof b && (b = [b]), b.length) { this.tag = new Map(); - this.D = []; - this.R = []; - for (let e = 0, f, g; e < c.length; e++) { - f = c[e]; + this.F = []; + this.P = []; + for (let d = 0, f, g; d < b.length; d++) { + f = b[d]; g = f.field || f; if (!g) { throw Error("The tag field from the document descriptor is undefined."); } - f.custom ? this.D[e] = f.custom : (this.D[e] = ob(g, this.J), f.filter && ("string" === typeof this.D[e] && (this.D[e] = new String(this.D[e])), this.D[e].I = f.filter)); - this.R[e] = g; + f.custom ? this.F[d] = f.custom : (this.F[d] = hb(g, this.K), f.filter && ("string" === typeof this.F[d] && (this.F[d] = new String(this.F[d])), this.F[d].J = f.filter)); + this.P[d] = g; this.tag.set(g, new Map()); } } @@ -1822,126 +1866,126 @@ function Qa(a) { if (this.worker) { this.fastupdate = !1; a = []; - for (const e of this.index.values()) { - e.then && a.push(e); + for (const d of this.index.values()) { + d.then && a.push(d); } if (a.length) { - const e = this; + const d = this; return Promise.all(a).then(function(f) { let g = 0; - for (const h of e.index.entries()) { + for (const h of d.index.entries()) { const k = h[0]; - let l = h[1]; - l.then && (l = f[g], e.index.set(k, l), g++); + let m = h[1]; + m.then && (m = f[g], d.index.set(k, m), g++); } - return e; + return d; }); } } else { a.db && (this.fastupdate = !1, this.mount(a.db)); } } -w = Qa.prototype; +w = Na.prototype; w.mount = function(a) { if (this.worker) { throw Error("You can't use Worker-Indexes on a persistent model. That would be useless, since each of the persistent model acts like Worker-Index by default (Master/Slave)."); } - let b = this.field; + let c = this.field; if (this.tag) { - for (let f = 0, g; f < this.R.length; f++) { - g = this.R[f]; - var c = void 0; - this.index.set(g, c = new P({}, this.reg)); - b === this.field && (b = b.slice(0)); - b.push(g); - c.tag = this.tag.get(g); + for (let f = 0, g; f < this.P.length; f++) { + g = this.P[f]; + var b = void 0; + this.index.set(g, b = new V({}, this.reg)); + c === this.field && (c = c.slice(0)); + c.push(g); + b.tag = this.tag.get(g); } } - c = []; - const d = {db:a.db, type:a.type, fastupdate:a.fastupdate}; - for (let f = 0, g, h; f < b.length; f++) { - d.field = h = b[f]; + b = []; + const e = {db:a.db, type:a.type, fastupdate:a.fastupdate}; + for (let f = 0, g, h; f < c.length; f++) { + e.field = h = c[f]; g = this.index.get(h); - const k = new a.constructor(a.id, d); + const k = new a.constructor(a.id, e); k.id = a.id; - c[f] = k.mount(g); + b[f] = k.mount(g); g.document = !0; f ? g.bypass = !0 : g.store = this.store; } - const e = this; - return this.db = Promise.all(c).then(function() { - e.db = !0; + const d = this; + return this.db = Promise.all(b).then(function() { + d.db = !0; }); }; -w.commit = async function(a, b) { - const c = []; - for (const d of this.index.values()) { - c.push(d.commit(a, b)); +w.commit = async function(a, c) { + const b = []; + for (const e of this.index.values()) { + b.push(e.commit(a, c)); } - await Promise.all(c); + await Promise.all(b); this.reg.clear(); }; w.destroy = function() { const a = []; - for (const b of this.index.values()) { - a.push(b.destroy()); + for (const c of this.index.values()) { + a.push(c.destroy()); } return Promise.all(a); }; -function qb(a, b) { - const c = new Map(); - let d = b.index || b.field || b; - M(d) && (d = [d]); - for (let f = 0, g, h; f < d.length; f++) { - g = d[f]; - M(g) || (h = g, g = g.field); - h = da(h) ? Object.assign({}, a, h) : a; +function ib(a, c) { + const b = new Map(); + let e = c.index || c.field || c; + N(e) && (e = [e]); + for (let f = 0, g, h; f < e.length; f++) { + g = e[f]; + N(g) || (h = g, g = g.field); + h = ba(h) ? Object.assign({}, a, h) : a; if (this.worker) { - var e = void 0; - e = (e = h.encoder) && e.encode ? e : new na("string" === typeof e ? nb[e] : e || {}); - e = new Ba(h, e); - c.set(g, e); + var d = void 0; + d = (d = h.encoder) && d.encode ? d : new la("string" === typeof d ? wa[d] : d || {}); + d = new La(h, d); + b.set(g, d); } - this.worker || c.set(g, new P(h, this.reg)); - h.custom ? this.F[f] = h.custom : (this.F[f] = ob(g, this.J), h.filter && ("string" === typeof this.F[f] && (this.F[f] = new String(this.F[f])), this.F[f].I = h.filter)); + this.worker || b.set(g, new V(h, this.reg)); + h.custom ? this.G[f] = h.custom : (this.G[f] = hb(g, this.K), h.filter && ("string" === typeof this.G[f] && (this.G[f] = new String(this.G[f])), this.G[f].J = h.filter)); this.field[f] = g; } - if (this.C) { - a = b.store; - M(a) && (a = [a]); + if (this.D) { + a = c.store; + N(a) && (a = [a]); for (let f = 0, g, h; f < a.length; f++) { - g = a[f], h = g.field || g, g.custom ? (this.C[f] = g.custom, g.custom.V = h) : (this.C[f] = ob(h, this.J), g.filter && ("string" === typeof this.C[f] && (this.C[f] = new String(this.C[f])), this.C[f].I = g.filter)); + g = a[f], h = g.field || g, g.custom ? (this.D[f] = g.custom, g.custom.U = h) : (this.D[f] = hb(h, this.K), g.filter && ("string" === typeof this.D[f] && (this.D[f] = new String(this.D[f])), this.D[f].J = g.filter)); } } - return c; + return b; } -function ob(a, b) { - const c = a.split(":"); - let d = 0; - for (let e = 0; e < c.length; e++) { - a = c[e], "]" === a[a.length - 1] && (a = a.substring(0, a.length - 2)) && (b[d] = !0), a && (c[d++] = a); +function hb(a, c) { + const b = a.split(":"); + let e = 0; + for (let d = 0; d < b.length; d++) { + a = b[d], "]" === a[a.length - 1] && (a = a.substring(0, a.length - 2)) && (c[e] = !0), a && (b[e++] = a); } - d < c.length && (c.length = d); - return 1 < d ? c : c[0]; + e < b.length && (b.length = e); + return 1 < e ? b : b[0]; } -w.append = function(a, b) { - return this.add(a, b, !0); +w.append = function(a, c) { + return this.add(a, c, !0); }; -w.update = function(a, b) { - return this.remove(a).add(a, b); +w.update = function(a, c) { + return this.remove(a).add(a, c); }; w.remove = function(a) { - da(a) && (a = fa(a, this.key)); - for (var b of this.index.values()) { - b.remove(a, !0); + ba(a) && (a = da(a, this.key)); + for (var c of this.index.values()) { + c.remove(a, !0); } if (this.reg.has(a)) { if (this.tag && !this.fastupdate) { - for (let c of this.tag.values()) { - for (let d of c) { - b = d[0]; - const e = d[1], f = e.indexOf(a); - -1 < f && (1 < e.length ? e.splice(f, 1) : c.delete(b)); + for (let b of this.tag.values()) { + for (let e of b) { + c = e[0]; + const d = e[1], f = d.indexOf(a); + -1 < f && (1 < d.length ? d.splice(f, 1) : b.delete(c)); } } } @@ -1953,13 +1997,13 @@ w.remove = function(a) { }; w.clear = function() { const a = []; - for (const b of this.index.values()) { - const c = b.clear(); - c.then && a.push(c); + for (const c of this.index.values()) { + const b = c.clear(); + b.then && a.push(b); } if (this.tag) { - for (const b of this.tag.values()) { - b.clear(); + for (const c of this.tag.values()) { + c.clear(); } } this.store && this.store.clear(); @@ -1976,233 +2020,279 @@ w.cleanup = function() { return this; }; w.get = function(a) { - return this.db ? this.index.get(this.field[0]).db.enrich(a).then(function(b) { - return b[0] && b[0].doc || null; + return this.db ? this.index.get(this.field[0]).db.enrich(a).then(function(c) { + return c[0] && c[0].doc || null; }) : this.store.get(a) || null; }; -w.set = function(a, b) { - "object" === typeof a && (b = a, a = fa(b, this.key)); - this.store.set(a, b); +w.set = function(a, c) { + "object" === typeof a && (c = a, a = da(c, this.key)); + this.store.set(a, c); return this; }; -w.searchCache = rb; -w.export = function(a, b, c = 0, d = 0) { - if (c < this.field.length) { - const g = this.field[c]; - if ((b = this.index.get(g).export(a, g, c, d = 1)) && b.then) { +w.searchCache = ma; +w.export = jb; +w.import = kb; +Fa(Na.prototype); +function lb(a, c = 0) { + let b = [], e = []; + c && (c = 250000 / c * 5000 | 0); + for (const d of a.entries()) { + e.push(d), e.length === c && (b.push(e), e = []); + } + e.length && b.push(e); + return b; +} +function mb(a, c) { + c || (c = new Map()); + for (let b = 0, e; b < a.length; b++) { + e = a[b], c.set(e[0], e[1]); + } + return c; +} +function nb(a, c = 0) { + let b = [], e = []; + c && (c = 250000 / c * 1000 | 0); + for (const d of a.entries()) { + e.push([d[0], lb(d[1])[0]]), e.length === c && (b.push(e), e = []); + } + e.length && b.push(e); + return b; +} +function ob(a, c) { + c || (c = new Map()); + for (let b = 0, e, d; b < a.length; b++) { + e = a[b], d = c.get(e[0]), c.set(e[0], mb(e[1], d)); + } + return c; +} +function pb(a) { + let c = [], b = []; + for (const e of a.keys()) { + b.push(e), 250000 === b.length && (c.push(b), b = []); + } + b.length && c.push(b); + return c; +} +function qb(a, c) { + c || (c = new Set()); + for (let b = 0; b < a.length; b++) { + c.add(a[b]); + } + return c; +} +function rb(a, c, b, e, d, f, g = 0) { + const h = e && e.constructor === Array; + var k = h ? e.shift() : e; + if (!k) { + return this.export(a, c, d, f + 1); + } + if ((k = a((c ? c + "." : "") + (g + 1) + "." + b, JSON.stringify(k))) && k.then) { + const m = this; + return k.then(function() { + return rb.call(m, a, c, b, h ? e : null, d, f, g + 1); + }); + } + return rb.call(this, a, c, b, h ? e : null, d, f, g + 1); +} +function jb(a, c, b = 0, e = 0) { + if (b < this.field.length) { + const g = this.field[b]; + if ((c = this.index.get(g).export(a, g, b, e = 1)) && c.then) { const h = this; - return b.then(function() { - return h.export(a, g, c + 1); + return c.then(function() { + return h.export(a, g, b + 1); }); } - return this.export(a, g, c + 1); + return this.export(a, g, b + 1); } - let e, f; - switch(d) { + let d, f; + switch(e) { case 0: - e = "reg"; - f = Ha(this.reg); - b = null; + d = "reg"; + f = pb(this.reg); + c = null; break; case 1: - e = "tag"; - f = this.tag && Fa(this.tag, this.reg.size); - b = null; + d = "tag"; + f = this.tag && nb(this.tag, this.reg.size); + c = null; break; case 2: - e = "doc"; - f = this.store && Da(this.store); - b = null; + d = "doc"; + f = this.store && lb(this.store); + c = null; break; default: return; } - return Ja.call(this, a, b, e, f, c, d); -}; -w.import = function(a, b) { - var c = a.split("."); - "json" === c[c.length - 1] && c.pop(); - const d = 2 < c.length ? c[0] : ""; - c = 2 < c.length ? c[2] : c[1]; - if (this.worker && d) { - return this.index.get(d).import(a); + return rb.call(this, a, c, d, f, b, e); +} +function kb(a, c) { + var b = a.split("."); + "json" === b[b.length - 1] && b.pop(); + const e = 2 < b.length ? b[0] : ""; + b = 2 < b.length ? b[2] : b[1]; + if (this.worker && e) { + return this.index.get(e).import(a); } - if (b) { - "string" === typeof b && (b = JSON.parse(b)); - if (d) { - return this.index.get(d).import(c, b); + if (c) { + "string" === typeof c && (c = JSON.parse(c)); + if (e) { + return this.index.get(e).import(b, c); } - switch(c) { + switch(b) { case "reg": this.fastupdate = !1; - this.reg = Ia(b, this.reg); - for (let e = 0, f; e < this.field.length; e++) { - f = this.index.get(this.field[e]), f.fastupdate = !1, f.reg = this.reg; + this.reg = qb(c, this.reg); + for (let d = 0, f; d < this.field.length; d++) { + f = this.index.get(this.field[d]), f.fastupdate = !1, f.reg = this.reg; } if (this.worker) { - b = []; - for (const e of this.index.values()) { - b.push(e.import(a)); + c = []; + for (const d of this.index.values()) { + c.push(d.import(a)); } - return Promise.all(b); + return Promise.all(c); } break; case "tag": - this.tag = Ga(b, this.tag); + this.tag = ob(c, this.tag); break; case "doc": - this.store = Ea(b, this.store); + this.store = mb(c, this.store); } } -}; -sa(Qa.prototype); -function rb(a, b, c) { - const d = (b ? "" + a : "object" === typeof a ? "" + a.query : a).toLowerCase(); - this.cache || (this.cache = new pb()); - let e = this.cache.get(d); - if (!e) { - e = this.search(a, b, c); - if (e.then) { - const f = this; - e.then(function(g) { - f.cache.set(d, g); - return g; - }); +} +function sb(a, c) { + let b = ""; + for (const e of a.entries()) { + a = e[0]; + const d = e[1]; + let f = ""; + for (let g = 0, h; g < d.length; g++) { + h = d[g] || [""]; + let k = ""; + for (let m = 0; m < h.length; m++) { + k += (k ? "," : "") + ("string" === c ? '"' + h[m] + '"' : h[m]); + } + k = "[" + k + "]"; + f += (f ? "," : "") + k; } - this.cache.set(d, e); + f = '["' + a + '",[' + f + "]]"; + b += (b ? "," : "") + f; } - return e; -} -function pb(a) { - this.limit = a && !0 !== a ? a : 1000; - this.cache = new Map(); - this.h = ""; -} -pb.prototype.set = function(a, b) { - this.cache.set(this.h = a, b); - this.cache.size > this.limit && this.cache.delete(this.cache.keys().next().value); -}; -pb.prototype.get = function(a) { - const b = this.cache.get(a); - b && this.h !== a && (this.cache.delete(a), this.cache.set(this.h = a, b)); return b; -}; -pb.prototype.remove = function(a) { - for (const b of this.cache) { - const c = b[0]; - b[1].includes(a) && this.cache.delete(c); - } -}; -pb.prototype.clear = function() { - this.cache.clear(); - this.h = ""; -}; -P.prototype.remove = function(a, b) { - const c = this.reg.size && (this.fastupdate ? this.reg.get(a) : this.reg.has(a)); - if (c) { +} +;V.prototype.remove = function(a, c) { + const b = this.reg.size && (this.fastupdate ? this.reg.get(a) : this.reg.has(a)); + if (b) { if (this.fastupdate) { - for (let d = 0, e, f; d < c.length; d++) { - if ((e = c[d]) && (f = e.length)) { - if (e[f - 1] === a) { - e.pop(); + for (let e = 0, d, f; e < b.length; e++) { + if ((d = b[e]) && (f = d.length)) { + if (d[f - 1] === a) { + d.pop(); } else { - const g = e.indexOf(a); - 0 <= g && e.splice(g, 1); + const g = d.indexOf(a); + 0 <= g && d.splice(g, 1); } } } } else { - sb(this.map, a), this.depth && sb(this.ctx, a); + tb(this.map, a), this.depth && tb(this.ctx, a); } - b || this.reg.delete(a); + c || this.reg.delete(a); } - this.db && (this.commit_task.push({del:a}), this.T && tb(this)); + this.db && (this.commit_task.push({del:a}), this.S && ub(this)); this.cache && this.cache.remove(a); return this; }; -function sb(a, b) { - let c = 0; - var d = "undefined" === typeof b; +function tb(a, c) { + let b = 0; + var e = "undefined" === typeof c; if (a.constructor === Array) { - for (let e = 0, f, g; e < a.length; e++) { - if ((f = a[e]) && f.length) { - if (d) { - c++; - } else { - if (g = f.indexOf(b), 0 <= g) { - if (1 < f.length) { - f.splice(g, 1); - c++; - break; - } else { - delete a[e]; - } - } else { - c++; + for (let d = 0, f, g, h; d < a.length; d++) { + if ((f = a[d]) && f.length) { + if (e) { + return 1; + } + g = f.indexOf(c); + if (0 <= g) { + if (1 < f.length) { + return f.splice(g, 1), 1; } + delete a[d]; + if (b) { + return 1; + } + h = 1; + } else { + if (h) { + return 1; + } + b++; } } } } else { - for (let e of a.entries()) { - d = e[0], sb(e[1], b) ? c++ : a.delete(d); + for (let d of a.entries()) { + e = d[0], tb(d[1], c) ? b++ : a.delete(e); } } - return c; + return b; } -;const ub = {memory:{resolution:1}, performance:{resolution:3, fastupdate:!0, context:{depth:1, resolution:1}}, match:{tokenize:"forward"}, score:{resolution:9, context:{depth:2, resolution:3}}}; -P.prototype.add = function(a, b, c, d) { - if (b && (a || 0 === a)) { - if (!d && !c && this.reg.has(a)) { - return this.update(a, b); +;const vb = {memory:{resolution:1}, performance:{resolution:3, fastupdate:!0, context:{depth:1, resolution:1}}, match:{tokenize:"forward"}, score:{resolution:9, context:{depth:2, resolution:3}}}; +V.prototype.add = function(a, c, b, e) { + if (c && (a || 0 === a)) { + if (!e && !b && this.reg.has(a)) { + return this.update(a, c); } - d = this.depth; - b = this.encoder.encode(b, !d); - const l = b.length; - if (l) { - const m = G(), p = G(), u = this.resolution; - for (let q = 0; q < l; q++) { - let r = b[this.rtl ? l - 1 - q : q]; - var e = r.length; - if (e && (d || !p[r])) { - var f = this.score ? this.score(b, r, q, null, 0) : vb(u, l, q), g = ""; + e = this.depth; + c = this.encoder.encode(c, !e); + const m = c.length; + if (m) { + const l = I(), n = I(), u = this.resolution; + for (let p = 0; p < m; p++) { + let t = c[this.rtl ? m - 1 - p : p]; + var d = t.length; + if (d && (e || !n[t])) { + var f = this.score ? this.score(c, t, p, null, 0) : wb(u, m, p), g = ""; switch(this.tokenize) { case "full": - if (2 < e) { - for (let n = 0, t; n < e; n++) { - for (f = e; f > n; f--) { - g = r.substring(n, f); - t = this.rtl ? e - 1 - n : n; - var h = this.score ? this.score(b, r, q, g, t) : vb(u, l, q, e, t); - wb(this, p, g, h, a, c); + if (2 < d) { + for (let r = 0, q; r < d; r++) { + for (f = d; f > r; f--) { + g = t.substring(r, f); + q = this.rtl ? d - 1 - r : r; + var h = this.score ? this.score(c, t, p, g, q) : wb(u, m, p, d, q); + xb(this, n, g, h, a, b); } } break; } case "bidirectional": case "reverse": - if (1 < e) { - for (h = e - 1; 0 < h; h--) { - g = r[this.rtl ? e - 1 - h : h] + g; - var k = this.score ? this.score(b, r, q, g, h) : vb(u, l, q, e, h); - wb(this, p, g, k, a, c); + if (1 < d) { + for (h = d - 1; 0 < h; h--) { + g = t[this.rtl ? d - 1 - h : h] + g; + var k = this.score ? this.score(c, t, p, g, h) : wb(u, m, p, d, h); + xb(this, n, g, k, a, b); } g = ""; } case "forward": - if (1 < e) { - for (h = 0; h < e; h++) { - g += r[this.rtl ? e - 1 - h : h], wb(this, p, g, f, a, c); + if (1 < d) { + for (h = 0; h < d; h++) { + g += t[this.rtl ? d - 1 - h : h], xb(this, n, g, f, a, b); } break; } default: - if (wb(this, p, r, f, a, c), d && 1 < l && q < l - 1) { - for (e = G(), g = this.U, f = r, h = Math.min(d + 1, this.rtl ? q + 1 : l - q), e[f] = 1, k = 1; k < h; k++) { - if ((r = b[this.rtl ? l - 1 - q - k : q + k]) && !e[r]) { - e[r] = 1; - const n = this.score ? this.score(b, f, q, r, k - 1) : vb(g + (l / 2 > g ? 0 : 1), l, q, h - 1, k - 1), t = this.bidirectional && r > f; - wb(this, m, t ? f : r, n, a, c, t ? r : f); + if (xb(this, n, t, f, a, b), e && 1 < m && p < m - 1) { + for (d = I(), g = this.T, f = t, h = Math.min(e + 1, this.rtl ? p + 1 : m - p), d[f] = 1, k = 1; k < h; k++) { + if ((t = c[this.rtl ? m - 1 - p - k : p + k]) && !d[t]) { + d[t] = 1; + const r = this.score ? this.score(c, f, p, t, k - 1) : wb(g + (m / 2 > g ? 0 : 1), m, p, h - 1, k - 1), q = this.bidirectional && t > f; + xb(this, l, q ? f : t, r, a, b, q ? t : f); } } } @@ -2211,184 +2301,184 @@ P.prototype.add = function(a, b, c, d) { } this.fastupdate || this.reg.add(a); } else { - b = ""; + c = ""; } } - this.db && (b || this.commit_task.push({del:a}), this.T && tb(this)); + this.db && (c || this.commit_task.push({del:a}), this.S && ub(this)); return this; }; -function wb(a, b, c, d, e, f, g) { +function xb(a, c, b, e, d, f, g) { let h = g ? a.ctx : a.map, k; - if (!b[c] || g && !(k = b[c])[g]) { - if (g ? (b = k || (b[c] = G()), b[g] = 1, (k = h.get(g)) ? h = k : h.set(g, h = new Map())) : b[c] = 1, (k = h.get(c)) ? h = k : h.set(c, h = k = []), h = h[d] || (h[d] = []), !f || !h.includes(e)) { + if (!c[b] || g && !(k = c[b])[g]) { + if (g ? (c = k || (c[b] = I()), c[g] = 1, (k = h.get(g)) ? h = k : h.set(g, h = new Map())) : c[b] = 1, (k = h.get(b)) ? h = k : h.set(b, h = k = []), h = h[e] || (h[e] = []), !f || !h.includes(d)) { if (h.length === 2 ** 31 - 1) { - b = new U(h); + c = new Aa(h); if (a.fastupdate) { - for (let l of a.reg.values()) { - l.includes(h) && (l[l.indexOf(h)] = b); + for (let m of a.reg.values()) { + m.includes(h) && (m[m.indexOf(h)] = c); } } - k[d] = h = b; + k[e] = h = c; } - h.push(e); - a.fastupdate && ((d = a.reg.get(e)) ? d.push(h) : a.reg.set(e, [h])); + h.push(d); + a.fastupdate && ((e = a.reg.get(d)) ? e.push(h) : a.reg.set(d, [h])); } } } -function vb(a, b, c, d, e) { - return c && 1 < a ? b + (d || 0) <= a ? c + (e || 0) : (a - 1) / (b + (d || 0)) * (c + (e || 0)) + 1 | 0 : 0; +function wb(a, c, b, e, d) { + return b && 1 < a ? c + (e || 0) <= a ? b + (d || 0) : (a - 1) / (c + (e || 0)) * (b + (d || 0)) + 1 | 0 : 0; } -;P.prototype.search = function(a, b, c) { - c || (b || "object" !== typeof a ? "object" === typeof b && (c = b, b = 0) : (c = a, a = "")); - if (c && c.cache) { - return c.cache = !1, a = this.searchCache(a, b, c), c.cache = !0, a; +;V.prototype.search = function(a, c, b) { + b || (c || "object" !== typeof a ? "object" === typeof c && (b = c, c = 0) : (b = a, a = "")); + if (b && b.cache) { + return b.cache = !1, a = this.searchCache(a, c, b), b.cache = !0, a; } - let d = [], e, f, g, h = 0, k, l, m, p, u; - c && (a = c.query || a, b = c.limit || b, h = c.offset || 0, f = c.context, g = c.suggest, u = (k = c.resolve) && c.enrich, m = c.boost, p = c.resolution, l = this.db && c.tag); + let e = [], d, f, g, h = 0, k, m, l, n, u; + b && (a = b.query || a, c = b.limit || c, h = b.offset || 0, f = b.context, g = b.suggest, u = (k = b.resolve) && b.enrich, l = b.boost, n = b.resolution, m = this.db && b.tag); "undefined" === typeof k && (k = this.resolve); f = this.depth && !1 !== f; - let q = this.encoder.encode(a, !f); - e = q.length; - b = b || (k ? 100 : 0); - if (1 === e) { - return xb.call(this, q[0], "", b, h, k, u, l); + let p = this.encoder.encode(a, !f); + d = p.length; + c = c || (k ? 100 : 0); + if (1 === d) { + return yb.call(this, p[0], "", c, h, k, u, m); } - if (2 === e && f && !g) { - return xb.call(this, q[1], q[0], b, h, k, u, l); + if (2 === d && f && !g) { + return yb.call(this, p[1], p[0], c, h, k, u, m); } - let r = G(), n = 0, t; - f && (t = q[0], n = 1); - p || 0 === p || (p = t ? this.U : this.resolution); + let t = I(), r = 0, q; + f && (q = p[0], r = 1); + n || 0 === n || (n = q ? this.T : this.resolution); if (this.db) { - if (this.db.search && (c = this.db.search(this, q, b, h, g, k, u, l), !1 !== c)) { - return c; + if (this.db.search && (b = this.db.search(this, p, c, h, g, k, u, m), !1 !== b)) { + return b; } - const x = this; + const y = this; return async function() { - for (let y, D; n < e; n++) { - if ((D = q[n]) && !r[D]) { - r[D] = 1; - y = await yb(x, D, t, 0, 0, !1, !1); - if (y = zb(y, d, g, p)) { - d = y; + for (let v, A; r < d; r++) { + if ((A = p[r]) && !t[A]) { + t[A] = 1; + v = await zb(y, A, q, 0, 0, !1, !1); + if (v = Ab(v, e, g, n)) { + e = v; break; } - t && (g && y && d.length || (t = D)); + q && (g && v && e.length || (q = A)); } - g && t && n === e - 1 && !d.length && (p = x.resolution, t = "", n = -1, r = G()); + g && q && r === d - 1 && !e.length && (n = y.resolution, q = "", r = -1, t = I()); } - return Ab(d, p, b, h, g, m, k); + return Bb(e, n, c, h, g, l, k); }(); } - for (let x, y; n < e; n++) { - if ((y = q[n]) && !r[y]) { - r[y] = 1; - x = yb(this, y, t, 0, 0, !1, !1); - if (x = zb(x, d, g, p)) { - d = x; + for (let y, v; r < d; r++) { + if ((v = p[r]) && !t[v]) { + t[v] = 1; + y = zb(this, v, q, 0, 0, !1, !1); + if (y = Ab(y, e, g, n)) { + e = y; break; } - t && (g && x && d.length || (t = y)); + q && (g && y && e.length || (q = v)); } - g && t && n === e - 1 && !d.length && (p = this.resolution, t = "", n = -1, r = G()); + g && q && r === d - 1 && !e.length && (n = this.resolution, q = "", r = -1, t = I()); } - return Ab(d, p, b, h, g, m, k); + return Bb(e, n, c, h, g, l, k); }; -function Ab(a, b, c, d, e, f, g) { +function Bb(a, c, b, e, d, f, g) { let h = a.length, k = a; if (1 < h) { - k = Ya(a, b, c, d, e, f, g); + k = $a(a, c, b, e, d, f, g); } else if (1 === h) { - return g ? Ta.call(null, a[0], c, d) : new Y(a[0], this); + return g ? Sa.call(null, a[0], b, e) : new Y(a[0], this); } return g ? k : new Y(k, this); } -function xb(a, b, c, d, e, f, g) { - a = yb(this, a, b, c, d, e, f, g); +function yb(a, c, b, e, d, f, g) { + a = zb(this, a, c, b, e, d, f, g); return this.db ? a.then(function(h) { - return e ? h || [] : new Y(h, this); - }) : a && a.length ? e ? Ta.call(this, a, c, d) : new Y(a, this) : e ? [] : new Y([], this); + return d ? h || [] : new Y(h, this); + }) : a && a.length ? d ? Sa.call(this, a, b, e) : new Y(a, this) : d ? [] : new Y([], this); } -function zb(a, b, c, d) { - let e = []; +function Ab(a, c, b, e) { + let d = []; if (a && a.length) { - if (a.length <= d) { - b.push(a); + if (a.length <= e) { + c.push(a); return; } - for (let f = 0, g; f < d; f++) { + for (let f = 0, g; f < e; f++) { if (g = a[f]) { - e[f] = g; + d[f] = g; } } - if (e.length) { - b.push(e); + if (d.length) { + c.push(d); return; } } - if (!c) { - return e; + if (!b) { + return d; } } -function yb(a, b, c, d, e, f, g, h) { +function zb(a, c, b, e, d, f, g, h) { let k; - c && (k = a.bidirectional && b > c) && (k = c, c = b, b = k); + b && (k = a.bidirectional && c > b) && (k = b, b = c, c = k); if (a.db) { - return a.db.get(b, c, d, e, f, g, h); + return a.db.get(c, b, e, d, f, g, h); } - a = c ? (a = a.ctx.get(c)) && a.get(b) : a.map.get(b); + a = b ? (a = a.ctx.get(b)) && a.get(c) : a.map.get(c); return a; } -;function P(a, b) { - if (!this || this.constructor !== P) { - return new P(a); +;function V(a, c) { + if (!this || this.constructor !== V) { + return new V(a); } if (a) { - var c = M(a) ? a : a.preset; - c && (ub[c] || console.warn("Preset not found: " + c), a = Object.assign({}, ub[c], a)); + var b = N(a) ? a : a.preset; + b && (vb[b] || console.warn("Preset not found: " + b), a = Object.assign({}, vb[b], a)); } else { a = {}; } - c = a.context; - const d = !0 === c ? {depth:1} : c || {}, e = M(a.encoder) ? nb[a.encoder] : a.encode || a.encoder || {}; - this.encoder = e.encode ? e : "object" === typeof e ? new na(e) : {encode:e}; + b = a.context; + const e = !0 === b ? {depth:1} : b || {}, d = N(a.encoder) ? wa[a.encoder] : a.encode || a.encoder || {}; + this.encoder = d.encode ? d : "object" === typeof d ? new la(d) : {encode:d}; this.resolution = a.resolution || 9; - this.tokenize = c = (c = a.tokenize) && "default" !== c && "exact" !== c && c || "strict"; - this.depth = "strict" === c && d.depth || 0; - this.bidirectional = !1 !== d.bidirectional; + this.tokenize = b = (b = a.tokenize) && "default" !== b && "exact" !== b && b || "strict"; + this.depth = "strict" === b && e.depth || 0; + this.bidirectional = !1 !== e.bidirectional; this.fastupdate = !!a.fastupdate; this.score = a.score || null; - d && d.depth && "strict" !== this.tokenize && console.warn('Context-Search could not applied, because it is just supported when using the tokenizer "strict".'); - (c = a.keystore || 0) && (this.keystore = c); - this.map = c ? new V(c) : new Map(); - this.ctx = c ? new V(c) : new Map(); - this.reg = b || (this.fastupdate ? c ? new V(c) : new Map() : c ? new W(c) : new Set()); - this.U = d.resolution || 3; - this.rtl = e.rtl || a.rtl || !1; - this.cache = (c = a.cache || null) && new pb(c); + e && e.depth && "strict" !== this.tokenize && console.warn('Context-Search could not applied, because it is just supported when using the tokenizer "strict".'); + (b = a.keystore || 0) && (this.keystore = b); + this.map = b ? new R(b) : new Map(); + this.ctx = b ? new R(b) : new Map(); + this.reg = c || (this.fastupdate ? b ? new R(b) : new Map() : b ? new S(b) : new Set()); + this.T = e.resolution || 3; + this.rtl = d.rtl || a.rtl || !1; + this.cache = (b = a.cache || null) && new na(b); this.resolve = !1 !== a.resolve; - if (c = a.db) { - this.db = this.mount(c); + if (b = a.db) { + this.db = this.mount(b); } - this.T = !1 !== a.commit; + this.S = !1 !== a.commit; this.commit_task = []; this.commit_timer = null; this.priority = a.priority || 4; } -w = P.prototype; +w = V.prototype; w.mount = function(a) { this.commit_timer && (clearTimeout(this.commit_timer), this.commit_timer = null); return a.mount(this); }; -w.commit = function(a, b) { +w.commit = function(a, c) { this.commit_timer && (clearTimeout(this.commit_timer), this.commit_timer = null); - return this.db.commit(this, a, b); + return this.db.commit(this, a, c); }; w.destroy = function() { this.commit_timer && (clearTimeout(this.commit_timer), this.commit_timer = null); return this.db.destroy(); }; -function tb(a) { +function ub(a) { a.commit_timer || (a.commit_timer = setTimeout(function() { a.commit_timer = null; a.db.commit(a, void 0, void 0); @@ -2402,101 +2492,101 @@ w.clear = function() { this.db && (this.commit_timer && clearTimeout(this.commit_timer), this.commit_timer = null, this.commit_task = [{clear:!0}]); return this; }; -w.append = function(a, b) { - return this.add(a, b, !0); +w.append = function(a, c) { + return this.add(a, c, !0); }; w.contain = function(a) { return this.db ? this.db.has(a) : this.reg.has(a); }; -w.update = function(a, b) { - const c = this, d = this.remove(a); - return d && d.then ? d.then(() => c.add(a, b)) : this.add(a, b); +w.update = function(a, c) { + const b = this, e = this.remove(a); + return e && e.then ? e.then(() => b.add(a, c)) : this.add(a, c); }; w.cleanup = function() { if (!this.fastupdate) { return console.info('Cleanup the index isn\'t required when not using "fastupdate".'), this; } - sb(this.map); - this.depth && sb(this.ctx); + tb(this.map); + this.depth && tb(this.ctx); return this; }; -w.searchCache = rb; -w.export = function(a, b, c = 0, d = 0) { - let e, f; - switch(d) { +w.searchCache = ma; +w.export = function(a, c, b = 0, e = 0) { + let d, f; + switch(e) { case 0: - e = "reg"; - f = Ha(this.reg); + d = "reg"; + f = pb(this.reg); break; case 1: - e = "cfg"; + d = "cfg"; f = null; break; case 2: - e = "map"; - f = Da(this.map, this.reg.size); + d = "map"; + f = lb(this.map, this.reg.size); break; case 3: - e = "ctx"; - f = Fa(this.ctx, this.reg.size); + d = "ctx"; + f = nb(this.ctx, this.reg.size); break; default: return; } - return Ja.call(this, a, b, e, f, c, d); + return rb.call(this, a, c, d, f, b, e); }; -w.import = function(a, b) { - if (b) { - switch("string" === typeof b && (b = JSON.parse(b)), a = a.split("."), "json" === a[a.length - 1] && a.pop(), 3 === a.length && a.shift(), a = 1 < a.length ? a[1] : a[0], a) { +w.import = function(a, c) { + if (c) { + switch("string" === typeof c && (c = JSON.parse(c)), a = a.split("."), "json" === a[a.length - 1] && a.pop(), 3 === a.length && a.shift(), a = 1 < a.length ? a[1] : a[0], a) { case "reg": this.fastupdate = !1; - this.reg = Ia(b, this.reg); + this.reg = qb(c, this.reg); break; case "map": - this.map = Ea(b, this.map); + this.map = mb(c, this.map); break; case "ctx": - this.ctx = Ga(b, this.ctx); + this.ctx = ob(c, this.ctx); } } }; w.serialize = function(a = !0) { - let b = "", c = "", d = ""; + let c = "", b = "", e = ""; if (this.reg.size) { let f; - for (var e of this.reg.keys()) { - f || (f = typeof e), b += (b ? "," : "") + ("string" === f ? '"' + e + '"' : e); + for (var d of this.reg.keys()) { + f || (f = typeof d), c += (c ? "," : "") + ("string" === f ? '"' + d + '"' : d); } - b = "index.reg=new Set([" + b + "]);"; - c = Ka(this.map, f); - c = "index.map=new Map([" + c + "]);"; + c = "index.reg=new Set([" + c + "]);"; + b = sb(this.map, f); + b = "index.map=new Map([" + b + "]);"; for (const g of this.ctx.entries()) { - e = g[0]; - let h = Ka(g[1], f); + d = g[0]; + let h = sb(g[1], f); h = "new Map([" + h + "])"; - h = '["' + e + '",' + h + "]"; - d += (d ? "," : "") + h; + h = '["' + d + '",' + h + "]"; + e += (e ? "," : "") + h; } - d = "index.ctx=new Map([" + d + "]);"; + e = "index.ctx=new Map([" + e + "]);"; } - return a ? "function inject(index){" + b + c + d + "}" : b + c + d; + return a ? "function inject(index){" + c + b + e + "}" : c + b + e; }; -sa(P.prototype); -const Bb = "undefined" !== typeof window && (window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB), Cb = ["map", "ctx", "tag", "reg", "cfg"], Db = G(); -function Eb(a, b = {}) { - if (!this || this.constructor !== Eb) { - return new Eb(a, b); +Fa(V.prototype); +const Cb = "undefined" !== typeof window && (window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB), Db = ["map", "ctx", "tag", "reg", "cfg"], Eb = I(); +function Fb(a, c = {}) { + if (!this || this.constructor !== Fb) { + return new Fb(a, c); } - "object" === typeof a && (b = a, a = a.name); + "object" === typeof a && (c = a, a = a.name); a || console.info("Default storage space was used, because a name was not passed."); this.id = "flexsearch" + (a ? ":" + a.toLowerCase().replace(/[^a-z0-9_\-]/g, "") : ""); - this.field = b.field ? b.field.toLowerCase().replace(/[^a-z0-9_\-]/g, "") : ""; - this.type = b.type; + this.field = c.field ? c.field.toLowerCase().replace(/[^a-z0-9_\-]/g, "") : ""; + this.type = c.type; this.fastupdate = this.support_tag_search = !1; this.db = null; this.h = {}; } -w = Eb.prototype; +w = Fb.prototype; w.mount = function(a) { if (a.index) { return a.mount(this); @@ -2510,20 +2600,20 @@ w.open = function() { } let a = this; navigator.storage && navigator.storage.persist(); - Db[a.id] || (Db[a.id] = []); - Db[a.id].push(a.field); - const b = Bb.open(a.id, 1); - b.onupgradeneeded = function() { - const c = a.db = this.result; - for (let d = 0, e; d < Cb.length; d++) { - e = Cb[d]; - for (let f = 0, g; f < Db[a.id].length; f++) { - g = Db[a.id][f], c.objectStoreNames.contains(e + ("reg" !== e ? g ? ":" + g : "" : "")) || c.createObjectStore(e + ("reg" !== e ? g ? ":" + g : "" : "")); + Eb[a.id] || (Eb[a.id] = []); + Eb[a.id].push(a.field); + const c = Cb.open(a.id, 1); + c.onupgradeneeded = function() { + const b = a.db = this.result; + for (let e = 0, d; e < Db.length; e++) { + d = Db[e]; + for (let f = 0, g; f < Eb[a.id].length; f++) { + g = Eb[a.id][f], b.objectStoreNames.contains(d + ("reg" !== d ? g ? ":" + g : "" : "")) || b.createObjectStore(d + ("reg" !== d ? g ? ":" + g : "" : "")); } } }; - return a.db = Z(b, function(c) { - a.db = c; + return a.db = Z(c, function(b) { + a.db = b; a.db.onversionchange = function() { a.close(); }; @@ -2534,47 +2624,47 @@ w.close = function() { this.db = null; }; w.destroy = function() { - const a = Bb.deleteDatabase(this.id); + const a = Cb.deleteDatabase(this.id); return Z(a); }; w.clear = function() { const a = []; - for (let c = 0, d; c < Cb.length; c++) { - d = Cb[c]; - for (let e = 0, f; e < Db[this.id].length; e++) { - f = Db[this.id][e], a.push(d + ("reg" !== d ? f ? ":" + f : "" : "")); + for (let b = 0, e; b < Db.length; b++) { + e = Db[b]; + for (let d = 0, f; d < Eb[this.id].length; d++) { + f = Eb[this.id][d], a.push(e + ("reg" !== e ? f ? ":" + f : "" : "")); } } - const b = this.db.transaction(a, "readwrite"); - for (let c = 0; c < a.length; c++) { - b.objectStore(a[c]).clear(); + const c = this.db.transaction(a, "readwrite"); + for (let b = 0; b < a.length; b++) { + c.objectStore(a[b]).clear(); } - return Z(b); + return Z(c); }; -w.get = function(a, b, c = 0, d = 0, e = !0, f = !1) { - a = this.db.transaction((b ? "ctx" : "map") + (this.field ? ":" + this.field : ""), "readonly").objectStore((b ? "ctx" : "map") + (this.field ? ":" + this.field : "")).get(b ? b + ":" + a : a); +w.get = function(a, c, b = 0, e = 0, d = !0, f = !1) { + a = this.db.transaction((c ? "ctx" : "map") + (this.field ? ":" + this.field : ""), "readonly").objectStore((c ? "ctx" : "map") + (this.field ? ":" + this.field : "")).get(c ? c + ":" + a : a); const g = this; return Z(a).then(function(h) { let k = []; if (!h || !h.length) { return k; } - if (e) { - if (!c && !d && 1 === h.length) { + if (d) { + if (!b && !e && 1 === h.length) { return h[0]; } - for (let l = 0, m; l < h.length; l++) { - if ((m = h[l]) && m.length) { - if (d >= m.length) { - d -= m.length; + for (let m = 0, l; m < h.length; m++) { + if ((l = h[m]) && l.length) { + if (e >= l.length) { + e -= l.length; continue; } - const p = c ? d + Math.min(m.length - d, c) : m.length; - for (let u = d; u < p; u++) { - k.push(m[u]); + const n = b ? e + Math.min(l.length - e, b) : l.length; + for (let u = e; u < n; u++) { + k.push(l[u]); } - d = 0; - if (k.length === c) { + e = 0; + if (k.length === b) { break; } } @@ -2584,90 +2674,90 @@ w.get = function(a, b, c = 0, d = 0, e = !0, f = !1) { return h; }); }; -w.tag = function(a, b = 0, c = 0, d = !1) { +w.tag = function(a, c = 0, b = 0, e = !1) { a = this.db.transaction("tag" + (this.field ? ":" + this.field : ""), "readonly").objectStore("tag" + (this.field ? ":" + this.field : "")).get(a); - const e = this; + const d = this; return Z(a).then(function(f) { - if (!f || !f.length || c >= f.length) { + if (!f || !f.length || b >= f.length) { return []; } - if (!b && !c) { + if (!c && !b) { return f; } - f = f.slice(c, c + b); - return d ? e.enrich(f) : f; + f = f.slice(b, b + c); + return e ? d.enrich(f) : f; }); }; w.enrich = function(a) { "object" !== typeof a && (a = [a]); - const b = this.db.transaction("reg", "readonly").objectStore("reg"), c = []; - for (let d = 0; d < a.length; d++) { - c[d] = Z(b.get(a[d])); + const c = this.db.transaction("reg", "readonly").objectStore("reg"), b = []; + for (let e = 0; e < a.length; e++) { + b[e] = Z(c.get(a[e])); } - return Promise.all(c).then(function(d) { - for (let e = 0; e < d.length; e++) { - d[e] = {id:a[e], doc:d[e] ? JSON.parse(d[e]) : null}; + return Promise.all(b).then(function(e) { + for (let d = 0; d < e.length; d++) { + e[d] = {id:a[d], doc:e[d] ? JSON.parse(e[d]) : null}; } - return d; + return e; }); }; w.has = function(a) { a = this.db.transaction("reg", "readonly").objectStore("reg").getKey(a); - return Z(a).then(function(b) { - return !!b; + return Z(a).then(function(c) { + return !!c; }); }; w.search = null; w.info = function() { }; -w.transaction = function(a, b, c) { +w.transaction = function(a, c, b) { a += "reg" !== a ? this.field ? ":" + this.field : "" : ""; - let d = this.h[a + ":" + b]; - if (d) { - return c.call(this, d); + let e = this.h[a + ":" + c]; + if (e) { + return b.call(this, e); } - let e = this.db.transaction(a, b); - this.h[a + ":" + b] = d = e.objectStore(a); - const f = c.call(this, d); - this.h[a + ":" + b] = null; - return Z(e).finally(function() { - e = d = null; + let d = this.db.transaction(a, c); + this.h[a + ":" + c] = e = d.objectStore(a); + const f = b.call(this, e); + this.h[a + ":" + c] = null; + return Z(d).finally(function() { + d = e = null; return f; }); }; -w.commit = async function(a, b, c) { - if (b) { +w.commit = async function(a, c, b) { + if (c) { await this.clear(), a.commit_task = []; } else { - let d = a.commit_task; + let e = a.commit_task; a.commit_task = []; - for (let e = 0, f; e < d.length; e++) { - if (f = d[e], f.clear) { + for (let d = 0, f; d < e.length; d++) { + if (f = e[d], f.clear) { await this.clear(); - b = !0; + c = !0; break; } else { - d[e] = f.del; + e[d] = f.del; } } - b || (c || (d = d.concat(ea(a.reg))), d.length && await this.remove(d)); + c || (b || (e = e.concat(ca(a.reg))), e.length && await this.remove(e)); } - a.reg.size && (await this.transaction("map", "readwrite", function(d) { - for (const e of a.map) { - const f = e[0], g = e[1]; - g.length && (b ? d.put(g, f) : d.get(f).onsuccess = function() { + a.reg.size && (await this.transaction("map", "readwrite", function(e) { + for (const d of a.map) { + const f = d[0], g = d[1]; + g.length && (c ? e.put(g, f) : e.get(f).onsuccess = function() { let h = this.result; var k; if (h && h.length) { - const l = Math.max(h.length, g.length); - for (let m = 0, p, u; m < l; m++) { - if ((u = g[m]) && u.length) { - if ((p = h[m]) && p.length) { + const m = Math.max(h.length, g.length); + for (let l = 0, n, u; l < m; l++) { + if ((u = g[l]) && u.length) { + if ((n = h[l]) && n.length) { for (k = 0; k < u.length; k++) { - p.push(u[k]); + n.push(u[k]); } } else { - h[m] = u; + h[l] = u; } k = 1; } @@ -2675,116 +2765,116 @@ w.commit = async function(a, b, c) { } else { h = g, k = 1; } - k && d.put(h, f); + k && e.put(h, f); }); } - }), await this.transaction("ctx", "readwrite", function(d) { - for (const e of a.ctx) { - const f = e[0], g = e[1]; + }), await this.transaction("ctx", "readwrite", function(e) { + for (const d of a.ctx) { + const f = d[0], g = d[1]; for (const h of g) { - const k = h[0], l = h[1]; - l.length && (b ? d.put(l, f + ":" + k) : d.get(f + ":" + k).onsuccess = function() { - let m = this.result; - var p; - if (m && m.length) { - const u = Math.max(m.length, l.length); - for (let q = 0, r, n; q < u; q++) { - if ((n = l[q]) && n.length) { - if ((r = m[q]) && r.length) { - for (p = 0; p < n.length; p++) { - r.push(n[p]); + const k = h[0], m = h[1]; + m.length && (c ? e.put(m, f + ":" + k) : e.get(f + ":" + k).onsuccess = function() { + let l = this.result; + var n; + if (l && l.length) { + const u = Math.max(l.length, m.length); + for (let p = 0, t, r; p < u; p++) { + if ((r = m[p]) && r.length) { + if ((t = l[p]) && t.length) { + for (n = 0; n < r.length; n++) { + t.push(r[n]); } } else { - m[q] = n; + l[p] = r; } - p = 1; + n = 1; } } } else { - m = l, p = 1; + l = m, n = 1; } - p && d.put(m, f + ":" + k); + n && e.put(l, f + ":" + k); }); } } - }), a.store ? await this.transaction("reg", "readwrite", function(d) { - for (const e of a.store) { - const f = e[0], g = e[1]; - d.put("object" === typeof g ? JSON.stringify(g) : 1, f); + }), a.store ? await this.transaction("reg", "readwrite", function(e) { + for (const d of a.store) { + const f = d[0], g = d[1]; + e.put("object" === typeof g ? JSON.stringify(g) : 1, f); } - }) : a.bypass || await this.transaction("reg", "readwrite", function(d) { - for (const e of a.reg.keys()) { - d.put(1, e); + }) : a.bypass || await this.transaction("reg", "readwrite", function(e) { + for (const d of a.reg.keys()) { + e.put(1, d); } - }), a.tag && await this.transaction("tag", "readwrite", function(d) { - for (const e of a.tag) { - const f = e[0], g = e[1]; - g.length && (d.get(f).onsuccess = function() { + }), a.tag && await this.transaction("tag", "readwrite", function(e) { + for (const d of a.tag) { + const f = d[0], g = d[1]; + g.length && (e.get(f).onsuccess = function() { let h = this.result; h = h && h.length ? h.concat(g) : g; - d.put(h, f); + e.put(h, f); }); } }), a.map.clear(), a.ctx.clear(), a.tag && a.tag.clear(), a.store && a.store.clear(), a.document || a.reg.clear()); }; -function Fb(a, b, c) { - const d = a.value; - let e, f = 0; - for (let g = 0, h; g < d.length; g++) { - if (h = c ? d : d[g]) { - for (let k = 0, l, m; k < b.length; k++) { - if (m = b[k], l = h.indexOf(m), 0 <= l) { - if (e = 1, 1 < h.length) { - h.splice(l, 1); +function Gb(a, c, b) { + const e = a.value; + let d, f = 0; + for (let g = 0, h; g < e.length; g++) { + if (h = b ? e : e[g]) { + for (let k = 0, m, l; k < c.length; k++) { + if (l = c[k], m = h.indexOf(l), 0 <= m) { + if (d = 1, 1 < h.length) { + h.splice(m, 1); } else { - d[g] = []; + e[g] = []; break; } } } f += h.length; } - if (c) { + if (b) { break; } } - f ? e && a.update(d) : a.delete(); + f ? d && a.update(e) : a.delete(); a.continue(); } w.remove = function(a) { "object" !== typeof a && (a = [a]); - return Promise.all([this.transaction("map", "readwrite", function(b) { - b.openCursor().onsuccess = function() { - const c = this.result; - c && Fb(c, a); + return Promise.all([this.transaction("map", "readwrite", function(c) { + c.openCursor().onsuccess = function() { + const b = this.result; + b && Gb(b, a); }; - }), this.transaction("ctx", "readwrite", function(b) { - b.openCursor().onsuccess = function() { - const c = this.result; - c && Fb(c, a); + }), this.transaction("ctx", "readwrite", function(c) { + c.openCursor().onsuccess = function() { + const b = this.result; + b && Gb(b, a); }; - }), this.transaction("tag", "readwrite", function(b) { - b.openCursor().onsuccess = function() { - const c = this.result; - c && Fb(c, a, !0); + }), this.transaction("tag", "readwrite", function(c) { + c.openCursor().onsuccess = function() { + const b = this.result; + b && Gb(b, a, !0); }; - }), this.transaction("reg", "readwrite", function(b) { - for (let c = 0; c < a.length; c++) { - b.delete(a[c]); + }), this.transaction("reg", "readwrite", function(c) { + for (let b = 0; b < a.length; b++) { + c.delete(a[b]); } })]); }; -function Z(a, b) { - return new Promise((c, d) => { +function Z(a, c) { + return new Promise((b, e) => { a.onsuccess = a.oncomplete = function() { - b && b(this.result); - b = null; - c(this.result); + c && c(this.result); + c = null; + b(this.result); }; - a.onerror = a.onblocked = d; + a.onerror = a.onblocked = e; a = null; }); } -;export default {Index:P, Charset:nb, Encoder:na, Document:Qa, Worker:Ba, Resolver:Y, IndexedDB:Eb, Language:{}}; +;export default {Index:V, Charset:wa, Encoder:la, Document:Na, Worker:La, Resolver:Y, IndexedDB:Fb, Language:{}}; -export const Index=P;export const Charset=nb;export const Encoder=na;export const Document=Qa;export const Worker=Ba;export const Resolver=Y;export const IndexedDB=Eb;export const Language={}; \ No newline at end of file +export const Index=V;export const Charset=wa;export const Encoder=la;export const Document=Na;export const Worker=La;export const Resolver=Y;export const IndexedDB=Fb;export const Language={}; \ No newline at end of file diff --git a/dist/flexsearch.bundle.module.min.js b/dist/flexsearch.bundle.module.min.js index 782f7d7..10a4fda 100644 --- a/dist/flexsearch.bundle.module.min.js +++ b/dist/flexsearch.bundle.module.min.js @@ -1,102 +1,106 @@ /**! - * FlexSearch.js v0.8.167 (Bundle/Module) + * FlexSearch.js v0.8.200 (Bundle/Module) * Author and Copyright: Thomas Wilkerling * Licence: Apache-2.0 * Hosted by Nextapps GmbH * https://github.com/nextapps-de/flexsearch */ -var v;function E(a,b,c){const d=typeof c,e=typeof a;if("undefined"!==d){if("undefined"!==e){if(c){if("function"===e&&d===e)return function(h){return a(c(h))};b=a.constructor;if(b===c.constructor){if(b===Array)return c.concat(a);if(b===Map){var f=new Map(c);for(var g of a)f.set(g[0],g[1]);return f}if(b===Set){g=new Set(c);for(f of a.values())g.add(f);return g}}}return a}return c}return"undefined"===e?b:a}function ca(a,b){return"undefined"===typeof a?b:a}function G(){return Object.create(null)} -function M(a){return"string"===typeof a}function da(a){return"object"===typeof a}function ea(a){const b=[];for(const c of a.keys())b.push(c);return b}function fa(a,b){if(M(b))a=a[b];else for(let c=0;a&&c"a1a".split(c).length; -this.numeric=E(a.numeric,d)}else{try{this.split=E(this.split,ia)}catch(e){this.split=/\s+/}this.numeric=E(a.numeric,E(this.numeric,!0))}this.prepare=E(a.prepare,null,this.prepare);this.finalize=E(a.finalize,null,this.finalize);c=a.filter;this.filter="function"===typeof c?c:E(c&&new Set(c),null,this.filter);this.dedupe=E(a.dedupe,!0,this.dedupe);this.matcher=E((c=a.matcher)&&new Map(c),null,this.matcher);this.mapper=E((c=a.mapper)&&new Map(c),null,this.mapper);this.stemmer=E((c=a.stemmer)&&new Map(c), -null,this.stemmer);this.replacer=E(a.replacer,null,this.replacer);this.minlength=E(a.minlength,1,this.minlength);this.maxlength=E(a.maxlength,1024,this.maxlength);this.rtl=E(a.rtl,!1,this.rtl);if(this.cache=c=E(a.cache,!0,this.cache))this.H=null,this.S="number"===typeof c?c:2E5,this.B=new Map,this.G=new Map,this.L=this.K=128;this.h="";this.M=null;this.A="";this.N=null;if(this.matcher)for(const e of this.matcher.keys())this.h+=(this.h?"|":"")+e;if(this.stemmer)for(const e of this.stemmer.keys())this.A+= -(this.A?"|":"")+e;return this};v.addStemmer=function(a,b){this.stemmer||(this.stemmer=new Map);this.stemmer.set(a,b);this.A+=(this.A?"|":"")+a;this.N=null;this.cache&&N(this);return this};v.addFilter=function(a){"function"===typeof a?this.filter=a:(this.filter||(this.filter=new Set),this.filter.add(a));this.cache&&N(this);return this}; -v.addMapper=function(a,b){if("object"===typeof a)return this.addReplacer(a,b);if(1a.length&&(this.dedupe||this.mapper))return this.addMapper(a,b);this.matcher||(this.matcher=new Map);this.matcher.set(a,b);this.h+=(this.h?"|":"")+a;this.M=null;this.cache&&N(this);return this}; -v.addReplacer=function(a,b){if("string"===typeof a)return this.addMatcher(a,b);this.replacer||(this.replacer=[]);this.replacer.push(a,b);this.cache&&N(this);return this}; -v.encode=function(a,b){if(this.cache&&a.length<=this.K)if(this.H){if(this.B.has(a))return this.B.get(a)}else this.H=setTimeout(N,50,this);this.normalize&&("function"===typeof this.normalize?a=this.normalize(a):a=ma?a.normalize("NFKD").replace(ma,"").toLowerCase():a.toLowerCase());this.prepare&&(a=this.prepare(a));this.numeric&&3this.maxlength)){if(b){if(e[m])continue;e[m]=1}else{if(f===m)continue;f=m}if(c)d.push(m);else if(!this.filter||("function"===typeof this.filter?this.filter(m):!this.filter.has(m))){if(this.cache&&m.length<=this.L)if(this.H){var k=this.G.get(m);if(k||""===k){k&&d.push(k);continue}}else this.H=setTimeout(N,50,this);if(this.stemmer){this.N||(this.N=new RegExp("(?!^)("+ -this.A+")$"));let u;for(;u!==m&&2this.stemmer.get(r))}if(m&&(this.mapper||this.dedupe&&1this.matcher.get(u)));if(m&&this.replacer)for(k=0;m&&kthis.S&&(this.G.clear(),this.L=this.L/1.1|0));if(m){if(m!==n)if(b){if(e[m])continue;e[m]=1}else{if(g===m)continue;g=m}d.push(m)}}}this.finalize&&(d=this.finalize(d)||d);this.cache&&a.length<=this.K&&(this.B.set(a,d),this.B.size>this.S&&(this.B.clear(),this.K=this.K/1.1|0));return d};function N(a){a.H=null;a.B.clear();a.G.clear()};let qa,ra;async function sa(a){a=a.data;var b=a.task;const c=a.id;let d=a.args;switch(b){case "init":ra=a.options||{};(b=a.factory)?(Function("return "+b)()(self),qa=new self.FlexSearch.Index(ra),delete self.FlexSearch):qa=new O(ra);postMessage({id:c});break;default:let e;"export"===b&&(d[1]?(d[0]=ra.export,d[2]=0,d[3]=1):d=null);"import"===b?d[0]&&(a=await ra.import.call(qa,d[0]),qa.import(d[0],a)):(e=d&&qa[b].apply(qa,d))&&e.then&&(e=await e);postMessage("search"===b?{id:c,msg:e}:{id:c})}};function ta(a){ua.call(a,"add");ua.call(a,"append");ua.call(a,"search");ua.call(a,"update");ua.call(a,"remove");ua.call(a,"searchCache")}let va,ya,za;function Aa(){va=za=0} -function ua(a){this[a+"Async"]=function(){const b=arguments;var c=b[b.length-1];let d;"function"===typeof c&&(d=c,delete b[b.length-1]);va?za||(za=Date.now()-ya>=this.priority*this.priority*3):(va=setTimeout(Aa,0),ya=Date.now());if(za){const f=this;return new Promise(g=>{setTimeout(function(){g(f[a+"Async"].apply(f,b))},0)})}const e=this[a].apply(this,b);c=e.then?e:new Promise(f=>f(e));d&&c.then(d);return c}};let Ba=0; -function Ca(a={},b){function c(h){function k(l){l=l.data||l;const m=l.id,n=m&&f.h[m];n&&(n(l.msg),delete f.h[m])}this.worker=h;this.h=G();if(this.worker){e?this.worker.on("message",k):this.worker.onmessage=k;if(a.config)return new Promise(function(l){f.h[++Ba]=function(){l(f);1E9=g.length)b-=g.length;else{b=g[d?"splice":"slice"](b,c);const h=b.length;if(h&&(e=e.length?e.concat(b):b,c-=h,d&&(a.length-=h),!c))break;b=0}return e} -function R(a){if(!this||this.constructor!==R)return new R(a);this.index=a?[a]:[];this.length=a?a.length:0;const b=this;return new Proxy([],{get(c,d){if("length"===d)return b.length;if("push"===d)return function(e){b.index[b.index.length-1].push(e);b.length++};if("pop"===d)return function(){if(b.length)return b.length--,b.index[b.index.length-1].pop()};if("indexOf"===d)return function(e){let f=0;for(let g=0,h,k;gb?b?a.slice(c,c+b):a.slice(c):a,d?X.call(this,a):a;let e=[];for(let f=0,g,h;f=h){c-=h;continue}cb&&(g=g.slice(0,b),h=b);if(!e.length&&h>=b)return d?X.call(this,g):g;e.push(g);b-=h;if(!b)break}e=1a.length?this.result=a[0]:(this.result=Wa(a,c,d,!1,this.h),d=0));return f?this.resolve(c,d,e):this};Y.prototype.and=function(){let a=this.result.length,b,c,d,e;if(!a){const f=arguments[0];f&&(a=!!f.suggest,e=f.resolve,b=f.limit,c=f.offset,d=f.enrich&&e)}if(a){const {O:f,P:g,limit:h,offset:k,enrich:l,resolve:m,suggest:n}=Ua(this,"and",arguments);return Xa.call(this,f,g,h,k,l,m,n)}return e?this.resolve(b,c,d):this}; -function Xa(a,b,c,d,e,f,g){if(b.length){const h=this;return Promise.all(b).then(function(k){a=[];for(let l=0,m;la.length)this.result=a[0];else{if(b=ha(a))return this.result=Ya(a,b,c,d,g,this.h,f),f?e?X.call(this.index,this.result):this.result:this;this.result=[]}else g||(this.result=a);return f?this.resolve(c,d,e):this};Y.prototype.xor=function(){const {O:a,P:b,limit:c,offset:d,enrich:e,resolve:f,suggest:g}=Ua(this,"xor",arguments);return Za.call(this,a,b,c,d,e,f,g)}; -function Za(a,b,c,d,e,f,g){if(b.length){const h=this;return Promise.all(b).then(function(k){a=[];for(let l=0,m;la.length)this.result=a[0];else return this.result=$a.call(this,a,c,d,f,this.h),f?e?X.call(this.index,this.result):this.result:this;else g||(this.result=a);return f?this.resolve(c,d,e):this} -function $a(a,b,c,d,e){const f=[],g=G();let h=0;for(let k=0,l;kD&&(D=t.length+(t?1:0)),H=t.length+(t?1:0)+F.length,B+=C,xa.push(w.length),w.push({match:F})),t+=(t?" ":"")+F)}if(!z)A=y[x],t+=(t?" ":"")+A,k&&w.push({text:A});else if(k&&B>=k)break}B=xa.length*(f.length-2);if(r||p||k&&t.length-B>k)if(B=k+B-2*u,x=H-D, -0y&&(y=0)),w=w.length-1){if(J>= -w.length){x[z+1]=1;J>=y.length&&(B[z+1]=1);continue}C-=u}t=w[J].text;if(K=p&&I[z])if(0K)if(x[z+1]=1,l)t=t.substring(0,K);else continue;(K-=t.length)||(K=-1);I[z]=K}else{x[z+1]=1;continue}if(C+t.length+1<=k)t=" "+t,H[z]+=t;else if(l)T=k-C-1,0=J){if(0>J){x[z]=1;B[z]=1;continue}C-=u}t=w[J].text;if(K=r&&A[z])if(0K)if(x[z]=1,l)t=t.substring(t.length- -K);else continue;(K-=t.length)||(K=-1);A[z]=K}else{x[z]=1;continue}if(C+t.length+1<=k)t+=" ",H[z]=t+H[z];else if(l)T=t.length+1-(k-C),0<=T&&T=y.length-1?Ma=1:Jc||d?k.slice(d,c+d):k;else{if(ac||d)k=k.slice(d,c+d)}else{e= -[];for(let n=0,u;nd)d-=u.length;else{if(c&&u.length>c||d)u=u.slice(d,c+d),c-=u.length,d&&(d-=u.length);e.push(u);if(!c)break}k=e}}return k} -function Wa(a,b,c,d,e){const f=[],g=G();let h;var k=a.length;let l;if(d)for(e=k-1;0<=e;e--){if(l=(d=a[e])&&d.length)for(k=0;kc||d)a=a.slice(d,d+c);e&&(a=X.call(this,a))}return a} -function X(a){if(!this||!this.store)return a;const b=Array(a.length);for(let c=0,d;cthis.limit&&this.cache.delete(this.cache.keys().next().value)}; -pb.prototype.get=function(a){const b=this.cache.get(a);b&&this.h!==a&&(this.cache.delete(a),this.cache.set(this.h=a,b));return b};pb.prototype.remove=function(a){for(const b of this.cache){const c=b[0];b[1].includes(a)&&this.cache.delete(c)}};pb.prototype.clear=function(){this.cache.clear();this.h=""};O.prototype.remove=function(a,b){const c=this.reg.size&&(this.fastupdate?this.reg.get(a):this.reg.has(a));if(c){if(this.fastupdate)for(let d=0,e,f;dq;f--){g=p.substring(q,f);t=this.rtl?e-1-q:q;var h=this.score?this.score(b,p,r,g,t):vb(u, -l,r,e,t);wb(this,n,g,h,a,c)}break}case "bidirectional":case "reverse":if(1g?0:1),l,r,h-1,k-1),t=this.bidirectional&&p>f;wb(this,m,t?f:p,q,a,c,t?p:f)}}}}this.fastupdate||this.reg.add(a)}else b=""}this.db&&(b||this.commit_task.push({del:a}),this.T&&tb(this));return this}; -function wb(a,b,c,d,e,f,g){let h=g?a.ctx:a.map,k;if(!b[c]||g&&!(k=b[c])[g])if(g?(b=k||(b[c]=G()),b[g]=1,(k=h.get(g))?h=k:h.set(g,h=new Map)):b[c]=1,(k=h.get(c))?h=k:h.set(c,h=k=[]),h=h[d]||(h[d]=[]),!f||!h.includes(e)){if(h.length===2**31-1){b=new R(h);if(a.fastupdate)for(let l of a.reg.values())l.includes(h)&&(l[l.indexOf(h)]=b);k[d]=h=b}h.push(e);a.fastupdate&&((d=a.reg.get(e))?d.push(h):a.reg.set(e,[h]))}} -function vb(a,b,c,d,e){return c&&1c)&&(k=c,c=b,b=k);if(a.db)return a.db.get(b,c,d,e,f,g,h);a=c?(a=a.ctx.get(c))&&a.get(b):a.map.get(b);return a};function O(a,b){if(!this||this.constructor!==O)return new O(a);if(a){var c=M(a)?a:a.preset;c&&(a=Object.assign({},ub[c],a))}else a={};c=a.context;const d=!0===c?{depth:1}:c||{},e=M(a.encoder)?nb[a.encoder]:a.encode||a.encoder||{};this.encoder=e.encode?e:"object"===typeof e?new na(e):{encode:e};this.resolution=a.resolution||9;this.tokenize=c=(c=a.tokenize)&&"default"!==c&&"exact"!==c&&c||"strict";this.depth="strict"===c&&d.depth||0;this.bidirectional=!1!==d.bidirectional;this.fastupdate=!!a.fastupdate; -this.score=a.score||null;(c=a.keystore||0)&&(this.keystore=c);this.map=c?new U(c):new Map;this.ctx=c?new U(c):new Map;this.reg=b||(this.fastupdate?c?new U(c):new Map:c?new V(c):new Set);this.U=d.resolution||3;this.rtl=e.rtl||a.rtl||!1;this.cache=(c=a.cache||null)&&new pb(c);this.resolve=!1!==a.resolve;if(c=a.db)this.db=this.mount(c);this.T=!1!==a.commit;this.commit_task=[];this.commit_timer=null;this.priority=a.priority||4}v=O.prototype; -v.mount=function(a){this.commit_timer&&(clearTimeout(this.commit_timer),this.commit_timer=null);return a.mount(this)};v.commit=function(a,b){this.commit_timer&&(clearTimeout(this.commit_timer),this.commit_timer=null);return this.db.commit(this,a,b)};v.destroy=function(){this.commit_timer&&(clearTimeout(this.commit_timer),this.commit_timer=null);return this.db.destroy()};function tb(a){a.commit_timer||(a.commit_timer=setTimeout(function(){a.commit_timer=null;a.db.commit(a,void 0,void 0)},1))} -v.clear=function(){this.map.clear();this.ctx.clear();this.reg.clear();this.cache&&this.cache.clear();this.db&&(this.commit_timer&&clearTimeout(this.commit_timer),this.commit_timer=null,this.commit_task=[{clear:!0}]);return this};v.append=function(a,b){return this.add(a,b,!0)};v.contain=function(a){return this.db?this.db.has(a):this.reg.has(a)};v.update=function(a,b){const c=this,d=this.remove(a);return d&&d.then?d.then(()=>c.add(a,b)):this.add(a,b)}; -v.cleanup=function(){if(!this.fastupdate)return this;sb(this.map);this.depth&&sb(this.ctx);return this};v.searchCache=rb;v.export=function(a,b,c=0,d=0){let e,f;switch(d){case 0:e="reg";f=Ia(this.reg);break;case 1:e="cfg";f=null;break;case 2:e="map";f=Ea(this.map,this.reg.size);break;case 3:e="ctx";f=Ga(this.ctx,this.reg.size);break;default:return}return Ka.call(this,a,b,e,f,c,d)}; -v.import=function(a,b){if(b)switch("string"===typeof b&&(b=JSON.parse(b)),a=a.split("."),"json"===a[a.length-1]&&a.pop(),3===a.length&&a.shift(),a=1=m.length){d-=m.length;continue}const n=c?d+Math.min(m.length-d,c):m.length;for(let u=d;u=f.length)return[];if(!b&&!c)return f;f=f.slice(c,c+b);return d?e.enrich(f):f})}; -v.enrich=function(a){"object"!==typeof a&&(a=[a]);const b=this.db.transaction("reg","readonly").objectStore("reg"),c=[];for(let d=0;d{a.onsuccess=a.oncomplete=function(){b&&b(this.result);b=null;c(this.result)};a.onerror=a.onblocked=d;a=null})};export default {Index:O,Charset:nb,Encoder:na,Document:W,Worker:Ca,Resolver:Y,IndexedDB:Eb,Language:{}}; -export const Index=O;export const Charset=nb;export const Encoder=na;export const Document=W;export const Worker=Ca;export const Resolver=Y;export const IndexedDB=Eb;export const Language={}; \ No newline at end of file +var w;function H(a,c,b){const e=typeof b,d=typeof a;if("undefined"!==e){if("undefined"!==d){if(b){if("function"===d&&e===d)return function(h){return a(b(h))};c=a.constructor;if(c===b.constructor){if(c===Array)return b.concat(a);if(c===Map){var f=new Map(b);for(var g of a)f.set(g[0],g[1]);return f}if(c===Set){g=new Set(b);for(f of a.values())g.add(f);return g}}}return a}return b}return"undefined"===d?c:a}function aa(a,c){return"undefined"===typeof a?c:a}function I(){return Object.create(null)} +function M(a){return"string"===typeof a}function ba(a){return"object"===typeof a}function ca(a){const c=[];for(const b of a.keys())c.push(b);return c}function ea(a,c){if(M(c))a=a[c];else for(let b=0;a&&b"a1a".split(b).length; +this.numeric=H(a.numeric,e)}else{try{this.split=H(this.split,fa)}catch(d){this.split=/\s+/}this.numeric=H(a.numeric,H(this.numeric,!0))}this.prepare=H(a.prepare,null,this.prepare);this.finalize=H(a.finalize,null,this.finalize);b=a.filter;this.filter="function"===typeof b?b:H(b&&new Set(b),null,this.filter);this.dedupe=H(a.dedupe,!0,this.dedupe);this.matcher=H((b=a.matcher)&&new Map(b),null,this.matcher);this.mapper=H((b=a.mapper)&&new Map(b),null,this.mapper);this.stemmer=H((b=a.stemmer)&&new Map(b), +null,this.stemmer);this.replacer=H(a.replacer,null,this.replacer);this.minlength=H(a.minlength,1,this.minlength);this.maxlength=H(a.maxlength,1024,this.maxlength);this.rtl=H(a.rtl,!1,this.rtl);if(this.cache=b=H(a.cache,!0,this.cache))this.I=null,this.R="number"===typeof b?b:2E5,this.B=new Map,this.H=new Map,this.M=this.L=128;this.h="";this.N=null;this.A="";this.O=null;if(this.matcher)for(const d of this.matcher.keys())this.h+=(this.h?"|":"")+d;if(this.stemmer)for(const d of this.stemmer.keys())this.A+= +(this.A?"|":"")+d;return this};w.addStemmer=function(a,c){this.stemmer||(this.stemmer=new Map);this.stemmer.set(a,c);this.A+=(this.A?"|":"")+a;this.O=null;this.cache&&Q(this);return this};w.addFilter=function(a){"function"===typeof a?this.filter=a:(this.filter||(this.filter=new Set),this.filter.add(a));this.cache&&Q(this);return this}; +w.addMapper=function(a,c){if("object"===typeof a)return this.addReplacer(a,c);if(1a.length&&(this.dedupe||this.mapper))return this.addMapper(a,c);this.matcher||(this.matcher=new Map);this.matcher.set(a,c);this.h+=(this.h?"|":"")+a;this.N=null;this.cache&&Q(this);return this}; +w.addReplacer=function(a,c){if("string"===typeof a)return this.addMatcher(a,c);this.replacer||(this.replacer=[]);this.replacer.push(a,c);this.cache&&Q(this);return this}; +w.encode=function(a,c){if(this.cache&&a.length<=this.L)if(this.I){if(this.B.has(a))return this.B.get(a)}else this.I=setTimeout(Q,50,this);this.normalize&&("function"===typeof this.normalize?a=this.normalize(a):a=ka?a.normalize("NFKD").replace(ka,"").toLowerCase():a.toLowerCase());this.prepare&&(a=this.prepare(a));this.numeric&&3this.maxlength)){if(c){if(d[l])continue;d[l]=1}else{if(f===l)continue;f=l}if(b)e.push(l);else if(!this.filter||("function"===typeof this.filter?this.filter(l):!this.filter.has(l))){if(this.cache&&l.length<=this.M)if(this.I){var k=this.H.get(l);if(k||""===k){k&&e.push(k);continue}}else this.I=setTimeout(Q,50,this);if(this.stemmer){this.O||(this.O=new RegExp("(?!^)("+ +this.A+")$"));let u;for(;u!==l&&2this.stemmer.get(p))}if(l&&(this.mapper||this.dedupe&&1this.matcher.get(u)));if(l&&this.replacer)for(k=0;l&&kthis.R&&(this.H.clear(),this.M=this.M/1.1|0));if(l){if(l!==n)if(c){if(d[l])continue;d[l]=1}else{if(g===l)continue;g=l}e.push(l)}}}this.finalize&&(e=this.finalize(e)||e);this.cache&&a.length<=this.L&&(this.B.set(a,e),this.B.size>this.R&&(this.B.clear(),this.L=this.L/1.1|0));return e};function Q(a){a.I=null;a.B.clear();a.H.clear()};function ma(a,c,b){b||(c||"object"!==typeof a?"object"===typeof c&&(b=c,c=0):b=a);b&&(a=b.query||a,c=b.limit||c);let e=""+(c||0);b&&(e+=(b.offset||0)+!!b.context+!!b.suggest+(!1!==b.resolve)+(b.resolution||this.resolution)+(b.boost||0));a=(""+a).toLowerCase();this.cache||(this.cache=new R);let d=this.cache.get(a+e);if(!d){const f=b&&b.cache;f&&(b.cache=!1);d=this.search(a,c,b);f&&(b.cache=f);this.cache.set(a+e,d)}return d}function R(a){this.limit=a&&!0!==a?a:1E3;this.cache=new Map;this.h=""} +R.prototype.set=function(a,c){this.cache.set(this.h=a,c);this.cache.size>this.limit&&this.cache.delete(this.cache.keys().next().value)};R.prototype.get=function(a){const c=this.cache.get(a);c&&this.h!==a&&(this.cache.delete(a),this.cache.set(this.h=a,c));return c};R.prototype.remove=function(a){for(const c of this.cache){const b=c[0];c[1].includes(a)&&this.cache.delete(b)}};R.prototype.clear=function(){this.cache.clear();this.h=""};const na={normalize:!1,numeric:!1,dedupe:!1};const qa={};const ra=new Map([["b","p"],["v","f"],["w","f"],["z","s"],["x","s"],["d","t"],["n","m"],["c","k"],["g","k"],["j","k"],["q","k"],["i","e"],["y","e"],["u","o"]]);const sa=new Map([["ae","a"],["oe","o"],["sh","s"],["kh","k"],["th","t"],["ph","f"],["pf","f"]]),ta=[/([^aeo])h(.)/g,"$1$2",/([aeo])h([^aeo]|$)/g,"$1$2",/(.)\1+/g,"$1"];const ua={a:"",e:"",i:"",o:"",u:"",y:"",b:1,f:1,p:1,v:1,c:2,g:2,j:2,k:2,q:2,s:2,x:2,z:2,"\u00df":2,d:3,t:3,l:4,m:5,n:5,r:6};var va={Exact:na,Default:qa,Normalize:qa,LatinBalance:{mapper:ra},LatinAdvanced:{mapper:ra,matcher:sa,replacer:ta},LatinExtra:{mapper:ra,replacer:ta.concat([/(?!^)[aeo]/g,""]),matcher:sa},LatinSoundex:{dedupe:!1,include:{letter:!0},finalize:function(a){for(let b=0;b=g.length)c-=g.length;else{c=g[e?"splice":"slice"](c,b);const h=c.length;if(h&&(d=d.length?d.concat(c):c,b-=h,e&&(a.length-=h),!b))break;c=0}return d} +function za(a){if(!this||this.constructor!==za)return new za(a);this.index=a?[a]:[];this.length=a?a.length:0;const c=this;return new Proxy([],{get(b,e){if("length"===e)return c.length;if("push"===e)return function(d){c.index[c.index.length-1].push(d);c.length++};if("pop"===e)return function(){if(c.length)return c.length--,c.index[c.index.length-1].pop()};if("indexOf"===e)return function(d){let f=0;for(let g=0,h,k;g=this.priority*this.priority*3):(Ha=setTimeout(Ka,0),Ia=Date.now());if(Ja){const f=this;return new Promise(g=>{setTimeout(function(){g(f[a+"Async"].apply(f,c))},0)})}const d=this[a].apply(this,c);b=d.then?d:new Promise(f=>f(d));e&&b.then(e);return b}};let W=0; +function La(a={},c){function b(h){function k(m){m=m.data||m;const l=m.id,n=l&&f.h[l];n&&(n(m.msg),delete f.h[l])}this.worker=h;this.h=I();if(this.worker){d?this.worker.on("message",k):this.worker.onmessage=k;if(a.config)return new Promise(function(m){1E9c?a.slice(b,b+c):a,e?Ta.call(this,a):a;let d=[];for(let f=0,g,h;f=h){b-=h;continue}g=g.slice(b,b+c);h=g.length;b=0}h>c&&(g=g.slice(0,c),h=c);if(!d.length&&h>=c)return e?Ta.call(this,g):g;d.push(g);c-=h;if(!c)break}d=1a.length?this.result=a[0]:(this.result=Ya(a,c,b,!1,this.h),b=0));d&&(this.await=null);return d?this.resolve(c,b,e):this};Y.prototype.and=function(){return Ua(this,"and",Za,arguments)};function Za(a,c,b,e,d,f){if(!f&&!this.result.length)return d?this.result:this;let g;if(a.length)if(this.result.length&&a.unshift(this.result),2>a.length)this.result=a[0];else{let h=0;for(let k=0,m,l;ka.length)this.result=a[0];else{a:{f=b;var g=this.h;const h=[],k=I();let m=0;for(let l=0,n;lb||e?k.slice(e,b+e):k;else{if(ab||e)k=k.slice(e,b+ +e)}else{d=[];for(let n=0,u;ne)e-=u.length;else{if(b&&u.length>b||e)u=u.slice(e,b+e),b-=u.length,e&&(e-=u.length);d.push(u);if(!b)break}k=d}}return k} +function Ya(a,c,b,e,d){const f=[],g=I();let h;var k=a.length;let m;if(e)for(d=k-1;0<=d;d--){if(m=(e=a[d])&&e.length)for(k=0;kA&&(A=q.length+(q?1:0)),B=q.length+(q?1:0)+G.length,E+=D,ya.push(v.length),v.push({match:G})),q+=(q?" ":"")+G)}if(!x)F=y[C],q+=(q?" ":"")+F,k&&v.push({text:F});else if(k&&E>=k)break}E=ya.length*(f.length-2);if(p||t||k&&q.length-E>k)if(E=k+E-2*u,C=B-A,0y&&(y=0)),v=v.length-1){if(J>= +v.length){C[x+1]=1;J>=y.length&&(E[x+1]=1);continue}D-=u}q=v[J].text;if(L=t&&z[x])if(0L)if(C[x+1]=1,m)q=q.substring(0,L);else continue;(L-=q.length)||(L=-1);z[x]=L}else{C[x+1]=1;continue}if(D+q.length+1<=k)q=" "+q,B[x]+=q;else if(m)U=k-D-1,0=J){if(0>J){C[x]=1;E[x]=1;continue}D-=u}q=v[J].text;if(L=p&&F[x])if(0L)if(C[x]=1,m)q=q.substring(q.length- +L);else continue;(L-=q.length)||(L=-1);F[x]=L}else{C[x]=1;continue}if(D+q.length+1<=k)q+=" ",B[x]=q+B[x];else if(m)U=q.length+1-(k-D),0<=U&&U=y.length-1?Pa=1:Jb||e)a=a.slice(e,e+b);d&&(a=Ta.call(this,a))}return a} +function Ta(a){if(!this||!this.store)return a;if(this.db)return this.index.get(this.field[0]).db.enrich(a);const c=Array(a.length);for(let b=0,e;br;f--){g=t.substring(r,f);q=this.rtl?d-1-r:r;var h=this.score?this.score(c,t,p,g,q):wb(u, +m,p,d,q);xb(this,n,g,h,a,b)}break}case "bidirectional":case "reverse":if(1g?0:1),m,p,h-1,k-1),q=this.bidirectional&&t>f;xb(this,l,q?f:t,r,a,b,q?t:f)}}}}this.fastupdate||this.reg.add(a)}else c=""}this.db&&(c||this.commit_task.push({del:a}),this.S&&ub(this));return this}; +function xb(a,c,b,e,d,f,g){let h=g?a.ctx:a.map,k;if(!c[b]||g&&!(k=c[b])[g])if(g?(c=k||(c[b]=I()),c[g]=1,(k=h.get(g))?h=k:h.set(g,h=new Map)):c[b]=1,(k=h.get(b))?h=k:h.set(b,h=k=[]),h=h[e]||(h[e]=[]),!f||!h.includes(d)){if(h.length===2**31-1){c=new za(h);if(a.fastupdate)for(let m of a.reg.values())m.includes(h)&&(m[m.indexOf(h)]=c);k[e]=h=c}h.push(d);a.fastupdate&&((e=a.reg.get(d))?e.push(h):a.reg.set(d,[h]))}} +function wb(a,c,b,e,d){return b&&1b)&&(k=b,b=c,c=k);if(a.db)return a.db.get(c,b,e,d,f,g,h);a=b?(a=a.ctx.get(b))&&a.get(c):a.map.get(c);return a};function V(a,c){if(!this||this.constructor!==V)return new V(a);if(a){var b=M(a)?a:a.preset;b&&(a=Object.assign({},vb[b],a))}else a={};b=a.context;const e=!0===b?{depth:1}:b||{},d=M(a.encoder)?va[a.encoder]:a.encode||a.encoder||{};this.encoder=d.encode?d:"object"===typeof d?new la(d):{encode:d};this.resolution=a.resolution||9;this.tokenize=b=(b=a.tokenize)&&"default"!==b&&"exact"!==b&&b||"strict";this.depth="strict"===b&&e.depth||0;this.bidirectional=!1!==e.bidirectional;this.fastupdate=!!a.fastupdate; +this.score=a.score||null;(b=a.keystore||0)&&(this.keystore=b);this.map=b?new S(b):new Map;this.ctx=b?new S(b):new Map;this.reg=c||(this.fastupdate?b?new S(b):new Map:b?new T(b):new Set);this.T=e.resolution||3;this.rtl=d.rtl||a.rtl||!1;this.cache=(b=a.cache||null)&&new R(b);this.resolve=!1!==a.resolve;if(b=a.db)this.db=this.mount(b);this.S=!1!==a.commit;this.commit_task=[];this.commit_timer=null;this.priority=a.priority||4}w=V.prototype; +w.mount=function(a){this.commit_timer&&(clearTimeout(this.commit_timer),this.commit_timer=null);return a.mount(this)};w.commit=function(a,c){this.commit_timer&&(clearTimeout(this.commit_timer),this.commit_timer=null);return this.db.commit(this,a,c)};w.destroy=function(){this.commit_timer&&(clearTimeout(this.commit_timer),this.commit_timer=null);return this.db.destroy()};function ub(a){a.commit_timer||(a.commit_timer=setTimeout(function(){a.commit_timer=null;a.db.commit(a,void 0,void 0)},1))} +w.clear=function(){this.map.clear();this.ctx.clear();this.reg.clear();this.cache&&this.cache.clear();this.db&&(this.commit_timer&&clearTimeout(this.commit_timer),this.commit_timer=null,this.commit_task=[{clear:!0}]);return this};w.append=function(a,c){return this.add(a,c,!0)};w.contain=function(a){return this.db?this.db.has(a):this.reg.has(a)};w.update=function(a,c){const b=this,e=this.remove(a);return e&&e.then?e.then(()=>b.add(a,c)):this.add(a,c)}; +w.cleanup=function(){if(!this.fastupdate)return this;tb(this.map);this.depth&&tb(this.ctx);return this};w.searchCache=ma;w.export=function(a,c,b=0,e=0){let d,f;switch(e){case 0:d="reg";f=pb(this.reg);break;case 1:d="cfg";f=null;break;case 2:d="map";f=lb(this.map,this.reg.size);break;case 3:d="ctx";f=nb(this.ctx,this.reg.size);break;default:return}return rb.call(this,a,c,d,f,b,e)}; +w.import=function(a,c){if(c)switch("string"===typeof c&&(c=JSON.parse(c)),a=a.split("."),"json"===a[a.length-1]&&a.pop(),3===a.length&&a.shift(),a=1=l.length){e-=l.length;continue}const n=b?e+Math.min(l.length-e,b):l.length;for(let u=e;u=f.length)return[];if(!c&&!b)return f;f=f.slice(b,b+c);return e?d.enrich(f):f})}; +w.enrich=function(a){"object"!==typeof a&&(a=[a]);const c=this.db.transaction("reg","readonly").objectStore("reg"),b=[];for(let e=0;e{a.onsuccess=a.oncomplete=function(){c&&c(this.result);c=null;b(this.result)};a.onerror=a.onblocked=e;a=null})};export default {Index:V,Charset:va,Encoder:la,Document:Na,Worker:La,Resolver:Y,IndexedDB:Fb,Language:{}}; +export const Index=V;export const Charset=va;export const Encoder=la;export const Document=Na;export const Worker=La;export const Resolver=Y;export const IndexedDB=Fb;export const Language={}; \ No newline at end of file diff --git a/dist/flexsearch.compact.debug.js b/dist/flexsearch.compact.debug.js index 4b617d2..dca2712 100644 --- a/dist/flexsearch.compact.debug.js +++ b/dist/flexsearch.compact.debug.js @@ -1,20 +1,20 @@ /**! - * FlexSearch.js v0.8.167 (Bundle/Debug) + * FlexSearch.js v0.8.200 (Bundle/Debug) * Author and Copyright: Thomas Wilkerling * Licence: Apache-2.0 * Hosted by Nextapps GmbH * https://github.com/nextapps-de/flexsearch */ (function(self){'use strict'; -var B; +var A; function I(a, c, b) { const e = typeof b, d = typeof a; if ("undefined" !== e) { if ("undefined" !== d) { if (b) { if ("function" === d && e === d) { - return function(k) { - return a(b(k)); + return function(h) { + return a(b(h)); }; } c = a.constructor; @@ -47,7 +47,7 @@ function I(a, c, b) { function J(a, c) { return "undefined" === typeof a ? c : a; } -function L() { +function M() { return Object.create(null); } function P(a) { @@ -79,8 +79,8 @@ function ka(a = {}) { this.assign(a); } } -B = ka.prototype; -B.assign = function(a) { +A = ka.prototype; +A.assign = function(a) { this.normalize = I(a.normalize, !0, this.normalize); let c = a.include, b = c || a.exclude || a.split, e; if (b || "" === b) { @@ -144,20 +144,20 @@ B.assign = function(a) { } return this; }; -B.addStemmer = function(a, c) { +A.addStemmer = function(a, c) { this.stemmer || (this.stemmer = new Map()); this.stemmer.set(a, c); this.C += (this.C ? "|" : "") + a; this.N = null; - this.cache && V(this); + this.cache && T(this); return this; }; -B.addFilter = function(a) { +A.addFilter = function(a) { "function" === typeof a ? this.filter = a : (this.filter || (this.filter = new Set()), this.filter.add(a)); - this.cache && V(this); + this.cache && T(this); return this; }; -B.addMapper = function(a, c) { +A.addMapper = function(a, c) { if ("object" === typeof a) { return this.addReplacer(a, c); } @@ -166,10 +166,10 @@ B.addMapper = function(a, c) { } this.mapper || (this.mapper = new Map()); this.mapper.set(a, c); - this.cache && V(this); + this.cache && T(this); return this; }; -B.addMatcher = function(a, c) { +A.addMatcher = function(a, c) { if ("object" === typeof a) { return this.addReplacer(a, c); } @@ -180,97 +180,97 @@ B.addMatcher = function(a, c) { this.matcher.set(a, c); this.h += (this.h ? "|" : "") + a; this.M = null; - this.cache && V(this); + this.cache && T(this); return this; }; -B.addReplacer = function(a, c) { +A.addReplacer = function(a, c) { if ("string" === typeof a) { return this.addMatcher(a, c); } this.replacer || (this.replacer = []); this.replacer.push(a, c); - this.cache && V(this); + this.cache && T(this); return this; }; -B.encode = function(a, c) { +A.encode = function(a, c) { if (this.cache && a.length <= this.K) { if (this.H) { if (this.F.has(a)) { return this.F.get(a); } } else { - this.H = setTimeout(V, 50, this); + this.H = setTimeout(T, 50, this); } } this.normalize && ("function" === typeof this.normalize ? a = this.normalize(a) : a = ha ? a.normalize("NFKD").replace(ha, "").toLowerCase() : a.toLowerCase()); this.prepare && (a = this.prepare(a)); this.numeric && 3 < a.length && (a = a.replace(ea, "$1 $2").replace(fa, "$1 $2").replace(da, "$1 ")); const b = !(this.dedupe || this.mapper || this.filter || this.matcher || this.stemmer || this.replacer); - let e = [], d = L(), f, g, k = this.split || "" === this.split ? a.split(this.split) : [a]; - for (let m = 0, n, z; m < k.length; m++) { - if ((n = z = k[m]) && !(n.length < this.minlength || n.length > this.maxlength)) { + let e = [], d = M(), f, g, h = this.split || "" === this.split ? a.split(this.split) : [a]; + for (let n = 0, l, D; n < h.length; n++) { + if ((l = D = h[n]) && !(l.length < this.minlength || l.length > this.maxlength)) { if (c) { - if (d[n]) { + if (d[l]) { continue; } - d[n] = 1; + d[l] = 1; } else { - if (f === n) { + if (f === l) { continue; } - f = n; + f = l; } if (b) { - e.push(n); + e.push(l); } else { - if (!this.filter || ("function" === typeof this.filter ? this.filter(n) : !this.filter.has(n))) { - if (this.cache && n.length <= this.L) { + if (!this.filter || ("function" === typeof this.filter ? this.filter(l) : !this.filter.has(l))) { + if (this.cache && l.length <= this.L) { if (this.H) { - var h = this.G.get(n); - if (h || "" === h) { - h && e.push(h); + var k = this.G.get(l); + if (k || "" === k) { + k && e.push(k); continue; } } else { - this.H = setTimeout(V, 50, this); + this.H = setTimeout(T, 50, this); } } if (this.stemmer) { this.N || (this.N = new RegExp("(?!^)(" + this.C + ")$")); - let x; - for (; x !== n && 2 < n.length;) { - x = n, n = n.replace(this.N, q => this.stemmer.get(q)); + let w; + for (; w !== l && 2 < l.length;) { + w = l, l = l.replace(this.N, t => this.stemmer.get(t)); } } - if (n && (this.mapper || this.dedupe && 1 < n.length)) { - h = ""; - for (let x = 0, q = "", r, w; x < n.length; x++) { - r = n.charAt(x), r === q && this.dedupe || ((w = this.mapper && this.mapper.get(r)) || "" === w ? w === q && this.dedupe || !(q = w) || (h += w) : h += q = r); + if (l && (this.mapper || this.dedupe && 1 < l.length)) { + k = ""; + for (let w = 0, t = "", p, x; w < l.length; w++) { + p = l.charAt(w), p === t && this.dedupe || ((x = this.mapper && this.mapper.get(p)) || "" === x ? x === t && this.dedupe || !(t = x) || (k += x) : k += t = p); } - n = h; + l = k; } - this.matcher && 1 < n.length && (this.M || (this.M = new RegExp("(" + this.h + ")", "g")), n = n.replace(this.M, x => this.matcher.get(x))); - if (n && this.replacer) { - for (h = 0; n && h < this.replacer.length; h += 2) { - n = n.replace(this.replacer[h], this.replacer[h + 1]); + this.matcher && 1 < l.length && (this.M || (this.M = new RegExp("(" + this.h + ")", "g")), l = l.replace(this.M, w => this.matcher.get(w))); + if (l && this.replacer) { + for (k = 0; l && k < this.replacer.length; k += 2) { + l = l.replace(this.replacer[k], this.replacer[k + 1]); } } - this.cache && z.length <= this.L && (this.G.set(z, n), this.G.size > this.O && (this.G.clear(), this.L = this.L / 1.1 | 0)); - if (n) { - if (n !== z) { + this.cache && D.length <= this.L && (this.G.set(D, l), this.G.size > this.O && (this.G.clear(), this.L = this.L / 1.1 | 0)); + if (l) { + if (l !== D) { if (c) { - if (d[n]) { + if (d[l]) { continue; } - d[n] = 1; + d[l] = 1; } else { - if (g === n) { + if (g === l) { continue; } - g = n; + g = l; } } - e.push(n); + e.push(l); } } } @@ -280,31 +280,86 @@ B.encode = function(a, c) { this.cache && a.length <= this.K && (this.F.set(a, e), this.F.size > this.O && (this.F.clear(), this.K = this.K / 1.1 | 0)); return e; }; -function V(a) { +function T(a) { a.H = null; a.F.clear(); a.G.clear(); } -;function la(a) { - W.call(a, "add"); - W.call(a, "append"); - W.call(a, "search"); - W.call(a, "update"); - W.call(a, "remove"); - W.call(a, "searchCache"); -} -let ma, na, oa; -function pa() { - ma = oa = 0; +;function la(a, c, b) { + b || (c || "object" !== typeof a ? "object" === typeof c && (b = c, c = 0) : b = a); + b && (a = b.query || a, c = b.limit || c); + let e = "" + (c || 0); + b && (e += (b.offset || 0) + !!b.context + !!b.suggest + (!1 !== b.resolve) + (b.resolution || this.resolution) + (b.boost || 0)); + a = ("" + a).toLowerCase(); + this.cache || (this.cache = new W()); + let d = this.cache.get(a + e); + if (!d) { + const f = b && b.cache; + f && (b.cache = !1); + d = this.search(a, c, b); + f && (b.cache = f); + this.cache.set(a + e, d); + } + return d; } function W(a) { + this.limit = a && !0 !== a ? a : 1000; + this.cache = new Map(); + this.h = ""; +} +W.prototype.set = function(a, c) { + this.cache.set(this.h = a, c); + this.cache.size > this.limit && this.cache.delete(this.cache.keys().next().value); +}; +W.prototype.get = function(a) { + const c = this.cache.get(a); + c && this.h !== a && (this.cache.delete(a), this.cache.set(this.h = a, c)); + return c; +}; +W.prototype.remove = function(a) { + for (const c of this.cache) { + const b = c[0]; + c[1].includes(a) && this.cache.delete(b); + } +}; +W.prototype.clear = function() { + this.cache.clear(); + this.h = ""; +}; +const ma = {normalize:!1, numeric:!1, dedupe:!1}; +const na = {}; +const oa = new Map([["b", "p"], ["v", "f"], ["w", "f"], ["z", "s"], ["x", "s"], ["d", "t"], ["n", "m"], ["c", "k"], ["g", "k"], ["j", "k"], ["q", "k"], ["i", "e"], ["y", "e"], ["u", "o"]]); +const pa = new Map([["ae", "a"], ["oe", "o"], ["sh", "s"], ["kh", "k"], ["th", "t"], ["ph", "f"], ["pf", "f"]]), qa = [/([^aeo])h(.)/g, "$1$2", /([aeo])h([^aeo]|$)/g, "$1$2", /(.)\1+/g, "$1"]; +const ra = {a:"", e:"", i:"", o:"", u:"", y:"", b:1, f:1, p:1, v:1, c:2, g:2, j:2, k:2, q:2, s:2, x:2, z:2, "\u00df":2, d:3, t:3, l:4, m:5, n:5, r:6}; +var sa = {Exact:ma, Default:na, Normalize:na, LatinBalance:{mapper:oa}, LatinAdvanced:{mapper:oa, matcher:pa, replacer:qa}, LatinExtra:{mapper:oa, replacer:qa.concat([/(?!^)[aeo]/g, ""]), matcher:pa}, LatinSoundex:{dedupe:!1, include:{letter:!0}, finalize:function(a) { + for (let b = 0; b < a.length; b++) { + var c = a[b]; + let e = c.charAt(0), d = ra[e]; + for (let f = 1, g; f < c.length && (g = c.charAt(f), "h" === g || "w" === g || !(g = ra[g]) || g === d || (e += g, d = g, 4 !== e.length)); f++) { + } + a[b] = e; + } +}}, CJK:{split:""}, LatinExact:ma, LatinDefault:na, LatinSimple:na}; +function va(a) { + X.call(a, "add"); + X.call(a, "append"); + X.call(a, "search"); + X.call(a, "update"); + X.call(a, "remove"); + X.call(a, "searchCache"); +} +let wa, xa, ya; +function za() { + wa = ya = 0; +} +function X(a) { this[a + "Async"] = function() { const c = arguments; var b = c[c.length - 1]; let e; "function" === typeof b && (e = b, delete c[c.length - 1]); - ma ? oa || (oa = Date.now() - na >= this.priority * this.priority * 3) : (ma = setTimeout(pa, 0), na = Date.now()); - if (oa) { + wa ? ya || (ya = Date.now() - xa >= this.priority * this.priority * 3) : (wa = setTimeout(za, 0), xa = Date.now()); + if (ya) { const f = this; return new Promise(g => { setTimeout(function() { @@ -318,102 +373,21 @@ function W(a) { return b; }; } -;function qa(a, c = 0) { - let b = [], e = []; - c && (c = 250000 / c * 5000 | 0); - for (const d of a.entries()) { - e.push(d), e.length === c && (b.push(e), e = []); - } - e.length && b.push(e); - return b; -} -function ra(a, c) { - c || (c = new Map()); - for (let b = 0, e; b < a.length; b++) { - e = a[b], c.set(e[0], e[1]); - } - return c; -} -function sa(a, c = 0) { - let b = [], e = []; - c && (c = 250000 / c * 1000 | 0); - for (const d of a.entries()) { - e.push([d[0], qa(d[1])[0]]), e.length === c && (b.push(e), e = []); - } - e.length && b.push(e); - return b; -} -function ta(a, c) { - c || (c = new Map()); - for (let b = 0, e, d; b < a.length; b++) { - e = a[b], d = c.get(e[0]), c.set(e[0], ra(e[1], d)); - } - return c; -} -function wa(a) { - let c = [], b = []; - for (const e of a.keys()) { - b.push(e), 250000 === b.length && (c.push(b), b = []); - } - b.length && c.push(b); - return c; -} -function xa(a, c) { - c || (c = new Set()); - for (let b = 0; b < a.length; b++) { - c.add(a[b]); - } - return c; -} -function ya(a, c, b, e, d, f, g = 0) { - const k = e && e.constructor === Array; - var h = k ? e.shift() : e; - if (!h) { - return this.export(a, c, d, f + 1); - } - if ((h = a((c ? c + "." : "") + (g + 1) + "." + b, JSON.stringify(h))) && h.then) { - const m = this; - return h.then(function() { - return ya.call(m, a, c, b, k ? e : null, d, f, g + 1); - }); - } - return ya.call(this, a, c, b, k ? e : null, d, f, g + 1); -} -function za(a, c) { - let b = ""; - for (const e of a.entries()) { - a = e[0]; - const d = e[1]; - let f = ""; - for (let g = 0, k; g < d.length; g++) { - k = d[g] || [""]; - let h = ""; - for (let m = 0; m < k.length; m++) { - h += (h ? "," : "") + ("string" === c ? '"' + k[m] + '"' : k[m]); - } - h = "[" + h + "]"; - f += (f ? "," : "") + h; - } - f = '["' + a + '",[' + f + "]]"; - b += (b ? "," : "") + f; - } - return b; -} -;X.prototype.add = function(a, c, b) { +;Y.prototype.add = function(a, c, b) { R(a) && (c = a, a = S(c, this.key)); if (c && (a || 0 === a)) { if (!b && this.reg.has(a)) { return this.update(a, c); } - for (let k = 0, h; k < this.field.length; k++) { - h = this.D[k]; - var e = this.index.get(this.field[k]); - if ("function" === typeof h) { - var d = h(c); + for (let h = 0, k; h < this.field.length; h++) { + k = this.D[h]; + var e = this.index.get(this.field[h]); + if ("function" === typeof k) { + var d = k(c); d && e.add(a, d, !1, !0); } else { - if (d = h.I, !d || d(c)) { - h.constructor === String ? h = ["" + h] : P(h) && (h = [h]), Aa(c, h, this.J, 0, e, a, h[0], b); + if (d = k.I, !d || d(c)) { + k.constructor === String ? k = ["" + k] : P(k) && (k = [k]), Aa(c, k, this.J, 0, e, a, k[0], b); } } } @@ -421,14 +395,14 @@ function za(a, c) { for (e = 0; e < this.B.length; e++) { var f = this.B[e], g = this.R[e]; d = this.tag.get(g); - let k = L(); + let h = M(); if ("function" === typeof f) { if (f = f(c), !f) { continue; } } else { - const h = f.I; - if (h && !h(c)) { + const k = f.I; + if (k && !k(c)) { continue; } f.constructor === String && (f = "" + f); @@ -436,8 +410,8 @@ function za(a, c) { } if (d && f) { P(f) && (f = [f]); - for (let h = 0, m, n; h < f.length; h++) { - m = f[h], k[m] || (k[m] = 1, (g = d.get(m)) ? n = g : d.set(m, n = []), b && n.includes(a) || (n.push(a), this.fastupdate && ((g = this.reg.get(a)) ? g.push(n) : this.reg.set(a, [n])))); + for (let k = 0, n, l; k < f.length; k++) { + n = f[k], h[n] || (h[n] = 1, (g = d.get(n)) ? l = g : d.set(n, l = []), b && l.includes(a) || (l.push(a), this.fastupdate && ((g = this.reg.get(a)) ? g.push(l) : this.reg.set(a, [l])))); } } else { d || console.warn("Tag '" + g + "' was not found"); @@ -445,29 +419,29 @@ function za(a, c) { } } if (this.store && (!b || !this.store.has(a))) { - let k; + let h; if (this.A) { - k = L(); - for (let h = 0, m; h < this.A.length; h++) { - m = this.A[h]; - if ((b = m.I) && !b(c)) { + h = M(); + for (let k = 0, n; k < this.A.length; k++) { + n = this.A[k]; + if ((b = n.I) && !b(c)) { continue; } - let n; - if ("function" === typeof m) { - n = m(c); - if (!n) { + let l; + if ("function" === typeof n) { + l = n(c); + if (!l) { continue; } - m = [m.S]; - } else if (P(m) || m.constructor === String) { - k[m] = c[m]; + n = [n.S]; + } else if (P(n) || n.constructor === String) { + h[n] = c[n]; continue; } - Ba(c, k, m, 0, m[0], n); + Ba(c, h, n, 0, n[0], l); } } - this.store.set(a, k || c); + this.store.set(a, h || c); } } return this; @@ -482,11 +456,11 @@ function Ba(a, c, b, e, d, f) { Ba(a, c, b, e, d); } } else { - c = c[d] || (c[d] = L()), d = b[++e], Ba(a, c, b, e, d); + c = c[d] || (c[d] = M()), d = b[++e], Ba(a, c, b, e, d); } } } -function Aa(a, c, b, e, d, f, g, k) { +function Aa(a, c, b, e, d, f, g, h) { if (a = a[g]) { if (e === c.length - 1) { if (a.constructor === Array) { @@ -498,14 +472,14 @@ function Aa(a, c, b, e, d, f, g, k) { } a = a.join(" "); } - d.add(f, a, k, !0); + d.add(f, a, h, !0); } else { if (a.constructor === Array) { for (g = 0; g < a.length; g++) { - Aa(a, c, b, e, d, f, g, k); + Aa(a, c, b, e, d, f, g, h); } } else { - g = c[++e], Aa(a, c, b, e, d, f, g, k); + g = c[++e], Aa(a, c, b, e, d, f, g, h); } } } @@ -515,7 +489,7 @@ function Aa(a, c, b, e, d, f, g, k) { return a; } if (1 === a.length) { - return a = a[0], a = b || a.length > c ? c ? a.slice(b, b + c) : a.slice(b) : a; + return a = a[0], a = b || a.length > c ? a.slice(b, b + c) : a; } let e = []; for (let d = 0, f, g; d < a.length; d++) { @@ -525,7 +499,9 @@ function Aa(a, c, b, e, d, f, g, k) { b -= g; continue; } - b < g && (f = c ? f.slice(b, b + c) : f.slice(b), g = f.length, b = 0); + f = f.slice(b, b + c); + g = f.length; + b = 0; } g > c && (f = f.slice(0, c), g = c); if (!e.length && g >= c) { @@ -540,241 +516,8 @@ function Aa(a, c, b, e, d, f, g, k) { } return e = 1 < e.length ? [].concat.apply([], e) : e[0]; } -;function Da(a, c, b, e, d) { - let f, g, k; - "string" === typeof d ? (f = d, d = "") : f = d.template; - if (!f) { - throw Error('No template pattern was specified by the search option "highlight"'); - } - g = f.indexOf("$1"); - if (-1 === g) { - throw Error('Invalid highlight template. The replacement pattern "$1" was not found in template: ' + f); - } - k = f.substring(g + 2); - g = f.substring(0, g); - let h = d && d.boundary, m = !d || !1 !== d.clip, n = d && d.merge && k && g && new RegExp(k + " " + g, "g"); - d = d && d.ellipsis; - var z = 0; - if ("object" === typeof d) { - var x = d.template; - z = x.length - 2; - d = d.pattern; - } - "string" !== typeof d && (d = !1 === d ? "" : "..."); - z && (d = x.replace("$1", d)); - x = d.length - z; - let q, r; - "object" === typeof h && (q = h.before, 0 === q && (q = -1), r = h.after, 0 === r && (r = -1), h = h.total || 9e5); - z = new Map(); - for (let ua = 0, T, Ja, ba; ua < c.length; ua++) { - let ca; - if (e) { - ca = c, ba = e; - } else { - var w = c[ua]; - ba = w.field; - if (!ba) { - continue; - } - ca = w.result; - } - Ja = b.get(ba); - T = Ja.encoder; - w = z.get(T); - "string" !== typeof w && (w = T.encode(a), z.set(T, w)); - for (let ia = 0; ia < ca.length; ia++) { - var l = ca[ia].doc; - if (!l) { - continue; - } - l = S(l, ba); - if (!l) { - continue; - } - var u = l.trim().split(/\s+/); - if (!u.length) { - continue; - } - l = ""; - var v = []; - let ja = []; - var E = -1, t = -1, C = 0; - for (var y = 0; y < u.length; y++) { - var A = u[y], H = T.encode(A); - H = 1 < H.length ? H.join(" ") : H[0]; - let p; - if (H && A) { - var D = A.length, M = (T.split ? A.replace(T.split, "") : A).length - H.length, K = "", Q = 0; - for (var U = 0; U < w.length; U++) { - var N = w[U]; - if (N) { - var G = N.length; - G += M; - Q && G <= Q || (N = H.indexOf(N), -1 < N && (K = (N ? A.substring(0, N) : "") + g + A.substring(N, N + G) + k + (N + G < D ? A.substring(N + G) : ""), Q = G, p = !0)); - } - } - K && (h && (0 > E && (E = l.length + (l ? 1 : 0)), t = l.length + (l ? 1 : 0) + K.length, C += D, ja.push(v.length), v.push({match:K})), l += (l ? " " : "") + K); - } - if (!p) { - A = u[y], l += (l ? " " : "") + A, h && v.push({text:A}); - } else if (h && C >= h) { - break; - } - } - C = ja.length * (f.length - 2); - if (q || r || h && l.length - C > h) { - if (C = h + C - 2 * x, y = t - E, 0 < q && (y += q), 0 < r && (y += r), y <= C) { - u = q ? E - (0 < q ? q : 0) : E - ((C - y) / 2 | 0), v = r ? t + (0 < r ? r : 0) : u + C, m || (0 < u && " " !== l.charAt(u) && " " !== l.charAt(u - 1) && (u = l.indexOf(" ", u), 0 > u && (u = 0)), v < l.length && " " !== l.charAt(v - 1) && " " !== l.charAt(v) && (v = l.lastIndexOf(" ", v), v < t ? v = t : ++v)), l = (u ? d : "") + l.substring(u, v) + (v < l.length ? d : ""); - } else { - t = []; - E = {}; - C = {}; - y = {}; - A = {}; - H = {}; - K = M = D = 0; - for (U = Q = 1;;) { - var O = void 0; - for (let p = 0, F; p < ja.length; p++) { - F = ja[p]; - if (K) { - if (M !== K) { - if (y[p + 1]) { - continue; - } - F += K; - if (E[F]) { - D -= x; - C[p + 1] = 1; - y[p + 1] = 1; - continue; - } - if (F >= v.length - 1) { - if (F >= v.length) { - y[p + 1] = 1; - F >= u.length && (C[p + 1] = 1); - continue; - } - D -= x; - } - l = v[F].text; - if (G = r && H[p]) { - if (0 < G) { - if (l.length > G) { - if (y[p + 1] = 1, m) { - l = l.substring(0, G); - } else { - continue; - } - } - (G -= l.length) || (G = -1); - H[p] = G; - } else { - y[p + 1] = 1; - continue; - } - } - if (D + l.length + 1 <= h) { - l = " " + l, t[p] += l; - } else if (m) { - O = h - D - 1, 0 < O && (l = " " + l.substring(0, O), t[p] += l), y[p + 1] = 1; - } else { - y[p + 1] = 1; - continue; - } - } else { - if (y[p]) { - continue; - } - F -= M; - if (E[F]) { - D -= x; - y[p] = 1; - C[p] = 1; - continue; - } - if (0 >= F) { - if (0 > F) { - y[p] = 1; - C[p] = 1; - continue; - } - D -= x; - } - l = v[F].text; - if (G = q && A[p]) { - if (0 < G) { - if (l.length > G) { - if (y[p] = 1, m) { - l = l.substring(l.length - G); - } else { - continue; - } - } - (G -= l.length) || (G = -1); - A[p] = G; - } else { - y[p] = 1; - continue; - } - } - if (D + l.length + 1 <= h) { - l += " ", t[p] = l + t[p]; - } else if (m) { - O = l.length + 1 - (h - D), 0 <= O && O < l.length && (l = l.substring(O) + " ", t[p] = l + t[p]), y[p] = 1; - } else { - y[p] = 1; - continue; - } - } - } else { - l = v[F].match; - q && (A[p] = q); - r && (H[p] = r); - p && D++; - let va; - F ? !p && x && (D += x) : (C[p] = 1, y[p] = 1); - F >= u.length - 1 ? va = 1 : F < v.length - 1 && v[F + 1].match ? va = 1 : x && (D += x); - D -= f.length - 2; - if (!p || D + l.length <= h) { - t[p] = l; - } else { - O = Q = U = C[p] = 0; - break; - } - va && (C[p + 1] = 1, y[p + 1] = 1); - } - D += l.length; - O = E[F] = 1; - } - if (O) { - M === K ? K++ : M++; - } else { - M === K ? Q = 0 : U = 0; - if (!Q && !U) { - break; - } - Q ? (M++, K = M) : K++; - } - } - l = ""; - for (let p = 0, F; p < t.length; p++) { - F = (p && C[p] ? " " : (p && !d ? " " : "") + d) + t[p], l += F; - } - d && !C[t.length] && (l += d); - } - } - n && (l = l.replace(n, " ")); - ca[ia].highlight = l; - } - if (e) { - break; - } - } - return c; -} -;function Ea(a, c) { - const b = L(), e = []; +;function Da(a, c) { + const b = M(), e = []; for (let d = 0, f; d < c.length; d++) { f = c[d]; for (let g = 0; g < f.length; g++) { @@ -786,169 +529,403 @@ function Aa(a, c, b, e, d, f, g, k) { } return e; } -;L(); -X.prototype.search = function(a, c, b, e) { - b || (!c && R(a) ? (b = a, a = "") : R(c) && (b = c, c = 0)); - if (b && b.cache) { - b.cache = !1; - var d = this.searchCache(a, c, b); - b.cache = !0; - return d; +;M(); +function Ea(a, c, b, e, d) { + let f, g, h; + "string" === typeof d ? (f = d, d = "") : f = d.template; + if (!f) { + throw Error('No template pattern was specified by the search option "highlight"'); } - let f = []; - var g = []; - let k, h, m; - let n = 0, z = !0, x; + g = f.indexOf("$1"); + if (-1 === g) { + throw Error('Invalid highlight template. The replacement pattern "$1" was not found in template: ' + f); + } + h = f.substring(g + 2); + g = f.substring(0, g); + let k = d && d.boundary, n = !d || !1 !== d.clip, l = d && d.merge && h && g && new RegExp(h + " " + g, "g"); + d = d && d.ellipsis; + var D = 0; + if ("object" === typeof d) { + var w = d.template; + D = w.length - 2; + d = d.pattern; + } + "string" !== typeof d && (d = !1 === d ? "" : "..."); + D && (d = w.replace("$1", d)); + w = d.length - D; + let t, p; + "object" === typeof k && (t = k.before, 0 === t && (t = -1), p = k.after, 0 === p && (p = -1), k = k.total || 9e5); + D = new Map(); + for (let ta = 0, U, Ka, ba; ta < c.length; ta++) { + let ca; + if (e) { + ca = c, ba = e; + } else { + var x = c[ta]; + ba = x.field; + if (!ba) { + continue; + } + ca = x.result; + } + Ka = b.get(ba); + U = Ka.encoder; + x = D.get(U); + "string" !== typeof x && (x = U.encode(a), D.set(U, x)); + for (let ia = 0; ia < ca.length; ia++) { + var m = ca[ia].doc; + if (!m) { + continue; + } + m = S(m, ba); + if (!m) { + continue; + } + var v = m.trim().split(/\s+/); + if (!v.length) { + continue; + } + m = ""; + var u = []; + let ja = []; + var F = -1, z = -1, r = 0; + for (var y = 0; y < v.length; y++) { + var E = v[y], B = U.encode(E); + B = 1 < B.length ? B.join(" ") : B[0]; + let q; + if (B && E) { + var C = E.length, K = (U.split ? E.replace(U.split, "") : E).length - B.length, L = "", Q = 0; + for (var V = 0; V < x.length; V++) { + var N = x[V]; + if (N) { + var H = N.length; + H += K; + Q && H <= Q || (N = B.indexOf(N), -1 < N && (L = (N ? E.substring(0, N) : "") + g + E.substring(N, N + H) + h + (N + H < C ? E.substring(N + H) : ""), Q = H, q = !0)); + } + } + L && (k && (0 > F && (F = m.length + (m ? 1 : 0)), z = m.length + (m ? 1 : 0) + L.length, r += C, ja.push(u.length), u.push({match:L})), m += (m ? " " : "") + L); + } + if (!q) { + E = v[y], m += (m ? " " : "") + E, k && u.push({text:E}); + } else if (k && r >= k) { + break; + } + } + r = ja.length * (f.length - 2); + if (t || p || k && m.length - r > k) { + if (r = k + r - 2 * w, y = z - F, 0 < t && (y += t), 0 < p && (y += p), y <= r) { + v = t ? F - (0 < t ? t : 0) : F - ((r - y) / 2 | 0), u = p ? z + (0 < p ? p : 0) : v + r, n || (0 < v && " " !== m.charAt(v) && " " !== m.charAt(v - 1) && (v = m.indexOf(" ", v), 0 > v && (v = 0)), u < m.length && " " !== m.charAt(u - 1) && " " !== m.charAt(u) && (u = m.lastIndexOf(" ", u), u < z ? u = z : ++u)), m = (v ? d : "") + m.substring(v, u) + (u < m.length ? d : ""); + } else { + z = []; + F = {}; + r = {}; + y = {}; + E = {}; + B = {}; + L = K = C = 0; + for (V = Q = 1;;) { + var O = void 0; + for (let q = 0, G; q < ja.length; q++) { + G = ja[q]; + if (L) { + if (K !== L) { + if (y[q + 1]) { + continue; + } + G += L; + if (F[G]) { + C -= w; + r[q + 1] = 1; + y[q + 1] = 1; + continue; + } + if (G >= u.length - 1) { + if (G >= u.length) { + y[q + 1] = 1; + G >= v.length && (r[q + 1] = 1); + continue; + } + C -= w; + } + m = u[G].text; + if (H = p && B[q]) { + if (0 < H) { + if (m.length > H) { + if (y[q + 1] = 1, n) { + m = m.substring(0, H); + } else { + continue; + } + } + (H -= m.length) || (H = -1); + B[q] = H; + } else { + y[q + 1] = 1; + continue; + } + } + if (C + m.length + 1 <= k) { + m = " " + m, z[q] += m; + } else if (n) { + O = k - C - 1, 0 < O && (m = " " + m.substring(0, O), z[q] += m), y[q + 1] = 1; + } else { + y[q + 1] = 1; + continue; + } + } else { + if (y[q]) { + continue; + } + G -= K; + if (F[G]) { + C -= w; + y[q] = 1; + r[q] = 1; + continue; + } + if (0 >= G) { + if (0 > G) { + y[q] = 1; + r[q] = 1; + continue; + } + C -= w; + } + m = u[G].text; + if (H = t && E[q]) { + if (0 < H) { + if (m.length > H) { + if (y[q] = 1, n) { + m = m.substring(m.length - H); + } else { + continue; + } + } + (H -= m.length) || (H = -1); + E[q] = H; + } else { + y[q] = 1; + continue; + } + } + if (C + m.length + 1 <= k) { + m += " ", z[q] = m + z[q]; + } else if (n) { + O = m.length + 1 - (k - C), 0 <= O && O < m.length && (m = m.substring(O) + " ", z[q] = m + z[q]), y[q] = 1; + } else { + y[q] = 1; + continue; + } + } + } else { + m = u[G].match; + t && (E[q] = t); + p && (B[q] = p); + q && C++; + let ua; + G ? !q && w && (C += w) : (r[q] = 1, y[q] = 1); + G >= v.length - 1 ? ua = 1 : G < u.length - 1 && u[G + 1].match ? ua = 1 : w && (C += w); + C -= f.length - 2; + if (!q || C + m.length <= k) { + z[q] = m; + } else { + O = Q = V = r[q] = 0; + break; + } + ua && (r[q + 1] = 1, y[q + 1] = 1); + } + C += m.length; + O = F[G] = 1; + } + if (O) { + K === L ? L++ : K++; + } else { + K === L ? Q = 0 : V = 0; + if (!Q && !V) { + break; + } + Q ? (K++, L = K) : L++; + } + } + m = ""; + for (let q = 0, G; q < z.length; q++) { + G = (q && r[q] ? " " : (q && !d ? " " : "") + d) + z[q], m += G; + } + d && !r[z.length] && (m += d); + } + } + l && (m = m.replace(l, " ")); + ca[ia].highlight = m; + } + if (e) { + break; + } + } + return c; +} +;Y.prototype.search = function(a, c, b, e) { + b || (!c && R(a) ? (b = a, a = "") : R(c) && (b = c, c = 0)); + let d = []; + var f = []; + let g; + let h, k, n, l; + let D = 0, w = !0, t; if (b) { b.constructor === Array && (b = {index:b}); a = b.query || a; - var q = b.pluck; - var r = b.merge; - h = q || b.field || (h = b.index) && (h.index ? null : h); - m = this.tag && b.tag; - k = b.suggest; - z = !0; - this.store && b.highlight && !z ? console.warn("Highlighting results can only be done on a final resolver task or when calling .resolve({ highlight: ... })") : this.store && b.enrich && !z && console.warn("Enrich results can only be done on a final resolver task or when calling .resolve({ enrich: true })"); - x = z && this.store && b.highlight; - d = !!x || z && this.store && b.enrich; + var p = b.pluck; + var x = b.merge; + n = p || b.field || (n = b.index) && (n.index ? null : n); + l = this.tag && b.tag; + h = b.suggest; + w = !0; + k = b.cache; + this.store && b.highlight && !w ? console.warn("Highlighting results can only be done on a final resolver task or when calling .resolve({ highlight: ... })") : this.store && b.enrich && !w && console.warn("Enrich results can only be done on a final resolver task or when calling .resolve({ enrich: true })"); + t = w && this.store && b.highlight; + g = !!t || w && this.store && b.enrich; c = b.limit || c; - var w = b.offset || 0; - c || (c = z ? 100 : 0); - if (m) { - m.constructor !== Array && (m = [m]); - var l = []; - for (let E = 0, t; E < m.length; E++) { - t = m[E]; - if (P(t)) { + var m = b.offset || 0; + c || (c = w ? 100 : 0); + if (l) { + l.constructor !== Array && (l = [l]); + var v = []; + for (let z = 0, r; z < l.length; z++) { + r = l[z]; + if (P(r)) { throw Error("A tag option can't be a string, instead it needs a { field: tag } format."); } - if (t.field && t.tag) { - var u = t.tag; + if (r.field && r.tag) { + var u = r.tag; if (u.constructor === Array) { - for (var v = 0; v < u.length; v++) { - l.push(t.field, u[v]); + for (var F = 0; F < u.length; F++) { + v.push(r.field, u[F]); } } else { - l.push(t.field, u); + v.push(r.field, u); } } else { - u = Object.keys(t); - for (let C = 0, y, A; C < u.length; C++) { - if (y = u[C], A = t[y], A.constructor === Array) { - for (v = 0; v < A.length; v++) { - l.push(y, A[v]); + u = Object.keys(r); + for (let y = 0, E, B; y < u.length; y++) { + if (E = u[y], B = r[E], B.constructor === Array) { + for (F = 0; F < B.length; F++) { + v.push(E, B[F]); } } else { - l.push(y, A); + v.push(E, B); } } } } - if (!l.length) { + if (!v.length) { throw Error("Your tag definition within the search options is probably wrong. No valid tags found."); } - m = l; + l = v; if (!a) { - g = []; - if (l.length) { - for (q = 0; q < l.length; q += 2) { - r = Fa.call(this, l[q], l[q + 1], c, w, d), f.push({field:l[q], tag:l[q + 1], result:r}); + f = []; + if (v.length) { + for (p = 0; p < v.length; p += 2) { + x = Fa.call(this, v[p], v[p + 1], c, m, g), d.push({field:v[p], tag:v[p + 1], result:x}); } } - return g.length ? Promise.all(g).then(function(E) { - for (let t = 0; t < E.length; t++) { - f[t].result = E[t]; + return f.length ? Promise.all(f).then(function(z) { + for (let r = 0; r < z.length; r++) { + d[r].result = z[r]; } - return f; - }) : f; + return d; + }) : d; } } - h && h.constructor !== Array && (h = [h]); + n && n.constructor !== Array && (n = [n]); } - h || (h = this.field); - l = !1; - for (let E = 0, t, C, y; E < h.length; E++) { - C = h[E]; - let A; - P(C) || (A = C, C = A.field, a = A.query || a, c = J(A.limit, c), w = J(A.offset, w), k = J(A.suggest, k), x = z && this.store && J(A.highlight, x), d = !!x || z && this.store && J(A.enrich, d)); + n || (n = this.field); + v = !1; + for (let z = 0, r, y, E; z < n.length; z++) { + y = n[z]; + let B; + P(y) || (B = y, y = B.field, a = B.query || a, c = J(B.limit, c), m = J(B.offset, m), h = J(B.suggest, h), t = w && this.store && J(B.highlight, t), g = !!t || w && this.store && J(B.enrich, g), k = J(B.cache, k)); if (e) { - t = e[E]; + r = e[z]; } else { - if (u = A || b, v = this.index.get(C), m && (u.enrich = !1), l) { - l[E] = v.search(a, c, u); - u && d && (u.enrich = d); + u = B || b || {}; + F = u.enrich; + const C = this.index.get(y); + l && F && (u.enrich = !1); + r = k ? C.searchCache(a, c, u) : C.search(a, c, u); + F && (u.enrich = F); + if (v) { + v[z] = r; continue; - } else { - t = v.search(a, c, u), u && d && (u.enrich = d); } } - y = t && (z ? t.length : t.result.length); - if (m && y) { + E = (r = r.result || r) && r.length; + if (l && E) { u = []; - v = 0; - for (let H = 0, D, M; H < m.length; H += 2) { - D = this.tag.get(m[H]); - if (!D) { - if (console.warn("Tag '" + m[H] + ":" + m[H + 1] + "' will be skipped because there is no field '" + m[H] + "'."), k) { + F = 0; + for (let C = 0, K, L; C < l.length; C += 2) { + K = this.tag.get(l[C]); + if (!K) { + if (console.warn("Tag '" + l[C] + ":" + l[C + 1] + "' will be skipped because there is no field '" + l[C] + "'."), h) { continue; } else { - return f; + return d; } } - if (M = (D = D && D.get(m[H + 1])) && D.length) { - v++, u.push(D); - } else if (!k) { - return f; + if (L = (K = K && K.get(l[C + 1])) && K.length) { + F++, u.push(K); + } else if (!h) { + return d; } } - if (v) { - t = Ea(t, u); - y = t.length; - if (!y && !k) { - return t; + if (F) { + r = Da(r, u); + E = r.length; + if (!E && !h) { + return r; } - v--; + F--; } } - if (y) { - g[n] = C, f.push(t), n++; - } else if (1 === h.length) { - return f; + if (E) { + f[D] = y, d.push(r), D++; + } else if (1 === n.length) { + return d; } } - if (l) { - const E = this; - return Promise.all(l).then(function(t) { - return t.length ? E.search(a, c, b, t) : t; + if (v) { + const z = this; + return Promise.all(v).then(function(r) { + b && (b.resolve = w); + r.length && (r = z.search(a, c, b, r)); + return r; }); } - if (!n) { - return f; + if (!D) { + return d; } - if (q && (!d || !this.store)) { - return f = f[0]; + if (p && (!g || !this.store)) { + return d = d[0]; } - l = []; - for (w = 0; w < g.length; w++) { - e = f[w]; - d && e.length && "undefined" === typeof e[0].doc && (e = Ga.call(this, e)); - if (q) { - return x ? Da(a, e, this.index, q, x) : e; + v = []; + for (m = 0; m < f.length; m++) { + e = d[m]; + g && e.length && "undefined" === typeof e[0].doc && (e = Ga.call(this, e)); + if (p) { + return t ? Ea(a, e, this.index, p, t) : e; } - f[w] = {field:g[w], result:e}; + d[m] = {field:f[m], result:e}; } - x && (f = Da(a, f, this.index, q, x)); - return r ? Ha(f) : f; + t && (d = Ea(a, d, this.index, p, t)); + return x ? Ha(d) : d; }; function Ha(a) { - const c = [], b = L(), e = L(); - for (let d = 0, f, g, k, h, m, n, z; d < a.length; d++) { + const c = [], b = M(), e = M(); + for (let d = 0, f, g, h, k, n, l, D; d < a.length; d++) { f = a[d]; g = f.field; - k = f.result; - for (let x = 0; x < k.length; x++) { - if (m = k[x], "object" !== typeof m ? m = {id:h = m} : h = m.id, (n = b[h]) ? n.push(g) : (m.field = b[h] = [g], c.push(m)), z = m.highlight) { - n = e[h], n || (e[h] = n = {}, m.highlight = n), n[g] = z; + h = f.result; + for (let w = 0; w < h.length; w++) { + if (n = h[w], "object" !== typeof n ? n = {id:k = n} : k = n.id, (l = b[k]) ? l.push(g) : (n.field = b[k] = [g], c.push(n)), D = n.highlight) { + l = e[k], l || (e[k] = l = {}, n.highlight = l), l[g] = D; } } } @@ -982,47 +959,33 @@ function Ga(a) { } return c; } -;const Ia = {normalize:!1, numeric:!1, dedupe:!1}; -const Ka = {}; -const La = new Map([["b", "p"], ["v", "f"], ["w", "f"], ["z", "s"], ["x", "s"], ["d", "t"], ["n", "m"], ["c", "k"], ["g", "k"], ["j", "k"], ["q", "k"], ["i", "e"], ["y", "e"], ["u", "o"]]); -const Ma = new Map([["ae", "a"], ["oe", "o"], ["sh", "s"], ["kh", "k"], ["th", "t"], ["ph", "f"], ["pf", "f"]]), Na = [/([^aeo])h(.)/g, "$1$2", /([aeo])h([^aeo]|$)/g, "$1$2", /(.)\1+/g, "$1"]; -const Oa = {a:"", e:"", i:"", o:"", u:"", y:"", b:1, f:1, p:1, v:1, c:2, g:2, j:2, k:2, q:2, s:2, x:2, z:2, "\u00df":2, d:3, t:3, l:4, m:5, n:5, r:6}; -var Pa = {Exact:Ia, Default:Ka, Normalize:Ka, LatinBalance:{mapper:La}, LatinAdvanced:{mapper:La, matcher:Ma, replacer:Na}, LatinExtra:{mapper:La, replacer:Na.concat([/(?!^)[aeo]/g, ""]), matcher:Ma}, LatinSoundex:{dedupe:!1, include:{letter:!0}, finalize:function(a) { - for (let b = 0; b < a.length; b++) { - var c = a[b]; - let e = c.charAt(0), d = Oa[e]; - for (let f = 1, g; f < c.length && (g = c.charAt(f), "h" === g || "w" === g || !(g = Oa[g]) || g === d || (e += g, d = g, 4 !== e.length)); f++) { - } - a[b] = e; - } -}}, CJK:{split:""}, LatinExact:Ia, LatinDefault:Ka, LatinSimple:Ka}; -function X(a) { - if (!this || this.constructor !== X) { - return new X(a); +;function Y(a) { + if (!this || this.constructor !== Y) { + return new Y(a); } const c = a.document || a.doc || a; var b; this.D = []; this.field = []; this.J = []; - this.key = (b = c.key || c.id) && Qa(b, this.J) || "id"; + this.key = (b = c.key || c.id) && Ia(b, this.J) || "id"; this.reg = (this.fastupdate = !!a.fastupdate) ? new Map() : new Set(); this.A = (b = c.store || null) && b && !0 !== b && []; this.store = b && new Map(); - this.cache = (b = a.cache || null) && new Y(b); + this.cache = (b = a.cache || null) && new W(b); a.cache = !1; this.priority = a.priority || 4; b = new Map(); let e = c.index || c.field || c; P(e) && (e = [e]); for (let d = 0, f, g; d < e.length; d++) { - f = e[d], P(f) || (g = f, f = f.field), g = R(g) ? Object.assign({}, a, g) : a, b.set(f, new Z(g, this.reg)), g.custom ? this.D[d] = g.custom : (this.D[d] = Qa(f, this.J), g.filter && ("string" === typeof this.D[d] && (this.D[d] = new String(this.D[d])), this.D[d].I = g.filter)), this.field[d] = f; + f = e[d], P(f) || (g = f, f = f.field), g = R(g) ? Object.assign({}, a, g) : a, b.set(f, new Z(g, this.reg)), g.custom ? this.D[d] = g.custom : (this.D[d] = Ia(f, this.J), g.filter && ("string" === typeof this.D[d] && (this.D[d] = new String(this.D[d])), this.D[d].I = g.filter)), this.field[d] = f; } if (this.A) { a = c.store; P(a) && (a = [a]); for (let d = 0, f, g; d < a.length; d++) { - f = a[d], g = f.field || f, f.custom ? (this.A[d] = f.custom, f.custom.S = g) : (this.A[d] = Qa(g, this.J), f.filter && ("string" === typeof this.A[d] && (this.A[d] = new String(this.A[d])), this.A[d].I = f.filter)); + f = a[d], g = f.field || f, f.custom ? (this.A[d] = f.custom, f.custom.S = g) : (this.A[d] = Ia(g, this.J), f.filter && ("string" === typeof this.A[d] && (this.A[d] = new String(this.A[d])), this.A[d].I = f.filter)); } } this.index = b; @@ -1038,14 +1001,14 @@ function X(a) { if (!g) { throw Error("The tag field from the document descriptor is undefined."); } - f.custom ? this.B[d] = f.custom : (this.B[d] = Qa(g, this.J), f.filter && ("string" === typeof this.B[d] && (this.B[d] = new String(this.B[d])), this.B[d].I = f.filter)); + f.custom ? this.B[d] = f.custom : (this.B[d] = Ia(g, this.J), f.filter && ("string" === typeof this.B[d] && (this.B[d] = new String(this.B[d])), this.B[d].I = f.filter)); this.R[d] = g; this.tag.set(g, new Map()); } } } } -function Qa(a, c) { +function Ia(a, c) { const b = a.split(":"); let e = 0; for (let d = 0; d < b.length; d++) { @@ -1054,14 +1017,14 @@ function Qa(a, c) { e < b.length && (b.length = e); return 1 < e ? b : b[0]; } -B = X.prototype; -B.append = function(a, c) { +A = Y.prototype; +A.append = function(a, c) { return this.add(a, c, !0); }; -B.update = function(a, c) { +A.update = function(a, c) { return this.remove(a).add(a, c); }; -B.remove = function(a) { +A.remove = function(a) { R(a) && (a = S(a, this.key)); for (var c of this.index.values()) { c.remove(a, !0); @@ -1082,7 +1045,7 @@ B.remove = function(a) { this.cache && this.cache.remove(a); return this; }; -B.clear = function() { +A.clear = function() { const a = []; for (const c of this.index.values()) { const b = c.clear(); @@ -1097,31 +1060,95 @@ B.clear = function() { this.cache && this.cache.clear(); return a.length ? Promise.all(a) : this; }; -B.contain = function(a) { +A.contain = function(a) { return this.reg.has(a); }; -B.cleanup = function() { +A.cleanup = function() { for (const a of this.index.values()) { a.cleanup(); } return this; }; -B.get = function(a) { +A.get = function(a) { return this.store.get(a) || null; }; -B.set = function(a, c) { +A.set = function(a, c) { "object" === typeof a && (c = a, a = S(c, this.key)); this.store.set(a, c); return this; }; -B.searchCache = Ra; -B.export = function(a, c, b = 0, e = 0) { +A.searchCache = la; +A.export = Ja; +A.import = La; +va(Y.prototype); +function Ma(a, c = 0) { + let b = [], e = []; + c && (c = 250000 / c * 5000 | 0); + for (const d of a.entries()) { + e.push(d), e.length === c && (b.push(e), e = []); + } + e.length && b.push(e); + return b; +} +function Na(a, c) { + c || (c = new Map()); + for (let b = 0, e; b < a.length; b++) { + e = a[b], c.set(e[0], e[1]); + } + return c; +} +function Oa(a, c = 0) { + let b = [], e = []; + c && (c = 250000 / c * 1000 | 0); + for (const d of a.entries()) { + e.push([d[0], Ma(d[1])[0]]), e.length === c && (b.push(e), e = []); + } + e.length && b.push(e); + return b; +} +function Pa(a, c) { + c || (c = new Map()); + for (let b = 0, e, d; b < a.length; b++) { + e = a[b], d = c.get(e[0]), c.set(e[0], Na(e[1], d)); + } + return c; +} +function Qa(a) { + let c = [], b = []; + for (const e of a.keys()) { + b.push(e), 250000 === b.length && (c.push(b), b = []); + } + b.length && c.push(b); + return c; +} +function Ra(a, c) { + c || (c = new Set()); + for (let b = 0; b < a.length; b++) { + c.add(a[b]); + } + return c; +} +function Sa(a, c, b, e, d, f, g = 0) { + const h = e && e.constructor === Array; + var k = h ? e.shift() : e; + if (!k) { + return this.export(a, c, d, f + 1); + } + if ((k = a((c ? c + "." : "") + (g + 1) + "." + b, JSON.stringify(k))) && k.then) { + const n = this; + return k.then(function() { + return Sa.call(n, a, c, b, h ? e : null, d, f, g + 1); + }); + } + return Sa.call(this, a, c, b, h ? e : null, d, f, g + 1); +} +function Ja(a, c, b = 0, e = 0) { if (b < this.field.length) { const g = this.field[b]; if ((c = this.index.get(g).export(a, g, b, e = 1)) && c.then) { - const k = this; + const h = this; return c.then(function() { - return k.export(a, g, b + 1); + return h.export(a, g, b + 1); }); } return this.export(a, g, b + 1); @@ -1130,25 +1157,25 @@ B.export = function(a, c, b = 0, e = 0) { switch(e) { case 0: d = "reg"; - f = wa(this.reg); + f = Qa(this.reg); c = null; break; case 1: d = "tag"; - f = this.tag && sa(this.tag, this.reg.size); + f = this.tag && Oa(this.tag, this.reg.size); c = null; break; case 2: d = "doc"; - f = this.store && qa(this.store); + f = this.store && Ma(this.store); c = null; break; default: return; } - return ya.call(this, a, c, d, f, b, e); -}; -B.import = function(a, c) { + return Sa.call(this, a, c, d, f, b, e); +} +function La(a, c) { var b = a.split("."); "json" === b[b.length - 1] && b.pop(); a = 2 < b.length ? b[0] : ""; @@ -1161,62 +1188,40 @@ B.import = function(a, c) { switch(b) { case "reg": this.fastupdate = !1; - this.reg = xa(c, this.reg); + this.reg = Ra(c, this.reg); for (let e = 0, d; e < this.field.length; e++) { d = this.index.get(this.field[e]), d.fastupdate = !1, d.reg = this.reg; } break; case "tag": - this.tag = ta(c, this.tag); + this.tag = Pa(c, this.tag); break; case "doc": - this.store = ra(c, this.store); + this.store = Na(c, this.store); } } -}; -la(X.prototype); -function Ra(a, c, b) { - const e = (c ? "" + a : "object" === typeof a ? "" + a.query : a).toLowerCase(); - this.cache || (this.cache = new Y()); - let d = this.cache.get(e); - if (!d) { - d = this.search(a, c, b); - if (d.then) { - const f = this; - d.then(function(g) { - f.cache.set(e, g); - return g; - }); +} +function Ta(a, c) { + let b = ""; + for (const e of a.entries()) { + a = e[0]; + const d = e[1]; + let f = ""; + for (let g = 0, h; g < d.length; g++) { + h = d[g] || [""]; + let k = ""; + for (let n = 0; n < h.length; n++) { + k += (k ? "," : "") + ("string" === c ? '"' + h[n] + '"' : h[n]); + } + k = "[" + k + "]"; + f += (f ? "," : "") + k; } - this.cache.set(e, d); + f = '["' + a + '",[' + f + "]]"; + b += (b ? "," : "") + f; } - return d; + return b; } -function Y(a) { - this.limit = a && !0 !== a ? a : 1000; - this.cache = new Map(); - this.h = ""; -} -Y.prototype.set = function(a, c) { - this.cache.set(this.h = a, c); - this.cache.size > this.limit && this.cache.delete(this.cache.keys().next().value); -}; -Y.prototype.get = function(a) { - const c = this.cache.get(a); - c && this.h !== a && (this.cache.delete(a), this.cache.set(this.h = a, c)); - return c; -}; -Y.prototype.remove = function(a) { - for (const c of this.cache) { - const b = c[0]; - c[1].includes(a) && this.cache.delete(b); - } -}; -Y.prototype.clear = function() { - this.cache.clear(); - this.h = ""; -}; -Z.prototype.remove = function(a, c) { +;Z.prototype.remove = function(a, c) { const b = this.reg.size && (this.fastupdate ? this.reg.get(a) : this.reg.has(a)); if (b) { if (this.fastupdate) { @@ -1231,44 +1236,48 @@ Z.prototype.remove = function(a, c) { } } } else { - Sa(this.map, a), this.depth && Sa(this.ctx, a); + Ua(this.map, a), this.depth && Ua(this.ctx, a); } c || this.reg.delete(a); } this.cache && this.cache.remove(a); return this; }; -function Sa(a, c) { +function Ua(a, c) { let b = 0; var e = "undefined" === typeof c; if (a.constructor === Array) { - for (let d = 0, f, g; d < a.length; d++) { + for (let d = 0, f, g, h; d < a.length; d++) { if ((f = a[d]) && f.length) { if (e) { - b++; - } else { - if (g = f.indexOf(c), 0 <= g) { - if (1 < f.length) { - f.splice(g, 1); - b++; - break; - } else { - delete a[d]; - } - } else { - b++; + return 1; + } + g = f.indexOf(c); + if (0 <= g) { + if (1 < f.length) { + return f.splice(g, 1), 1; } + delete a[d]; + if (b) { + return 1; + } + h = 1; + } else { + if (h) { + return 1; + } + b++; } } } } else { for (let d of a.entries()) { - e = d[0], Sa(d[1], c) ? b++ : a.delete(e); + e = d[0], Ua(d[1], c) ? b++ : a.delete(e); } } return b; } -;const Ta = {memory:{resolution:1}, performance:{resolution:3, fastupdate:!0, context:{depth:1, resolution:1}}, match:{tokenize:"forward"}, score:{resolution:9, context:{depth:2, resolution:3}}}; +;const Va = {memory:{resolution:1}, performance:{resolution:3, fastupdate:!0, context:{depth:1, resolution:1}}, match:{tokenize:"forward"}, score:{resolution:9, context:{depth:2, resolution:3}}}; Z.prototype.add = function(a, c, b, e) { if (c && (a || 0 === a)) { if (!e && !b && this.reg.has(a)) { @@ -1276,23 +1285,23 @@ Z.prototype.add = function(a, c, b, e) { } e = this.depth; c = this.encoder.encode(c, !e); - const m = c.length; - if (m) { - const n = L(), z = L(), x = this.resolution; - for (let q = 0; q < m; q++) { - let r = c[this.rtl ? m - 1 - q : q]; - var d = r.length; - if (d && (e || !z[r])) { - var f = this.score ? this.score(c, r, q, null, 0) : Ua(x, m, q), g = ""; + const n = c.length; + if (n) { + const l = M(), D = M(), w = this.resolution; + for (let t = 0; t < n; t++) { + let p = c[this.rtl ? n - 1 - t : t]; + var d = p.length; + if (d && (e || !D[p])) { + var f = this.score ? this.score(c, p, t, null, 0) : Wa(w, n, t), g = ""; switch(this.tokenize) { case "full": if (2 < d) { - for (let w = 0, l; w < d; w++) { - for (f = d; f > w; f--) { - g = r.substring(w, f); - l = this.rtl ? d - 1 - w : w; - var k = this.score ? this.score(c, r, q, g, l) : Ua(x, m, q, d, l); - Va(this, z, g, k, a, b); + for (let x = 0, m; x < d; x++) { + for (f = d; f > x; f--) { + g = p.substring(x, f); + m = this.rtl ? d - 1 - x : x; + var h = this.score ? this.score(c, p, t, g, m) : Wa(w, n, t, d, m); + Xa(this, D, g, h, a, b); } } break; @@ -1300,27 +1309,27 @@ Z.prototype.add = function(a, c, b, e) { case "bidirectional": case "reverse": if (1 < d) { - for (k = d - 1; 0 < k; k--) { - g = r[this.rtl ? d - 1 - k : k] + g; - var h = this.score ? this.score(c, r, q, g, k) : Ua(x, m, q, d, k); - Va(this, z, g, h, a, b); + for (h = d - 1; 0 < h; h--) { + g = p[this.rtl ? d - 1 - h : h] + g; + var k = this.score ? this.score(c, p, t, g, h) : Wa(w, n, t, d, h); + Xa(this, D, g, k, a, b); } g = ""; } case "forward": if (1 < d) { - for (k = 0; k < d; k++) { - g += r[this.rtl ? d - 1 - k : k], Va(this, z, g, f, a, b); + for (h = 0; h < d; h++) { + g += p[this.rtl ? d - 1 - h : h], Xa(this, D, g, f, a, b); } break; } default: - if (Va(this, z, r, f, a, b), e && 1 < m && q < m - 1) { - for (d = L(), g = this.P, f = r, k = Math.min(e + 1, this.rtl ? q + 1 : m - q), d[f] = 1, h = 1; h < k; h++) { - if ((r = c[this.rtl ? m - 1 - q - h : q + h]) && !d[r]) { - d[r] = 1; - const w = this.score ? this.score(c, f, q, r, h - 1) : Ua(g + (m / 2 > g ? 0 : 1), m, q, k - 1, h - 1), l = this.bidirectional && r > f; - Va(this, n, l ? f : r, w, a, b, l ? r : f); + if (Xa(this, D, p, f, a, b), e && 1 < n && t < n - 1) { + for (d = M(), g = this.P, f = p, h = Math.min(e + 1, this.rtl ? t + 1 : n - t), d[f] = 1, k = 1; k < h; k++) { + if ((p = c[this.rtl ? n - 1 - t - k : t + k]) && !d[p]) { + d[p] = 1; + const x = this.score ? this.score(c, f, t, p, k - 1) : Wa(g + (n / 2 > g ? 0 : 1), n, t, h - 1, k - 1), m = this.bidirectional && p > f; + Xa(this, l, m ? f : p, x, a, b, m ? p : f); } } } @@ -1332,13 +1341,13 @@ Z.prototype.add = function(a, c, b, e) { } return this; }; -function Va(a, c, b, e, d, f, g) { - let k = g ? a.ctx : a.map, h; - if (!c[b] || g && !(h = c[b])[g]) { - g ? (c = h || (c[b] = L()), c[g] = 1, (h = k.get(g)) ? k = h : k.set(g, k = new Map())) : c[b] = 1, (h = k.get(b)) ? k = h : k.set(b, k = []), k = k[e] || (k[e] = []), f && k.includes(d) || (k.push(d), a.fastupdate && ((c = a.reg.get(d)) ? c.push(k) : a.reg.set(d, [k]))); +function Xa(a, c, b, e, d, f, g) { + let h = g ? a.ctx : a.map, k; + if (!c[b] || g && !(k = c[b])[g]) { + g ? (c = k || (c[b] = M()), c[g] = 1, (k = h.get(g)) ? h = k : h.set(g, h = new Map())) : c[b] = 1, (k = h.get(b)) ? h = k : h.set(b, h = []), h = h[e] || (h[e] = []), f && h.includes(d) || (h.push(d), a.fastupdate && ((c = a.reg.get(d)) ? c.push(h) : a.reg.set(d, [h]))); } } -function Ua(a, c, b, e, d) { +function Wa(a, c, b, e, d) { return b && 1 < a ? c + (e || 0) <= a ? b + (d || 0) : (a - 1) / (c + (e || 0)) * (b + (d || 0)) + 1 | 0 : 0; } ;Z.prototype.search = function(a, c, b) { @@ -1353,78 +1362,78 @@ function Ua(a, c, b, e, d) { d = b.offset || 0; var f = b.context; var g = b.suggest; - var k = !0; - var h = b.resolution; + var h = !0; + var k = b.resolution; } - "undefined" === typeof k && (k = !0); + "undefined" === typeof h && (h = !0); f = this.depth && !1 !== f; b = this.encoder.encode(a, !f); a = b.length; - c = c || (k ? 100 : 0); + c = c || (h ? 100 : 0); if (1 === a) { - return g = d, (d = Wa(this, b[0], "")) && d.length ? Ca.call(this, d, c, g) : []; + return g = d, (d = Ya(this, b[0], "")) && d.length ? Ca.call(this, d, c, g) : []; } if (2 === a && f && !g) { - return g = d, (d = Wa(this, b[1], b[0])) && d.length ? Ca.call(this, d, c, g) : []; + return g = d, (d = Ya(this, b[1], b[0])) && d.length ? Ca.call(this, d, c, g) : []; } - k = L(); - var m = 0; + h = M(); + var n = 0; if (f) { - var n = b[0]; - m = 1; + var l = b[0]; + n = 1; } - h || 0 === h || (h = n ? this.P : this.resolution); - for (let r, w; m < a; m++) { - if ((w = b[m]) && !k[w]) { - k[w] = 1; - r = Wa(this, w, n); + k || 0 === k || (k = l ? this.P : this.resolution); + for (let p, x; n < a; n++) { + if ((x = b[n]) && !h[x]) { + h[x] = 1; + p = Ya(this, x, l); a: { - f = r; - var z = e, x = g, q = h; - let l = []; + f = p; + var D = e, w = g, t = k; + let m = []; if (f && f.length) { - if (f.length <= q) { - z.push(f); - r = void 0; + if (f.length <= t) { + D.push(f); + p = void 0; break a; } - for (let u = 0, v; u < q; u++) { - if (v = f[u]) { - l[u] = v; + for (let v = 0, u; v < t; v++) { + if (u = f[v]) { + m[v] = u; } } - if (l.length) { - z.push(l); - r = void 0; + if (m.length) { + D.push(m); + p = void 0; break a; } } - r = x ? void 0 : l; + p = w ? void 0 : m; } - if (r) { - e = r; + if (p) { + e = p; break; } - n && (g && r && e.length || (n = w)); + l && (g && p && e.length || (l = x)); } - g && n && m === a - 1 && !e.length && (h = this.resolution, n = "", m = -1, k = L()); + g && l && n === a - 1 && !e.length && (k = this.resolution, l = "", n = -1, h = M()); } a: { b = e; e = b.length; - n = b; + l = b; if (1 < e) { b: { e = g; - n = b.length; + l = b.length; g = []; - a = L(); - for (let r = 0, w, l, u, v; r < h; r++) { - for (m = 0; m < n; m++) { - if (u = b[m], r < u.length && (w = u[r])) { - for (f = 0; f < w.length; f++) { - if (l = w[f], (k = a[l]) ? a[l]++ : (k = 0, a[l] = 1), v = g[k] || (g[k] = []), v.push(l), c && k === n - 1 && v.length - d === c) { - n = d ? v.slice(d) : v; + a = M(); + for (let p = 0, x, m, v, u; p < k; p++) { + for (n = 0; n < l; n++) { + if (v = b[n], p < v.length && (x = v[p])) { + for (f = 0; f < x.length; f++) { + if (m = x[f], (h = a[m]) ? a[m]++ : (h = 0, a[m] = 1), u = g[h] || (g[h] = []), u.push(m), c && h === l - 1 && u.length - d === c) { + l = d ? u.slice(d) : u; break b; } } @@ -1435,14 +1444,14 @@ function Ua(a, c, b, e, d) { if (e) { if (1 < g.length) { c: { - for (b = [], h = L(), e = g.length, k = e - 1; 0 <= k; k--) { - if (a = (e = g[k]) && e.length) { - for (m = 0; m < a; m++) { - if (n = e[m], !h[n]) { - if (h[n] = 1, d) { + for (b = [], k = M(), e = g.length, h = e - 1; 0 <= h; h--) { + if (a = (e = g[h]) && e.length) { + for (n = 0; n < a; n++) { + if (l = e[n], !k[l]) { + if (k[l] = 1, d) { d--; } else { - if (b.push(n), b.length === c) { + if (b.push(l), b.length === c) { break c; } } @@ -1452,12 +1461,12 @@ function Ua(a, c, b, e, d) { } } } else { - b = (g = g[0]).length > c || d ? g.slice(d, c + d) : g; + b = (g = g[0]) && c && g.length > c || d ? g.slice(d, c + d) : g; } g = b; } else { - if (b < n) { - n = []; + if (b < l) { + l = []; break b; } g = g[b - 1]; @@ -1468,17 +1477,17 @@ function Ua(a, c, b, e, d) { } } } - n = g; + l = g; } } else if (1 === e) { c = Ca.call(null, b[0], c, d); break a; } - c = n; + c = l; } return c; }; -function Wa(a, c, b) { +function Ya(a, c, b) { let e; b && (e = a.bidirectional && c > b) && (e = b, b = c, c = e); a = b ? (a = a.ctx.get(b)) && a.get(c) : a.map.get(c); @@ -1490,12 +1499,12 @@ function Wa(a, c, b) { } if (a) { var b = P(a) ? a : a.preset; - b && (Ta[b] || console.warn("Preset not found: " + b), a = Object.assign({}, Ta[b], a)); + b && (Va[b] || console.warn("Preset not found: " + b), a = Object.assign({}, Va[b], a)); } else { a = {}; } b = a.context; - const e = !0 === b ? {depth:1} : b || {}, d = P(a.encoder) ? Pa[a.encoder] : a.encode || a.encoder || {}; + const e = !0 === b ? {depth:1} : b || {}, d = P(a.encoder) ? sa[a.encoder] : a.encode || a.encoder || {}; this.encoder = d.encode ? d : "object" === typeof d ? new ka(d) : {encode:d}; this.resolution = a.resolution || 9; this.tokenize = b = (b = a.tokenize) && "default" !== b && "exact" !== b && b || "strict"; @@ -1510,42 +1519,42 @@ function Wa(a, c, b) { this.reg = c || (this.fastupdate ? new Map() : new Set()); this.P = e.resolution || 3; this.rtl = d.rtl || a.rtl || !1; - this.cache = (b = a.cache || null) && new Y(b); + this.cache = (b = a.cache || null) && new W(b); this.priority = a.priority || 4; } -B = Z.prototype; -B.clear = function() { +A = Z.prototype; +A.clear = function() { this.map.clear(); this.ctx.clear(); this.reg.clear(); this.cache && this.cache.clear(); return this; }; -B.append = function(a, c) { +A.append = function(a, c) { return this.add(a, c, !0); }; -B.contain = function(a) { +A.contain = function(a) { return this.reg.has(a); }; -B.update = function(a, c) { +A.update = function(a, c) { const b = this, e = this.remove(a); return e && e.then ? e.then(() => b.add(a, c)) : this.add(a, c); }; -B.cleanup = function() { +A.cleanup = function() { if (!this.fastupdate) { return console.info('Cleanup the index isn\'t required when not using "fastupdate".'), this; } - Sa(this.map); - this.depth && Sa(this.ctx); + Ua(this.map); + this.depth && Ua(this.ctx); return this; }; -B.searchCache = Ra; -B.export = function(a, c, b = 0, e = 0) { +A.searchCache = la; +A.export = function(a, c, b = 0, e = 0) { let d, f; switch(e) { case 0: d = "reg"; - f = wa(this.reg); + f = Qa(this.reg); break; case 1: d = "cfg"; @@ -1553,33 +1562,33 @@ B.export = function(a, c, b = 0, e = 0) { break; case 2: d = "map"; - f = qa(this.map, this.reg.size); + f = Ma(this.map, this.reg.size); break; case 3: d = "ctx"; - f = sa(this.ctx, this.reg.size); + f = Oa(this.ctx, this.reg.size); break; default: return; } - return ya.call(this, a, c, d, f, b, e); + return Sa.call(this, a, c, d, f, b, e); }; -B.import = function(a, c) { +A.import = function(a, c) { if (c) { switch("string" === typeof c && (c = JSON.parse(c)), a = a.split("."), "json" === a[a.length - 1] && a.pop(), 3 === a.length && a.shift(), a = 1 < a.length ? a[1] : a[0], a) { case "reg": this.fastupdate = !1; - this.reg = xa(c, this.reg); + this.reg = Ra(c, this.reg); break; case "map": - this.map = ra(c, this.map); + this.map = Na(c, this.map); break; case "ctx": - this.ctx = ta(c, this.ctx); + this.ctx = Pa(c, this.ctx); } } }; -B.serialize = function(a = !0) { +A.serialize = function(a = !0) { let c = "", b = "", e = ""; if (this.reg.size) { let f; @@ -1587,24 +1596,24 @@ B.serialize = function(a = !0) { f || (f = typeof d), c += (c ? "," : "") + ("string" === f ? '"' + d + '"' : d); } c = "index.reg=new Set([" + c + "]);"; - b = za(this.map, f); + b = Ta(this.map, f); b = "index.map=new Map([" + b + "]);"; for (const g of this.ctx.entries()) { d = g[0]; - let k = za(g[1], f); - k = "new Map([" + k + "])"; - k = '["' + d + '",' + k + "]"; - e += (e ? "," : "") + k; + let h = Ta(g[1], f); + h = "new Map([" + h + "])"; + h = '["' + d + '",' + h + "]"; + e += (e ? "," : "") + h; } e = "index.ctx=new Map([" + e + "]);"; } return a ? "function inject(index){" + c + b + e + "}" : c + b + e; }; -la(Z.prototype); -L(); -const Xa = {Index:Z, Charset:Pa, Encoder:ka, Document:X, Worker:null, Resolver:null, IndexedDB:null, Language:{}}, Ya = "undefined" !== typeof self ? self : "undefined" !== typeof global ? global : self; -let Za; -(Za = Ya.define) && Za.amd ? Za([], function() { - return Xa; -}) : "object" === typeof Ya.exports ? Ya.exports = Xa : Ya.FlexSearch = Xa; +va(Z.prototype); +M(); +const Za = {Index:Z, Charset:sa, Encoder:ka, Document:Y, Worker:null, Resolver:null, IndexedDB:null, Language:{}}, $a = "undefined" !== typeof self ? self : "undefined" !== typeof global ? global : self; +let ab; +(ab = $a.define) && ab.amd ? ab([], function() { + return Za; +}) : "object" === typeof $a.exports ? $a.exports = Za : $a.FlexSearch = Za; }(this||self)); diff --git a/dist/flexsearch.compact.min.js b/dist/flexsearch.compact.min.js index 2f7f2d1..9e84cd2 100644 --- a/dist/flexsearch.compact.min.js +++ b/dist/flexsearch.compact.min.js @@ -1,57 +1,57 @@ /**! - * FlexSearch.js v0.8.167 (Bundle) + * FlexSearch.js v0.8.200 (Bundle) * Author and Copyright: Thomas Wilkerling * Licence: Apache-2.0 * Hosted by Nextapps GmbH * https://github.com/nextapps-de/flexsearch */ -(function(self){'use strict';var A;function H(a,c,b){const e=typeof b,d=typeof a;if("undefined"!==e){if("undefined"!==d){if(b){if("function"===d&&e===d)return function(k){return a(b(k))};c=a.constructor;if(c===b.constructor){if(c===Array)return b.concat(a);if(c===Map){var f=new Map(b);for(var g of a)f.set(g[0],g[1]);return f}if(c===Set){g=new Set(b);for(f of a.values())g.add(f);return g}}}return a}return b}return"undefined"===d?c:a}function I(a,c){return"undefined"===typeof a?c:a}function N(){return Object.create(null)} +(function(self){'use strict';var A;function I(a,c,b){const e=typeof b,d=typeof a;if("undefined"!==e){if("undefined"!==d){if(b){if("function"===d&&e===d)return function(h){return a(b(h))};c=a.constructor;if(c===b.constructor){if(c===Array)return b.concat(a);if(c===Map){var f=new Map(b);for(var g of a)f.set(g[0],g[1]);return f}if(c===Set){g=new Set(b);for(f of a.values())g.add(f);return g}}}return a}return b}return"undefined"===d?c:a}function J(a,c){return"undefined"===typeof a?c:a}function N(){return Object.create(null)} function P(a){return"string"===typeof a}function R(a){return"object"===typeof a}function S(a,c){if(P(c))a=a[c];else for(let b=0;a&&b"a1a".split(b).length; -this.numeric=H(a.numeric,e)}else{try{this.split=H(this.split,aa)}catch(d){this.split=/\s+/}this.numeric=H(a.numeric,H(this.numeric,!0))}this.prepare=H(a.prepare,null,this.prepare);this.finalize=H(a.finalize,null,this.finalize);b=a.filter;this.filter="function"===typeof b?b:H(b&&new Set(b),null,this.filter);this.dedupe=H(a.dedupe,!0,this.dedupe);this.matcher=H((b=a.matcher)&&new Map(b),null,this.matcher);this.mapper=H((b=a.mapper)&&new Map(b),null,this.mapper);this.stemmer=H((b=a.stemmer)&&new Map(b), -null,this.stemmer);this.replacer=H(a.replacer,null,this.replacer);this.minlength=H(a.minlength,1,this.minlength);this.maxlength=H(a.maxlength,1024,this.maxlength);this.rtl=H(a.rtl,!1,this.rtl);if(this.cache=b=H(a.cache,!0,this.cache))this.H=null,this.O="number"===typeof b?b:2E5,this.F=new Map,this.G=new Map,this.L=this.K=128;this.h="";this.M=null;this.C="";this.N=null;if(this.matcher)for(const d of this.matcher.keys())this.h+=(this.h?"|":"")+d;if(this.stemmer)for(const d of this.stemmer.keys())this.C+= -(this.C?"|":"")+d;return this};A.addStemmer=function(a,c){this.stemmer||(this.stemmer=new Map);this.stemmer.set(a,c);this.C+=(this.C?"|":"")+a;this.N=null;this.cache&&V(this);return this};A.addFilter=function(a){"function"===typeof a?this.filter=a:(this.filter||(this.filter=new Set),this.filter.add(a));this.cache&&V(this);return this}; -A.addMapper=function(a,c){if("object"===typeof a)return this.addReplacer(a,c);if(1a.length&&(this.dedupe||this.mapper))return this.addMapper(a,c);this.matcher||(this.matcher=new Map);this.matcher.set(a,c);this.h+=(this.h?"|":"")+a;this.M=null;this.cache&&V(this);return this}; -A.addReplacer=function(a,c){if("string"===typeof a)return this.addMatcher(a,c);this.replacer||(this.replacer=[]);this.replacer.push(a,c);this.cache&&V(this);return this}; -A.encode=function(a,c){if(this.cache&&a.length<=this.K)if(this.H){if(this.F.has(a))return this.F.get(a)}else this.H=setTimeout(V,50,this);this.normalize&&("function"===typeof this.normalize?a=this.normalize(a):a=ha?a.normalize("NFKD").replace(ha,"").toLowerCase():a.toLowerCase());this.prepare&&(a=this.prepare(a));this.numeric&&3this.maxlength)){if(c){if(d[n])continue;d[n]=1}else{if(f===n)continue;f=n}if(b)e.push(n);else if(!this.filter||("function"===typeof this.filter?this.filter(n):!this.filter.has(n))){if(this.cache&&n.length<=this.L)if(this.H){var h=this.G.get(n);if(h||""===h){h&&e.push(h);continue}}else this.H=setTimeout(V,50,this);if(this.stemmer){this.N||(this.N=new RegExp("(?!^)("+ -this.C+")$"));let x;for(;x!==n&&2this.stemmer.get(q))}if(n&&(this.mapper||this.dedupe&&1this.matcher.get(x)));if(n&&this.replacer)for(h=0;n&&hthis.O&&(this.G.clear(),this.L=this.L/1.1|0));if(n){if(n!==B)if(c){if(d[n])continue;d[n]=1}else{if(g===n)continue;g=n}e.push(n)}}}this.finalize&&(e=this.finalize(e)||e);this.cache&&a.length<=this.K&&(this.F.set(a,e),this.F.size>this.O&&(this.F.clear(),this.K=this.K/1.1|0));return e};function V(a){a.H=null;a.F.clear();a.G.clear()};function la(a){W.call(a,"add");W.call(a,"append");W.call(a,"search");W.call(a,"update");W.call(a,"remove");W.call(a,"searchCache")}let ma,na,oa;function pa(){ma=oa=0} -function W(a){this[a+"Async"]=function(){const c=arguments;var b=c[c.length-1];let e;"function"===typeof b&&(e=b,delete c[c.length-1]);ma?oa||(oa=Date.now()-na>=this.priority*this.priority*3):(ma=setTimeout(pa,0),na=Date.now());if(oa){const f=this;return new Promise(g=>{setTimeout(function(){g(f[a+"Async"].apply(f,c))},0)})}const d=this[a].apply(this,c);b=d.then?d:new Promise(f=>f(d));e&&b.then(e);return b}};function qa(a,c=0){let b=[],e=[];c&&(c=25E4/c*5E3|0);for(const d of a.entries())e.push(d),e.length===c&&(b.push(e),e=[]);e.length&&b.push(e);return b}function ra(a,c){c||(c=new Map);for(let b=0,e;bc?c?a.slice(b,b+c):a.slice(b):a;let e=[];for(let d=0,f,g;d=g){b-=g;continue}bc&&(f=f.slice(0,c),g=c);if(!e.length&&g>=c)return f;e.push(f);c-=g;if(!c)break}return e=1E&&(E=l.length+(l?1:0)),t=l.length+(l?1:0)+K.length,C+=D,ja.push(v.length),v.push({match:K})),l+=(l?" ":"")+K)}if(!p)z=u[y],l+=(l?" ":"")+z,h&&v.push({text:z});else if(h&&C>=h)break}C=ja.length*(f.length-2);if(q||r||h&&l.length-C>h)if(C=h+C-2*x,y=t-E,0u&&(u=0)),v=v.length-1){if(F>=v.length){y[p+1]= -1;F>=u.length&&(C[p+1]=1);continue}D-=x}l=v[F].text;if(G=r&&J[p])if(0G)if(y[p+1]=1,m)l=l.substring(0,G);else continue;(G-=l.length)||(G=-1);J[p]=G}else{y[p+1]=1;continue}if(D+l.length+1<=h)l=" "+l,t[p]+=l;else if(m)O=h-D-1,0=F){if(0>F){y[p]=1;C[p]=1;continue}D-=x}l=v[F].text;if(G=q&&z[p])if(0G)if(y[p]=1,m)l=l.substring(l.length- -G);else continue;(G-=l.length)||(G=-1);z[p]=G}else{y[p]=1;continue}if(D+l.length+1<=h)l+=" ",t[p]=l+t[p];else if(m)O=l.length+1-(h-D),0<=O&&O=u.length-1?va=1:Fb||e)a=a.slice(e,e+b);d&&(a=Ga.call(this,a))}return a}function Ga(a){if(!this||!this.store)return a;const c=Array(a.length);for(let b=0,e;b"a1a".split(b).length; +this.numeric=I(a.numeric,e)}else{try{this.split=I(this.split,aa)}catch(d){this.split=/\s+/}this.numeric=I(a.numeric,I(this.numeric,!0))}this.prepare=I(a.prepare,null,this.prepare);this.finalize=I(a.finalize,null,this.finalize);b=a.filter;this.filter="function"===typeof b?b:I(b&&new Set(b),null,this.filter);this.dedupe=I(a.dedupe,!0,this.dedupe);this.matcher=I((b=a.matcher)&&new Map(b),null,this.matcher);this.mapper=I((b=a.mapper)&&new Map(b),null,this.mapper);this.stemmer=I((b=a.stemmer)&&new Map(b), +null,this.stemmer);this.replacer=I(a.replacer,null,this.replacer);this.minlength=I(a.minlength,1,this.minlength);this.maxlength=I(a.maxlength,1024,this.maxlength);this.rtl=I(a.rtl,!1,this.rtl);if(this.cache=b=I(a.cache,!0,this.cache))this.H=null,this.O="number"===typeof b?b:2E5,this.F=new Map,this.G=new Map,this.L=this.K=128;this.h="";this.M=null;this.C="";this.N=null;if(this.matcher)for(const d of this.matcher.keys())this.h+=(this.h?"|":"")+d;if(this.stemmer)for(const d of this.stemmer.keys())this.C+= +(this.C?"|":"")+d;return this};A.addStemmer=function(a,c){this.stemmer||(this.stemmer=new Map);this.stemmer.set(a,c);this.C+=(this.C?"|":"")+a;this.N=null;this.cache&&T(this);return this};A.addFilter=function(a){"function"===typeof a?this.filter=a:(this.filter||(this.filter=new Set),this.filter.add(a));this.cache&&T(this);return this}; +A.addMapper=function(a,c){if("object"===typeof a)return this.addReplacer(a,c);if(1a.length&&(this.dedupe||this.mapper))return this.addMapper(a,c);this.matcher||(this.matcher=new Map);this.matcher.set(a,c);this.h+=(this.h?"|":"")+a;this.M=null;this.cache&&T(this);return this}; +A.addReplacer=function(a,c){if("string"===typeof a)return this.addMatcher(a,c);this.replacer||(this.replacer=[]);this.replacer.push(a,c);this.cache&&T(this);return this}; +A.encode=function(a,c){if(this.cache&&a.length<=this.K)if(this.H){if(this.F.has(a))return this.F.get(a)}else this.H=setTimeout(T,50,this);this.normalize&&("function"===typeof this.normalize?a=this.normalize(a):a=ha?a.normalize("NFKD").replace(ha,"").toLowerCase():a.toLowerCase());this.prepare&&(a=this.prepare(a));this.numeric&&3this.maxlength)){if(c){if(d[l])continue;d[l]=1}else{if(f===l)continue;f=l}if(b)e.push(l);else if(!this.filter||("function"===typeof this.filter?this.filter(l):!this.filter.has(l))){if(this.cache&&l.length<=this.L)if(this.H){var k=this.G.get(l);if(k||""===k){k&&e.push(k);continue}}else this.H=setTimeout(T,50,this);if(this.stemmer){this.N||(this.N=new RegExp("(?!^)("+ +this.C+")$"));let w;for(;w!==l&&2this.stemmer.get(t))}if(l&&(this.mapper||this.dedupe&&1this.matcher.get(w)));if(l&&this.replacer)for(k=0;l&&kthis.O&&(this.G.clear(),this.L=this.L/1.1|0));if(l){if(l!==C)if(c){if(d[l])continue;d[l]=1}else{if(g===l)continue;g=l}e.push(l)}}}this.finalize&&(e=this.finalize(e)||e);this.cache&&a.length<=this.K&&(this.F.set(a,e),this.F.size>this.O&&(this.F.clear(),this.K=this.K/1.1|0));return e};function T(a){a.H=null;a.F.clear();a.G.clear()};function la(a,c,b){b||(c||"object"!==typeof a?"object"===typeof c&&(b=c,c=0):b=a);b&&(a=b.query||a,c=b.limit||c);let e=""+(c||0);b&&(e+=(b.offset||0)+!!b.context+!!b.suggest+(!1!==b.resolve)+(b.resolution||this.resolution)+(b.boost||0));a=(""+a).toLowerCase();this.cache||(this.cache=new W);let d=this.cache.get(a+e);if(!d){const f=b&&b.cache;f&&(b.cache=!1);d=this.search(a,c,b);f&&(b.cache=f);this.cache.set(a+e,d)}return d}function W(a){this.limit=a&&!0!==a?a:1E3;this.cache=new Map;this.h=""} +W.prototype.set=function(a,c){this.cache.set(this.h=a,c);this.cache.size>this.limit&&this.cache.delete(this.cache.keys().next().value)};W.prototype.get=function(a){const c=this.cache.get(a);c&&this.h!==a&&(this.cache.delete(a),this.cache.set(this.h=a,c));return c};W.prototype.remove=function(a){for(const c of this.cache){const b=c[0];c[1].includes(a)&&this.cache.delete(b)}};W.prototype.clear=function(){this.cache.clear();this.h=""};const ma={normalize:!1,numeric:!1,dedupe:!1};const na={};const oa=new Map([["b","p"],["v","f"],["w","f"],["z","s"],["x","s"],["d","t"],["n","m"],["c","k"],["g","k"],["j","k"],["q","k"],["i","e"],["y","e"],["u","o"]]);const pa=new Map([["ae","a"],["oe","o"],["sh","s"],["kh","k"],["th","t"],["ph","f"],["pf","f"]]),qa=[/([^aeo])h(.)/g,"$1$2",/([aeo])h([^aeo]|$)/g,"$1$2",/(.)\1+/g,"$1"];const ra={a:"",e:"",i:"",o:"",u:"",y:"",b:1,f:1,p:1,v:1,c:2,g:2,j:2,k:2,q:2,s:2,x:2,z:2,"\u00df":2,d:3,t:3,l:4,m:5,n:5,r:6};var sa={Exact:ma,Default:na,Normalize:na,LatinBalance:{mapper:oa},LatinAdvanced:{mapper:oa,matcher:pa,replacer:qa},LatinExtra:{mapper:oa,replacer:qa.concat([/(?!^)[aeo]/g,""]),matcher:pa},LatinSoundex:{dedupe:!1,include:{letter:!0},finalize:function(a){for(let b=0;b=this.priority*this.priority*3):(wa=setTimeout(za,0),xa=Date.now());if(ya){const f=this;return new Promise(g=>{setTimeout(function(){g(f[a+"Async"].apply(f,c))},0)})}const d=this[a].apply(this,c);b=d.then?d:new Promise(f=>f(d));e&&b.then(e);return b}};Y.prototype.add=function(a,c,b){R(a)&&(c=a,a=S(c,this.key));if(c&&(a||0===a)){if(!b&&this.reg.has(a))return this.update(a,c);for(let h=0,k;hc?a.slice(b,b+c):a;let e=[];for(let d=0,f,g;d=g){b-=g;continue}f=f.slice(b,b+c);g=f.length;b=0}g>c&&(f=f.slice(0,c),g=c);if(!e.length&&g>=c)return f;e.push(f);c-=g;if(!c)break}return e=1E&&(E=m.length+(m?1:0)),z=m.length+(m?1:0)+L.length,r+=F,ja.push(u.length),u.push({match:L})),m+=(m?" ":"")+L)}if(!q)D=v[y],m+=(m?" ":"")+D,k&&u.push({text:D});else if(k&&r>=k)break}r=ja.length*(f.length-2);if(t||p||k&&m.length-r>k)if(r=k+r-2*w,y=z-E,0v&&(v=0)),u=u.length-1){if(G>=u.length){y[q+1]= +1;G>=v.length&&(r[q+1]=1);continue}F-=w}m=u[G].text;if(H=p&&B[q])if(0H)if(y[q+1]=1,n)m=m.substring(0,H);else continue;(H-=m.length)||(H=-1);B[q]=H}else{y[q+1]=1;continue}if(F+m.length+1<=k)m=" "+m,z[q]+=m;else if(n)O=k-F-1,0=G){if(0>G){y[q]=1;r[q]=1;continue}F-=w}m=u[G].text;if(H=t&&D[q])if(0H)if(y[q]=1,n)m=m.substring(m.length- +H);else continue;(H-=m.length)||(H=-1);D[q]=H}else{y[q]=1;continue}if(F+m.length+1<=k)m+=" ",z[q]=m+z[q];else if(n)O=m.length+1-(k-F),0<=O&&O=v.length-1?ua=1:Gb||e)a=a.slice(e,e+b);d&&(a=Ga.call(this,a))}return a}function Ga(a){if(!this||!this.store)return a;const c=Array(a.length);for(let b=0,e;bthis.limit&&this.cache.delete(this.cache.keys().next().value)}; -Y.prototype.get=function(a){const c=this.cache.get(a);c&&this.h!==a&&(this.cache.delete(a),this.cache.set(this.h=a,c));return c};Y.prototype.remove=function(a){for(const c of this.cache){const b=c[0];c[1].includes(a)&&this.cache.delete(b)}};Y.prototype.clear=function(){this.cache.clear();this.h=""};Z.prototype.remove=function(a,c){const b=this.reg.size&&(this.fastupdate?this.reg.get(a):this.reg.has(a));if(b){if(this.fastupdate)for(let e=0,d,f;ew;f--){g=r.substring(w,f);l=this.rtl?d-1-w:w;var k=this.score?this.score(c,r,q,g,l):Ua(x, -m,q,d,l);Va(this,B,g,k,a,b)}break}case "bidirectional":case "reverse":if(1g?0:1),m,q,k-1,h-1),l=this.bidirectional&&r>f;Va(this,n,l?f:r,w,a,b,l?r:f)}}}}this.fastupdate||this.reg.add(a)}}return this};function Va(a,c,b,e,d,f,g){let k=g?a.ctx:a.map,h;if(!c[b]||g&&!(h=c[b])[g])g?(c=h||(c[b]=N()),c[g]=1,(h=k.get(g))?k=h:k.set(g,k=new Map)):c[b]=1,(h=k.get(b))?k=h:k.set(b,k=[]),k=k[e]||(k[e]=[]),f&&k.includes(d)||(k.push(d),a.fastupdate&&((c=a.reg.get(d))?c.push(k):a.reg.set(d,[k])))} -function Ua(a,c,b,e,d){return b&&1c||d?g.slice(d,c+d):g;g=b}else{if(b< -n){n=[];break b}g=g[b-1];if(c||d)if(g.length>c||d)g=g.slice(d,c+d)}n=g}else if(1===e){c=Ca.call(null,b[0],c,d);break a}c=n}return c};function Wa(a,c,b){let e;b&&(e=a.bidirectional&&c>b)&&(e=b,b=c,c=e);a=b?(a=a.ctx.get(b))&&a.get(c):a.map.get(c);return a};function Z(a,c){if(!this||this.constructor!==Z)return new Z(a);if(a){var b=P(a)?a:a.preset;b&&(a=Object.assign({},Ta[b],a))}else a={};b=a.context;const e=!0===b?{depth:1}:b||{},d=P(a.encoder)?Pa[a.encoder]:a.encode||a.encoder||{};this.encoder=d.encode?d:"object"===typeof d?new ka(d):{encode:d};this.resolution=a.resolution||9;this.tokenize=b=(b=a.tokenize)&&"default"!==b&&"exact"!==b&&b||"strict";this.depth="strict"===b&&e.depth||0;this.bidirectional=!1!==e.bidirectional;this.fastupdate=!!a.fastupdate; -this.score=a.score||null;b=!1;this.map=new Map;this.ctx=new Map;this.reg=c||(this.fastupdate?new Map:new Set);this.P=e.resolution||3;this.rtl=d.rtl||a.rtl||!1;this.cache=(b=a.cache||null)&&new Y(b);this.priority=a.priority||4}A=Z.prototype;A.clear=function(){this.map.clear();this.ctx.clear();this.reg.clear();this.cache&&this.cache.clear();return this};A.append=function(a,c){return this.add(a,c,!0)};A.contain=function(a){return this.reg.has(a)}; -A.update=function(a,c){const b=this,e=this.remove(a);return e&&e.then?e.then(()=>b.add(a,c)):this.add(a,c)};A.cleanup=function(){if(!this.fastupdate)return this;Sa(this.map);this.depth&&Sa(this.ctx);return this};A.searchCache=Ra;A.export=function(a,c,b=0,e=0){let d,f;switch(e){case 0:d="reg";f=wa(this.reg);break;case 1:d="cfg";f=null;break;case 2:d="map";f=qa(this.map,this.reg.size);break;case 3:d="ctx";f=sa(this.ctx,this.reg.size);break;default:return}return ya.call(this,a,c,d,f,b,e)}; -A.import=function(a,c){if(c)switch("string"===typeof c&&(c=JSON.parse(c)),a=a.split("."),"json"===a[a.length-1]&&a.pop(),3===a.length&&a.shift(),a=1x;f--){g=p.substring(x,f);m=this.rtl?d-1-x:x;var h=this.score?this.score(c,p,t,g,m):Wa(w, +n,t,d,m);Xa(this,C,g,h,a,b)}break}case "bidirectional":case "reverse":if(1g?0:1),n,t,h-1,k-1),m=this.bidirectional&&p>f;Xa(this,l,m?f:p,x,a,b,m?p:f)}}}}this.fastupdate||this.reg.add(a)}}return this};function Xa(a,c,b,e,d,f,g){let h=g?a.ctx:a.map,k;if(!c[b]||g&&!(k=c[b])[g])g?(c=k||(c[b]=N()),c[g]=1,(k=h.get(g))?h=k:h.set(g,h=new Map)):c[b]=1,(k=h.get(b))?h=k:h.set(b,h=[]),h=h[e]||(h[e]=[]),f&&h.includes(d)||(h.push(d),a.fastupdate&&((c=a.reg.get(d))?c.push(h):a.reg.set(d,[h])))} +function Wa(a,c,b,e,d){return b&&1c||d?g.slice(d,c+d):g;g=b}else{if(b< +l){l=[];break b}g=g[b-1];if(c||d)if(g.length>c||d)g=g.slice(d,c+d)}l=g}else if(1===e){c=Ca.call(null,b[0],c,d);break a}c=l}return c};function Ya(a,c,b){let e;b&&(e=a.bidirectional&&c>b)&&(e=b,b=c,c=e);a=b?(a=a.ctx.get(b))&&a.get(c):a.map.get(c);return a};function Z(a,c){if(!this||this.constructor!==Z)return new Z(a);if(a){var b=P(a)?a:a.preset;b&&(a=Object.assign({},Va[b],a))}else a={};b=a.context;const e=!0===b?{depth:1}:b||{},d=P(a.encoder)?sa[a.encoder]:a.encode||a.encoder||{};this.encoder=d.encode?d:"object"===typeof d?new ka(d):{encode:d};this.resolution=a.resolution||9;this.tokenize=b=(b=a.tokenize)&&"default"!==b&&"exact"!==b&&b||"strict";this.depth="strict"===b&&e.depth||0;this.bidirectional=!1!==e.bidirectional;this.fastupdate=!!a.fastupdate; +this.score=a.score||null;b=!1;this.map=new Map;this.ctx=new Map;this.reg=c||(this.fastupdate?new Map:new Set);this.P=e.resolution||3;this.rtl=d.rtl||a.rtl||!1;this.cache=(b=a.cache||null)&&new W(b);this.priority=a.priority||4}A=Z.prototype;A.clear=function(){this.map.clear();this.ctx.clear();this.reg.clear();this.cache&&this.cache.clear();return this};A.append=function(a,c){return this.add(a,c,!0)};A.contain=function(a){return this.reg.has(a)}; +A.update=function(a,c){const b=this,e=this.remove(a);return e&&e.then?e.then(()=>b.add(a,c)):this.add(a,c)};A.cleanup=function(){if(!this.fastupdate)return this;Ua(this.map);this.depth&&Ua(this.ctx);return this};A.searchCache=la;A.export=function(a,c,b=0,e=0){let d,f;switch(e){case 0:d="reg";f=Qa(this.reg);break;case 1:d="cfg";f=null;break;case 2:d="map";f=Ma(this.map,this.reg.size);break;case 3:d="ctx";f=Oa(this.ctx,this.reg.size);break;default:return}return Sa.call(this,a,c,d,f,b,e)}; +A.import=function(a,c){if(c)switch("string"===typeof c&&(c=JSON.parse(c)),a=a.split("."),"json"===a[a.length-1]&&a.pop(),3===a.length&&a.shift(),a=1 this.maxlength)) { + let e = [], d = M(), f, g, h = this.split || "" === this.split ? a.split(this.split) : [a]; + for (let n = 0, l, D; n < h.length; n++) { + if ((l = D = h[n]) && !(l.length < this.minlength || l.length > this.maxlength)) { if (c) { - if (d[n]) { + if (d[l]) { continue; } - d[n] = 1; + d[l] = 1; } else { - if (f === n) { + if (f === l) { continue; } - f = n; + f = l; } if (b) { - e.push(n); + e.push(l); } else { - if (!this.filter || ("function" === typeof this.filter ? this.filter(n) : !this.filter.has(n))) { - if (this.cache && n.length <= this.L) { + if (!this.filter || ("function" === typeof this.filter ? this.filter(l) : !this.filter.has(l))) { + if (this.cache && l.length <= this.L) { if (this.H) { - var h = this.G.get(n); - if (h || "" === h) { - h && e.push(h); + var k = this.G.get(l); + if (k || "" === k) { + k && e.push(k); continue; } } else { - this.H = setTimeout(V, 50, this); + this.H = setTimeout(T, 50, this); } } if (this.stemmer) { this.N || (this.N = new RegExp("(?!^)(" + this.C + ")$")); - let x; - for (; x !== n && 2 < n.length;) { - x = n, n = n.replace(this.N, q => this.stemmer.get(q)); + let w; + for (; w !== l && 2 < l.length;) { + w = l, l = l.replace(this.N, t => this.stemmer.get(t)); } } - if (n && (this.mapper || this.dedupe && 1 < n.length)) { - h = ""; - for (let x = 0, q = "", r, w; x < n.length; x++) { - r = n.charAt(x), r === q && this.dedupe || ((w = this.mapper && this.mapper.get(r)) || "" === w ? w === q && this.dedupe || !(q = w) || (h += w) : h += q = r); + if (l && (this.mapper || this.dedupe && 1 < l.length)) { + k = ""; + for (let w = 0, t = "", p, x; w < l.length; w++) { + p = l.charAt(w), p === t && this.dedupe || ((x = this.mapper && this.mapper.get(p)) || "" === x ? x === t && this.dedupe || !(t = x) || (k += x) : k += t = p); } - n = h; + l = k; } - this.matcher && 1 < n.length && (this.M || (this.M = new RegExp("(" + this.h + ")", "g")), n = n.replace(this.M, x => this.matcher.get(x))); - if (n && this.replacer) { - for (h = 0; n && h < this.replacer.length; h += 2) { - n = n.replace(this.replacer[h], this.replacer[h + 1]); + this.matcher && 1 < l.length && (this.M || (this.M = new RegExp("(" + this.h + ")", "g")), l = l.replace(this.M, w => this.matcher.get(w))); + if (l && this.replacer) { + for (k = 0; l && k < this.replacer.length; k += 2) { + l = l.replace(this.replacer[k], this.replacer[k + 1]); } } - this.cache && z.length <= this.L && (this.G.set(z, n), this.G.size > this.O && (this.G.clear(), this.L = this.L / 1.1 | 0)); - if (n) { - if (n !== z) { + this.cache && D.length <= this.L && (this.G.set(D, l), this.G.size > this.O && (this.G.clear(), this.L = this.L / 1.1 | 0)); + if (l) { + if (l !== D) { if (c) { - if (d[n]) { + if (d[l]) { continue; } - d[n] = 1; + d[l] = 1; } else { - if (g === n) { + if (g === l) { continue; } - g = n; + g = l; } } - e.push(n); + e.push(l); } } } @@ -279,31 +279,86 @@ B.encode = function(a, c) { this.cache && a.length <= this.K && (this.F.set(a, e), this.F.size > this.O && (this.F.clear(), this.K = this.K / 1.1 | 0)); return e; }; -function V(a) { +function T(a) { a.H = null; a.F.clear(); a.G.clear(); } -;function la(a) { - W.call(a, "add"); - W.call(a, "append"); - W.call(a, "search"); - W.call(a, "update"); - W.call(a, "remove"); - W.call(a, "searchCache"); -} -let ma, na, oa; -function pa() { - ma = oa = 0; +;function la(a, c, b) { + b || (c || "object" !== typeof a ? "object" === typeof c && (b = c, c = 0) : b = a); + b && (a = b.query || a, c = b.limit || c); + let e = "" + (c || 0); + b && (e += (b.offset || 0) + !!b.context + !!b.suggest + (!1 !== b.resolve) + (b.resolution || this.resolution) + (b.boost || 0)); + a = ("" + a).toLowerCase(); + this.cache || (this.cache = new W()); + let d = this.cache.get(a + e); + if (!d) { + const f = b && b.cache; + f && (b.cache = !1); + d = this.search(a, c, b); + f && (b.cache = f); + this.cache.set(a + e, d); + } + return d; } function W(a) { + this.limit = a && !0 !== a ? a : 1000; + this.cache = new Map(); + this.h = ""; +} +W.prototype.set = function(a, c) { + this.cache.set(this.h = a, c); + this.cache.size > this.limit && this.cache.delete(this.cache.keys().next().value); +}; +W.prototype.get = function(a) { + const c = this.cache.get(a); + c && this.h !== a && (this.cache.delete(a), this.cache.set(this.h = a, c)); + return c; +}; +W.prototype.remove = function(a) { + for (const c of this.cache) { + const b = c[0]; + c[1].includes(a) && this.cache.delete(b); + } +}; +W.prototype.clear = function() { + this.cache.clear(); + this.h = ""; +}; +const ma = {normalize:!1, numeric:!1, dedupe:!1}; +const na = {}; +const oa = new Map([["b", "p"], ["v", "f"], ["w", "f"], ["z", "s"], ["x", "s"], ["d", "t"], ["n", "m"], ["c", "k"], ["g", "k"], ["j", "k"], ["q", "k"], ["i", "e"], ["y", "e"], ["u", "o"]]); +const pa = new Map([["ae", "a"], ["oe", "o"], ["sh", "s"], ["kh", "k"], ["th", "t"], ["ph", "f"], ["pf", "f"]]), qa = [/([^aeo])h(.)/g, "$1$2", /([aeo])h([^aeo]|$)/g, "$1$2", /(.)\1+/g, "$1"]; +const ra = {a:"", e:"", i:"", o:"", u:"", y:"", b:1, f:1, p:1, v:1, c:2, g:2, j:2, k:2, q:2, s:2, x:2, z:2, "\u00df":2, d:3, t:3, l:4, m:5, n:5, r:6}; +var ua = {Exact:ma, Default:na, Normalize:na, LatinBalance:{mapper:oa}, LatinAdvanced:{mapper:oa, matcher:pa, replacer:qa}, LatinExtra:{mapper:oa, replacer:qa.concat([/(?!^)[aeo]/g, ""]), matcher:pa}, LatinSoundex:{dedupe:!1, include:{letter:!0}, finalize:function(a) { + for (let b = 0; b < a.length; b++) { + var c = a[b]; + let e = c.charAt(0), d = ra[e]; + for (let f = 1, g; f < c.length && (g = c.charAt(f), "h" === g || "w" === g || !(g = ra[g]) || g === d || (e += g, d = g, 4 !== e.length)); f++) { + } + a[b] = e; + } +}}, CJK:{split:""}, LatinExact:ma, LatinDefault:na, LatinSimple:na}; +function va(a) { + X.call(a, "add"); + X.call(a, "append"); + X.call(a, "search"); + X.call(a, "update"); + X.call(a, "remove"); + X.call(a, "searchCache"); +} +let wa, xa, ya; +function za() { + wa = ya = 0; +} +function X(a) { this[a + "Async"] = function() { const c = arguments; var b = c[c.length - 1]; let e; "function" === typeof b && (e = b, delete c[c.length - 1]); - ma ? oa || (oa = Date.now() - na >= this.priority * this.priority * 3) : (ma = setTimeout(pa, 0), na = Date.now()); - if (oa) { + wa ? ya || (ya = Date.now() - xa >= this.priority * this.priority * 3) : (wa = setTimeout(za, 0), xa = Date.now()); + if (ya) { const f = this; return new Promise(g => { setTimeout(function() { @@ -317,102 +372,21 @@ function W(a) { return b; }; } -;function qa(a, c = 0) { - let b = [], e = []; - c && (c = 250000 / c * 5000 | 0); - for (const d of a.entries()) { - e.push(d), e.length === c && (b.push(e), e = []); - } - e.length && b.push(e); - return b; -} -function ra(a, c) { - c || (c = new Map()); - for (let b = 0, e; b < a.length; b++) { - e = a[b], c.set(e[0], e[1]); - } - return c; -} -function sa(a, c = 0) { - let b = [], e = []; - c && (c = 250000 / c * 1000 | 0); - for (const d of a.entries()) { - e.push([d[0], qa(d[1])[0]]), e.length === c && (b.push(e), e = []); - } - e.length && b.push(e); - return b; -} -function va(a, c) { - c || (c = new Map()); - for (let b = 0, e, d; b < a.length; b++) { - e = a[b], d = c.get(e[0]), c.set(e[0], ra(e[1], d)); - } - return c; -} -function wa(a) { - let c = [], b = []; - for (const e of a.keys()) { - b.push(e), 250000 === b.length && (c.push(b), b = []); - } - b.length && c.push(b); - return c; -} -function xa(a, c) { - c || (c = new Set()); - for (let b = 0; b < a.length; b++) { - c.add(a[b]); - } - return c; -} -function ya(a, c, b, e, d, f, g = 0) { - const k = e && e.constructor === Array; - var h = k ? e.shift() : e; - if (!h) { - return this.export(a, c, d, f + 1); - } - if ((h = a((c ? c + "." : "") + (g + 1) + "." + b, JSON.stringify(h))) && h.then) { - const m = this; - return h.then(function() { - return ya.call(m, a, c, b, k ? e : null, d, f, g + 1); - }); - } - return ya.call(this, a, c, b, k ? e : null, d, f, g + 1); -} -function za(a, c) { - let b = ""; - for (const e of a.entries()) { - a = e[0]; - const d = e[1]; - let f = ""; - for (let g = 0, k; g < d.length; g++) { - k = d[g] || [""]; - let h = ""; - for (let m = 0; m < k.length; m++) { - h += (h ? "," : "") + ("string" === c ? '"' + k[m] + '"' : k[m]); - } - h = "[" + h + "]"; - f += (f ? "," : "") + h; - } - f = '["' + a + '",[' + f + "]]"; - b += (b ? "," : "") + f; - } - return b; -} -;X.prototype.add = function(a, c, b) { +;Y.prototype.add = function(a, c, b) { R(a) && (c = a, a = S(c, this.key)); if (c && (a || 0 === a)) { if (!b && this.reg.has(a)) { return this.update(a, c); } - for (let k = 0, h; k < this.field.length; k++) { - h = this.D[k]; - var e = this.index.get(this.field[k]); - if ("function" === typeof h) { - var d = h(c); + for (let h = 0, k; h < this.field.length; h++) { + k = this.D[h]; + var e = this.index.get(this.field[h]); + if ("function" === typeof k) { + var d = k(c); d && e.add(a, d, !1, !0); } else { - if (d = h.I, !d || d(c)) { - h.constructor === String ? h = ["" + h] : P(h) && (h = [h]), Aa(c, h, this.J, 0, e, a, h[0], b); + if (d = k.I, !d || d(c)) { + k.constructor === String ? k = ["" + k] : P(k) && (k = [k]), Aa(c, k, this.J, 0, e, a, k[0], b); } } } @@ -420,14 +394,14 @@ function za(a, c) { for (e = 0; e < this.B.length; e++) { var f = this.B[e], g = this.R[e]; d = this.tag.get(g); - let k = L(); + let h = M(); if ("function" === typeof f) { if (f = f(c), !f) { continue; } } else { - const h = f.I; - if (h && !h(c)) { + const k = f.I; + if (k && !k(c)) { continue; } f.constructor === String && (f = "" + f); @@ -435,8 +409,8 @@ function za(a, c) { } if (d && f) { P(f) && (f = [f]); - for (let h = 0, m, n; h < f.length; h++) { - m = f[h], k[m] || (k[m] = 1, (g = d.get(m)) ? n = g : d.set(m, n = []), b && n.includes(a) || (n.push(a), this.fastupdate && ((g = this.reg.get(a)) ? g.push(n) : this.reg.set(a, [n])))); + for (let k = 0, n, l; k < f.length; k++) { + n = f[k], h[n] || (h[n] = 1, (g = d.get(n)) ? l = g : d.set(n, l = []), b && l.includes(a) || (l.push(a), this.fastupdate && ((g = this.reg.get(a)) ? g.push(l) : this.reg.set(a, [l])))); } } else { d || console.warn("Tag '" + g + "' was not found"); @@ -444,29 +418,29 @@ function za(a, c) { } } if (this.store && (!b || !this.store.has(a))) { - let k; + let h; if (this.A) { - k = L(); - for (let h = 0, m; h < this.A.length; h++) { - m = this.A[h]; - if ((b = m.I) && !b(c)) { + h = M(); + for (let k = 0, n; k < this.A.length; k++) { + n = this.A[k]; + if ((b = n.I) && !b(c)) { continue; } - let n; - if ("function" === typeof m) { - n = m(c); - if (!n) { + let l; + if ("function" === typeof n) { + l = n(c); + if (!l) { continue; } - m = [m.S]; - } else if (P(m) || m.constructor === String) { - k[m] = c[m]; + n = [n.S]; + } else if (P(n) || n.constructor === String) { + h[n] = c[n]; continue; } - Ba(c, k, m, 0, m[0], n); + Ba(c, h, n, 0, n[0], l); } } - this.store.set(a, k || c); + this.store.set(a, h || c); } } return this; @@ -481,11 +455,11 @@ function Ba(a, c, b, e, d, f) { Ba(a, c, b, e, d); } } else { - c = c[d] || (c[d] = L()), d = b[++e], Ba(a, c, b, e, d); + c = c[d] || (c[d] = M()), d = b[++e], Ba(a, c, b, e, d); } } } -function Aa(a, c, b, e, d, f, g, k) { +function Aa(a, c, b, e, d, f, g, h) { if (a = a[g]) { if (e === c.length - 1) { if (a.constructor === Array) { @@ -497,14 +471,14 @@ function Aa(a, c, b, e, d, f, g, k) { } a = a.join(" "); } - d.add(f, a, k, !0); + d.add(f, a, h, !0); } else { if (a.constructor === Array) { for (g = 0; g < a.length; g++) { - Aa(a, c, b, e, d, f, g, k); + Aa(a, c, b, e, d, f, g, h); } } else { - g = c[++e], Aa(a, c, b, e, d, f, g, k); + g = c[++e], Aa(a, c, b, e, d, f, g, h); } } } @@ -514,7 +488,7 @@ function Aa(a, c, b, e, d, f, g, k) { return a; } if (1 === a.length) { - return a = a[0], a = b || a.length > c ? c ? a.slice(b, b + c) : a.slice(b) : a; + return a = a[0], a = b || a.length > c ? a.slice(b, b + c) : a; } let e = []; for (let d = 0, f, g; d < a.length; d++) { @@ -524,7 +498,9 @@ function Aa(a, c, b, e, d, f, g, k) { b -= g; continue; } - b < g && (f = c ? f.slice(b, b + c) : f.slice(b), g = f.length, b = 0); + f = f.slice(b, b + c); + g = f.length; + b = 0; } g > c && (f = f.slice(0, c), g = c); if (!e.length && g >= c) { @@ -539,241 +515,8 @@ function Aa(a, c, b, e, d, f, g, k) { } return e = 1 < e.length ? [].concat.apply([], e) : e[0]; } -;function Da(a, c, b, e, d) { - let f, g, k; - "string" === typeof d ? (f = d, d = "") : f = d.template; - if (!f) { - throw Error('No template pattern was specified by the search option "highlight"'); - } - g = f.indexOf("$1"); - if (-1 === g) { - throw Error('Invalid highlight template. The replacement pattern "$1" was not found in template: ' + f); - } - k = f.substring(g + 2); - g = f.substring(0, g); - let h = d && d.boundary, m = !d || !1 !== d.clip, n = d && d.merge && k && g && new RegExp(k + " " + g, "g"); - d = d && d.ellipsis; - var z = 0; - if ("object" === typeof d) { - var x = d.template; - z = x.length - 2; - d = d.pattern; - } - "string" !== typeof d && (d = !1 === d ? "" : "..."); - z && (d = x.replace("$1", d)); - x = d.length - z; - let q, r; - "object" === typeof h && (q = h.before, 0 === q && (q = -1), r = h.after, 0 === r && (r = -1), h = h.total || 9e5); - z = new Map(); - for (let ta = 0, T, Ga, ba; ta < c.length; ta++) { - let ca; - if (e) { - ca = c, ba = e; - } else { - var w = c[ta]; - ba = w.field; - if (!ba) { - continue; - } - ca = w.result; - } - Ga = b.get(ba); - T = Ga.encoder; - w = z.get(T); - "string" !== typeof w && (w = T.encode(a), z.set(T, w)); - for (let ia = 0; ia < ca.length; ia++) { - var l = ca[ia].doc; - if (!l) { - continue; - } - l = S(l, ba); - if (!l) { - continue; - } - var u = l.trim().split(/\s+/); - if (!u.length) { - continue; - } - l = ""; - var v = []; - let ja = []; - var E = -1, t = -1, C = 0; - for (var y = 0; y < u.length; y++) { - var A = u[y], H = T.encode(A); - H = 1 < H.length ? H.join(" ") : H[0]; - let p; - if (H && A) { - var D = A.length, M = (T.split ? A.replace(T.split, "") : A).length - H.length, K = "", Q = 0; - for (var U = 0; U < w.length; U++) { - var N = w[U]; - if (N) { - var G = N.length; - G += M; - Q && G <= Q || (N = H.indexOf(N), -1 < N && (K = (N ? A.substring(0, N) : "") + g + A.substring(N, N + G) + k + (N + G < D ? A.substring(N + G) : ""), Q = G, p = !0)); - } - } - K && (h && (0 > E && (E = l.length + (l ? 1 : 0)), t = l.length + (l ? 1 : 0) + K.length, C += D, ja.push(v.length), v.push({match:K})), l += (l ? " " : "") + K); - } - if (!p) { - A = u[y], l += (l ? " " : "") + A, h && v.push({text:A}); - } else if (h && C >= h) { - break; - } - } - C = ja.length * (f.length - 2); - if (q || r || h && l.length - C > h) { - if (C = h + C - 2 * x, y = t - E, 0 < q && (y += q), 0 < r && (y += r), y <= C) { - u = q ? E - (0 < q ? q : 0) : E - ((C - y) / 2 | 0), v = r ? t + (0 < r ? r : 0) : u + C, m || (0 < u && " " !== l.charAt(u) && " " !== l.charAt(u - 1) && (u = l.indexOf(" ", u), 0 > u && (u = 0)), v < l.length && " " !== l.charAt(v - 1) && " " !== l.charAt(v) && (v = l.lastIndexOf(" ", v), v < t ? v = t : ++v)), l = (u ? d : "") + l.substring(u, v) + (v < l.length ? d : ""); - } else { - t = []; - E = {}; - C = {}; - y = {}; - A = {}; - H = {}; - K = M = D = 0; - for (U = Q = 1;;) { - var O = void 0; - for (let p = 0, F; p < ja.length; p++) { - F = ja[p]; - if (K) { - if (M !== K) { - if (y[p + 1]) { - continue; - } - F += K; - if (E[F]) { - D -= x; - C[p + 1] = 1; - y[p + 1] = 1; - continue; - } - if (F >= v.length - 1) { - if (F >= v.length) { - y[p + 1] = 1; - F >= u.length && (C[p + 1] = 1); - continue; - } - D -= x; - } - l = v[F].text; - if (G = r && H[p]) { - if (0 < G) { - if (l.length > G) { - if (y[p + 1] = 1, m) { - l = l.substring(0, G); - } else { - continue; - } - } - (G -= l.length) || (G = -1); - H[p] = G; - } else { - y[p + 1] = 1; - continue; - } - } - if (D + l.length + 1 <= h) { - l = " " + l, t[p] += l; - } else if (m) { - O = h - D - 1, 0 < O && (l = " " + l.substring(0, O), t[p] += l), y[p + 1] = 1; - } else { - y[p + 1] = 1; - continue; - } - } else { - if (y[p]) { - continue; - } - F -= M; - if (E[F]) { - D -= x; - y[p] = 1; - C[p] = 1; - continue; - } - if (0 >= F) { - if (0 > F) { - y[p] = 1; - C[p] = 1; - continue; - } - D -= x; - } - l = v[F].text; - if (G = q && A[p]) { - if (0 < G) { - if (l.length > G) { - if (y[p] = 1, m) { - l = l.substring(l.length - G); - } else { - continue; - } - } - (G -= l.length) || (G = -1); - A[p] = G; - } else { - y[p] = 1; - continue; - } - } - if (D + l.length + 1 <= h) { - l += " ", t[p] = l + t[p]; - } else if (m) { - O = l.length + 1 - (h - D), 0 <= O && O < l.length && (l = l.substring(O) + " ", t[p] = l + t[p]), y[p] = 1; - } else { - y[p] = 1; - continue; - } - } - } else { - l = v[F].match; - q && (A[p] = q); - r && (H[p] = r); - p && D++; - let ua; - F ? !p && x && (D += x) : (C[p] = 1, y[p] = 1); - F >= u.length - 1 ? ua = 1 : F < v.length - 1 && v[F + 1].match ? ua = 1 : x && (D += x); - D -= f.length - 2; - if (!p || D + l.length <= h) { - t[p] = l; - } else { - O = Q = U = C[p] = 0; - break; - } - ua && (C[p + 1] = 1, y[p + 1] = 1); - } - D += l.length; - O = E[F] = 1; - } - if (O) { - M === K ? K++ : M++; - } else { - M === K ? Q = 0 : U = 0; - if (!Q && !U) { - break; - } - Q ? (M++, K = M) : K++; - } - } - l = ""; - for (let p = 0, F; p < t.length; p++) { - F = (p && C[p] ? " " : (p && !d ? " " : "") + d) + t[p], l += F; - } - d && !C[t.length] && (l += d); - } - } - n && (l = l.replace(n, " ")); - ca[ia].highlight = l; - } - if (e) { - break; - } - } - return c; -} -;function Ea(a, c) { - const b = L(), e = []; +;function Da(a, c) { + const b = M(), e = []; for (let d = 0, f; d < c.length; d++) { f = c[d]; for (let g = 0; g < f.length; g++) { @@ -785,169 +528,403 @@ function Aa(a, c, b, e, d, f, g, k) { } return e; } -;L(); -X.prototype.search = function(a, c, b, e) { - b || (!c && R(a) ? (b = a, a = "") : R(c) && (b = c, c = 0)); - if (b && b.cache) { - b.cache = !1; - var d = this.searchCache(a, c, b); - b.cache = !0; - return d; +;M(); +function Ea(a, c, b, e, d) { + let f, g, h; + "string" === typeof d ? (f = d, d = "") : f = d.template; + if (!f) { + throw Error('No template pattern was specified by the search option "highlight"'); } - let f = []; - var g = []; - let k, h, m; - let n = 0, z = !0, x; + g = f.indexOf("$1"); + if (-1 === g) { + throw Error('Invalid highlight template. The replacement pattern "$1" was not found in template: ' + f); + } + h = f.substring(g + 2); + g = f.substring(0, g); + let k = d && d.boundary, n = !d || !1 !== d.clip, l = d && d.merge && h && g && new RegExp(h + " " + g, "g"); + d = d && d.ellipsis; + var D = 0; + if ("object" === typeof d) { + var w = d.template; + D = w.length - 2; + d = d.pattern; + } + "string" !== typeof d && (d = !1 === d ? "" : "..."); + D && (d = w.replace("$1", d)); + w = d.length - D; + let t, p; + "object" === typeof k && (t = k.before, 0 === t && (t = -1), p = k.after, 0 === p && (p = -1), k = k.total || 9e5); + D = new Map(); + for (let sa = 0, U, Ha, ba; sa < c.length; sa++) { + let ca; + if (e) { + ca = c, ba = e; + } else { + var x = c[sa]; + ba = x.field; + if (!ba) { + continue; + } + ca = x.result; + } + Ha = b.get(ba); + U = Ha.encoder; + x = D.get(U); + "string" !== typeof x && (x = U.encode(a), D.set(U, x)); + for (let ia = 0; ia < ca.length; ia++) { + var m = ca[ia].doc; + if (!m) { + continue; + } + m = S(m, ba); + if (!m) { + continue; + } + var v = m.trim().split(/\s+/); + if (!v.length) { + continue; + } + m = ""; + var u = []; + let ja = []; + var F = -1, z = -1, r = 0; + for (var y = 0; y < v.length; y++) { + var E = v[y], B = U.encode(E); + B = 1 < B.length ? B.join(" ") : B[0]; + let q; + if (B && E) { + var C = E.length, K = (U.split ? E.replace(U.split, "") : E).length - B.length, L = "", Q = 0; + for (var V = 0; V < x.length; V++) { + var N = x[V]; + if (N) { + var H = N.length; + H += K; + Q && H <= Q || (N = B.indexOf(N), -1 < N && (L = (N ? E.substring(0, N) : "") + g + E.substring(N, N + H) + h + (N + H < C ? E.substring(N + H) : ""), Q = H, q = !0)); + } + } + L && (k && (0 > F && (F = m.length + (m ? 1 : 0)), z = m.length + (m ? 1 : 0) + L.length, r += C, ja.push(u.length), u.push({match:L})), m += (m ? " " : "") + L); + } + if (!q) { + E = v[y], m += (m ? " " : "") + E, k && u.push({text:E}); + } else if (k && r >= k) { + break; + } + } + r = ja.length * (f.length - 2); + if (t || p || k && m.length - r > k) { + if (r = k + r - 2 * w, y = z - F, 0 < t && (y += t), 0 < p && (y += p), y <= r) { + v = t ? F - (0 < t ? t : 0) : F - ((r - y) / 2 | 0), u = p ? z + (0 < p ? p : 0) : v + r, n || (0 < v && " " !== m.charAt(v) && " " !== m.charAt(v - 1) && (v = m.indexOf(" ", v), 0 > v && (v = 0)), u < m.length && " " !== m.charAt(u - 1) && " " !== m.charAt(u) && (u = m.lastIndexOf(" ", u), u < z ? u = z : ++u)), m = (v ? d : "") + m.substring(v, u) + (u < m.length ? d : ""); + } else { + z = []; + F = {}; + r = {}; + y = {}; + E = {}; + B = {}; + L = K = C = 0; + for (V = Q = 1;;) { + var O = void 0; + for (let q = 0, G; q < ja.length; q++) { + G = ja[q]; + if (L) { + if (K !== L) { + if (y[q + 1]) { + continue; + } + G += L; + if (F[G]) { + C -= w; + r[q + 1] = 1; + y[q + 1] = 1; + continue; + } + if (G >= u.length - 1) { + if (G >= u.length) { + y[q + 1] = 1; + G >= v.length && (r[q + 1] = 1); + continue; + } + C -= w; + } + m = u[G].text; + if (H = p && B[q]) { + if (0 < H) { + if (m.length > H) { + if (y[q + 1] = 1, n) { + m = m.substring(0, H); + } else { + continue; + } + } + (H -= m.length) || (H = -1); + B[q] = H; + } else { + y[q + 1] = 1; + continue; + } + } + if (C + m.length + 1 <= k) { + m = " " + m, z[q] += m; + } else if (n) { + O = k - C - 1, 0 < O && (m = " " + m.substring(0, O), z[q] += m), y[q + 1] = 1; + } else { + y[q + 1] = 1; + continue; + } + } else { + if (y[q]) { + continue; + } + G -= K; + if (F[G]) { + C -= w; + y[q] = 1; + r[q] = 1; + continue; + } + if (0 >= G) { + if (0 > G) { + y[q] = 1; + r[q] = 1; + continue; + } + C -= w; + } + m = u[G].text; + if (H = t && E[q]) { + if (0 < H) { + if (m.length > H) { + if (y[q] = 1, n) { + m = m.substring(m.length - H); + } else { + continue; + } + } + (H -= m.length) || (H = -1); + E[q] = H; + } else { + y[q] = 1; + continue; + } + } + if (C + m.length + 1 <= k) { + m += " ", z[q] = m + z[q]; + } else if (n) { + O = m.length + 1 - (k - C), 0 <= O && O < m.length && (m = m.substring(O) + " ", z[q] = m + z[q]), y[q] = 1; + } else { + y[q] = 1; + continue; + } + } + } else { + m = u[G].match; + t && (E[q] = t); + p && (B[q] = p); + q && C++; + let ta; + G ? !q && w && (C += w) : (r[q] = 1, y[q] = 1); + G >= v.length - 1 ? ta = 1 : G < u.length - 1 && u[G + 1].match ? ta = 1 : w && (C += w); + C -= f.length - 2; + if (!q || C + m.length <= k) { + z[q] = m; + } else { + O = Q = V = r[q] = 0; + break; + } + ta && (r[q + 1] = 1, y[q + 1] = 1); + } + C += m.length; + O = F[G] = 1; + } + if (O) { + K === L ? L++ : K++; + } else { + K === L ? Q = 0 : V = 0; + if (!Q && !V) { + break; + } + Q ? (K++, L = K) : L++; + } + } + m = ""; + for (let q = 0, G; q < z.length; q++) { + G = (q && r[q] ? " " : (q && !d ? " " : "") + d) + z[q], m += G; + } + d && !r[z.length] && (m += d); + } + } + l && (m = m.replace(l, " ")); + ca[ia].highlight = m; + } + if (e) { + break; + } + } + return c; +} +;Y.prototype.search = function(a, c, b, e) { + b || (!c && R(a) ? (b = a, a = "") : R(c) && (b = c, c = 0)); + let d = []; + var f = []; + let g; + let h, k, n, l; + let D = 0, w = !0, t; if (b) { b.constructor === Array && (b = {index:b}); a = b.query || a; - var q = b.pluck; - var r = b.merge; - h = q || b.field || (h = b.index) && (h.index ? null : h); - m = this.tag && b.tag; - k = b.suggest; - z = !0; - this.store && b.highlight && !z ? console.warn("Highlighting results can only be done on a final resolver task or when calling .resolve({ highlight: ... })") : this.store && b.enrich && !z && console.warn("Enrich results can only be done on a final resolver task or when calling .resolve({ enrich: true })"); - x = z && this.store && b.highlight; - d = !!x || z && this.store && b.enrich; + var p = b.pluck; + var x = b.merge; + n = p || b.field || (n = b.index) && (n.index ? null : n); + l = this.tag && b.tag; + h = b.suggest; + w = !0; + k = b.cache; + this.store && b.highlight && !w ? console.warn("Highlighting results can only be done on a final resolver task or when calling .resolve({ highlight: ... })") : this.store && b.enrich && !w && console.warn("Enrich results can only be done on a final resolver task or when calling .resolve({ enrich: true })"); + t = w && this.store && b.highlight; + g = !!t || w && this.store && b.enrich; c = b.limit || c; - var w = b.offset || 0; - c || (c = z ? 100 : 0); - if (m) { - m.constructor !== Array && (m = [m]); - var l = []; - for (let E = 0, t; E < m.length; E++) { - t = m[E]; - if (P(t)) { + var m = b.offset || 0; + c || (c = w ? 100 : 0); + if (l) { + l.constructor !== Array && (l = [l]); + var v = []; + for (let z = 0, r; z < l.length; z++) { + r = l[z]; + if (P(r)) { throw Error("A tag option can't be a string, instead it needs a { field: tag } format."); } - if (t.field && t.tag) { - var u = t.tag; + if (r.field && r.tag) { + var u = r.tag; if (u.constructor === Array) { - for (var v = 0; v < u.length; v++) { - l.push(t.field, u[v]); + for (var F = 0; F < u.length; F++) { + v.push(r.field, u[F]); } } else { - l.push(t.field, u); + v.push(r.field, u); } } else { - u = Object.keys(t); - for (let C = 0, y, A; C < u.length; C++) { - if (y = u[C], A = t[y], A.constructor === Array) { - for (v = 0; v < A.length; v++) { - l.push(y, A[v]); + u = Object.keys(r); + for (let y = 0, E, B; y < u.length; y++) { + if (E = u[y], B = r[E], B.constructor === Array) { + for (F = 0; F < B.length; F++) { + v.push(E, B[F]); } } else { - l.push(y, A); + v.push(E, B); } } } } - if (!l.length) { + if (!v.length) { throw Error("Your tag definition within the search options is probably wrong. No valid tags found."); } - m = l; + l = v; if (!a) { - g = []; - if (l.length) { - for (q = 0; q < l.length; q += 2) { - r = Fa.call(this, l[q], l[q + 1], c, w, d), f.push({field:l[q], tag:l[q + 1], result:r}); + f = []; + if (v.length) { + for (p = 0; p < v.length; p += 2) { + x = Fa.call(this, v[p], v[p + 1], c, m, g), d.push({field:v[p], tag:v[p + 1], result:x}); } } - return g.length ? Promise.all(g).then(function(E) { - for (let t = 0; t < E.length; t++) { - f[t].result = E[t]; + return f.length ? Promise.all(f).then(function(z) { + for (let r = 0; r < z.length; r++) { + d[r].result = z[r]; } - return f; - }) : f; + return d; + }) : d; } } - h && h.constructor !== Array && (h = [h]); + n && n.constructor !== Array && (n = [n]); } - h || (h = this.field); - l = !1; - for (let E = 0, t, C, y; E < h.length; E++) { - C = h[E]; - let A; - P(C) || (A = C, C = A.field, a = A.query || a, c = J(A.limit, c), w = J(A.offset, w), k = J(A.suggest, k), x = z && this.store && J(A.highlight, x), d = !!x || z && this.store && J(A.enrich, d)); + n || (n = this.field); + v = !1; + for (let z = 0, r, y, E; z < n.length; z++) { + y = n[z]; + let B; + P(y) || (B = y, y = B.field, a = B.query || a, c = J(B.limit, c), m = J(B.offset, m), h = J(B.suggest, h), t = w && this.store && J(B.highlight, t), g = !!t || w && this.store && J(B.enrich, g), k = J(B.cache, k)); if (e) { - t = e[E]; + r = e[z]; } else { - if (u = A || b, v = this.index.get(C), m && (u.enrich = !1), l) { - l[E] = v.search(a, c, u); - u && d && (u.enrich = d); + u = B || b || {}; + F = u.enrich; + const C = this.index.get(y); + l && F && (u.enrich = !1); + r = k ? C.searchCache(a, c, u) : C.search(a, c, u); + F && (u.enrich = F); + if (v) { + v[z] = r; continue; - } else { - t = v.search(a, c, u), u && d && (u.enrich = d); } } - y = t && (z ? t.length : t.result.length); - if (m && y) { + E = (r = r.result || r) && r.length; + if (l && E) { u = []; - v = 0; - for (let H = 0, D, M; H < m.length; H += 2) { - D = this.tag.get(m[H]); - if (!D) { - if (console.warn("Tag '" + m[H] + ":" + m[H + 1] + "' will be skipped because there is no field '" + m[H] + "'."), k) { + F = 0; + for (let C = 0, K, L; C < l.length; C += 2) { + K = this.tag.get(l[C]); + if (!K) { + if (console.warn("Tag '" + l[C] + ":" + l[C + 1] + "' will be skipped because there is no field '" + l[C] + "'."), h) { continue; } else { - return f; + return d; } } - if (M = (D = D && D.get(m[H + 1])) && D.length) { - v++, u.push(D); - } else if (!k) { - return f; + if (L = (K = K && K.get(l[C + 1])) && K.length) { + F++, u.push(K); + } else if (!h) { + return d; } } - if (v) { - t = Ea(t, u); - y = t.length; - if (!y && !k) { - return t; + if (F) { + r = Da(r, u); + E = r.length; + if (!E && !h) { + return r; } - v--; + F--; } } - if (y) { - g[n] = C, f.push(t), n++; - } else if (1 === h.length) { - return f; + if (E) { + f[D] = y, d.push(r), D++; + } else if (1 === n.length) { + return d; } } - if (l) { - const E = this; - return Promise.all(l).then(function(t) { - return t.length ? E.search(a, c, b, t) : t; + if (v) { + const z = this; + return Promise.all(v).then(function(r) { + b && (b.resolve = w); + r.length && (r = z.search(a, c, b, r)); + return r; }); } - if (!n) { - return f; + if (!D) { + return d; } - if (q && (!d || !this.store)) { - return f = f[0]; + if (p && (!g || !this.store)) { + return d = d[0]; } - l = []; - for (w = 0; w < g.length; w++) { - e = f[w]; - d && e.length && "undefined" === typeof e[0].doc && (e = Ha.call(this, e)); - if (q) { - return x ? Da(a, e, this.index, q, x) : e; + v = []; + for (m = 0; m < f.length; m++) { + e = d[m]; + g && e.length && "undefined" === typeof e[0].doc && (e = Ga.call(this, e)); + if (p) { + return t ? Ea(a, e, this.index, p, t) : e; } - f[w] = {field:g[w], result:e}; + d[m] = {field:f[m], result:e}; } - x && (f = Da(a, f, this.index, q, x)); - return r ? Ia(f) : f; + t && (d = Ea(a, d, this.index, p, t)); + return x ? Ia(d) : d; }; function Ia(a) { - const c = [], b = L(), e = L(); - for (let d = 0, f, g, k, h, m, n, z; d < a.length; d++) { + const c = [], b = M(), e = M(); + for (let d = 0, f, g, h, k, n, l, D; d < a.length; d++) { f = a[d]; g = f.field; - k = f.result; - for (let x = 0; x < k.length; x++) { - if (m = k[x], "object" !== typeof m ? m = {id:h = m} : h = m.id, (n = b[h]) ? n.push(g) : (m.field = b[h] = [g], c.push(m)), z = m.highlight) { - n = e[h], n || (e[h] = n = {}, m.highlight = n), n[g] = z; + h = f.result; + for (let w = 0; w < h.length; w++) { + if (n = h[w], "object" !== typeof n ? n = {id:k = n} : k = n.id, (l = b[k]) ? l.push(g) : (n.field = b[k] = [g], c.push(n)), D = n.highlight) { + l = e[k], l || (e[k] = l = {}, n.highlight = l), l[g] = D; } } } @@ -967,11 +944,11 @@ function Fa(a, c, b, e, d) { if (b && c > b || e) { a = a.slice(e, e + b); } - d && (a = Ha.call(this, a)); + d && (a = Ga.call(this, a)); } return a; } -function Ha(a) { +function Ga(a) { if (!this || !this.store) { return a; } @@ -981,47 +958,33 @@ function Ha(a) { } return c; } -;const Ja = {normalize:!1, numeric:!1, dedupe:!1}; -const Ka = {}; -const La = new Map([["b", "p"], ["v", "f"], ["w", "f"], ["z", "s"], ["x", "s"], ["d", "t"], ["n", "m"], ["c", "k"], ["g", "k"], ["j", "k"], ["q", "k"], ["i", "e"], ["y", "e"], ["u", "o"]]); -const Ma = new Map([["ae", "a"], ["oe", "o"], ["sh", "s"], ["kh", "k"], ["th", "t"], ["ph", "f"], ["pf", "f"]]), Na = [/([^aeo])h(.)/g, "$1$2", /([aeo])h([^aeo]|$)/g, "$1$2", /(.)\1+/g, "$1"]; -const Oa = {a:"", e:"", i:"", o:"", u:"", y:"", b:1, f:1, p:1, v:1, c:2, g:2, j:2, k:2, q:2, s:2, x:2, z:2, "\u00df":2, d:3, t:3, l:4, m:5, n:5, r:6}; -var Pa = {Exact:Ja, Default:Ka, Normalize:Ka, LatinBalance:{mapper:La}, LatinAdvanced:{mapper:La, matcher:Ma, replacer:Na}, LatinExtra:{mapper:La, replacer:Na.concat([/(?!^)[aeo]/g, ""]), matcher:Ma}, LatinSoundex:{dedupe:!1, include:{letter:!0}, finalize:function(a) { - for (let b = 0; b < a.length; b++) { - var c = a[b]; - let e = c.charAt(0), d = Oa[e]; - for (let f = 1, g; f < c.length && (g = c.charAt(f), "h" === g || "w" === g || !(g = Oa[g]) || g === d || (e += g, d = g, 4 !== e.length)); f++) { - } - a[b] = e; - } -}}, CJK:{split:""}, LatinExact:Ja, LatinDefault:Ka, LatinSimple:Ka}; -function X(a) { - if (!this || this.constructor !== X) { - return new X(a); +;function Y(a) { + if (!this || this.constructor !== Y) { + return new Y(a); } const c = a.document || a.doc || a; var b; this.D = []; this.field = []; this.J = []; - this.key = (b = c.key || c.id) && Qa(b, this.J) || "id"; + this.key = (b = c.key || c.id) && Ja(b, this.J) || "id"; this.reg = (this.fastupdate = !!a.fastupdate) ? new Map() : new Set(); this.A = (b = c.store || null) && b && !0 !== b && []; this.store = b && new Map(); - this.cache = (b = a.cache || null) && new Y(b); + this.cache = (b = a.cache || null) && new W(b); a.cache = !1; this.priority = a.priority || 4; b = new Map(); let e = c.index || c.field || c; P(e) && (e = [e]); for (let d = 0, f, g; d < e.length; d++) { - f = e[d], P(f) || (g = f, f = f.field), g = R(g) ? Object.assign({}, a, g) : a, b.set(f, new Z(g, this.reg)), g.custom ? this.D[d] = g.custom : (this.D[d] = Qa(f, this.J), g.filter && ("string" === typeof this.D[d] && (this.D[d] = new String(this.D[d])), this.D[d].I = g.filter)), this.field[d] = f; + f = e[d], P(f) || (g = f, f = f.field), g = R(g) ? Object.assign({}, a, g) : a, b.set(f, new Z(g, this.reg)), g.custom ? this.D[d] = g.custom : (this.D[d] = Ja(f, this.J), g.filter && ("string" === typeof this.D[d] && (this.D[d] = new String(this.D[d])), this.D[d].I = g.filter)), this.field[d] = f; } if (this.A) { a = c.store; P(a) && (a = [a]); for (let d = 0, f, g; d < a.length; d++) { - f = a[d], g = f.field || f, f.custom ? (this.A[d] = f.custom, f.custom.S = g) : (this.A[d] = Qa(g, this.J), f.filter && ("string" === typeof this.A[d] && (this.A[d] = new String(this.A[d])), this.A[d].I = f.filter)); + f = a[d], g = f.field || f, f.custom ? (this.A[d] = f.custom, f.custom.S = g) : (this.A[d] = Ja(g, this.J), f.filter && ("string" === typeof this.A[d] && (this.A[d] = new String(this.A[d])), this.A[d].I = f.filter)); } } this.index = b; @@ -1037,14 +1000,14 @@ function X(a) { if (!g) { throw Error("The tag field from the document descriptor is undefined."); } - f.custom ? this.B[d] = f.custom : (this.B[d] = Qa(g, this.J), f.filter && ("string" === typeof this.B[d] && (this.B[d] = new String(this.B[d])), this.B[d].I = f.filter)); + f.custom ? this.B[d] = f.custom : (this.B[d] = Ja(g, this.J), f.filter && ("string" === typeof this.B[d] && (this.B[d] = new String(this.B[d])), this.B[d].I = f.filter)); this.R[d] = g; this.tag.set(g, new Map()); } } } } -function Qa(a, c) { +function Ja(a, c) { const b = a.split(":"); let e = 0; for (let d = 0; d < b.length; d++) { @@ -1053,14 +1016,14 @@ function Qa(a, c) { e < b.length && (b.length = e); return 1 < e ? b : b[0]; } -B = X.prototype; -B.append = function(a, c) { +A = Y.prototype; +A.append = function(a, c) { return this.add(a, c, !0); }; -B.update = function(a, c) { +A.update = function(a, c) { return this.remove(a).add(a, c); }; -B.remove = function(a) { +A.remove = function(a) { R(a) && (a = S(a, this.key)); for (var c of this.index.values()) { c.remove(a, !0); @@ -1081,7 +1044,7 @@ B.remove = function(a) { this.cache && this.cache.remove(a); return this; }; -B.clear = function() { +A.clear = function() { const a = []; for (const c of this.index.values()) { const b = c.clear(); @@ -1096,31 +1059,95 @@ B.clear = function() { this.cache && this.cache.clear(); return a.length ? Promise.all(a) : this; }; -B.contain = function(a) { +A.contain = function(a) { return this.reg.has(a); }; -B.cleanup = function() { +A.cleanup = function() { for (const a of this.index.values()) { a.cleanup(); } return this; }; -B.get = function(a) { +A.get = function(a) { return this.store.get(a) || null; }; -B.set = function(a, c) { +A.set = function(a, c) { "object" === typeof a && (c = a, a = S(c, this.key)); this.store.set(a, c); return this; }; -B.searchCache = Ra; -B.export = function(a, c, b = 0, e = 0) { +A.searchCache = la; +A.export = Ka; +A.import = La; +va(Y.prototype); +function Ma(a, c = 0) { + let b = [], e = []; + c && (c = 250000 / c * 5000 | 0); + for (const d of a.entries()) { + e.push(d), e.length === c && (b.push(e), e = []); + } + e.length && b.push(e); + return b; +} +function Na(a, c) { + c || (c = new Map()); + for (let b = 0, e; b < a.length; b++) { + e = a[b], c.set(e[0], e[1]); + } + return c; +} +function Oa(a, c = 0) { + let b = [], e = []; + c && (c = 250000 / c * 1000 | 0); + for (const d of a.entries()) { + e.push([d[0], Ma(d[1])[0]]), e.length === c && (b.push(e), e = []); + } + e.length && b.push(e); + return b; +} +function Pa(a, c) { + c || (c = new Map()); + for (let b = 0, e, d; b < a.length; b++) { + e = a[b], d = c.get(e[0]), c.set(e[0], Na(e[1], d)); + } + return c; +} +function Qa(a) { + let c = [], b = []; + for (const e of a.keys()) { + b.push(e), 250000 === b.length && (c.push(b), b = []); + } + b.length && c.push(b); + return c; +} +function Ra(a, c) { + c || (c = new Set()); + for (let b = 0; b < a.length; b++) { + c.add(a[b]); + } + return c; +} +function Sa(a, c, b, e, d, f, g = 0) { + const h = e && e.constructor === Array; + var k = h ? e.shift() : e; + if (!k) { + return this.export(a, c, d, f + 1); + } + if ((k = a((c ? c + "." : "") + (g + 1) + "." + b, JSON.stringify(k))) && k.then) { + const n = this; + return k.then(function() { + return Sa.call(n, a, c, b, h ? e : null, d, f, g + 1); + }); + } + return Sa.call(this, a, c, b, h ? e : null, d, f, g + 1); +} +function Ka(a, c, b = 0, e = 0) { if (b < this.field.length) { const g = this.field[b]; if ((c = this.index.get(g).export(a, g, b, e = 1)) && c.then) { - const k = this; + const h = this; return c.then(function() { - return k.export(a, g, b + 1); + return h.export(a, g, b + 1); }); } return this.export(a, g, b + 1); @@ -1129,25 +1156,25 @@ B.export = function(a, c, b = 0, e = 0) { switch(e) { case 0: d = "reg"; - f = wa(this.reg); + f = Qa(this.reg); c = null; break; case 1: d = "tag"; - f = this.tag && sa(this.tag, this.reg.size); + f = this.tag && Oa(this.tag, this.reg.size); c = null; break; case 2: d = "doc"; - f = this.store && qa(this.store); + f = this.store && Ma(this.store); c = null; break; default: return; } - return ya.call(this, a, c, d, f, b, e); -}; -B.import = function(a, c) { + return Sa.call(this, a, c, d, f, b, e); +} +function La(a, c) { var b = a.split("."); "json" === b[b.length - 1] && b.pop(); a = 2 < b.length ? b[0] : ""; @@ -1160,62 +1187,40 @@ B.import = function(a, c) { switch(b) { case "reg": this.fastupdate = !1; - this.reg = xa(c, this.reg); + this.reg = Ra(c, this.reg); for (let e = 0, d; e < this.field.length; e++) { d = this.index.get(this.field[e]), d.fastupdate = !1, d.reg = this.reg; } break; case "tag": - this.tag = va(c, this.tag); + this.tag = Pa(c, this.tag); break; case "doc": - this.store = ra(c, this.store); + this.store = Na(c, this.store); } } -}; -la(X.prototype); -function Ra(a, c, b) { - const e = (c ? "" + a : "object" === typeof a ? "" + a.query : a).toLowerCase(); - this.cache || (this.cache = new Y()); - let d = this.cache.get(e); - if (!d) { - d = this.search(a, c, b); - if (d.then) { - const f = this; - d.then(function(g) { - f.cache.set(e, g); - return g; - }); +} +function Ta(a, c) { + let b = ""; + for (const e of a.entries()) { + a = e[0]; + const d = e[1]; + let f = ""; + for (let g = 0, h; g < d.length; g++) { + h = d[g] || [""]; + let k = ""; + for (let n = 0; n < h.length; n++) { + k += (k ? "," : "") + ("string" === c ? '"' + h[n] + '"' : h[n]); + } + k = "[" + k + "]"; + f += (f ? "," : "") + k; } - this.cache.set(e, d); + f = '["' + a + '",[' + f + "]]"; + b += (b ? "," : "") + f; } - return d; + return b; } -function Y(a) { - this.limit = a && !0 !== a ? a : 1000; - this.cache = new Map(); - this.h = ""; -} -Y.prototype.set = function(a, c) { - this.cache.set(this.h = a, c); - this.cache.size > this.limit && this.cache.delete(this.cache.keys().next().value); -}; -Y.prototype.get = function(a) { - const c = this.cache.get(a); - c && this.h !== a && (this.cache.delete(a), this.cache.set(this.h = a, c)); - return c; -}; -Y.prototype.remove = function(a) { - for (const c of this.cache) { - const b = c[0]; - c[1].includes(a) && this.cache.delete(b); - } -}; -Y.prototype.clear = function() { - this.cache.clear(); - this.h = ""; -}; -Z.prototype.remove = function(a, c) { +;Z.prototype.remove = function(a, c) { const b = this.reg.size && (this.fastupdate ? this.reg.get(a) : this.reg.has(a)); if (b) { if (this.fastupdate) { @@ -1230,44 +1235,48 @@ Z.prototype.remove = function(a, c) { } } } else { - Sa(this.map, a), this.depth && Sa(this.ctx, a); + Ua(this.map, a), this.depth && Ua(this.ctx, a); } c || this.reg.delete(a); } this.cache && this.cache.remove(a); return this; }; -function Sa(a, c) { +function Ua(a, c) { let b = 0; var e = "undefined" === typeof c; if (a.constructor === Array) { - for (let d = 0, f, g; d < a.length; d++) { + for (let d = 0, f, g, h; d < a.length; d++) { if ((f = a[d]) && f.length) { if (e) { - b++; - } else { - if (g = f.indexOf(c), 0 <= g) { - if (1 < f.length) { - f.splice(g, 1); - b++; - break; - } else { - delete a[d]; - } - } else { - b++; + return 1; + } + g = f.indexOf(c); + if (0 <= g) { + if (1 < f.length) { + return f.splice(g, 1), 1; } + delete a[d]; + if (b) { + return 1; + } + h = 1; + } else { + if (h) { + return 1; + } + b++; } } } } else { for (let d of a.entries()) { - e = d[0], Sa(d[1], c) ? b++ : a.delete(e); + e = d[0], Ua(d[1], c) ? b++ : a.delete(e); } } return b; } -;const Ta = {memory:{resolution:1}, performance:{resolution:3, fastupdate:!0, context:{depth:1, resolution:1}}, match:{tokenize:"forward"}, score:{resolution:9, context:{depth:2, resolution:3}}}; +;const Va = {memory:{resolution:1}, performance:{resolution:3, fastupdate:!0, context:{depth:1, resolution:1}}, match:{tokenize:"forward"}, score:{resolution:9, context:{depth:2, resolution:3}}}; Z.prototype.add = function(a, c, b, e) { if (c && (a || 0 === a)) { if (!e && !b && this.reg.has(a)) { @@ -1275,23 +1284,23 @@ Z.prototype.add = function(a, c, b, e) { } e = this.depth; c = this.encoder.encode(c, !e); - const m = c.length; - if (m) { - const n = L(), z = L(), x = this.resolution; - for (let q = 0; q < m; q++) { - let r = c[this.rtl ? m - 1 - q : q]; - var d = r.length; - if (d && (e || !z[r])) { - var f = this.score ? this.score(c, r, q, null, 0) : Ua(x, m, q), g = ""; + const n = c.length; + if (n) { + const l = M(), D = M(), w = this.resolution; + for (let t = 0; t < n; t++) { + let p = c[this.rtl ? n - 1 - t : t]; + var d = p.length; + if (d && (e || !D[p])) { + var f = this.score ? this.score(c, p, t, null, 0) : Wa(w, n, t), g = ""; switch(this.tokenize) { case "full": if (2 < d) { - for (let w = 0, l; w < d; w++) { - for (f = d; f > w; f--) { - g = r.substring(w, f); - l = this.rtl ? d - 1 - w : w; - var k = this.score ? this.score(c, r, q, g, l) : Ua(x, m, q, d, l); - Va(this, z, g, k, a, b); + for (let x = 0, m; x < d; x++) { + for (f = d; f > x; f--) { + g = p.substring(x, f); + m = this.rtl ? d - 1 - x : x; + var h = this.score ? this.score(c, p, t, g, m) : Wa(w, n, t, d, m); + Xa(this, D, g, h, a, b); } } break; @@ -1299,27 +1308,27 @@ Z.prototype.add = function(a, c, b, e) { case "bidirectional": case "reverse": if (1 < d) { - for (k = d - 1; 0 < k; k--) { - g = r[this.rtl ? d - 1 - k : k] + g; - var h = this.score ? this.score(c, r, q, g, k) : Ua(x, m, q, d, k); - Va(this, z, g, h, a, b); + for (h = d - 1; 0 < h; h--) { + g = p[this.rtl ? d - 1 - h : h] + g; + var k = this.score ? this.score(c, p, t, g, h) : Wa(w, n, t, d, h); + Xa(this, D, g, k, a, b); } g = ""; } case "forward": if (1 < d) { - for (k = 0; k < d; k++) { - g += r[this.rtl ? d - 1 - k : k], Va(this, z, g, f, a, b); + for (h = 0; h < d; h++) { + g += p[this.rtl ? d - 1 - h : h], Xa(this, D, g, f, a, b); } break; } default: - if (Va(this, z, r, f, a, b), e && 1 < m && q < m - 1) { - for (d = L(), g = this.P, f = r, k = Math.min(e + 1, this.rtl ? q + 1 : m - q), d[f] = 1, h = 1; h < k; h++) { - if ((r = c[this.rtl ? m - 1 - q - h : q + h]) && !d[r]) { - d[r] = 1; - const w = this.score ? this.score(c, f, q, r, h - 1) : Ua(g + (m / 2 > g ? 0 : 1), m, q, k - 1, h - 1), l = this.bidirectional && r > f; - Va(this, n, l ? f : r, w, a, b, l ? r : f); + if (Xa(this, D, p, f, a, b), e && 1 < n && t < n - 1) { + for (d = M(), g = this.P, f = p, h = Math.min(e + 1, this.rtl ? t + 1 : n - t), d[f] = 1, k = 1; k < h; k++) { + if ((p = c[this.rtl ? n - 1 - t - k : t + k]) && !d[p]) { + d[p] = 1; + const x = this.score ? this.score(c, f, t, p, k - 1) : Wa(g + (n / 2 > g ? 0 : 1), n, t, h - 1, k - 1), m = this.bidirectional && p > f; + Xa(this, l, m ? f : p, x, a, b, m ? p : f); } } } @@ -1331,13 +1340,13 @@ Z.prototype.add = function(a, c, b, e) { } return this; }; -function Va(a, c, b, e, d, f, g) { - let k = g ? a.ctx : a.map, h; - if (!c[b] || g && !(h = c[b])[g]) { - g ? (c = h || (c[b] = L()), c[g] = 1, (h = k.get(g)) ? k = h : k.set(g, k = new Map())) : c[b] = 1, (h = k.get(b)) ? k = h : k.set(b, k = []), k = k[e] || (k[e] = []), f && k.includes(d) || (k.push(d), a.fastupdate && ((c = a.reg.get(d)) ? c.push(k) : a.reg.set(d, [k]))); +function Xa(a, c, b, e, d, f, g) { + let h = g ? a.ctx : a.map, k; + if (!c[b] || g && !(k = c[b])[g]) { + g ? (c = k || (c[b] = M()), c[g] = 1, (k = h.get(g)) ? h = k : h.set(g, h = new Map())) : c[b] = 1, (k = h.get(b)) ? h = k : h.set(b, h = []), h = h[e] || (h[e] = []), f && h.includes(d) || (h.push(d), a.fastupdate && ((c = a.reg.get(d)) ? c.push(h) : a.reg.set(d, [h]))); } } -function Ua(a, c, b, e, d) { +function Wa(a, c, b, e, d) { return b && 1 < a ? c + (e || 0) <= a ? b + (d || 0) : (a - 1) / (c + (e || 0)) * (b + (d || 0)) + 1 | 0 : 0; } ;Z.prototype.search = function(a, c, b) { @@ -1352,78 +1361,78 @@ function Ua(a, c, b, e, d) { d = b.offset || 0; var f = b.context; var g = b.suggest; - var k = !0; - var h = b.resolution; + var h = !0; + var k = b.resolution; } - "undefined" === typeof k && (k = !0); + "undefined" === typeof h && (h = !0); f = this.depth && !1 !== f; b = this.encoder.encode(a, !f); a = b.length; - c = c || (k ? 100 : 0); + c = c || (h ? 100 : 0); if (1 === a) { - return g = d, (d = Wa(this, b[0], "")) && d.length ? Ca.call(this, d, c, g) : []; + return g = d, (d = Ya(this, b[0], "")) && d.length ? Ca.call(this, d, c, g) : []; } if (2 === a && f && !g) { - return g = d, (d = Wa(this, b[1], b[0])) && d.length ? Ca.call(this, d, c, g) : []; + return g = d, (d = Ya(this, b[1], b[0])) && d.length ? Ca.call(this, d, c, g) : []; } - k = L(); - var m = 0; + h = M(); + var n = 0; if (f) { - var n = b[0]; - m = 1; + var l = b[0]; + n = 1; } - h || 0 === h || (h = n ? this.P : this.resolution); - for (let r, w; m < a; m++) { - if ((w = b[m]) && !k[w]) { - k[w] = 1; - r = Wa(this, w, n); + k || 0 === k || (k = l ? this.P : this.resolution); + for (let p, x; n < a; n++) { + if ((x = b[n]) && !h[x]) { + h[x] = 1; + p = Ya(this, x, l); a: { - f = r; - var z = e, x = g, q = h; - let l = []; + f = p; + var D = e, w = g, t = k; + let m = []; if (f && f.length) { - if (f.length <= q) { - z.push(f); - r = void 0; + if (f.length <= t) { + D.push(f); + p = void 0; break a; } - for (let u = 0, v; u < q; u++) { - if (v = f[u]) { - l[u] = v; + for (let v = 0, u; v < t; v++) { + if (u = f[v]) { + m[v] = u; } } - if (l.length) { - z.push(l); - r = void 0; + if (m.length) { + D.push(m); + p = void 0; break a; } } - r = x ? void 0 : l; + p = w ? void 0 : m; } - if (r) { - e = r; + if (p) { + e = p; break; } - n && (g && r && e.length || (n = w)); + l && (g && p && e.length || (l = x)); } - g && n && m === a - 1 && !e.length && (h = this.resolution, n = "", m = -1, k = L()); + g && l && n === a - 1 && !e.length && (k = this.resolution, l = "", n = -1, h = M()); } a: { b = e; e = b.length; - n = b; + l = b; if (1 < e) { b: { e = g; - n = b.length; + l = b.length; g = []; - a = L(); - for (let r = 0, w, l, u, v; r < h; r++) { - for (m = 0; m < n; m++) { - if (u = b[m], r < u.length && (w = u[r])) { - for (f = 0; f < w.length; f++) { - if (l = w[f], (k = a[l]) ? a[l]++ : (k = 0, a[l] = 1), v = g[k] || (g[k] = []), v.push(l), c && k === n - 1 && v.length - d === c) { - n = d ? v.slice(d) : v; + a = M(); + for (let p = 0, x, m, v, u; p < k; p++) { + for (n = 0; n < l; n++) { + if (v = b[n], p < v.length && (x = v[p])) { + for (f = 0; f < x.length; f++) { + if (m = x[f], (h = a[m]) ? a[m]++ : (h = 0, a[m] = 1), u = g[h] || (g[h] = []), u.push(m), c && h === l - 1 && u.length - d === c) { + l = d ? u.slice(d) : u; break b; } } @@ -1434,14 +1443,14 @@ function Ua(a, c, b, e, d) { if (e) { if (1 < g.length) { c: { - for (b = [], h = L(), e = g.length, k = e - 1; 0 <= k; k--) { - if (a = (e = g[k]) && e.length) { - for (m = 0; m < a; m++) { - if (n = e[m], !h[n]) { - if (h[n] = 1, d) { + for (b = [], k = M(), e = g.length, h = e - 1; 0 <= h; h--) { + if (a = (e = g[h]) && e.length) { + for (n = 0; n < a; n++) { + if (l = e[n], !k[l]) { + if (k[l] = 1, d) { d--; } else { - if (b.push(n), b.length === c) { + if (b.push(l), b.length === c) { break c; } } @@ -1451,12 +1460,12 @@ function Ua(a, c, b, e, d) { } } } else { - b = (g = g[0]).length > c || d ? g.slice(d, c + d) : g; + b = (g = g[0]) && c && g.length > c || d ? g.slice(d, c + d) : g; } g = b; } else { - if (b < n) { - n = []; + if (b < l) { + l = []; break b; } g = g[b - 1]; @@ -1467,17 +1476,17 @@ function Ua(a, c, b, e, d) { } } } - n = g; + l = g; } } else if (1 === e) { c = Ca.call(null, b[0], c, d); break a; } - c = n; + c = l; } return c; }; -function Wa(a, c, b) { +function Ya(a, c, b) { let e; b && (e = a.bidirectional && c > b) && (e = b, b = c, c = e); a = b ? (a = a.ctx.get(b)) && a.get(c) : a.map.get(c); @@ -1489,12 +1498,12 @@ function Wa(a, c, b) { } if (a) { var b = P(a) ? a : a.preset; - b && (Ta[b] || console.warn("Preset not found: " + b), a = Object.assign({}, Ta[b], a)); + b && (Va[b] || console.warn("Preset not found: " + b), a = Object.assign({}, Va[b], a)); } else { a = {}; } b = a.context; - const e = !0 === b ? {depth:1} : b || {}, d = P(a.encoder) ? Pa[a.encoder] : a.encode || a.encoder || {}; + const e = !0 === b ? {depth:1} : b || {}, d = P(a.encoder) ? ua[a.encoder] : a.encode || a.encoder || {}; this.encoder = d.encode ? d : "object" === typeof d ? new ka(d) : {encode:d}; this.resolution = a.resolution || 9; this.tokenize = b = (b = a.tokenize) && "default" !== b && "exact" !== b && b || "strict"; @@ -1509,42 +1518,42 @@ function Wa(a, c, b) { this.reg = c || (this.fastupdate ? new Map() : new Set()); this.P = e.resolution || 3; this.rtl = d.rtl || a.rtl || !1; - this.cache = (b = a.cache || null) && new Y(b); + this.cache = (b = a.cache || null) && new W(b); this.priority = a.priority || 4; } -B = Z.prototype; -B.clear = function() { +A = Z.prototype; +A.clear = function() { this.map.clear(); this.ctx.clear(); this.reg.clear(); this.cache && this.cache.clear(); return this; }; -B.append = function(a, c) { +A.append = function(a, c) { return this.add(a, c, !0); }; -B.contain = function(a) { +A.contain = function(a) { return this.reg.has(a); }; -B.update = function(a, c) { +A.update = function(a, c) { const b = this, e = this.remove(a); return e && e.then ? e.then(() => b.add(a, c)) : this.add(a, c); }; -B.cleanup = function() { +A.cleanup = function() { if (!this.fastupdate) { return console.info('Cleanup the index isn\'t required when not using "fastupdate".'), this; } - Sa(this.map); - this.depth && Sa(this.ctx); + Ua(this.map); + this.depth && Ua(this.ctx); return this; }; -B.searchCache = Ra; -B.export = function(a, c, b = 0, e = 0) { +A.searchCache = la; +A.export = function(a, c, b = 0, e = 0) { let d, f; switch(e) { case 0: d = "reg"; - f = wa(this.reg); + f = Qa(this.reg); break; case 1: d = "cfg"; @@ -1552,33 +1561,33 @@ B.export = function(a, c, b = 0, e = 0) { break; case 2: d = "map"; - f = qa(this.map, this.reg.size); + f = Ma(this.map, this.reg.size); break; case 3: d = "ctx"; - f = sa(this.ctx, this.reg.size); + f = Oa(this.ctx, this.reg.size); break; default: return; } - return ya.call(this, a, c, d, f, b, e); + return Sa.call(this, a, c, d, f, b, e); }; -B.import = function(a, c) { +A.import = function(a, c) { if (c) { switch("string" === typeof c && (c = JSON.parse(c)), a = a.split("."), "json" === a[a.length - 1] && a.pop(), 3 === a.length && a.shift(), a = 1 < a.length ? a[1] : a[0], a) { case "reg": this.fastupdate = !1; - this.reg = xa(c, this.reg); + this.reg = Ra(c, this.reg); break; case "map": - this.map = ra(c, this.map); + this.map = Na(c, this.map); break; case "ctx": - this.ctx = va(c, this.ctx); + this.ctx = Pa(c, this.ctx); } } }; -B.serialize = function(a = !0) { +A.serialize = function(a = !0) { let c = "", b = "", e = ""; if (this.reg.size) { let f; @@ -1586,21 +1595,21 @@ B.serialize = function(a = !0) { f || (f = typeof d), c += (c ? "," : "") + ("string" === f ? '"' + d + '"' : d); } c = "index.reg=new Set([" + c + "]);"; - b = za(this.map, f); + b = Ta(this.map, f); b = "index.map=new Map([" + b + "]);"; for (const g of this.ctx.entries()) { d = g[0]; - let k = za(g[1], f); - k = "new Map([" + k + "])"; - k = '["' + d + '",' + k + "]"; - e += (e ? "," : "") + k; + let h = Ta(g[1], f); + h = "new Map([" + h + "])"; + h = '["' + d + '",' + h + "]"; + e += (e ? "," : "") + h; } e = "index.ctx=new Map([" + e + "]);"; } return a ? "function inject(index){" + c + b + e + "}" : c + b + e; }; -la(Z.prototype); -L(); -export default {Index:Z, Charset:Pa, Encoder:ka, Document:X, Worker:null, Resolver:null, IndexedDB:null, Language:{}}; +va(Z.prototype); +M(); +export default {Index:Z, Charset:ua, Encoder:ka, Document:Y, Worker:null, Resolver:null, IndexedDB:null, Language:{}}; -export const Index=Z;export const Charset=Pa;export const Encoder=ka;export const Document=X;export const Worker=null;export const Resolver=null;export const IndexedDB=null;export const Language={}; \ No newline at end of file +export const Index=Z;export const Charset=ua;export const Encoder=ka;export const Document=Y;export const Worker=null;export const Resolver=null;export const IndexedDB=null;export const Language={}; \ No newline at end of file diff --git a/dist/flexsearch.compact.module.min.js b/dist/flexsearch.compact.module.min.js index 1fbbc65..c6d5b91 100644 --- a/dist/flexsearch.compact.module.min.js +++ b/dist/flexsearch.compact.module.min.js @@ -1,58 +1,58 @@ /**! - * FlexSearch.js v0.8.167 (Bundle) + * FlexSearch.js v0.8.200 (Bundle) * Author and Copyright: Thomas Wilkerling * Licence: Apache-2.0 * Hosted by Nextapps GmbH * https://github.com/nextapps-de/flexsearch */ -var A;function H(a,c,b){const e=typeof b,d=typeof a;if("undefined"!==e){if("undefined"!==d){if(b){if("function"===d&&e===d)return function(k){return a(b(k))};c=a.constructor;if(c===b.constructor){if(c===Array)return b.concat(a);if(c===Map){var f=new Map(b);for(var g of a)f.set(g[0],g[1]);return f}if(c===Set){g=new Set(b);for(f of a.values())g.add(f);return g}}}return a}return b}return"undefined"===d?c:a}function I(a,c){return"undefined"===typeof a?c:a}function N(){return Object.create(null)} +var A;function I(a,c,b){const e=typeof b,d=typeof a;if("undefined"!==e){if("undefined"!==d){if(b){if("function"===d&&e===d)return function(h){return a(b(h))};c=a.constructor;if(c===b.constructor){if(c===Array)return b.concat(a);if(c===Map){var f=new Map(b);for(var g of a)f.set(g[0],g[1]);return f}if(c===Set){g=new Set(b);for(f of a.values())g.add(f);return g}}}return a}return b}return"undefined"===d?c:a}function J(a,c){return"undefined"===typeof a?c:a}function N(){return Object.create(null)} function P(a){return"string"===typeof a}function R(a){return"object"===typeof a}function S(a,c){if(P(c))a=a[c];else for(let b=0;a&&b"a1a".split(b).length; -this.numeric=H(a.numeric,e)}else{try{this.split=H(this.split,aa)}catch(d){this.split=/\s+/}this.numeric=H(a.numeric,H(this.numeric,!0))}this.prepare=H(a.prepare,null,this.prepare);this.finalize=H(a.finalize,null,this.finalize);b=a.filter;this.filter="function"===typeof b?b:H(b&&new Set(b),null,this.filter);this.dedupe=H(a.dedupe,!0,this.dedupe);this.matcher=H((b=a.matcher)&&new Map(b),null,this.matcher);this.mapper=H((b=a.mapper)&&new Map(b),null,this.mapper);this.stemmer=H((b=a.stemmer)&&new Map(b), -null,this.stemmer);this.replacer=H(a.replacer,null,this.replacer);this.minlength=H(a.minlength,1,this.minlength);this.maxlength=H(a.maxlength,1024,this.maxlength);this.rtl=H(a.rtl,!1,this.rtl);if(this.cache=b=H(a.cache,!0,this.cache))this.H=null,this.O="number"===typeof b?b:2E5,this.F=new Map,this.G=new Map,this.L=this.K=128;this.h="";this.M=null;this.C="";this.N=null;if(this.matcher)for(const d of this.matcher.keys())this.h+=(this.h?"|":"")+d;if(this.stemmer)for(const d of this.stemmer.keys())this.C+= -(this.C?"|":"")+d;return this};A.addStemmer=function(a,c){this.stemmer||(this.stemmer=new Map);this.stemmer.set(a,c);this.C+=(this.C?"|":"")+a;this.N=null;this.cache&&V(this);return this};A.addFilter=function(a){"function"===typeof a?this.filter=a:(this.filter||(this.filter=new Set),this.filter.add(a));this.cache&&V(this);return this}; -A.addMapper=function(a,c){if("object"===typeof a)return this.addReplacer(a,c);if(1a.length&&(this.dedupe||this.mapper))return this.addMapper(a,c);this.matcher||(this.matcher=new Map);this.matcher.set(a,c);this.h+=(this.h?"|":"")+a;this.M=null;this.cache&&V(this);return this}; -A.addReplacer=function(a,c){if("string"===typeof a)return this.addMatcher(a,c);this.replacer||(this.replacer=[]);this.replacer.push(a,c);this.cache&&V(this);return this}; -A.encode=function(a,c){if(this.cache&&a.length<=this.K)if(this.H){if(this.F.has(a))return this.F.get(a)}else this.H=setTimeout(V,50,this);this.normalize&&("function"===typeof this.normalize?a=this.normalize(a):a=ha?a.normalize("NFKD").replace(ha,"").toLowerCase():a.toLowerCase());this.prepare&&(a=this.prepare(a));this.numeric&&3this.maxlength)){if(c){if(d[n])continue;d[n]=1}else{if(f===n)continue;f=n}if(b)e.push(n);else if(!this.filter||("function"===typeof this.filter?this.filter(n):!this.filter.has(n))){if(this.cache&&n.length<=this.L)if(this.H){var h=this.G.get(n);if(h||""===h){h&&e.push(h);continue}}else this.H=setTimeout(V,50,this);if(this.stemmer){this.N||(this.N=new RegExp("(?!^)("+ -this.C+")$"));let x;for(;x!==n&&2this.stemmer.get(q))}if(n&&(this.mapper||this.dedupe&&1this.matcher.get(x)));if(n&&this.replacer)for(h=0;n&&hthis.O&&(this.G.clear(),this.L=this.L/1.1|0));if(n){if(n!==B)if(c){if(d[n])continue;d[n]=1}else{if(g===n)continue;g=n}e.push(n)}}}this.finalize&&(e=this.finalize(e)||e);this.cache&&a.length<=this.K&&(this.F.set(a,e),this.F.size>this.O&&(this.F.clear(),this.K=this.K/1.1|0));return e};function V(a){a.H=null;a.F.clear();a.G.clear()};function la(a){W.call(a,"add");W.call(a,"append");W.call(a,"search");W.call(a,"update");W.call(a,"remove");W.call(a,"searchCache")}let ma,na,oa;function pa(){ma=oa=0} -function W(a){this[a+"Async"]=function(){const c=arguments;var b=c[c.length-1];let e;"function"===typeof b&&(e=b,delete c[c.length-1]);ma?oa||(oa=Date.now()-na>=this.priority*this.priority*3):(ma=setTimeout(pa,0),na=Date.now());if(oa){const f=this;return new Promise(g=>{setTimeout(function(){g(f[a+"Async"].apply(f,c))},0)})}const d=this[a].apply(this,c);b=d.then?d:new Promise(f=>f(d));e&&b.then(e);return b}};function qa(a,c=0){let b=[],e=[];c&&(c=25E4/c*5E3|0);for(const d of a.entries())e.push(d),e.length===c&&(b.push(e),e=[]);e.length&&b.push(e);return b}function ra(a,c){c||(c=new Map);for(let b=0,e;bc?c?a.slice(b,b+c):a.slice(b):a;let e=[];for(let d=0,f,g;d=g){b-=g;continue}bc&&(f=f.slice(0,c),g=c);if(!e.length&&g>=c)return f;e.push(f);c-=g;if(!c)break}return e=1E&&(E=l.length+(l?1:0)),t=l.length+(l?1:0)+K.length,C+=D,ja.push(v.length),v.push({match:K})),l+=(l?" ":"")+K)}if(!p)z=u[y],l+=(l?" ":"")+z,h&&v.push({text:z});else if(h&&C>=h)break}C=ja.length*(f.length-2);if(q||r||h&&l.length-C>h)if(C=h+C-2*x,y=t-E,0u&&(u=0)),v=v.length-1){if(F>=v.length){y[p+1]= -1;F>=u.length&&(C[p+1]=1);continue}D-=x}l=v[F].text;if(G=r&&J[p])if(0G)if(y[p+1]=1,m)l=l.substring(0,G);else continue;(G-=l.length)||(G=-1);J[p]=G}else{y[p+1]=1;continue}if(D+l.length+1<=h)l=" "+l,t[p]+=l;else if(m)O=h-D-1,0=F){if(0>F){y[p]=1;C[p]=1;continue}D-=x}l=v[F].text;if(G=q&&z[p])if(0G)if(y[p]=1,m)l=l.substring(l.length- -G);else continue;(G-=l.length)||(G=-1);z[p]=G}else{y[p]=1;continue}if(D+l.length+1<=h)l+=" ",t[p]=l+t[p];else if(m)O=l.length+1-(h-D),0<=O&&O=u.length-1?ua=1:Fb||e)a=a.slice(e,e+b);d&&(a=Ha.call(this,a))}return a}function Ha(a){if(!this||!this.store)return a;const c=Array(a.length);for(let b=0,e;b"a1a".split(b).length; +this.numeric=I(a.numeric,e)}else{try{this.split=I(this.split,aa)}catch(d){this.split=/\s+/}this.numeric=I(a.numeric,I(this.numeric,!0))}this.prepare=I(a.prepare,null,this.prepare);this.finalize=I(a.finalize,null,this.finalize);b=a.filter;this.filter="function"===typeof b?b:I(b&&new Set(b),null,this.filter);this.dedupe=I(a.dedupe,!0,this.dedupe);this.matcher=I((b=a.matcher)&&new Map(b),null,this.matcher);this.mapper=I((b=a.mapper)&&new Map(b),null,this.mapper);this.stemmer=I((b=a.stemmer)&&new Map(b), +null,this.stemmer);this.replacer=I(a.replacer,null,this.replacer);this.minlength=I(a.minlength,1,this.minlength);this.maxlength=I(a.maxlength,1024,this.maxlength);this.rtl=I(a.rtl,!1,this.rtl);if(this.cache=b=I(a.cache,!0,this.cache))this.H=null,this.O="number"===typeof b?b:2E5,this.F=new Map,this.G=new Map,this.L=this.K=128;this.h="";this.M=null;this.C="";this.N=null;if(this.matcher)for(const d of this.matcher.keys())this.h+=(this.h?"|":"")+d;if(this.stemmer)for(const d of this.stemmer.keys())this.C+= +(this.C?"|":"")+d;return this};A.addStemmer=function(a,c){this.stemmer||(this.stemmer=new Map);this.stemmer.set(a,c);this.C+=(this.C?"|":"")+a;this.N=null;this.cache&&T(this);return this};A.addFilter=function(a){"function"===typeof a?this.filter=a:(this.filter||(this.filter=new Set),this.filter.add(a));this.cache&&T(this);return this}; +A.addMapper=function(a,c){if("object"===typeof a)return this.addReplacer(a,c);if(1a.length&&(this.dedupe||this.mapper))return this.addMapper(a,c);this.matcher||(this.matcher=new Map);this.matcher.set(a,c);this.h+=(this.h?"|":"")+a;this.M=null;this.cache&&T(this);return this}; +A.addReplacer=function(a,c){if("string"===typeof a)return this.addMatcher(a,c);this.replacer||(this.replacer=[]);this.replacer.push(a,c);this.cache&&T(this);return this}; +A.encode=function(a,c){if(this.cache&&a.length<=this.K)if(this.H){if(this.F.has(a))return this.F.get(a)}else this.H=setTimeout(T,50,this);this.normalize&&("function"===typeof this.normalize?a=this.normalize(a):a=ha?a.normalize("NFKD").replace(ha,"").toLowerCase():a.toLowerCase());this.prepare&&(a=this.prepare(a));this.numeric&&3this.maxlength)){if(c){if(d[l])continue;d[l]=1}else{if(f===l)continue;f=l}if(b)e.push(l);else if(!this.filter||("function"===typeof this.filter?this.filter(l):!this.filter.has(l))){if(this.cache&&l.length<=this.L)if(this.H){var k=this.G.get(l);if(k||""===k){k&&e.push(k);continue}}else this.H=setTimeout(T,50,this);if(this.stemmer){this.N||(this.N=new RegExp("(?!^)("+ +this.C+")$"));let w;for(;w!==l&&2this.stemmer.get(t))}if(l&&(this.mapper||this.dedupe&&1this.matcher.get(w)));if(l&&this.replacer)for(k=0;l&&kthis.O&&(this.G.clear(),this.L=this.L/1.1|0));if(l){if(l!==C)if(c){if(d[l])continue;d[l]=1}else{if(g===l)continue;g=l}e.push(l)}}}this.finalize&&(e=this.finalize(e)||e);this.cache&&a.length<=this.K&&(this.F.set(a,e),this.F.size>this.O&&(this.F.clear(),this.K=this.K/1.1|0));return e};function T(a){a.H=null;a.F.clear();a.G.clear()};function la(a,c,b){b||(c||"object"!==typeof a?"object"===typeof c&&(b=c,c=0):b=a);b&&(a=b.query||a,c=b.limit||c);let e=""+(c||0);b&&(e+=(b.offset||0)+!!b.context+!!b.suggest+(!1!==b.resolve)+(b.resolution||this.resolution)+(b.boost||0));a=(""+a).toLowerCase();this.cache||(this.cache=new W);let d=this.cache.get(a+e);if(!d){const f=b&&b.cache;f&&(b.cache=!1);d=this.search(a,c,b);f&&(b.cache=f);this.cache.set(a+e,d)}return d}function W(a){this.limit=a&&!0!==a?a:1E3;this.cache=new Map;this.h=""} +W.prototype.set=function(a,c){this.cache.set(this.h=a,c);this.cache.size>this.limit&&this.cache.delete(this.cache.keys().next().value)};W.prototype.get=function(a){const c=this.cache.get(a);c&&this.h!==a&&(this.cache.delete(a),this.cache.set(this.h=a,c));return c};W.prototype.remove=function(a){for(const c of this.cache){const b=c[0];c[1].includes(a)&&this.cache.delete(b)}};W.prototype.clear=function(){this.cache.clear();this.h=""};const ma={normalize:!1,numeric:!1,dedupe:!1};const na={};const oa=new Map([["b","p"],["v","f"],["w","f"],["z","s"],["x","s"],["d","t"],["n","m"],["c","k"],["g","k"],["j","k"],["q","k"],["i","e"],["y","e"],["u","o"]]);const pa=new Map([["ae","a"],["oe","o"],["sh","s"],["kh","k"],["th","t"],["ph","f"],["pf","f"]]),qa=[/([^aeo])h(.)/g,"$1$2",/([aeo])h([^aeo]|$)/g,"$1$2",/(.)\1+/g,"$1"];const ra={a:"",e:"",i:"",o:"",u:"",y:"",b:1,f:1,p:1,v:1,c:2,g:2,j:2,k:2,q:2,s:2,x:2,z:2,"\u00df":2,d:3,t:3,l:4,m:5,n:5,r:6};var ua={Exact:ma,Default:na,Normalize:na,LatinBalance:{mapper:oa},LatinAdvanced:{mapper:oa,matcher:pa,replacer:qa},LatinExtra:{mapper:oa,replacer:qa.concat([/(?!^)[aeo]/g,""]),matcher:pa},LatinSoundex:{dedupe:!1,include:{letter:!0},finalize:function(a){for(let b=0;b=this.priority*this.priority*3):(wa=setTimeout(za,0),xa=Date.now());if(ya){const f=this;return new Promise(g=>{setTimeout(function(){g(f[a+"Async"].apply(f,c))},0)})}const d=this[a].apply(this,c);b=d.then?d:new Promise(f=>f(d));e&&b.then(e);return b}};Y.prototype.add=function(a,c,b){R(a)&&(c=a,a=S(c,this.key));if(c&&(a||0===a)){if(!b&&this.reg.has(a))return this.update(a,c);for(let h=0,k;hc?a.slice(b,b+c):a;let e=[];for(let d=0,f,g;d=g){b-=g;continue}f=f.slice(b,b+c);g=f.length;b=0}g>c&&(f=f.slice(0,c),g=c);if(!e.length&&g>=c)return f;e.push(f);c-=g;if(!c)break}return e=1E&&(E=m.length+(m?1:0)),z=m.length+(m?1:0)+L.length,r+=F,ja.push(u.length),u.push({match:L})),m+=(m?" ":"")+L)}if(!q)D=v[y],m+=(m?" ":"")+D,k&&u.push({text:D});else if(k&&r>=k)break}r=ja.length*(f.length-2);if(t||p||k&&m.length-r>k)if(r=k+r-2*w,y=z-E,0v&&(v=0)),u=u.length-1){if(G>=u.length){y[q+1]= +1;G>=v.length&&(r[q+1]=1);continue}F-=w}m=u[G].text;if(H=p&&B[q])if(0H)if(y[q+1]=1,n)m=m.substring(0,H);else continue;(H-=m.length)||(H=-1);B[q]=H}else{y[q+1]=1;continue}if(F+m.length+1<=k)m=" "+m,z[q]+=m;else if(n)O=k-F-1,0=G){if(0>G){y[q]=1;r[q]=1;continue}F-=w}m=u[G].text;if(H=t&&D[q])if(0H)if(y[q]=1,n)m=m.substring(m.length- +H);else continue;(H-=m.length)||(H=-1);D[q]=H}else{y[q]=1;continue}if(F+m.length+1<=k)m+=" ",z[q]=m+z[q];else if(n)O=m.length+1-(k-F),0<=O&&O=v.length-1?ta=1:Gb||e)a=a.slice(e,e+b);d&&(a=Ga.call(this,a))}return a}function Ga(a){if(!this||!this.store)return a;const c=Array(a.length);for(let b=0,e;bthis.limit&&this.cache.delete(this.cache.keys().next().value)}; -Y.prototype.get=function(a){const c=this.cache.get(a);c&&this.h!==a&&(this.cache.delete(a),this.cache.set(this.h=a,c));return c};Y.prototype.remove=function(a){for(const c of this.cache){const b=c[0];c[1].includes(a)&&this.cache.delete(b)}};Y.prototype.clear=function(){this.cache.clear();this.h=""};Z.prototype.remove=function(a,c){const b=this.reg.size&&(this.fastupdate?this.reg.get(a):this.reg.has(a));if(b){if(this.fastupdate)for(let e=0,d,f;ew;f--){g=r.substring(w,f);l=this.rtl?d-1-w:w;var k=this.score?this.score(c,r,q,g,l):Ua(x, -m,q,d,l);Va(this,B,g,k,a,b)}break}case "bidirectional":case "reverse":if(1g?0:1),m,q,k-1,h-1),l=this.bidirectional&&r>f;Va(this,n,l?f:r,w,a,b,l?r:f)}}}}this.fastupdate||this.reg.add(a)}}return this};function Va(a,c,b,e,d,f,g){let k=g?a.ctx:a.map,h;if(!c[b]||g&&!(h=c[b])[g])g?(c=h||(c[b]=N()),c[g]=1,(h=k.get(g))?k=h:k.set(g,k=new Map)):c[b]=1,(h=k.get(b))?k=h:k.set(b,k=[]),k=k[e]||(k[e]=[]),f&&k.includes(d)||(k.push(d),a.fastupdate&&((c=a.reg.get(d))?c.push(k):a.reg.set(d,[k])))} -function Ua(a,c,b,e,d){return b&&1c||d?g.slice(d,c+d):g;g=b}else{if(b< -n){n=[];break b}g=g[b-1];if(c||d)if(g.length>c||d)g=g.slice(d,c+d)}n=g}else if(1===e){c=Ca.call(null,b[0],c,d);break a}c=n}return c};function Wa(a,c,b){let e;b&&(e=a.bidirectional&&c>b)&&(e=b,b=c,c=e);a=b?(a=a.ctx.get(b))&&a.get(c):a.map.get(c);return a};function Z(a,c){if(!this||this.constructor!==Z)return new Z(a);if(a){var b=P(a)?a:a.preset;b&&(a=Object.assign({},Ta[b],a))}else a={};b=a.context;const e=!0===b?{depth:1}:b||{},d=P(a.encoder)?Pa[a.encoder]:a.encode||a.encoder||{};this.encoder=d.encode?d:"object"===typeof d?new ka(d):{encode:d};this.resolution=a.resolution||9;this.tokenize=b=(b=a.tokenize)&&"default"!==b&&"exact"!==b&&b||"strict";this.depth="strict"===b&&e.depth||0;this.bidirectional=!1!==e.bidirectional;this.fastupdate=!!a.fastupdate; -this.score=a.score||null;b=!1;this.map=new Map;this.ctx=new Map;this.reg=c||(this.fastupdate?new Map:new Set);this.P=e.resolution||3;this.rtl=d.rtl||a.rtl||!1;this.cache=(b=a.cache||null)&&new Y(b);this.priority=a.priority||4}A=Z.prototype;A.clear=function(){this.map.clear();this.ctx.clear();this.reg.clear();this.cache&&this.cache.clear();return this};A.append=function(a,c){return this.add(a,c,!0)};A.contain=function(a){return this.reg.has(a)}; -A.update=function(a,c){const b=this,e=this.remove(a);return e&&e.then?e.then(()=>b.add(a,c)):this.add(a,c)};A.cleanup=function(){if(!this.fastupdate)return this;Sa(this.map);this.depth&&Sa(this.ctx);return this};A.searchCache=Ra;A.export=function(a,c,b=0,e=0){let d,f;switch(e){case 0:d="reg";f=wa(this.reg);break;case 1:d="cfg";f=null;break;case 2:d="map";f=qa(this.map,this.reg.size);break;case 3:d="ctx";f=sa(this.ctx,this.reg.size);break;default:return}return ya.call(this,a,c,d,f,b,e)}; -A.import=function(a,c){if(c)switch("string"===typeof c&&(c=JSON.parse(c)),a=a.split("."),"json"===a[a.length-1]&&a.pop(),3===a.length&&a.shift(),a=1x;f--){g=p.substring(x,f);m=this.rtl?d-1-x:x;var h=this.score?this.score(c,p,t,g,m):Wa(w, +n,t,d,m);Xa(this,C,g,h,a,b)}break}case "bidirectional":case "reverse":if(1g?0:1),n,t,h-1,k-1),m=this.bidirectional&&p>f;Xa(this,l,m?f:p,x,a,b,m?p:f)}}}}this.fastupdate||this.reg.add(a)}}return this};function Xa(a,c,b,e,d,f,g){let h=g?a.ctx:a.map,k;if(!c[b]||g&&!(k=c[b])[g])g?(c=k||(c[b]=N()),c[g]=1,(k=h.get(g))?h=k:h.set(g,h=new Map)):c[b]=1,(k=h.get(b))?h=k:h.set(b,h=[]),h=h[e]||(h[e]=[]),f&&h.includes(d)||(h.push(d),a.fastupdate&&((c=a.reg.get(d))?c.push(h):a.reg.set(d,[h])))} +function Wa(a,c,b,e,d){return b&&1c||d?g.slice(d,c+d):g;g=b}else{if(b< +l){l=[];break b}g=g[b-1];if(c||d)if(g.length>c||d)g=g.slice(d,c+d)}l=g}else if(1===e){c=Ca.call(null,b[0],c,d);break a}c=l}return c};function Ya(a,c,b){let e;b&&(e=a.bidirectional&&c>b)&&(e=b,b=c,c=e);a=b?(a=a.ctx.get(b))&&a.get(c):a.map.get(c);return a};function Z(a,c){if(!this||this.constructor!==Z)return new Z(a);if(a){var b=P(a)?a:a.preset;b&&(a=Object.assign({},Va[b],a))}else a={};b=a.context;const e=!0===b?{depth:1}:b||{},d=P(a.encoder)?ua[a.encoder]:a.encode||a.encoder||{};this.encoder=d.encode?d:"object"===typeof d?new ka(d):{encode:d};this.resolution=a.resolution||9;this.tokenize=b=(b=a.tokenize)&&"default"!==b&&"exact"!==b&&b||"strict";this.depth="strict"===b&&e.depth||0;this.bidirectional=!1!==e.bidirectional;this.fastupdate=!!a.fastupdate; +this.score=a.score||null;b=!1;this.map=new Map;this.ctx=new Map;this.reg=c||(this.fastupdate?new Map:new Set);this.P=e.resolution||3;this.rtl=d.rtl||a.rtl||!1;this.cache=(b=a.cache||null)&&new W(b);this.priority=a.priority||4}A=Z.prototype;A.clear=function(){this.map.clear();this.ctx.clear();this.reg.clear();this.cache&&this.cache.clear();return this};A.append=function(a,c){return this.add(a,c,!0)};A.contain=function(a){return this.reg.has(a)}; +A.update=function(a,c){const b=this,e=this.remove(a);return e&&e.then?e.then(()=>b.add(a,c)):this.add(a,c)};A.cleanup=function(){if(!this.fastupdate)return this;Ua(this.map);this.depth&&Ua(this.ctx);return this};A.searchCache=la;A.export=function(a,c,b=0,e=0){let d,f;switch(e){case 0:d="reg";f=Qa(this.reg);break;case 1:d="cfg";f=null;break;case 2:d="map";f=Ma(this.map,this.reg.size);break;case 3:d="ctx";f=Oa(this.ctx,this.reg.size);break;default:return}return Sa.call(this,a,c,d,f,b,e)}; +A.import=function(a,c){if(c)switch("string"===typeof c&&(c=JSON.parse(c)),a=a.split("."),"json"===a[a.length-1]&&a.pop(),3===a.length&&a.shift(),a=1 c && (c = Math.max(c + e, 0)); c < e; c++) { - var f = d[c]; - if (f === b || Object.is(f, b)) { + var g = d[c]; + if (g === b || Object.is(g, b)) { return !0; } } @@ -800,6 +793,27 @@ H("String.prototype.includes", function(a) { return -1 !== this.indexOf(b, c || 0); }; }); +H("Array.prototype.entries", function(a) { + return a ? a : function() { + return ya(this, function(b, c) { + return [b, c]; + }); + }; +}); +var Aa = "function" == typeof Object.assign ? Object.assign : function(a, b) { + for (var c = 1; c < arguments.length; c++) { + var d = arguments[c]; + if (d) { + for (var e in d) { + za(d, e) && (a[e] = d[e]); + } + } + } + return a; +}; +H("Object.assign", function(a) { + return a || Aa; +}); H("Array.prototype.flat", function(a) { return a ? a : function(b) { b = void 0 === b ? 1 : b; @@ -810,20 +824,6 @@ H("Array.prototype.flat", function(a) { return c; }; }); -var Ba = "function" == typeof Object.assign ? Object.assign : function(a, b) { - for (var c = 1; c < arguments.length; c++) { - var d = arguments[c]; - if (d) { - for (var e in d) { - Aa(d, e) && (a[e] = d[e]); - } - } - } - return a; -}; -H("Object.assign", function(a) { - return a || Ba; -}); H("Promise.prototype.finally", function(a) { return a ? a : function(b) { return this.then(function(c) { @@ -843,8 +843,8 @@ function P(a, b, c) { if ("undefined" !== e) { if (c) { if ("function" === e && d === e) { - return function(f) { - return a(c(f)); + return function(g) { + return a(c(g)); }; } b = a.constructor; @@ -854,7 +854,7 @@ function P(a, b, c) { } if (b === Map) { b = new Map(c); - d = A(a); + d = C(a); for (e = d.next(); !e.done; e = d.next()) { e = e.value, b.set(e[0], e[1]); } @@ -862,7 +862,7 @@ function P(a, b, c) { } if (b === Set) { b = new Set(c); - d = A(a.values()); + d = C(a.values()); for (e = d.next(); !e.done; e = d.next()) { b.add(e.value); } @@ -876,7 +876,7 @@ function P(a, b, c) { } return "undefined" === e ? b : a; } -function Ca(a, b) { +function Ba(a, b) { return "undefined" === typeof a ? b : a; } function S() { @@ -885,18 +885,18 @@ function S() { function T(a) { return "string" === typeof a; } -function Da(a) { +function Ca(a) { return "object" === typeof a; } -function Ea(a) { +function Da(a) { var b = []; - a = A(a.keys()); + a = C(a.keys()); for (var c = a.next(); !c.done; c = a.next()) { b.push(c.value); } return b; } -function Fa(a, b) { +function Ea(a, b) { if (T(b)) { a = a[b]; } else { @@ -906,27 +906,21 @@ function Fa(a, b) { } return a; } -function Ga(a) { - for (var b = 0, c = 0, d = void 0; c < a.length; c++) { - (d = a[c]) && b < d.length && (b = d.length); - } - return b; -} -;var Ha = /[^\p{L}\p{N}]+/u, Ia = /(\d{3})/g, Ja = /(\D)(\d{3})/g, Ka = /(\d{3})(\D)/g, La = /[\u0300-\u036f]/g; -function Ma(a) { +;var Fa = /[^\p{L}\p{N}]+/u, Ga = /(\d{3})/g, Ha = /(\D)(\d{3})/g, Ia = /(\d{3})(\D)/g, Ja = /[\u0300-\u036f]/g; +function Ka(a) { a = void 0 === a ? {} : a; - if (!this || this.constructor !== Ma) { + if (!this || this.constructor !== Ka) { var b = Function.prototype.bind, c = b.apply, d = [null], e = d.concat; if (arguments instanceof Array) { - var f = arguments; + var g = arguments; } else { - f = A(arguments); - for (var g, h = []; !(g = f.next()).done;) { - h.push(g.value); + g = C(arguments); + for (var f, h = []; !(f = g.next()).done;) { + h.push(f.value); } - f = h; + g = h; } - return new (c.call(b, Ma, e.call(d, f)))(); + return new (c.call(b, Ka, e.call(d, g)))(); } if (arguments.length) { for (b = 0; b < arguments.length; b++) { @@ -936,8 +930,8 @@ function Ma(a) { this.assign(a); } } -v = Ma.prototype; -v.assign = function(a) { +w = Ka.prototype; +w.assign = function(a) { this.normalize = P(a.normalize, !0, this.normalize); var b = a.include, c = b || a.exclude || a.split; if (c || "" === c) { @@ -955,7 +949,7 @@ v.assign = function(a) { } try { this.split = new RegExp("[" + (b ? "^" : "") + d + "]+", "u"); - } catch (f) { + } catch (g) { console.error("Your split configuration:", c, "is not supported on this platform. It falls back to using simple whitespace splitter instead: /s+/."), this.split = /\s+/; } } else { @@ -964,8 +958,8 @@ v.assign = function(a) { this.numeric = P(a.numeric, e); } else { try { - this.split = P(this.split, Ha); - } catch (f) { + this.split = P(this.split, Fa); + } catch (g) { console.warn("This platform does not support unicode regex. It falls back to using simple whitespace splitter instead: /s+/."), this.split = /\s+/; } this.numeric = P(a.numeric, P(this.numeric, !0)); @@ -983,38 +977,38 @@ v.assign = function(a) { this.maxlength = P(a.maxlength, 1024, this.maxlength); this.rtl = P(a.rtl, !1, this.rtl); if (this.cache = c = P(a.cache, !0, this.cache)) { - this.D = null, this.T = "number" === typeof c ? c : 2e5, this.B = new Map(), this.C = new Map(), this.H = this.G = 128; + this.D = null, this.U = "number" === typeof c ? c : 2e5, this.B = new Map(), this.C = new Map(), this.I = this.H = 128; } this.h = ""; - this.M = null; - this.A = ""; this.N = null; + this.A = ""; + this.O = null; if (this.matcher) { - for (a = A(this.matcher.keys()), b = a.next(); !b.done; b = a.next()) { + for (a = C(this.matcher.keys()), b = a.next(); !b.done; b = a.next()) { this.h += (this.h ? "|" : "") + b.value; } } if (this.stemmer) { - for (a = A(this.stemmer.keys()), b = a.next(); !b.done; b = a.next()) { + for (a = C(this.stemmer.keys()), b = a.next(); !b.done; b = a.next()) { this.A += (this.A ? "|" : "") + b.value; } } return this; }; -v.addStemmer = function(a, b) { +w.addStemmer = function(a, b) { this.stemmer || (this.stemmer = new Map()); this.stemmer.set(a, b); this.A += (this.A ? "|" : "") + a; - this.N = null; - this.cache && Na(this); + this.O = null; + this.cache && La(this); return this; }; -v.addFilter = function(a) { +w.addFilter = function(a) { "function" === typeof a ? this.filter = a : (this.filter || (this.filter = new Set()), this.filter.add(a)); - this.cache && Na(this); + this.cache && La(this); return this; }; -v.addMapper = function(a, b) { +w.addMapper = function(a, b) { if ("object" === typeof a) { return this.addReplacer(a, b); } @@ -1023,10 +1017,10 @@ v.addMapper = function(a, b) { } this.mapper || (this.mapper = new Map()); this.mapper.set(a, b); - this.cache && Na(this); + this.cache && La(this); return this; }; -v.addMatcher = function(a, b) { +w.addMatcher = function(a, b) { if ("object" === typeof a) { return this.addReplacer(a, b); } @@ -1036,91 +1030,91 @@ v.addMatcher = function(a, b) { this.matcher || (this.matcher = new Map()); this.matcher.set(a, b); this.h += (this.h ? "|" : "") + a; - this.M = null; - this.cache && Na(this); + this.N = null; + this.cache && La(this); return this; }; -v.addReplacer = function(a, b) { +w.addReplacer = function(a, b) { if ("string" === typeof a) { return this.addMatcher(a, b); } this.replacer || (this.replacer = []); this.replacer.push(a, b); - this.cache && Na(this); + this.cache && La(this); return this; }; -v.encode = function(a, b) { +w.encode = function(a, b) { var c = this; - if (this.cache && a.length <= this.G) { + if (this.cache && a.length <= this.H) { if (this.D) { if (this.B.has(a)) { return this.B.get(a); } } else { - this.D = setTimeout(Na, 50, this); + this.D = setTimeout(La, 50, this); } } - this.normalize && ("function" === typeof this.normalize ? a = this.normalize(a) : a = La ? a.normalize("NFKD").replace(La, "").toLowerCase() : a.toLowerCase()); + this.normalize && ("function" === typeof this.normalize ? a = this.normalize(a) : a = Ja ? a.normalize("NFKD").replace(Ja, "").toLowerCase() : a.toLowerCase()); this.prepare && (a = this.prepare(a)); - this.numeric && 3 < a.length && (a = a.replace(Ja, "$1 $2").replace(Ka, "$1 $2").replace(Ia, "$1 ")); - for (var d = !(this.dedupe || this.mapper || this.filter || this.matcher || this.stemmer || this.replacer), e = [], f = S(), g, h, k = this.split || "" === this.split ? a.split(this.split) : [a], l = 0, m = void 0, q = void 0; l < k.length; l++) { - if ((m = q = k[l]) && !(m.length < this.minlength || m.length > this.maxlength)) { + this.numeric && 3 < a.length && (a = a.replace(Ha, "$1 $2").replace(Ia, "$1 $2").replace(Ga, "$1 ")); + for (var d = !(this.dedupe || this.mapper || this.filter || this.matcher || this.stemmer || this.replacer), e = [], g = S(), f, h, k = this.split || "" === this.split ? a.split(this.split) : [a], l = 0, m = void 0, p = void 0; l < k.length; l++) { + if ((m = p = k[l]) && !(m.length < this.minlength || m.length > this.maxlength)) { if (b) { - if (f[m]) { + if (g[m]) { continue; } - f[m] = 1; + g[m] = 1; } else { - if (g === m) { + if (f === m) { continue; } - g = m; + f = m; } if (d) { e.push(m); } else { if (!this.filter || ("function" === typeof this.filter ? this.filter(m) : !this.filter.has(m))) { - if (this.cache && m.length <= this.H) { + if (this.cache && m.length <= this.I) { if (this.D) { - var p = this.C.get(m); - if (p || "" === p) { - p && e.push(p); + var n = this.C.get(m); + if (n || "" === n) { + n && e.push(n); continue; } } else { - this.D = setTimeout(Na, 50, this); + this.D = setTimeout(La, 50, this); } } if (this.stemmer) { - for (this.N || (this.N = new RegExp("(?!^)(" + this.A + ")$")), p = void 0; p !== m && 2 < m.length;) { - p = m, m = m.replace(this.N, function(w) { - return c.stemmer.get(w); + for (this.O || (this.O = new RegExp("(?!^)(" + this.A + ")$")), n = void 0; n !== m && 2 < m.length;) { + n = m, m = m.replace(this.O, function(x) { + return c.stemmer.get(x); }); } } if (m && (this.mapper || this.dedupe && 1 < m.length)) { - p = ""; - for (var n = 0, r = "", u = void 0, x = void 0; n < m.length; n++) { - u = m.charAt(n), u === r && this.dedupe || ((x = this.mapper && this.mapper.get(u)) || "" === x ? x === r && this.dedupe || !(r = x) || (p += x) : p += r = u); + n = ""; + for (var q = 0, t = "", u = void 0, v = void 0; q < m.length; q++) { + u = m.charAt(q), u === t && this.dedupe || ((v = this.mapper && this.mapper.get(u)) || "" === v ? v === t && this.dedupe || !(t = v) || (n += v) : n += t = u); } - m = p; + m = n; } - this.matcher && 1 < m.length && (this.M || (this.M = new RegExp("(" + this.h + ")", "g")), m = m.replace(this.M, function(w) { - return c.matcher.get(w); + this.matcher && 1 < m.length && (this.N || (this.N = new RegExp("(" + this.h + ")", "g")), m = m.replace(this.N, function(x) { + return c.matcher.get(x); })); if (m && this.replacer) { - for (p = 0; m && p < this.replacer.length; p += 2) { - m = m.replace(this.replacer[p], this.replacer[p + 1]); + for (n = 0; m && n < this.replacer.length; n += 2) { + m = m.replace(this.replacer[n], this.replacer[n + 1]); } } - this.cache && q.length <= this.H && (this.C.set(q, m), this.C.size > this.T && (this.C.clear(), this.H = this.H / 1.1 | 0)); + this.cache && p.length <= this.I && (this.C.set(p, m), this.C.size > this.U && (this.C.clear(), this.I = this.I / 1.1 | 0)); if (m) { - if (m !== q) { + if (m !== p) { if (b) { - if (f[m]) { + if (g[m]) { continue; } - f[m] = 1; + g[m] = 1; } else { if (h === m) { continue; @@ -1135,266 +1129,79 @@ v.encode = function(a, b) { } } this.finalize && (e = this.finalize(e) || e); - this.cache && a.length <= this.G && (this.B.set(a, e), this.B.size > this.T && (this.B.clear(), this.G = this.G / 1.1 | 0)); + this.cache && a.length <= this.H && (this.B.set(a, e), this.B.size > this.U && (this.B.clear(), this.H = this.H / 1.1 | 0)); return e; }; -function Na(a) { +function La(a) { a.D = null; a.B.clear(); a.C.clear(); } -;var Oa, Pa; -function Qa(a) { - var b, c, d, e, f, g; - return xa(function(h) { - switch(h.h) { - case 1: - a = a.data; - b = a.task; - c = a.id; - d = a.args; - switch(b) { - case "init": - Pa = a.options || {}; - (e = a.factory) ? (Function("return " + e)()(self), Oa = new self.FlexSearch.Index(Pa), delete self.FlexSearch) : Oa = new W(Pa); - postMessage({id:c}); - break; - default: - h.h = 2; - return; - }h.h = 0; - break; - case 2: - if ("export" === b) { - if (!Pa.export || "function" !== typeof Pa.export) { - throw Error('Either no extern configuration provided for the Worker-Index or no method was defined on the config property "export".'); - } - d[1] ? (d[0] = Pa.export, d[2] = 0, d[3] = 1) : d = null; - } - if ("import" === b) { - if (!Pa.import || "function" !== typeof Pa.import) { - throw Error('Either no extern configuration provided for the Worker-Index or no method was defined on the config property "import".'); - } - if (!d[0]) { - h.h = 5; - break; - } - return L(h, Pa.import.call(Oa, d[0]), 9); - } - f = d && Oa[b].apply(Oa, d); - if (!f || !f.then) { - h.h = 5; - break; - } - return L(h, f, 7); - case 7: - f = h.D; - h.h = 5; - break; - case 9: - g = h.D, Oa.import(d[0], g); - case 5: - postMessage("search" === b ? {id:c, msg:f} : {id:c}), h.h = 0; - } - }); -} -;function Ra(a) { - Sa.call(a, "add"); - Sa.call(a, "append"); - Sa.call(a, "search"); - Sa.call(a, "update"); - Sa.call(a, "remove"); - Sa.call(a, "searchCache"); -} -var Ta, Ua, Va; -function Wa() { - Ta = Va = 0; -} -function Sa(a) { - this[a + "Async"] = function() { - var b = arguments, c = b[b.length - 1]; - if ("function" === typeof c) { - var d = c; - delete b[b.length - 1]; - } - Ta ? Va || (Va = Date.now() - Ua >= this.priority * this.priority * 3) : (Ta = setTimeout(Wa, 0), Ua = Date.now()); - if (Va) { - var e = this; - return new Promise(function(g) { - setTimeout(function() { - g(e[a + "Async"].apply(e, b)); - }, 0); - }); - } - var f = this[a].apply(this, b); - c = f.then ? f : new Promise(function(g) { - return g(f); - }); - d && c.then(d); - return c; - }; -} -;var Xa = 0; -function Ya(a, b) { - function c(h) { - function k(l) { - l = l.data || l; - var m = l.id, q = m && f.h[m]; - q && (q(l.msg), delete f.h[m]); - } - this.worker = h; - this.h = S(); - if (this.worker) { - e ? this.worker.on("message", k) : this.worker.onmessage = k; - if (a.config) { - return new Promise(function(l) { - f.h[++Xa] = function() { - l(f); - 1e9 < Xa && (Xa = 0); - }; - f.worker.postMessage({id:Xa, task:"init", factory:d, options:a}); - }); - } - this.priority = a.priority || 4; - this.encoder = b || null; - this.worker.postMessage({task:"init", factory:d, options:a}); - return this; - } +;function Ma(a, b, c) { + c || (b || "object" !== typeof a ? "object" === typeof b && (c = b, b = 0) : c = a); + c && (a = c.query || a, b = c.limit || b); + var d = "" + (b || 0); + if (c) { + var e = c; + d += (e.offset || 0) + !!e.context + !!e.suggest + (!1 !== e.resolve) + (e.resolution || this.resolution) + (e.boost || 0); } - a = void 0 === a ? {} : a; - if (!this || this.constructor !== Ya) { - return new Ya(a); + a = ("" + a).toLowerCase(); + this.cache || (this.cache = new Na()); + e = this.cache.get(a + d); + if (!e) { + var g = c && c.cache; + g && (c.cache = !1); + e = this.search(a, b, c); + g && (c.cache = g); + this.cache.set(a + d, e); } - var d = "undefined" !== typeof self ? self._factory : "undefined" !== typeof window ? window._factory : null; - d && (d = d.toString()); - var e = "undefined" === typeof window, f = this, g = Za(d, e, a.worker); - return g.then ? g.then(function(h) { - return c.call(f, h); - }) : c.call(this, g); + return e; } -$a("add"); -$a("append"); -$a("search"); -$a("searchCache"); -$a("update"); -$a("remove"); -$a("clear"); -$a("export"); -$a("import"); -Ra(Ya.prototype); -function $a(a) { - Ya.prototype[a] = function() { - var b = this, c = [].slice.call(arguments), d = c[c.length - 1]; - if ("function" === typeof d) { - var e = d; - c.pop(); - } - d = new Promise(function(f) { - "export" === a && "function" === typeof c[0] && (c[0] = null); - b.h[++Xa] = f; - b.worker.postMessage({task:a, id:Xa, args:c}); - }); - return e ? (d.then(e), this) : d; - }; +function Na(a) { + this.limit = a && !0 !== a ? a : 1000; + this.cache = new Map(); + this.h = ""; } -function Za(a, b, c) { - return b ? "undefined" !== typeof module ? new(require("worker_threads")["Worker"])(__dirname+"/node/node.js") : import("worker_threads").then(function(worker){return new worker["Worker"]((1,eval)("import.meta.dirname")+"/node/node.mjs")}) : a ? new window.Worker(URL.createObjectURL(new Blob(["onmessage=" + Qa.toString()], {type:"text/javascript"}))) : new window.Worker("string" === typeof c ? c : (0,eval)("import.meta.url").replace("/worker.js", "/worker/worker.js").replace("flexsearch.bundle.module.min.js", - "module/worker/worker.js"), {type:"module"}); -} -;function ab(a, b) { - b = void 0 === b ? 0 : b; - var c = [], d = []; - b && (b = 250000 / b * 5000 | 0); - a = A(a.entries()); - for (var e = a.next(); !e.done; e = a.next()) { - d.push(e.value), d.length === b && (c.push(d), d = []); - } - d.length && c.push(d); - return c; -} -function bb(a, b) { - b || (b = new Map()); - for (var c = 0, d; c < a.length; c++) { - d = a[c], b.set(d[0], d[1]); - } +Na.prototype.set = function(a, b) { + this.cache.set(this.h = a, b); + this.cache.size > this.limit && this.cache.delete(this.cache.keys().next().value); +}; +Na.prototype.get = function(a) { + var b = this.cache.get(a); + b && this.h !== a && (this.cache.delete(a), this.cache.set(this.h = a, b)); return b; -} -function cb(a, b) { - b = void 0 === b ? 0 : b; - var c = [], d = []; - b && (b = 250000 / b * 1000 | 0); - a = A(a.entries()); - for (var e = a.next(); !e.done; e = a.next()) { - e = e.value, d.push([e[0], ab(e[1])[0]]), d.length === b && (c.push(d), d = []); +}; +Na.prototype.remove = function(a) { + for (var b = C(this.cache), c = b.next(); !c.done; c = b.next()) { + c = c.value; + var d = c[0]; + c[1].includes(a) && this.cache.delete(d); } - d.length && c.push(d); - return c; -} -function db(a, b) { - b || (b = new Map()); - for (var c = 0, d, e; c < a.length; c++) { - d = a[c], e = b.get(d[0]), b.set(d[0], bb(d[1], e)); - } - return b; -} -function eb(a) { - var b = [], c = []; - a = A(a.keys()); - for (var d = a.next(); !d.done; d = a.next()) { - c.push(d.value), 250000 === c.length && (b.push(c), c = []); - } - c.length && b.push(c); - return b; -} -function fb(a, b) { - b || (b = new Set()); - for (var c = 0; c < a.length; c++) { - b.add(a[c]); - } - return b; -} -function gb(a, b, c, d, e, f, g) { - g = void 0 === g ? 0 : g; - var h = d && d.constructor === Array, k = h ? d.shift() : d; - if (!k) { - return this.export(a, b, e, f + 1); - } - if ((k = a((b ? b + "." : "") + (g + 1) + "." + c, JSON.stringify(k))) && k.then) { - var l = this; - return k.then(function() { - return gb.call(l, a, b, c, h ? d : null, e, f, g + 1); - }); - } - return gb.call(this, a, b, c, h ? d : null, e, f, g + 1); -} -function hb(a, b) { - var c = ""; - a = A(a.entries()); - for (var d = a.next(); !d.done; d = a.next()) { - var e = d.value; - d = e[0]; - e = e[1]; - for (var f = "", g = 0, h; g < e.length; g++) { - h = e[g] || [""]; - for (var k = "", l = 0; l < h.length; l++) { - k += (k ? "," : "") + ("string" === b ? '"' + h[l] + '"' : h[l]); - } - k = "[" + k + "]"; - f += (f ? "," : "") + k; +}; +Na.prototype.clear = function() { + this.cache.clear(); + this.h = ""; +}; +var Oa = {normalize:!1, numeric:!1, dedupe:!1}; +var Pa = {}; +var Qa = new Map([["b", "p"], ["v", "f"], ["w", "f"], ["z", "s"], ["x", "s"], ["d", "t"], ["n", "m"], ["c", "k"], ["g", "k"], ["j", "k"], ["q", "k"], ["i", "e"], ["y", "e"], ["u", "o"]]); +var Ra = new Map([["ae", "a"], ["oe", "o"], ["sh", "s"], ["kh", "k"], ["th", "t"], ["ph", "f"], ["pf", "f"]]), Sa = [/([^aeo])h(.)/g, "$1$2", /([aeo])h([^aeo]|$)/g, "$1$2", /(.)\1+/g, "$1"]; +var Ta = {a:"", e:"", i:"", o:"", u:"", y:"", b:1, f:1, p:1, v:1, c:2, g:2, j:2, k:2, q:2, s:2, x:2, z:2, "\u00df":2, d:3, t:3, l:4, m:5, n:5, r:6}; +var Ua = {Exact:Oa, Default:Pa, Normalize:Pa, LatinBalance:{mapper:Qa}, LatinAdvanced:{mapper:Qa, matcher:Ra, replacer:Sa}, LatinExtra:{mapper:Qa, replacer:Sa.concat([/(?!^)[aeo]/g, ""]), matcher:Ra}, LatinSoundex:{dedupe:!1, include:{letter:!0}, finalize:function(a) { + for (var b = 0; b < a.length; b++) { + for (var c = a[b], d = c.charAt(0), e = Ta[d], g = 1, f; g < c.length && (f = c.charAt(g), "h" === f || "w" === f || !(f = Ta[f]) || f === e || (d += f, e = f, 4 !== d.length)); g++) { } - f = '["' + d + '",[' + f + "]]"; - c += (c ? "," : "") + f; + a[b] = d; } - return c; -} -;function ib(a, b, c, d) { - for (var e = [], f = 0, g; f < a.index.length; f++) { - if (g = a.index[f], b >= g.length) { - b -= g.length; +}}, CJK:{split:""}, LatinExact:Oa, LatinDefault:Pa, LatinSimple:Pa}; +function Va(a, b, c, d) { + for (var e = [], g = 0, f; g < a.index.length; g++) { + if (f = a.index[g], b >= f.length) { + b -= f.length; } else { - b = g[d ? "splice" : "slice"](b, c); - if (g = b.length) { - if (e = e.length ? e.concat(b) : b, c -= g, d && (a.length -= g), !c) { + b = f[d ? "splice" : "slice"](b, c); + if (f = b.length) { + if (e = e.length ? e.concat(b) : b, c -= f, d && (a.length -= f), !c) { break; } } @@ -1403,9 +1210,9 @@ function hb(a, b) { } return e; } -function jb(a) { - if (!this || this.constructor !== jb) { - return new jb(a); +function Wa(a) { + if (!this || this.constructor !== Wa) { + return new Wa(a); } this.index = a ? [a] : []; this.length = a ? a.length : 0; @@ -1429,21 +1236,21 @@ function jb(a) { } if ("indexOf" === d) { return function(e) { - for (var f = 0, g = 0, h, k; g < b.index.length; g++) { - h = b.index[g]; + for (var g = 0, f = 0, h, k; f < b.index.length; f++) { + h = b.index[f]; k = h.indexOf(e); if (0 <= k) { - return f + k; + return g + k; } - f += h.length; + g += h.length; } return -1; }; } if ("includes" === d) { return function(e) { - for (var f = 0; f < b.index.length; f++) { - if (b.index[f].includes(e)) { + for (var g = 0; g < b.index.length; g++) { + if (b.index[g].includes(e)) { return !0; } } @@ -1451,13 +1258,13 @@ function jb(a) { }; } if ("slice" === d) { - return function(e, f) { - return ib(b, e || 0, f || b.length, !1); + return function(e, g) { + return Va(b, e || 0, g || b.length, !1); }; } if ("splice" === d) { - return function(e, f) { - return ib(b, e || 0, f || b.length, !0); + return function(e, g) { + return Va(b, e || 0, g || b.length, !0); }; } if ("constructor" === d) { @@ -1473,13 +1280,31 @@ function jb(a) { return !0; }}); } -jb.prototype.clear = function() { +Wa.prototype.clear = function() { this.index.length = 0; }; -jb.prototype.destroy = function() { +Wa.prototype.destroy = function() { this.proxy = this.index = null; }; -jb.prototype.push = function() { +Wa.prototype.push = function() { +}; +function Xa(a) { + a = void 0 === a ? 8 : a; + if (!this || this.constructor !== Xa) { + return new Xa(a); + } + this.index = S(); + this.h = []; + this.size = 0; + 32 < a ? (this.A = Ya, this.B = BigInt(a)) : (this.A = Za, this.B = a); +} +Xa.prototype.get = function(a) { + var b = this.A(a); + return (b = this.index[b]) && b.get(a); +}; +Xa.prototype.set = function(a, b) { + var c = this.A(a), d = this.index[c]; + d ? (c = d.size, d.set(a, b), (c -= d.size) && this.size++) : (this.index[c] = d = new Map([[a, b]]), this.h.push(d), this.size++); }; function X(a) { a = void 0 === a ? 8 : a; @@ -1489,123 +1314,105 @@ function X(a) { this.index = S(); this.h = []; this.size = 0; - 32 < a ? (this.A = kb, this.B = BigInt(a)) : (this.A = lb, this.B = a); + 32 < a ? (this.A = Ya, this.B = BigInt(a)) : (this.A = Za, this.B = a); } -X.prototype.get = function(a) { - var b = this.A(a); - return (b = this.index[b]) && b.get(a); -}; -X.prototype.set = function(a, b) { - var c = this.A(a), d = this.index[c]; - d ? (c = d.size, d.set(a, b), (c -= d.size) && this.size++) : (this.index[c] = d = new Map([[a, b]]), this.h.push(d), this.size++); -}; -function Y(a) { - a = void 0 === a ? 8 : a; - if (!this || this.constructor !== Y) { - return new Y(a); - } - this.index = S(); - this.h = []; - this.size = 0; - 32 < a ? (this.A = kb, this.B = BigInt(a)) : (this.A = lb, this.B = a); -} -Y.prototype.add = function(a) { +X.prototype.add = function(a) { var b = this.A(a), c = this.index[b]; c ? (b = c.size, c.add(a), (b -= c.size) && this.size++) : (this.index[b] = c = new Set([a]), this.h.push(c), this.size++); }; -v = X.prototype; -v.has = Y.prototype.has = function(a) { +w = Xa.prototype; +w.has = X.prototype.has = function(a) { var b = this.A(a); return (b = this.index[b]) && b.has(a); }; -v.delete = Y.prototype.delete = function(a) { +w.delete = X.prototype.delete = function(a) { var b = this.A(a); (b = this.index[b]) && b.delete(a) && this.size--; }; -v.clear = Y.prototype.clear = function() { +w.clear = X.prototype.clear = function() { this.index = S(); this.h = []; this.size = 0; }; -v.values = Y.prototype.values = function mb() { - var b, c = this, d, e, f; - return va(mb, function(g) { - switch(g.h) { +w.values = X.prototype.values = function $a() { + var b, c = this, d, e, g; + return ua($a, function(f) { + switch(f.h) { case 1: b = 0; case 2: if (!(b < c.h.length)) { - g.h = 0; + f.h = 0; break; } - d = A(c.h[b].values()); + d = C(c.h[b].values()); e = d.next(); case 5: if (e.done) { b++; - g.h = 2; + f.h = 2; break; } - f = e.value; - return L(g, f, 6); + g = e.value; + return K(f, g, 6); case 6: - e = d.next(), g.h = 5; + e = d.next(), f.h = 5; } }); }; -v.keys = Y.prototype.keys = function nb() { - var b, c = this, d, e, f; - return va(nb, function(g) { - switch(g.h) { +w.keys = X.prototype.keys = function ab() { + var b, c = this, d, e, g; + return ua(ab, function(f) { + switch(f.h) { case 1: b = 0; case 2: if (!(b < c.h.length)) { - g.h = 0; + f.h = 0; break; } - d = A(c.h[b].keys()); + d = C(c.h[b].keys()); e = d.next(); case 5: if (e.done) { b++; - g.h = 2; + f.h = 2; break; } - f = e.value; - return L(g, f, 6); + g = e.value; + return K(f, g, 6); case 6: - e = d.next(), g.h = 5; + e = d.next(), f.h = 5; } }); }; -v.entries = Y.prototype.entries = function ob() { - var b, c = this, d, e, f; - return va(ob, function(g) { - switch(g.h) { +w.entries = X.prototype.entries = function bb() { + var b, c = this, d, e, g; + return ua(bb, function(f) { + switch(f.h) { case 1: b = 0; case 2: if (!(b < c.h.length)) { - g.h = 0; + f.h = 0; break; } - d = A(c.h[b].entries()); + d = C(c.h[b].entries()); e = d.next(); case 5: if (e.done) { b++; - g.h = 2; + f.h = 2; break; } - f = e.value; - return L(g, f, 6); + g = e.value; + return K(f, g, 6); case 6: - e = d.next(), g.h = 5; + e = d.next(), f.h = 5; } }); }; -function lb(a) { +function Za(a) { var b = Math.pow(2, this.B) - 1; if ("number" == typeof a) { return a & b; @@ -1615,61 +1422,231 @@ function lb(a) { } return 32 === this.B ? c + Math.pow(2, 31) : c; } -function kb() { +function Ya() { throw Error("The keystore is limited to 32 for EcmaScript5"); } +;var cb, db; +function eb(a) { + var b, c, d, e, g, f; + return wa(function(h) { + switch(h.h) { + case 1: + a = a.data; + b = a.task; + c = a.id; + d = a.args; + switch(b) { + case "init": + db = a.options || {}; + (e = a.factory) ? (Function("return " + e)()(self), cb = new self.FlexSearch.Index(db), delete self.FlexSearch) : cb = new Y(db); + postMessage({id:c}); + break; + default: + h.h = 2; + return; + }h.h = 0; + break; + case 2: + if ("export" === b) { + if (!db.export || "function" !== typeof db.export) { + throw Error('Either no extern configuration provided for the Worker-Index or no method was defined on the config property "export".'); + } + d[1] ? (d[0] = db.export, d[2] = 0, d[3] = 1) : d = null; + } + if ("import" === b) { + if (!db.import || "function" !== typeof db.import) { + throw Error('Either no extern configuration provided for the Worker-Index or no method was defined on the config property "import".'); + } + if (!d[0]) { + h.h = 5; + break; + } + return K(h, db.import.call(cb, d[0]), 11); + } + g = d && cb[b].apply(cb, d); + if (!g || !g.then) { + h.h = 6; + break; + } + return K(h, g, 7); + case 7: + g = h.B; + case 6: + if (!g || !g.await) { + h.h = 8; + break; + } + return K(h, g.await, 9); + case 9: + g = h.B; + case 8: + "search" === b && g.result && (g = g.result); + h.h = 5; + break; + case 11: + f = h.B, cb.import(d[0], f); + case 5: + postMessage("search" === b ? {id:c, msg:g} : {id:c}), h.h = 0; + } + }); +} +;function fb(a) { + gb.call(a, "add"); + gb.call(a, "append"); + gb.call(a, "search"); + gb.call(a, "update"); + gb.call(a, "remove"); + gb.call(a, "searchCache"); +} +var hb, ib, jb; +function kb() { + hb = jb = 0; +} +function gb(a) { + this[a + "Async"] = function() { + var b = arguments, c = b[b.length - 1]; + if ("function" === typeof c) { + var d = c; + delete b[b.length - 1]; + } + hb ? jb || (jb = Date.now() - ib >= this.priority * this.priority * 3) : (hb = setTimeout(kb, 0), ib = Date.now()); + if (jb) { + var e = this; + return new Promise(function(f) { + setTimeout(function() { + f(e[a + "Async"].apply(e, b)); + }, 0); + }); + } + var g = this[a].apply(this, b); + c = g.then ? g : new Promise(function(f) { + return f(g); + }); + d && c.then(d); + return c; + }; +} +;var lb = 0; +function mb(a, b) { + function c(h) { + function k(l) { + l = l.data || l; + var m = l.id, p = m && g.h[m]; + p && (p(l.msg), delete g.h[m]); + } + this.worker = h; + this.h = S(); + if (this.worker) { + e ? this.worker.on("message", k) : this.worker.onmessage = k; + if (a.config) { + return new Promise(function(l) { + 1e9 < lb && (lb = 0); + g.h[++lb] = function() { + l(g); + }; + g.worker.postMessage({id:lb, task:"init", factory:d, options:a}); + }); + } + this.priority = a.priority || 4; + this.encoder = b || null; + this.worker.postMessage({task:"init", factory:d, options:a}); + return this; + } + console.warn("Worker is not available on this platform. Please report on Github: https://github.com/nextapps-de/flexsearch/issues"); + } + a = void 0 === a ? {} : a; + if (!this || this.constructor !== mb) { + return new mb(a); + } + var d = "undefined" !== typeof self ? self._factory : "undefined" !== typeof window ? window._factory : null; + d && (d = d.toString()); + var e = "undefined" === typeof window, g = this, f = nb(d, e, a.worker); + return f.then ? f.then(function(h) { + return c.call(g, h); + }) : c.call(this, f); +} +ob("add"); +ob("append"); +ob("search"); +ob("update"); +ob("remove"); +ob("clear"); +ob("export"); +ob("import"); +mb.prototype.searchCache = Ma; +fb(mb.prototype); +function ob(a) { + mb.prototype[a] = function() { + var b = this, c = [].slice.call(arguments), d = c[c.length - 1]; + if ("function" === typeof d) { + var e = d; + c.pop(); + } + d = new Promise(function(g) { + "export" === a && "function" === typeof c[0] && (c[0] = null); + 1e9 < lb && (lb = 0); + b.h[++lb] = g; + b.worker.postMessage({task:a, id:lb, args:c}); + }); + return e ? (d.then(e), this) : d; + }; +} +function nb(a, b, c) { + return b ? "undefined" !== typeof module ? new(require("worker_threads")["Worker"])(__dirname+"/node/node.js") : import("worker_threads").then(function(worker){return new worker["Worker"]((1,eval)("import.meta.dirname")+"/node/node.mjs")}) : a ? new window.Worker(URL.createObjectURL(new Blob(["onmessage=" + eb.toString()], {type:"text/javascript"}))) : new window.Worker("string" === typeof c ? c : (0,eval)("import.meta.url").replace("/worker.js", "/worker/worker.js").replace("flexsearch.bundle.module.min.js", + "module/worker/worker.js"), {type:"module"}); +} ;pb.prototype.add = function(a, b, c) { - Da(a) && (b = a, a = Fa(b, this.key)); + Ca(a) && (b = a, a = Ea(b, this.key)); if (b && (a || 0 === a)) { if (!c && this.reg.has(a)) { return this.update(a, b); } for (var d = 0, e; d < this.field.length; d++) { - e = this.L[d]; - var f = this.index.get(this.field[d]); + e = this.M[d]; + var g = this.index.get(this.field[d]); if ("function" === typeof e) { - (e = e(b)) && f.add(a, e, !1, !0); + (e = e(b)) && g.add(a, e, !1, !0); } else { - var g = e.R; - if (!g || g(b)) { - e.constructor === String ? e = ["" + e] : T(e) && (e = [e]), qb(b, e, this.S, 0, f, a, e[0], c); + var f = e.S; + if (!f || f(b)) { + e.constructor === String ? e = ["" + e] : T(e) && (e = [e]), qb(b, e, this.T, 0, g, a, e[0], c); } } } if (this.tag) { - for (d = 0; d < this.K.length; d++) { - g = this.K[d]; - var h = this.aa[d]; - f = this.tag.get(h); + for (d = 0; d < this.L.length; d++) { + f = this.L[d]; + var h = this.ba[d]; + g = this.tag.get(h); e = S(); - if ("function" === typeof g) { - if (g = g(b), !g) { + if ("function" === typeof f) { + if (f = f(b), !f) { continue; } } else { - var k = g.R; + var k = f.S; if (k && !k(b)) { continue; } - g.constructor === String && (g = "" + g); - g = Fa(b, g); + f.constructor === String && (f = "" + f); + f = Ea(b, f); } - if (f && g) { - for (T(g) && (g = [g]), h = 0, k = void 0; h < g.length; h++) { - var l = g[h]; + if (g && f) { + for (T(f) && (f = [f]), h = 0, k = void 0; h < f.length; h++) { + var l = f[h]; if (!e[l]) { e[l] = 1; var m; - (m = f.get(l)) ? k = m : f.set(l, k = []); + (m = g.get(l)) ? k = m : g.set(l, k = []); if (!c || !k.includes(a)) { if (k.length === Math.pow(2, 31) - 1) { - m = new jb(k); + m = new Wa(k); if (this.fastupdate) { - for (var q = A(this.reg.values()), p = q.next(); !p.done; p = q.next()) { - p = p.value, p.includes(k) && (p[p.indexOf(k)] = m); + for (var p = C(this.reg.values()), n = p.next(); !n.done; n = p.next()) { + n = n.value, n.includes(k) && (n[n.indexOf(k)] = m); } } - f.set(l, k = m); + g.set(l, k = m); } k.push(a); this.fastupdate && ((l = this.reg.get(a)) ? l.push(k) : this.reg.set(a, [k])); @@ -1677,40 +1654,40 @@ function kb() { } } } else { - f || console.warn("Tag '" + h + "' was not found"); + g || console.warn("Tag '" + h + "' was not found"); } } } if (this.store && (!c || !this.store.has(a))) { - if (this.I) { - var n = S(); - for (c = 0; c < this.I.length; c++) { - if (d = this.I[c], f = d.R, !f || f(b)) { - f = void 0; + if (this.J) { + var q = S(); + for (c = 0; c < this.J.length; c++) { + if (d = this.J[c], g = d.S, !g || g(b)) { + g = void 0; if ("function" === typeof d) { - f = d(b); - if (!f) { + g = d(b); + if (!g) { continue; } - d = [d.ja]; + d = [d.na]; } else if (T(d) || d.constructor === String) { - n[d] = b[d]; + q[d] = b[d]; continue; } - rb(b, n, d, 0, d[0], f); + rb(b, q, d, 0, d[0], g); } } } - this.store.set(a, n || b); + this.store.set(a, q || b); } this.worker && (this.fastupdate || this.reg.add(a)); } return this; }; -function rb(a, b, c, d, e, f) { +function rb(a, b, c, d, e, g) { a = a[e]; if (d === c.length - 1) { - b[e] = f || a; + b[e] = g || a; } else if (a) { if (a.constructor === Array) { for (b = b[e] = Array(a.length), e = 0; e < a.length; e++) { @@ -1721,30 +1698,30 @@ function rb(a, b, c, d, e, f) { } } } -function qb(a, b, c, d, e, f, g, h) { - if (a = a[g]) { +function qb(a, b, c, d, e, g, f, h) { + if (a = a[f]) { if (d === b.length - 1) { if (a.constructor === Array) { if (c[d]) { for (b = 0; b < a.length; b++) { - e.add(f, a[b], !0, !0); + e.add(g, a[b], !0, !0); } return; } a = a.join(" "); } - e.add(f, a, h, !0); + e.add(g, a, h, !0); } else { if (a.constructor === Array) { - for (g = 0; g < a.length; g++) { - qb(a, b, c, d, e, f, g, h); + for (f = 0; f < a.length; f++) { + qb(a, b, c, d, e, g, f, h); } } else { - g = b[++d], qb(a, b, c, d, e, f, g, h); + f = b[++d], qb(a, b, c, d, e, g, f, h); } } } else { - e.db && e.remove(f); + e.db && e.remove(g); } } ;function sb(a, b, c, d) { @@ -1752,22 +1729,24 @@ function qb(a, b, c, d, e, f, g, h) { return a; } if (1 === a.length) { - return a = a[0], a = c || a.length > b ? b ? a.slice(c, c + b) : a.slice(c) : a, d ? tb.call(this, a) : a; + return a = a[0], a = c || a.length > b ? a.slice(c, c + b) : a, d ? tb.call(this, a) : a; } - for (var e = [], f = 0, g = void 0, h = void 0; f < a.length; f++) { - if ((g = a[f]) && (h = g.length)) { + for (var e = [], g = 0, f = void 0, h = void 0; g < a.length; g++) { + if ((f = a[g]) && (h = f.length)) { if (c) { if (c >= h) { c -= h; continue; } - c < h && (g = b ? g.slice(c, c + b) : g.slice(c), h = g.length, c = 0); + f = f.slice(c, c + b); + h = f.length; + c = 0; } - h > b && (g = g.slice(0, b), h = b); + h > b && (f = f.slice(0, b), h = b); if (!e.length && h >= b) { - return d ? tb.call(this, g) : g; + return d ? tb.call(this, f) : f; } - e.push(g); + e.push(f); b -= h; if (!b) { break; @@ -1777,484 +1756,319 @@ function qb(a, b, c, d, e, f, g, h) { e = 1 < e.length ? [].concat.apply([], e) : e[0]; return d ? tb.call(this, e) : e; } -;function ub(a, b, c) { - var d = c[0]; - if (d.then) { - return Promise.all(c).then(function(x) { - return a[b].apply(a, x); - }); +;function ub(a, b, c, d) { + var e = d[0]; + if (e[0] && e[0].query) { + return a[b].apply(a, e); } - if (d[0] && d[0].index) { - return a[b].apply(a, d); + if (!("and" !== b && "not" !== b || a.result.length || a.await || e.suggest)) { + return 1 < d.length && (e = d[d.length - 1]), (d = e.resolve) ? a.await || a.result : a; } - d = []; - for (var e = [], f = 0, g = 0, h, k, l, m, q, p = 0, n = void 0; p < c.length; p++) { - if (n = c[p]) { - var r = void 0; - if (n.constructor === Z) { - r = n.result; - } else if (n.constructor === Array) { - r = n; + var g = [], f = 0, h = 0, k; + b = {}; + for (e = 0; e < d.length; b = {aa:void 0, $:void 0, ca:void 0, fa:void 0}, e++) { + var l = d[e]; + if (l) { + var m = void 0; + if (l.constructor === Z) { + m = l.await || l.result; + } else if (l.then || l.constructor === Array) { + m = l; } else { - if (f = n.limit || 0, g = n.offset || 0, l = n.suggest, k = n.resolve, h = (m = n.highlight && k) || n.enrich && k, r = void 0, n.index ? a.index = r = n.index : r = a.index, n.query || n.tag) { + f = l.limit || 0; + h = l.offset || 0; + var p = l.suggest; + var n = l.resolve; + var q = (k = l.highlight && n) || l.enrich && n; + m = l.queue; + b.ca = l.async || m; + (k = l.index) ? a.index || (a.index = k) : k = a.index; + if (l.query || l.tag) { if (!a.index) { throw Error("Resolver can't apply because the corresponding Index was never specified"); } - var u = n.field || n.pluck; - if (u) { + var t = l.field || l.pluck; + if (t) { if (!a.index.index) { throw Error("Resolver can't apply because the corresponding Document Index was not specified"); } - r = a.index.index.get(u); - if (!r) { - throw Error("Resolver can't apply because the specified Document Field '" + u + "' was not found"); + k = a.index.index.get(t); + if (!k) { + throw Error("Resolver can't apply because the specified Document Field '" + t + "' was not found"); } } - n.resolve = !1; - r = r.search(n).result; - n.resolve = k; - m && (q = n.query); - } else if (n.and) { - r = a.and(n.and); - } else if (n.or) { - r = a.or(n.or); - } else if (n.xor) { - r = a.xor(n.xor); - } else if (n.not) { - r = a.not(n.not); + if (m && (u || a.await)) { + var u = 1; + b.aa = void 0; + b.fa = a.G.length; + b.$ = new Promise(function(x) { + return function(y) { + x.aa = y; + }; + }(b)); + (function(x) { + return function(y, E) { + x.$.H = function() { + E.index = null; + E.resolve = !1; + var B = x.ca ? y.searchAsync(E) : y.search(E); + if (B.then) { + return B.then(function(r) { + a.G[x.fa] = r = r.result || r; + (0,x.aa)(r); + return r; + }); + } + B = B.result || B; + (0,x.aa)(B); + return B; + }; + }; + })(b)(k, Object.assign({}, l)); + a.G.push(b.$); + g[e] = b.$; + continue; + } else { + l.resolve = !1, l.index = null, m = b.ca ? k.searchAsync(l) : k.search(l), l.resolve = n, l.index = k; + } + } else if (l.and) { + m = vb(l, "and", k); + } else if (l.or) { + m = vb(l, "or", k); + } else if (l.not) { + m = vb(l, "not", k); + } else if (l.xor) { + m = vb(l, "xor", k); } else { continue; } } - if (r.then) { - e.push(r); - } else if (r.length) { - d[p] = r; - } else if (!l && ("and" === b || "xor" === b)) { - d = []; - break; - } + m.await ? (u = 1, m = m.await) : m.then ? (u = 1, m = m.then(function(x) { + return x.result || x; + })) : m = m.result || m; + g[e] = m; } } - return {W:d, $:e, limit:f, offset:g, enrich:h, resolve:k, suggest:l, highlight:m, ma:q}; + u && !a.await && (a.await = new Promise(function(x) { + a.return = x; + })); + if (u) { + var v = Promise.all(g).then(function(x) { + for (var y = 0; y < a.G.length; y++) { + if (a.G[y] === v) { + a.G[y] = function() { + return c.call(a, x, f, h, q, n, p); + }; + break; + } + } + wb(a); + }); + a.G.push(v); + } else if (a.await) { + a.G.push(function() { + return c.call(a, g, f, h, q, n, p); + }); + } else { + return c.call(a, g, f, h, q, n, p); + } + return n ? a.await || a.result : a; +} +function vb(a, b, c) { + a = a[b]; + var d = a[0] || a; + d.index || (d.index = c); + c = new Z(d); + 1 < a.length && (c = c[b].apply(c, a.slice(1))); + return c; } ;Z.prototype.or = function() { - var a = ub(this, "or", arguments); - return vb.call(this, a.W, a.$, a.limit, a.offset, a.enrich, a.resolve); + return ub(this, "or", xb, arguments); }; -function vb(a, b, c, d, e, f) { - if (b.length) { - var g = this; - return Promise.all(b).then(function(h) { - a = []; - for (var k = 0, l = void 0; k < h.length; k++) { - (l = h[k]).length && (a[k] = l); - } - return vb.call(g, a, [], c, d, e, f); - }); - } - a.length && (this.result.length && a.push(this.result), 2 > a.length ? this.result = a[0] : (this.result = wb(a, c, d, !1, this.h), d = 0)); - return f ? this.resolve(c, d, e) : this; +function xb(a, b, c, d, e) { + a.length && (this.result.length && a.push(this.result), 2 > a.length ? this.result = a[0] : (this.result = yb(a, b, c, !1, this.h), c = 0)); + e && (this.await = null); + return e ? this.resolve(b, c, d) : this; } ;Z.prototype.and = function() { - var a = this.result.length; - if (!a) { - var b = arguments[0]; - if (b) { - a = !!b.suggest; - var c = b.resolve; - var d = b.limit; - var e = b.offset; - var f = b.enrich && c; - } - } - return a ? (a = ub(this, "and", arguments), xb.call(this, a.W, a.$, a.limit, a.offset, a.enrich, a.resolve, a.suggest)) : c ? this.resolve(d, e, f) : this; + return ub(this, "and", zb, arguments); }; -function xb(a, b, c, d, e, f, g) { - if (b.length) { - var h = this; - return Promise.all(b).then(function(k) { - a = []; - for (var l = 0, m = void 0; l < k.length; l++) { - (m = k[l]).length && (a[l] = m); - } - return xb.call(h, a, [], c, d, e, f, g); - }); +function zb(a, b, c, d, e, g) { + if (!g && !this.result.length) { + return e ? this.result : this; } if (a.length) { if (this.result.length && a.unshift(this.result), 2 > a.length) { this.result = a[0]; } else { - if (b = Ga(a)) { - return this.result = yb(a, b, c, d, g, this.h, f), f ? e ? tb.call(this.index, this.result) : this.result : this; + for (var f = 0, h = 0, k = void 0, l = void 0; h < a.length; h++) { + if ((k = a[h]) && (l = k.length)) { + f < l && (f = l); + } else if (!g) { + f = 0; + break; + } + } + if (f) { + this.result = Ab(a, f, b, c, g, this.h, e); + var m = !0; + } else { + this.result = []; } - this.result = []; } } else { g || (this.result = a); } - return f ? this.resolve(c, d, e) : this; + e && (this.await = null); + return e ? this.resolve(b, c, d, m) : this; } ;Z.prototype.xor = function() { - var a = ub(this, "xor", arguments); - return zb.call(this, a.W, a.$, a.limit, a.offset, a.enrich, a.resolve, a.suggest); + return ub(this, "xor", Bb, arguments); }; -function zb(a, b, c, d, e, f, g) { - if (b.length) { - var h = this; - return Promise.all(b).then(function(k) { - a = []; - for (var l = 0, m = void 0; l < k.length; l++) { - (m = k[l]).length && (a[l] = m); - } - return zb.call(h, a, [], c, d, e, f, g); - }); - } +function Bb(a, b, c, d, e, g) { if (a.length) { if (this.result.length && a.unshift(this.result), 2 > a.length) { this.result = a[0]; } else { - return this.result = Ab.call(this, a, c, d, f, this.h), f ? e ? tb.call(this.index, this.result) : this.result : this; + a: { + g = c; + var f = this.h; + for (var h = [], k = S(), l = 0, m = 0, p; m < a.length; m++) { + if (p = a[m]) { + l < p.length && (l = p.length); + for (var n = 0, q; n < p.length; n++) { + if (q = p[n]) { + for (var t = 0, u; t < q.length; t++) { + u = q[t], k[u] = k[u] ? 2 : 1; + } + } + } + } + } + for (p = m = 0; m < l; m++) { + for (n = 0; n < a.length; n++) { + if (q = a[n]) { + if (q = q[m]) { + for (t = 0; t < q.length; t++) { + if (u = q[t], 1 === k[u]) { + if (g) { + g--; + } else { + if (e) { + if (h.push(u), h.length === b) { + a = h; + break a; + } + } else { + var v = m + (n ? f : 0); + h[v] || (h[v] = []); + h[v].push(u); + if (++p === b) { + a = h; + break a; + } + } + } + } + } + } + } + } + } + a = h; + } + this.result = a; + f = !0; } } else { g || (this.result = a); } - return f ? this.resolve(c, d, e) : this; -} -function Ab(a, b, c, d, e) { - for (var f = [], g = S(), h = 0, k = 0, l; k < a.length; k++) { - if (l = a[k]) { - h < l.length && (h = l.length); - for (var m = 0, q; m < l.length; m++) { - if (q = l[m]) { - for (var p = 0, n; p < q.length; p++) { - n = q[p], g[n] = g[n] ? 2 : 1; - } - } - } - } - } - for (l = k = 0; k < h; k++) { - for (m = 0; m < a.length; m++) { - if (q = a[m]) { - if (q = q[k]) { - for (p = 0; p < q.length; p++) { - if (n = q[p], 1 === g[n]) { - if (c) { - c--; - } else { - if (d) { - if (f.push(n), f.length === b) { - return f; - } - } else { - var r = k + (m ? e : 0); - f[r] || (f[r] = []); - f[r].push(n); - if (++l === b) { - return f; - } - } - } - } - } - } - } - } - } - return f; + e && (this.await = null); + return e ? this.resolve(b, c, d, f) : this; } ;Z.prototype.not = function() { - var a = ub(this, "not", arguments); - return Bb.call(this, a.W, a.$, a.limit, a.offset, a.enrich, a.resolve, a.suggest); + return ub(this, "not", Cb, arguments); }; -function Bb(a, b, c, d, e, f, g) { - if (b.length) { - var h = this; - return Promise.all(b).then(function(k) { - a = []; - for (var l = 0, m = void 0; l < k.length; l++) { - (m = k[l]).length && (a[l] = m); - } - return Bb.call(h, a, [], c, d, e, f, g); - }); +function Cb(a, b, c, d, e, g) { + if (!g && !this.result.length) { + return e ? this.result : this; } if (a.length && this.result.length) { - this.result = Cb.call(this, a, c, d, f); - } else if (f) { - return this.resolve(c, d, e); - } - return f ? e ? tb.call(this.index, this.result) : this.result : this; -} -function Cb(a, b, c, d) { - var e = []; - a = new Set(a.flat().flat()); - for (var f = 0, g, h = 0; f < this.result.length; f++) { - if (g = this.result[f]) { - for (var k = 0, l; k < g.length; k++) { - if (l = g[k], !a.has(l)) { - if (c) { - c--; - } else { - if (d) { - if (e.push(l), e.length === b) { - return e; - } - } else { - if (e[f] || (e[f] = []), e[f].push(l), ++h === b) { - return e; - } - } - } - } - } - } - } - return e; -} -;function Db(a, b, c, d, e) { - if ("string" === typeof e) { - var f = e; - e = ""; - } else { - f = e.template; - } - if (!f) { - throw Error('No template pattern was specified by the search option "highlight"'); - } - var g = f.indexOf("$1"); - if (-1 === g) { - throw Error('Invalid highlight template. The replacement pattern "$1" was not found in template: ' + f); - } - var h = f.substring(g + 2); - g = f.substring(0, g); - var k = e && e.boundary, l = !e || !1 !== e.clip, m = e && e.merge && h && g && new RegExp(h + " " + g, "g"); - e = e && e.ellipsis; - var q = 0; - if ("object" === typeof e) { - var p = e.template; - q = p.length - 2; - e = e.pattern; - } - "string" !== typeof e && (e = !1 === e ? "" : "..."); - q && (e = p.replace("$1", e)); - p = e.length - q; - if ("object" === typeof k) { - var n = k.before; - 0 === n && (n = -1); - var r = k.after; - 0 === r && (r = -1); - k = k.total || 9e5; - } - q = new Map(); - for (var u, x = 0, w, E; x < b.length; x++) { - if (d) { - var B = b; - E = d; - } else { - B = b[x]; - E = B.field; - if (!E) { - continue; - } - B = B.result; - } - u = c.get(E); - w = u.encoder; - u = q.get(w); - "string" !== typeof u && (u = w.encode(a), q.set(w, u)); - for (var y = 0; y < B.length; y++) { - var t = B[y].doc; - if (t && (t = Fa(t, E))) { - var D = t.trim().split(/\s+/); - if (D.length) { - t = ""; - for (var C = [], O = [], Q = -1, M = -1, J = 0, F = 0; F < D.length; F++) { - var R = D[F], V = w.encode(R); - V = 1 < V.length ? V.join(" ") : V[0]; - var G = void 0; - if (V && R) { - for (var N = R.length, aa = (w.split ? R.replace(w.split, "") : R).length - V.length, ba = "", ma = 0, z = 0; z < u.length; z++) { - var I = u[z]; - if (I) { - var K = I.length; - K += aa; - ma && K <= ma || (I = V.indexOf(I), -1 < I && (ba = (I ? R.substring(0, I) : "") + g + R.substring(I, I + K) + h + (I + K < N ? R.substring(I + K) : ""), ma = K, G = !0)); - } - } - ba && (k && (0 > Q && (Q = t.length + (t ? 1 : 0)), M = t.length + (t ? 1 : 0) + ba.length, J += N, O.push(C.length), C.push({match:ba})), t += (t ? " " : "") + ba); - } - if (!G) { - R = D[F], t += (t ? " " : "") + R, k && C.push({text:R}); - } else if (k && J >= k) { - break; - } - } - J = O.length * (f.length - 2); - if (n || r || k && t.length - J > k) { - if (J = k + J - 2 * p, F = M - Q, 0 < n && (F += n), 0 < r && (F += r), F <= J) { - D = n ? Q - (0 < n ? n : 0) : Q - ((J - F) / 2 | 0), C = r ? M + (0 < r ? r : 0) : D + J, l || (0 < D && " " !== t.charAt(D) && " " !== t.charAt(D - 1) && (D = t.indexOf(" ", D), 0 > D && (D = 0)), C < t.length && " " !== t.charAt(C - 1) && " " !== t.charAt(C) && (C = t.lastIndexOf(" ", C), C < M ? C = M : ++C)), t = (D ? e : "") + t.substring(D, C) + (C < t.length ? e : ""); - } else { - M = []; - J = {}; - Q = {}; - F = {}; - R = {}; - V = {}; - aa = N = G = 0; - for (ma = ba = 1;;) { - I = void 0; - for (z = 0; z < O.length; z++) { - K = O[z]; - if (aa) { - if (N !== aa) { - if (F[z + 1]) { - continue; - } - K += aa; - if (J[K]) { - G -= p; - Q[z + 1] = 1; - F[z + 1] = 1; - continue; - } - if (K >= C.length - 1) { - if (K >= C.length) { - F[z + 1] = 1; - K >= D.length && (Q[z + 1] = 1); - continue; - } - G -= p; - } - t = C[K].text; - var U = r && V[z]; - if (U) { - if (0 < U) { - if (t.length > U) { - if (F[z + 1] = 1, l) { - t = t.substring(0, U); - } else { - continue; - } - } - (U -= t.length) || (U = -1); - V[z] = U; - } else { - F[z + 1] = 1; - continue; - } - } - if (G + t.length + 1 <= k) { - t = " " + t, M[z] += t; - } else if (l) { - I = k - G - 1, 0 < I && (t = " " + t.substring(0, I), M[z] += t), F[z + 1] = 1; - } else { - F[z + 1] = 1; - continue; - } - } else { - if (F[z]) { - continue; - } - K -= N; - if (J[K]) { - G -= p; - F[z] = 1; - Q[z] = 1; - continue; - } - if (0 >= K) { - if (0 > K) { - F[z] = 1; - Q[z] = 1; - continue; - } - G -= p; - } - t = C[K].text; - if (U = n && R[z]) { - if (0 < U) { - if (t.length > U) { - if (F[z] = 1, l) { - t = t.substring(t.length - U); - } else { - continue; - } - } - (U -= t.length) || (U = -1); - R[z] = U; - } else { - F[z] = 1; - continue; - } - } - if (G + t.length + 1 <= k) { - t += " ", M[z] = t + M[z]; - } else if (l) { - I = t.length + 1 - (k - G), 0 <= I && I < t.length && (t = t.substring(I) + " ", M[z] = t + M[z]), F[z] = 1; - } else { - F[z] = 1; - continue; - } - } - } else { - t = C[K].match; - n && (R[z] = n); - r && (V[z] = r); - z && G++; - I = void 0; - K ? !z && p && (G += p) : (Q[z] = 1, F[z] = 1); - K >= D.length - 1 ? I = 1 : K < C.length - 1 && C[K + 1].match ? I = 1 : p && (G += p); - G -= f.length - 2; - if (!z || G + t.length <= k) { - M[z] = t; - } else { - I = ba = ma = Q[z] = 0; - break; - } - I && (Q[z + 1] = 1, F[z + 1] = 1); + a: { + g = c; + var f = []; + a = new Set(a.flat().flat()); + for (var h = 0, k, l = 0; h < this.result.length; h++) { + if (k = this.result[h]) { + for (var m = 0, p; m < k.length; m++) { + if (p = k[m], !a.has(p)) { + if (g) { + g--; + } else { + if (e) { + if (f.push(p), f.length === b) { + g = f; + break a; } - G += t.length; - I = J[K] = 1; - } - if (I) { - N === aa ? aa++ : N++; } else { - N === aa ? ba = 0 : ma = 0; - if (!ba && !ma) { - break; + if (f[h] || (f[h] = []), f[h].push(p), ++l === b) { + g = f; + break a; } - ba ? (N++, aa = N) : aa++; } } - t = ""; - for (D = 0; D < M.length; D++) { - C = (D && Q[D] ? " " : (D && !e ? " " : "") + e) + M[D], t += C; - } - e && !Q[M.length] && (t += e); } } - m && (t = t.replace(m, " ")); - B[y].highlight = t; } } + g = f; } - if (d) { - break; - } + this.result = g; + f = !0; } - return b; + e && (this.await = null); + return e ? this.resolve(b, c, d, f) : this; } ;function Z(a, b) { if (!this || this.constructor !== Z) { return new Z(a, b); } + var c = 0, d; if (a && a.index) { - return a.resolve = !1, this.index = a.index, this.h = a.boost || 0, this.result = this.index.search(a).result, this; + var e = a; + b = e.index; + c = e.boost || 0; + if (e.query) { + var g = e.resolve; + a = e.async || e.queue; + e.resolve = !1; + e.index = null; + a = a ? b.searchAsync(e) : b.search(e); + e.resolve = g; + e.index = b; + a = a.result || a; + } else { + a = []; + } + } + if (a && a.then) { + var f = this; + a = a.then(function(l) { + f.G[0] = f.result = l.result || l; + wb(f); + }); + var h = [a]; + a = []; + var k = new Promise(function(l) { + d = l; + }); } this.index = b || null; this.result = a || []; - this.h = 0; + this.h = c; + this.G = h || []; + this.await = k || null; + this.return = d || null; } Z.prototype.limit = function(a) { if (this.result.length) { @@ -2289,30 +2103,58 @@ Z.prototype.boost = function(a) { this.h += a; return this; }; -Z.prototype.resolve = function(a, b, c) { - var d = this.index, e = this.result; - this.result = this.index = null; - e.length && ("object" === typeof a && (c = a.enrich, b = a.offset, a = a.limit), e = sb.call(d, e, a || 100, b, c)); +function wb(a, b) { + for (var c = a.result, d = 0, e; d < a.G.length; d++) { + if (e = a.G[d]) { + if ("function" === typeof e) { + c = e(), a.G[d] = c = c.result || c, d--; + } else if (e.H) { + c = e.H(), a.G[d] = c = c.result || c, d--; + } else if (e.then) { + return a.await; + } + } + } + d = a.return; + a.result = c; + a.G = []; + a.await = null; + a.return = null; + b || d(c); + return c; +} +Z.prototype.resolve = function(a, b, c, d) { + var e = this.await ? wb(this, !0) : this.result; + if (e.then) { + var g = this; + return e.then(function() { + return g.resolve(a, b, c); + }); + } + e.length && ("object" === typeof a && (c = a.enrich, b = a.offset, a.highlight && console.warn('Highlighting results is not supported within the resolve() method. Instead pass highlight options within the last resolver stage like { query: "...", resolve: true, highlight: ... }.'), a = a.limit), e = d ? c ? tb.call(this.index, e) : e : sb.call(this.index, e, a || 100, b, c)); + d = this.return; + this.return = this.await = this.G = this.result = this.index = null; + d && d(e); return e; }; -function yb(a, b, c, d, e, f, g) { +function Ab(a, b, c, d, e, g, f) { var h = a.length, k = []; var l = S(); - for (var m = 0, q = void 0, p, n; m < b; m++) { - for (var r = 0; r < h; r++) { - var u = a[r]; - if (m < u.length && (q = u[m])) { - for (var x = 0; x < q.length; x++) { - p = q[x]; - (u = l[p]) ? l[p]++ : (u = 0, l[p] = 1); - n = k[u] || (k[u] = []); - if (!g) { - var w = m + (r || !e ? 0 : f || 0); - n = n[w] || (n[w] = []); + for (var m = 0, p = void 0, n, q; m < b; m++) { + for (var t = 0; t < h; t++) { + var u = a[t]; + if (m < u.length && (p = u[m])) { + for (var v = 0; v < p.length; v++) { + n = p[v]; + (u = l[n]) ? l[n]++ : (u = 0, l[n] = 1); + q = k[u] || (k[u] = []); + if (!f) { + var x = m + (t || !e ? 0 : g || 0); + q = q[x] || (q[x] = []); } - n.push(p); - if (g && c && u === h - 1 && n.length - d === c) { - return d ? n.slice(d) : n; + q.push(n); + if (f && c && u === h - 1 && q.length - d === c) { + return d ? q.slice(d) : q; } } } @@ -2320,28 +2162,28 @@ function yb(a, b, c, d, e, f, g) { } if (a = k.length) { if (e) { - k = 1 < k.length ? wb(k, c, d, g, f) : (k = k[0]).length > c || d ? k.slice(d, c + d) : k; + k = 1 < k.length ? yb(k, c, d, f, g) : (k = k[0]) && c && k.length > c || d ? k.slice(d, c + d) : k; } else { if (a < h) { return []; } k = k[a - 1]; if (c || d) { - if (g) { + if (f) { if (k.length > c || d) { k = k.slice(d, c + d); } } else { e = []; - for (f = 0; f < k.length; f++) { - if (g = k[f]) { - if (d && g.length > d) { - d -= g.length; + for (g = 0; g < k.length; g++) { + if (f = k[g]) { + if (d && f.length > d) { + d -= f.length; } else { - if (c && g.length > c || d) { - g = g.slice(d, c + d), c -= g.length, d && (d -= g.length); + if (c && f.length > c || d) { + f = f.slice(d, c + d), c -= f.length, d && (d -= f.length); } - e.push(g); + e.push(f); if (!c) { break; } @@ -2355,19 +2197,19 @@ function yb(a, b, c, d, e, f, g) { } return k; } -function wb(a, b, c, d, e) { - var f = [], g = S(), h = a.length, k; +function yb(a, b, c, d, e) { + var g = [], f = S(), h = a.length, k; if (d) { for (e = h - 1; 0 <= e; e--) { if (k = (d = a[e]) && d.length) { for (h = 0; h < k; h++) { var l = d[h]; - if (!g[l]) { - if (g[l] = 1, c) { + if (!f[l]) { + if (f[l] = 1, c) { c--; } else { - if (f.push(l), f.length === b) { - return f; + if (g.push(l), g.length === b) { + return g; } } } @@ -2375,19 +2217,19 @@ function wb(a, b, c, d, e) { } } } else { - for (var m = h - 1, q, p = 0; 0 <= m; m--) { - q = a[m]; - for (var n = 0; n < q.length; n++) { - if (k = (d = q[n]) && d.length) { - for (var r = 0; r < k; r++) { - if (l = d[r], !g[l]) { - if (g[l] = 1, c) { + for (var m = h - 1, p, n = 0; 0 <= m; m--) { + p = a[m]; + for (var q = 0; q < p.length; q++) { + if (k = (d = p[q]) && d.length) { + for (var t = 0; t < k; t++) { + if (l = d[t], !f[l]) { + if (f[l] = 1, c) { c--; } else { - var u = (n + (m < h - 1 ? e || 0 : 0)) / (m + 1) | 0; - (f[u] || (f[u] = [])).push(l); - if (++p === b) { - return f; + var u = (q + (m < h - 1 ? e || 0 : 0)) / (m + 1) | 0; + (g[u] || (g[u] = [])).push(l); + if (++n === b) { + return g; } } } @@ -2396,13 +2238,13 @@ function wb(a, b, c, d, e) { } } } - return f; + return g; } -function Eb(a, b, c) { - for (var d = S(), e = [], f = 0, g; f < b.length; f++) { - g = b[f]; - for (var h = 0; h < g.length; h++) { - d[g[h]] = 1; +function Db(a, b, c) { + for (var d = S(), e = [], g = 0, f; g < b.length; g++) { + f = b[g]; + for (var h = 0; h < f.length; h++) { + d[f[h]] = 1; } } if (c) { @@ -2411,245 +2253,475 @@ function Eb(a, b, c) { } } else { for (b = 0; b < a.result.length; b++) { - for (c = a.result[b], g = 0; g < c.length; g++) { - f = c[g], d[f] && ((e[b] || (e[b] = [])).push(f), d[f] = 0); + for (c = a.result[b], f = 0; f < c.length; f++) { + g = c[f], d[g] && ((e[b] || (e[b] = [])).push(g), d[g] = 0); } } } return e; } ;S(); -pb.prototype.search = function(a, b, c, d) { - c || (!b && Da(a) ? (c = a, a = "") : Da(b) && (c = b, b = 0)); - if (c && c.cache) { - c.cache = !1; - var e = this.searchCache(a, b, c); - c.cache = !0; - return e; +function Eb(a, b, c, d, e) { + if ("string" === typeof e) { + var g = e; + e = ""; + } else { + g = e.template; } - var f = [], g = [], h = 0, k = !0; + if (!g) { + throw Error('No template pattern was specified by the search option "highlight"'); + } + var f = g.indexOf("$1"); + if (-1 === f) { + throw Error('Invalid highlight template. The replacement pattern "$1" was not found in template: ' + g); + } + var h = g.substring(f + 2); + f = g.substring(0, f); + var k = e && e.boundary, l = !e || !1 !== e.clip, m = e && e.merge && h && f && new RegExp(h + " " + f, "g"); + e = e && e.ellipsis; + var p = 0; + if ("object" === typeof e) { + var n = e.template; + p = n.length - 2; + e = e.pattern; + } + "string" !== typeof e && (e = !1 === e ? "" : "..."); + p && (e = n.replace("$1", e)); + n = e.length - p; + if ("object" === typeof k) { + var q = k.before; + 0 === q && (q = -1); + var t = k.after; + 0 === t && (t = -1); + k = k.total || 9e5; + } + p = new Map(); + for (var u, v = 0, x, y; v < b.length; v++) { + if (d) { + var E = b; + y = d; + } else { + E = b[v]; + y = E.field; + if (!y) { + continue; + } + E = E.result; + } + u = c.get(y); + x = u.encoder; + u = p.get(x); + "string" !== typeof u && (u = x.encode(a), p.set(x, u)); + for (var B = 0; B < E.length; B++) { + var r = E[B].doc; + if (r && (r = Ea(r, y))) { + var A = r.trim().split(/\s+/); + if (A.length) { + r = ""; + for (var F = [], U = [], G = -1, N = -1, Q = 0, D = 0; D < A.length; D++) { + var R = A[D], W = x.encode(R); + W = 1 < W.length ? W.join(" ") : W[0]; + var O = void 0; + if (W && R) { + for (var L = R.length, M = (x.split ? R.replace(x.split, "") : R).length - W.length, ba = "", oa = 0, z = 0; z < u.length; z++) { + var I = u[z]; + if (I) { + var J = I.length; + J += M; + oa && J <= oa || (I = W.indexOf(I), -1 < I && (ba = (I ? R.substring(0, I) : "") + f + R.substring(I, I + J) + h + (I + J < L ? R.substring(I + J) : ""), oa = J, O = !0)); + } + } + ba && (k && (0 > G && (G = r.length + (r ? 1 : 0)), N = r.length + (r ? 1 : 0) + ba.length, Q += L, U.push(F.length), F.push({match:ba})), r += (r ? " " : "") + ba); + } + if (!O) { + R = A[D], r += (r ? " " : "") + R, k && F.push({text:R}); + } else if (k && Q >= k) { + break; + } + } + Q = U.length * (g.length - 2); + if (q || t || k && r.length - Q > k) { + if (Q = k + Q - 2 * n, D = N - G, 0 < q && (D += q), 0 < t && (D += t), D <= Q) { + A = q ? G - (0 < q ? q : 0) : G - ((Q - D) / 2 | 0), F = t ? N + (0 < t ? t : 0) : A + Q, l || (0 < A && " " !== r.charAt(A) && " " !== r.charAt(A - 1) && (A = r.indexOf(" ", A), 0 > A && (A = 0)), F < r.length && " " !== r.charAt(F - 1) && " " !== r.charAt(F) && (F = r.lastIndexOf(" ", F), F < N ? F = N : ++F)), r = (A ? e : "") + r.substring(A, F) + (F < r.length ? e : ""); + } else { + N = []; + Q = {}; + G = {}; + D = {}; + R = {}; + W = {}; + M = L = O = 0; + for (oa = ba = 1;;) { + I = void 0; + for (z = 0; z < U.length; z++) { + J = U[z]; + if (M) { + if (L !== M) { + if (D[z + 1]) { + continue; + } + J += M; + if (Q[J]) { + O -= n; + G[z + 1] = 1; + D[z + 1] = 1; + continue; + } + if (J >= F.length - 1) { + if (J >= F.length) { + D[z + 1] = 1; + J >= A.length && (G[z + 1] = 1); + continue; + } + O -= n; + } + r = F[J].text; + var V = t && W[z]; + if (V) { + if (0 < V) { + if (r.length > V) { + if (D[z + 1] = 1, l) { + r = r.substring(0, V); + } else { + continue; + } + } + (V -= r.length) || (V = -1); + W[z] = V; + } else { + D[z + 1] = 1; + continue; + } + } + if (O + r.length + 1 <= k) { + r = " " + r, N[z] += r; + } else if (l) { + I = k - O - 1, 0 < I && (r = " " + r.substring(0, I), N[z] += r), D[z + 1] = 1; + } else { + D[z + 1] = 1; + continue; + } + } else { + if (D[z]) { + continue; + } + J -= L; + if (Q[J]) { + O -= n; + D[z] = 1; + G[z] = 1; + continue; + } + if (0 >= J) { + if (0 > J) { + D[z] = 1; + G[z] = 1; + continue; + } + O -= n; + } + r = F[J].text; + if (V = q && R[z]) { + if (0 < V) { + if (r.length > V) { + if (D[z] = 1, l) { + r = r.substring(r.length - V); + } else { + continue; + } + } + (V -= r.length) || (V = -1); + R[z] = V; + } else { + D[z] = 1; + continue; + } + } + if (O + r.length + 1 <= k) { + r += " ", N[z] = r + N[z]; + } else if (l) { + I = r.length + 1 - (k - O), 0 <= I && I < r.length && (r = r.substring(I) + " ", N[z] = r + N[z]), D[z] = 1; + } else { + D[z] = 1; + continue; + } + } + } else { + r = F[J].match; + q && (R[z] = q); + t && (W[z] = t); + z && O++; + I = void 0; + J ? !z && n && (O += n) : (G[z] = 1, D[z] = 1); + J >= A.length - 1 ? I = 1 : J < F.length - 1 && F[J + 1].match ? I = 1 : n && (O += n); + O -= g.length - 2; + if (!z || O + r.length <= k) { + N[z] = r; + } else { + I = ba = oa = G[z] = 0; + break; + } + I && (G[z + 1] = 1, D[z + 1] = 1); + } + O += r.length; + I = Q[J] = 1; + } + if (I) { + L === M ? M++ : L++; + } else { + L === M ? ba = 0 : oa = 0; + if (!ba && !oa) { + break; + } + ba ? (L++, M = L) : M++; + } + } + r = ""; + for (A = 0; A < N.length; A++) { + F = (A && G[A] ? " " : (A && !e ? " " : "") + e) + N[A], r += F; + } + e && !G[N.length] && (r += e); + } + } + m && (r = r.replace(m, " ")); + E[B].highlight = r; + } + } + } + if (d) { + break; + } + } + return b; +} +;pb.prototype.search = function(a, b, c, d) { + c || (!b && Ca(a) ? (c = a, a = "") : Ca(b) && (c = b, b = 0)); + var e = [], g = [], f = 0, h = !0; if (c) { c.constructor === Array && (c = {index:c}); a = c.query || a; - var l = c.pluck; - var m = c.merge; - var q = c.boost; - var p = l || c.field || (p = c.index) && (p.index ? null : p); + var k = c.pluck; + var l = c.merge; + var m = c.boost; + var p = k || c.field || (p = c.index) && (p.index ? null : p); var n = this.tag && c.tag; - var r = c.suggest; - k = !1 !== c.resolve; - this.store && c.highlight && !k ? console.warn("Highlighting results can only be done on a final resolver task or when calling .resolve({ highlight: ... })") : this.store && c.enrich && !k && console.warn("Enrich results can only be done on a final resolver task or when calling .resolve({ enrich: true })"); - var u = k && this.store && c.highlight; - e = !!u || k && this.store && c.enrich; + var q = c.suggest; + h = !1 !== c.resolve; + var t = c.cache; + this.store && c.highlight && !h ? console.warn("Highlighting results can only be done on a final resolver task or when calling .resolve({ highlight: ... })") : this.store && c.enrich && !h && console.warn("Enrich results can only be done on a final resolver task or when calling .resolve({ enrich: true })"); + var u = h && this.store && c.highlight; + var v = !!u || h && this.store && c.enrich; b = c.limit || b; var x = c.offset || 0; - b || (b = k ? 100 : 0); + b || (b = h ? 100 : 0); if (n && (!this.db || !d)) { n.constructor !== Array && (n = [n]); - for (var w = [], E = 0, B = void 0; E < n.length; E++) { + for (var y = [], E = 0, B = void 0; E < n.length; E++) { B = n[E]; if (T(B)) { throw Error("A tag option can't be a string, instead it needs a { field: tag } format."); } if (B.field && B.tag) { - var y = B.tag; - if (y.constructor === Array) { - for (var t = 0; t < y.length; t++) { - w.push(B.field, y[t]); + var r = B.tag; + if (r.constructor === Array) { + for (var A = 0; A < r.length; A++) { + y.push(B.field, r[A]); } } else { - w.push(B.field, y); + y.push(B.field, r); } } else { - y = Object.keys(B); - t = 0; - for (var D = void 0, C = void 0; t < y.length; t++) { - if (D = y[t], C = B[D], C.constructor === Array) { - for (var O = 0; O < C.length; O++) { - w.push(D, C[O]); + r = Object.keys(B); + A = 0; + for (var F = void 0, U = void 0; A < r.length; A++) { + if (F = r[A], U = B[F], U.constructor === Array) { + for (var G = 0; G < U.length; G++) { + y.push(F, U[G]); } } else { - w.push(D, C); + y.push(F, U); } } } } - if (!w.length) { + if (!y.length) { throw Error("Your tag definition within the search options is probably wrong. No valid tags found."); } - n = w; + n = y; if (!a) { g = []; - if (w.length) { - for (n = 0; n < w.length; n += 2) { + if (y.length) { + for (n = 0; n < y.length; n += 2) { d = void 0; if (this.db) { - d = this.index.get(w[n]); + d = this.index.get(y[n]); if (!d) { - console.warn("Tag '" + w[n] + ":" + w[n + 1] + "' will be skipped because there is no field '" + w[n] + "'."); + console.warn("Tag '" + y[n] + ":" + y[n + 1] + "' will be skipped because there is no field '" + y[n] + "'."); continue; } - g.push(d = d.db.tag(w[n + 1], b, x, e)); + g.push(d = d.db.tag(y[n + 1], b, x, v)); } else { - d = Fb.call(this, w[n], w[n + 1], b, x, e); + d = Fb.call(this, y[n], y[n + 1], b, x, v); } - f.push(k ? {field:w[n], tag:w[n + 1], result:d} : [d]); + e.push(h ? {field:y[n], tag:y[n + 1], result:d} : [d]); } } if (g.length) { - var Q = this; - return Promise.all(g).then(function(G) { - for (var N = 0; N < G.length; N++) { - k ? f[N].result = G[N] : f[N] = G[N]; + var N = this; + return Promise.all(g).then(function(L) { + for (var M = 0; M < L.length; M++) { + h ? e[M].result = L[M] : e[M] = L[M]; } - return k ? f : new Z(1 < f.length ? yb(f, 1, 0, 0, r, q) : f[0], Q); + return h ? e : new Z(1 < e.length ? Ab(e, 1, 0, 0, q, m) : e[0], N); }); } - return k ? f : new Z(1 < f.length ? yb(f, 1, 0, 0, r, q) : f[0], this); + return h ? e : new Z(1 < e.length ? Ab(e, 1, 0, 0, q, m) : e[0], this); } } - if (!k && !l) { + if (!h && !k) { if (p = p || this.field) { - T(p) ? l = p : (p.constructor === Array && 1 === p.length && (p = p[0]), l = p.field || p.index); + T(p) ? k = p : (p.constructor === Array && 1 === p.length && (p = p[0]), k = p.field || p.index); } - if (!l) { + if (!k) { throw Error("Apply resolver on document search requires either the option 'pluck' to be set or just select a single field name in your query."); } } p && p.constructor !== Array && (p = [p]); } p || (p = this.field); - w = (this.worker || this.db) && !d && []; + y = (this.worker || this.db) && !d && []; E = 0; - for (t = B = y = void 0; E < p.length; E++) { - if (B = p[E], !this.db || !this.tag || this.L[E]) { - y = void 0; - T(B) || (y = B, B = y.field, a = y.query || a, b = Ca(y.limit, b), x = Ca(y.offset, x), r = Ca(y.suggest, r), u = k && this.store && Ca(y.highlight, u), e = !!u || k && this.store && Ca(y.enrich, e)); + for (A = B = r = void 0; E < p.length; E++) { + if (B = p[E], !this.db || !this.tag || this.M[E]) { + r = void 0; + T(B) || (r = B, B = r.field, a = r.query || a, b = Ba(r.limit, b), x = Ba(r.offset, x), q = Ba(r.suggest, q), u = h && this.store && Ba(r.highlight, u), v = !!u || h && this.store && Ba(r.enrich, v), t = Ba(r.cache, t)); if (d) { - y = d[E]; + r = d[E]; } else { - t = y || c; - y = this.index.get(B); + A = r || c || {}; + F = A.enrich; + r = this.index.get(B); if (n) { if (this.db) { - t.tag = n; - var M = y.db.support_tag_search; - t.field = p; + A.tag = n; + var Q = r.db.support_tag_search; + A.field = p; } - M || (t.enrich = !1); + !Q && F && (A.enrich = !1); } - if (w) { - w[E] = y.search(a, b, t); - t && e && (t.enrich = e); + r = t ? r.searchCache(a, b, A) : r.search(a, b, A); + F && (A.enrich = F); + if (y) { + y[E] = r; continue; - } else { - y = y.search(a, b, t), t && e && (t.enrich = e); } } - t = y && (k ? y.length : y.result.length); - if (n && t) { - D = []; - C = 0; + A = (r = r.result || r) && r.length; + if (n && A) { + F = []; + U = 0; if (this.db && d) { - if (!M) { - for (O = p.length; O < d.length; O++) { - var J = d[O]; - if (J && J.length) { - C++, D.push(J); - } else if (!r) { - return k ? f : new Z(f, this); + if (!Q) { + for (G = p.length; G < d.length; G++) { + var D = d[G]; + if (D && D.length) { + U++, F.push(D); + } else if (!q) { + return h ? e : new Z(e, this); } } } } else { - O = 0; - for (var F = J = void 0; O < n.length; O += 2) { - J = this.tag.get(n[O]); - if (!J) { - if (console.warn("Tag '" + n[O] + ":" + n[O + 1] + "' will be skipped because there is no field '" + n[O] + "'."), r) { + G = 0; + for (var R = D = void 0; G < n.length; G += 2) { + D = this.tag.get(n[G]); + if (!D) { + if (console.warn("Tag '" + n[G] + ":" + n[G + 1] + "' will be skipped because there is no field '" + n[G] + "'."), q) { continue; } else { - return k ? f : new Z(f, this); + return h ? e : new Z(e, this); } } - if (F = (J = J && J.get(n[O + 1])) && J.length) { - C++, D.push(J); - } else if (!r) { - return k ? f : new Z(f, this); + if (R = (D = D && D.get(n[G + 1])) && D.length) { + U++, F.push(D); + } else if (!q) { + return h ? e : new Z(e, this); } } } - if (C) { - y = Eb(y, D, k); - t = y.length; - if (!t && !r) { - return k ? y : new Z(y, this); + if (U) { + r = Db(r, F, h); + A = r.length; + if (!A && !q) { + return h ? r : new Z(r, this); } - C--; + U--; } } - if (t) { - g[h] = B, f.push(y), h++; + if (A) { + g[f] = B, e.push(r), f++; } else if (1 === p.length) { - return k ? f : new Z(f, this); + return h ? e : new Z(e, this); } } } - if (w) { - if (this.db && n && n.length && !M) { - for (e = 0; e < n.length; e += 2) { - g = this.index.get(n[e]); + if (y) { + if (this.db && n && n.length && !Q) { + for (v = 0; v < n.length; v += 2) { + g = this.index.get(n[v]); if (!g) { - if (console.warn("Tag '" + n[e] + ":" + n[e + 1] + "' was not found because there is no field '" + n[e] + "'."), r) { + if (console.warn("Tag '" + n[v] + ":" + n[v + 1] + "' was not found because there is no field '" + n[v] + "'."), q) { continue; } else { - return k ? f : new Z(f, this); + return h ? e : new Z(e, this); } } - w.push(g.db.tag(n[e + 1], b, x, !1)); + y.push(g.db.tag(n[v + 1], b, x, !1)); } } - var R = this; - return Promise.all(w).then(function(G) { - return G.length ? R.search(a, b, c, G) : G; + var W = this; + return Promise.all(y).then(function(L) { + c && (c.resolve = h); + L.length && (L = W.search(a, b, c, L)); + return L; }); } - if (!h) { - return k ? f : new Z(f, this); + if (!f) { + return h ? e : new Z(e, this); } - if (l && (!e || !this.store)) { - return f = f[0], k || (f.index = this), f; + if (k && (!v || !this.store)) { + return e = e[0], h ? e : new Z(e, this); } - w = []; + y = []; for (x = 0; x < g.length; x++) { - n = f[x]; - e && n.length && "undefined" === typeof n[0].doc && (this.db ? w.push(n = this.index.get(this.field[0]).db.enrich(n)) : n = tb.call(this, n)); - if (l) { - return k ? u ? Db(a, n, this.index, l, u) : n : new Z(n, this); + n = e[x]; + v && n.length && "undefined" === typeof n[0].doc && (this.db ? y.push(n = this.index.get(this.field[0]).db.enrich(n)) : n = tb.call(this, n)); + if (k) { + return h ? u ? Eb(a, n, this.index, k, u) : n : new Z(n, this); } - f[x] = {field:g[x], result:n}; + e[x] = {field:g[x], result:n}; } - if (e && this.db && w.length) { - var V = this; - return Promise.all(w).then(function(G) { - for (var N = 0; N < G.length; N++) { - f[N].result = G[N]; + if (v && this.db && y.length) { + var O = this; + return Promise.all(y).then(function(L) { + for (var M = 0; M < L.length; M++) { + e[M].result = L[M]; } - u && (f = Db(a, f, V.index, l, u)); - return m ? Gb(f) : f; + u && (e = Eb(a, e, O.index, k, u)); + return l ? Gb(e) : e; }); } - u && (f = Db(a, f, this.index, l, u)); - return m ? Gb(f) : f; + u && (e = Eb(a, e, this.index, k, u)); + return l ? Gb(e) : e; }; function Gb(a) { - for (var b = [], c = S(), d = S(), e = 0, f, g, h = void 0, k, l, m; e < a.length; e++) { - f = a[e]; - g = f.field; - f = f.result; - for (var q = 0; q < f.length; q++) { - if (k = f[q], "object" !== typeof k ? k = {id:h = k} : h = k.id, (l = c[h]) ? l.push(g) : (k.field = c[h] = [g], b.push(k)), m = k.highlight) { - l = d[h], l || (d[h] = l = {}, k.highlight = l), l[g] = m; + for (var b = [], c = S(), d = S(), e = 0, g, f, h = void 0, k, l, m; e < a.length; e++) { + g = a[e]; + f = g.field; + g = g.result; + for (var p = 0; p < g.length; p++) { + if (k = g[p], "object" !== typeof k ? k = {id:h = k} : h = k.id, (l = c[h]) ? l.push(f) : (k.field = c[h] = [f], b.push(k)), m = k.highlight) { + l = d[h], l || (d[h] = l = {}, k.highlight = l), l[f] = m; } } } @@ -2677,48 +2749,39 @@ function tb(a) { if (!this || !this.store) { return a; } + if (this.db) { + return this.index.get(this.field[0]).db.enrich(a); + } for (var b = Array(a.length), c = 0, d; c < a.length; c++) { d = a[c], b[c] = {id:d, doc:this.store.get(d)}; } return b; } -;var Hb = {normalize:!1, numeric:!1, dedupe:!1}; -var Ib = {}; -var Jb = new Map([["b", "p"], ["v", "f"], ["w", "f"], ["z", "s"], ["x", "s"], ["d", "t"], ["n", "m"], ["c", "k"], ["g", "k"], ["j", "k"], ["q", "k"], ["i", "e"], ["y", "e"], ["u", "o"]]); -var Kb = new Map([["ae", "a"], ["oe", "o"], ["sh", "s"], ["kh", "k"], ["th", "t"], ["ph", "f"], ["pf", "f"]]), Lb = [/([^aeo])h(.)/g, "$1$2", /([aeo])h([^aeo]|$)/g, "$1$2", /(.)\1+/g, "$1"]; -var Mb = {a:"", e:"", i:"", o:"", u:"", y:"", b:1, f:1, p:1, v:1, c:2, g:2, j:2, k:2, q:2, s:2, x:2, z:2, "\u00df":2, d:3, t:3, l:4, m:5, n:5, r:6}; -var Nb = {Exact:Hb, Default:Ib, Normalize:Ib, LatinBalance:{mapper:Jb}, LatinAdvanced:{mapper:Jb, matcher:Kb, replacer:Lb}, LatinExtra:{mapper:Jb, replacer:Lb.concat([/(?!^)[aeo]/g, ""]), matcher:Kb}, LatinSoundex:{dedupe:!1, include:{letter:!0}, finalize:function(a) { - for (var b = 0; b < a.length; b++) { - for (var c = a[b], d = c.charAt(0), e = Mb[d], f = 1, g; f < c.length && (g = c.charAt(f), "h" === g || "w" === g || !(g = Mb[g]) || g === e || (d += g, e = g, 4 !== d.length)); f++) { - } - a[b] = d; - } -}}, CJK:{split:""}, LatinExact:Hb, LatinDefault:Ib, LatinSimple:Ib}; -function pb(a) { +;function pb(a) { if (!this || this.constructor !== pb) { return new pb(a); } var b = a.document || a.doc || a, c, d; - this.L = []; + this.M = []; this.field = []; - this.S = []; - this.key = (c = b.key || b.id) && Ob(c, this.S) || "id"; + this.T = []; + this.key = (c = b.key || b.id) && Hb(c, this.T) || "id"; (d = a.keystore || 0) && (this.keystore = d); this.fastupdate = !!a.fastupdate; - this.reg = !this.fastupdate || a.worker || a.db ? d ? new Y(d) : new Set() : d ? new X(d) : new Map(); - this.I = (c = b.store || null) && c && !0 !== c && []; - this.store = c && (d ? new X(d) : new Map()); - this.cache = (c = a.cache || null) && new Pb(c); + this.reg = !this.fastupdate || a.worker || a.db ? d ? new X(d) : new Set() : d ? new Xa(d) : new Map(); + this.J = (c = b.store || null) && c && !0 !== c && []; + this.store = c && (d ? new Xa(d) : new Map()); + this.cache = (c = a.cache || null) && new Na(c); a.cache = !1; this.worker = a.worker || !1; this.priority = a.priority || 4; - this.index = Qb.call(this, a, b); + this.index = Ib.call(this, a, b); this.tag = null; if (c = b.tag) { if ("string" === typeof c && (c = [c]), c.length) { this.tag = new Map(); - this.K = []; - this.aa = []; + this.L = []; + this.ba = []; b = 0; for (var e = d = void 0; b < c.length; b++) { d = c[b]; @@ -2726,8 +2789,8 @@ function pb(a) { if (!e) { throw Error("The tag field from the document descriptor is undefined."); } - d.custom ? this.K[b] = d.custom : (this.K[b] = Ob(e, this.S), d.filter && ("string" === typeof this.K[b] && (this.K[b] = new String(this.K[b])), this.K[b].R = d.filter)); - this.aa[b] = e; + d.custom ? this.L[b] = d.custom : (this.L[b] = Hb(e, this.T), d.filter && ("string" === typeof this.L[b] && (this.L[b] = new String(this.L[b])), this.L[b].S = d.filter)); + this.ba[b] = e; this.tag.set(e, new Map()); } } @@ -2735,37 +2798,37 @@ function pb(a) { if (this.worker) { this.fastupdate = !1; a = []; - c = A(this.index.values()); + c = C(this.index.values()); for (b = c.next(); !b.done; b = c.next()) { b = b.value, b.then && a.push(b); } if (a.length) { - var f = this; - return Promise.all(a).then(function(g) { - for (var h = 0, k = A(f.index.entries()), l = k.next(); !l.done; l = k.next()) { + var g = this; + return Promise.all(a).then(function(f) { + for (var h = 0, k = C(g.index.entries()), l = k.next(); !l.done; l = k.next()) { var m = l.value; l = m[0]; m = m[1]; - m.then && (m = g[h], f.index.set(l, m), h++); + m.then && (m = f[h], g.index.set(l, m), h++); } - return f; + return g; }); } } else { a.db && (this.fastupdate = !1, this.mount(a.db)); } } -v = pb.prototype; -v.mount = function(a) { +w = pb.prototype; +w.mount = function(a) { if (this.worker) { throw Error("You can't use Worker-Indexes on a persistent model. That would be useless, since each of the persistent model acts like Worker-Index by default (Master/Slave)."); } var b = this.field; if (this.tag) { - for (var c = 0, d = void 0; c < this.aa.length; c++) { - d = this.aa[c]; + for (var c = 0, d = void 0; c < this.ba.length; c++) { + d = this.ba[c]; var e = void 0; - this.index.set(d, e = new W({}, this.reg)); + this.index.set(d, e = new Y({}, this.reg)); b === this.field && (b = b.slice(0)); b.push(d); e.tag = this.tag.get(d); @@ -2774,93 +2837,93 @@ v.mount = function(a) { c = []; d = {db:a.db, type:a.type, fastupdate:a.fastupdate}; e = 0; - var f = void 0; - for (f = void 0; e < b.length; e++) { - d.field = f = b[e]; - f = this.index.get(f); - var g = new a.constructor(a.id, d); - g.id = a.id; - c[e] = g.mount(f); - f.document = !0; - e ? f.bypass = !0 : f.store = this.store; + var g = void 0; + for (g = void 0; e < b.length; e++) { + d.field = g = b[e]; + g = this.index.get(g); + var f = new a.constructor(a.id, d); + f.id = a.id; + c[e] = f.mount(g); + g.document = !0; + e ? g.bypass = !0 : g.store = this.store; } var h = this; return this.db = Promise.all(c).then(function() { h.db = !0; }); }; -v.commit = function(a, b) { - var c = this, d, e, f, g; - return xa(function(h) { +w.commit = function(a, b) { + var c = this, d, e, g, f; + return wa(function(h) { if (1 == h.h) { d = []; - e = A(c.index.values()); - for (f = e.next(); !f.done; f = e.next()) { - g = f.value, d.push(g.commit(a, b)); + e = C(c.index.values()); + for (g = e.next(); !g.done; g = e.next()) { + f = g.value, d.push(f.commit(a, b)); } - return L(h, Promise.all(d), 2); + return K(h, Promise.all(d), 2); } c.reg.clear(); h.h = 0; }); }; -v.destroy = function() { - for (var a = [], b = A(this.index.values()), c = b.next(); !c.done; c = b.next()) { +w.destroy = function() { + for (var a = [], b = C(this.index.values()), c = b.next(); !c.done; c = b.next()) { a.push(c.value.destroy()); } return Promise.all(a); }; -function Qb(a, b) { +function Ib(a, b) { var c = new Map(), d = b.index || b.field || b; T(d) && (d = [d]); - for (var e = 0, f, g = void 0; e < d.length; e++) { - f = d[e]; - T(f) || (g = f, f = f.field); - g = Da(g) ? Object.assign({}, a, g) : a; + for (var e = 0, g, f = void 0; e < d.length; e++) { + g = d[e]; + T(g) || (f = g, g = g.field); + f = Ca(f) ? Object.assign({}, a, f) : a; if (this.worker) { - var h = (h = g.encoder) && h.encode ? h : new Ma("string" === typeof h ? Nb[h] : h || {}); - h = new Ya(g, h); - c.set(f, h); + var h = (h = f.encoder) && h.encode ? h : new Ka("string" === typeof h ? Ua[h] : h || {}); + h = new mb(f, h); + c.set(g, h); } - this.worker || c.set(f, new W(g, this.reg)); - g.custom ? this.L[e] = g.custom : (this.L[e] = Ob(f, this.S), g.filter && ("string" === typeof this.L[e] && (this.L[e] = new String(this.L[e])), this.L[e].R = g.filter)); - this.field[e] = f; + this.worker || c.set(g, new Y(f, this.reg)); + f.custom ? this.M[e] = f.custom : (this.M[e] = Hb(g, this.T), f.filter && ("string" === typeof this.M[e] && (this.M[e] = new String(this.M[e])), this.M[e].S = f.filter)); + this.field[e] = g; } - if (this.I) { + if (this.J) { for (a = b.store, T(a) && (a = [a]), b = 0; b < a.length; b++) { - d = a[b], e = d.field || d, d.custom ? (this.I[b] = d.custom, d.custom.ja = e) : (this.I[b] = Ob(e, this.S), d.filter && ("string" === typeof this.I[b] && (this.I[b] = new String(this.I[b])), this.I[b].R = d.filter)); + d = a[b], e = d.field || d, d.custom ? (this.J[b] = d.custom, d.custom.na = e) : (this.J[b] = Hb(e, this.T), d.filter && ("string" === typeof this.J[b] && (this.J[b] = new String(this.J[b])), this.J[b].S = d.filter)); } } return c; } -function Ob(a, b) { +function Hb(a, b) { for (var c = a.split(":"), d = 0, e = 0; e < c.length; e++) { a = c[e], "]" === a[a.length - 1] && (a = a.substring(0, a.length - 2)) && (b[d] = !0), a && (c[d++] = a); } d < c.length && (c.length = d); return 1 < d ? c : c[0]; } -v.append = function(a, b) { +w.append = function(a, b) { return this.add(a, b, !0); }; -v.update = function(a, b) { +w.update = function(a, b) { return this.remove(a).add(a, b); }; -v.remove = function(a) { - Da(a) && (a = Fa(a, this.key)); - for (var b = A(this.index.values()), c = b.next(); !c.done; c = b.next()) { +w.remove = function(a) { + Ca(a) && (a = Ea(a, this.key)); + for (var b = C(this.index.values()), c = b.next(); !c.done; c = b.next()) { c.value.remove(a, !0); } if (this.reg.has(a)) { if (this.tag && !this.fastupdate) { - for (b = A(this.tag.values()), c = b.next(); !c.done; c = b.next()) { + for (b = C(this.tag.values()), c = b.next(); !c.done; c = b.next()) { c = c.value; - for (var d = A(c), e = d.next(); !e.done; e = d.next()) { - var f = e.value; - e = f[0]; - f = f[1]; - var g = f.indexOf(a); - -1 < g && (1 < f.length ? f.splice(g, 1) : c.delete(e)); + for (var d = C(c), e = d.next(); !e.done; e = d.next()) { + var g = e.value; + e = g[0]; + g = g[1]; + var f = g.indexOf(a); + -1 < f && (1 < g.length ? g.splice(f, 1) : c.delete(e)); } } } @@ -2870,12 +2933,12 @@ v.remove = function(a) { this.cache && this.cache.remove(a); return this; }; -v.clear = function() { - for (var a = [], b = A(this.index.values()), c = b.next(); !c.done; c = b.next()) { +w.clear = function() { + for (var a = [], b = C(this.index.values()), c = b.next(); !c.done; c = b.next()) { c = c.value.clear(), c.then && a.push(c); } if (this.tag) { - for (b = A(this.tag.values()), c = b.next(); !c.done; c = b.next()) { + for (b = C(this.tag.values()), c = b.next(); !c.done; c = b.next()) { c.value.clear(); } } @@ -2883,61 +2946,130 @@ v.clear = function() { this.cache && this.cache.clear(); return a.length ? Promise.all(a) : this; }; -v.contain = function(a) { +w.contain = function(a) { return this.db ? this.index.get(this.field[0]).db.has(a) : this.reg.has(a); }; -v.cleanup = function() { - for (var a = A(this.index.values()), b = a.next(); !b.done; b = a.next()) { +w.cleanup = function() { + for (var a = C(this.index.values()), b = a.next(); !b.done; b = a.next()) { b.value.cleanup(); } return this; }; -v.get = function(a) { +w.get = function(a) { return this.db ? this.index.get(this.field[0]).db.enrich(a).then(function(b) { return b[0] && b[0].doc || null; }) : this.store.get(a) || null; }; -v.set = function(a, b) { - "object" === typeof a && (b = a, a = Fa(b, this.key)); +w.set = function(a, b) { + "object" === typeof a && (b = a, a = Ea(b, this.key)); this.store.set(a, b); return this; }; -v.searchCache = Rb; -v.export = function(a, b, c, d) { +w.searchCache = Ma; +w.export = Jb; +w.import = Kb; +fb(pb.prototype); +function Lb(a, b) { + b = void 0 === b ? 0 : b; + var c = [], d = []; + b && (b = 250000 / b * 5000 | 0); + a = C(a.entries()); + for (var e = a.next(); !e.done; e = a.next()) { + d.push(e.value), d.length === b && (c.push(d), d = []); + } + d.length && c.push(d); + return c; +} +function Mb(a, b) { + b || (b = new Map()); + for (var c = 0, d; c < a.length; c++) { + d = a[c], b.set(d[0], d[1]); + } + return b; +} +function Nb(a, b) { + b = void 0 === b ? 0 : b; + var c = [], d = []; + b && (b = 250000 / b * 1000 | 0); + a = C(a.entries()); + for (var e = a.next(); !e.done; e = a.next()) { + e = e.value, d.push([e[0], Lb(e[1])[0]]), d.length === b && (c.push(d), d = []); + } + d.length && c.push(d); + return c; +} +function Ob(a, b) { + b || (b = new Map()); + for (var c = 0, d, e; c < a.length; c++) { + d = a[c], e = b.get(d[0]), b.set(d[0], Mb(d[1], e)); + } + return b; +} +function Pb(a) { + var b = [], c = []; + a = C(a.keys()); + for (var d = a.next(); !d.done; d = a.next()) { + c.push(d.value), 250000 === c.length && (b.push(c), c = []); + } + c.length && b.push(c); + return b; +} +function Qb(a, b) { + b || (b = new Set()); + for (var c = 0; c < a.length; c++) { + b.add(a[c]); + } + return b; +} +function Rb(a, b, c, d, e, g, f) { + f = void 0 === f ? 0 : f; + var h = d && d.constructor === Array, k = h ? d.shift() : d; + if (!k) { + return this.export(a, b, e, g + 1); + } + if ((k = a((b ? b + "." : "") + (f + 1) + "." + c, JSON.stringify(k))) && k.then) { + var l = this; + return k.then(function() { + return Rb.call(l, a, b, c, h ? d : null, e, g, f + 1); + }); + } + return Rb.call(this, a, b, c, h ? d : null, e, g, f + 1); +} +function Jb(a, b, c, d) { c = void 0 === c ? 0 : c; d = void 0 === d ? 0 : d; if (c < this.field.length) { var e = this.field[c]; if ((b = this.index.get(e).export(a, e, c, d = 1)) && b.then) { - var f = this; + var g = this; return b.then(function() { - return f.export(a, e, c + 1); + return g.export(a, e, c + 1); }); } return this.export(a, e, c + 1); } switch(d) { case 0: - var g = "reg"; - var h = eb(this.reg); + var f = "reg"; + var h = Pb(this.reg); b = null; break; case 1: - g = "tag"; - h = this.tag && cb(this.tag, this.reg.size); + f = "tag"; + h = this.tag && Nb(this.tag, this.reg.size); b = null; break; case 2: - g = "doc"; - h = this.store && ab(this.store); + f = "doc"; + h = this.store && Lb(this.store); b = null; break; default: return; } - return gb.call(this, a, b, g, h, c, d); -}; -v.import = function(a, b) { + return Rb.call(this, a, b, f, h, c, d); +} +function Kb(a, b) { var c = a.split("."); "json" === c[c.length - 1] && c.pop(); var d = 2 < c.length ? c[0] : ""; @@ -2953,13 +3085,13 @@ v.import = function(a, b) { switch(c) { case "reg": this.fastupdate = !1; - this.reg = fb(b, this.reg); + this.reg = Qb(b, this.reg); for (b = 0; b < this.field.length; b++) { d = this.index.get(this.field[b]), d.fastupdate = !1, d.reg = this.reg; } if (this.worker) { b = []; - d = A(this.index.values()); + d = C(this.index.values()); for (c = d.next(); !c.done; c = d.next()) { b.push(c.value.import(a)); } @@ -2967,110 +3099,91 @@ v.import = function(a, b) { } break; case "tag": - this.tag = db(b, this.tag); + this.tag = Ob(b, this.tag); break; case "doc": - this.store = bb(b, this.store); + this.store = Mb(b, this.store); } } -}; -Ra(pb.prototype); -function Rb(a, b, c) { - var d = (b ? "" + a : "object" === typeof a ? "" + a.query : a).toLowerCase(); - this.cache || (this.cache = new Pb()); - var e = this.cache.get(d); - if (!e) { - e = this.search(a, b, c); - if (e.then) { - var f = this; - e.then(function(g) { - f.cache.set(d, g); - return g; - }); +} +function Sb(a, b) { + var c = ""; + a = C(a.entries()); + for (var d = a.next(); !d.done; d = a.next()) { + var e = d.value; + d = e[0]; + e = e[1]; + for (var g = "", f = 0, h; f < e.length; f++) { + h = e[f] || [""]; + for (var k = "", l = 0; l < h.length; l++) { + k += (k ? "," : "") + ("string" === b ? '"' + h[l] + '"' : h[l]); + } + k = "[" + k + "]"; + g += (g ? "," : "") + k; } - this.cache.set(d, e); + g = '["' + d + '",[' + g + "]]"; + c += (c ? "," : "") + g; } - return e; + return c; } -function Pb(a) { - this.limit = a && !0 !== a ? a : 1000; - this.cache = new Map(); - this.h = ""; -} -Pb.prototype.set = function(a, b) { - this.cache.set(this.h = a, b); - this.cache.size > this.limit && this.cache.delete(this.cache.keys().next().value); -}; -Pb.prototype.get = function(a) { - var b = this.cache.get(a); - b && this.h !== a && (this.cache.delete(a), this.cache.set(this.h = a, b)); - return b; -}; -Pb.prototype.remove = function(a) { - for (var b = A(this.cache), c = b.next(); !c.done; c = b.next()) { - c = c.value; - var d = c[0]; - c[1].includes(a) && this.cache.delete(d); - } -}; -Pb.prototype.clear = function() { - this.cache.clear(); - this.h = ""; -}; -W.prototype.remove = function(a, b) { +;Y.prototype.remove = function(a, b) { var c = this.reg.size && (this.fastupdate ? this.reg.get(a) : this.reg.has(a)); if (c) { if (this.fastupdate) { - for (var d = 0, e = void 0, f = void 0; d < c.length; d++) { - if ((e = c[d]) && (f = e.length)) { - if (e[f - 1] === a) { + for (var d = 0, e = void 0, g = void 0; d < c.length; d++) { + if ((e = c[d]) && (g = e.length)) { + if (e[g - 1] === a) { e.pop(); } else { - var g = e.indexOf(a); - 0 <= g && e.splice(g, 1); + var f = e.indexOf(a); + 0 <= f && e.splice(f, 1); } } } } else { - Sb(this.map, a), this.depth && Sb(this.ctx, a); + Tb(this.map, a), this.depth && Tb(this.ctx, a); } b || this.reg.delete(a); } - this.db && (this.commit_task.push({del:a}), this.ca && Tb(this)); + this.db && (this.commit_task.push({del:a}), this.ea && Ub(this)); this.cache && this.cache.remove(a); return this; }; -function Sb(a, b) { +function Tb(a, b) { var c = 0, d = "undefined" === typeof b; if (a.constructor === Array) { - for (var e = 0, f = void 0, g; e < a.length; e++) { - if ((f = a[e]) && f.length) { + for (var e = 0, g = void 0, f, h = void 0; e < a.length; e++) { + if ((g = a[e]) && g.length) { if (d) { - c++; - } else { - if (g = f.indexOf(b), 0 <= g) { - if (1 < f.length) { - f.splice(g, 1); - c++; - break; - } else { - delete a[e]; - } - } else { - c++; + return 1; + } + f = g.indexOf(b); + if (0 <= f) { + if (1 < g.length) { + return g.splice(f, 1), 1; } + delete a[e]; + if (c) { + return 1; + } + h = 1; + } else { + if (h) { + return 1; + } + c++; } } } } else { - for (d = A(a.entries()), e = d.next(); !e.done; e = d.next()) { - e = e.value, f = e[0], Sb(e[1], b) ? c++ : a.delete(f); + for (d = C(a.entries()), e = d.next(); !e.done; e = d.next()) { + e = e.value, g = e[0], Tb(e[1], b) ? c++ : a.delete(g); } } return c; } -;var Ub = {memory:{resolution:1}, performance:{resolution:3, fastupdate:!0, context:{depth:1, resolution:1}}, match:{tokenize:"forward"}, score:{resolution:9, context:{depth:2, resolution:3}}}; -W.prototype.add = function(a, b, c, d) { +;var Vb = {memory:{resolution:1}, performance:{resolution:3, fastupdate:!0, context:{depth:1, resolution:1}}, match:{tokenize:"forward"}, score:{resolution:9, context:{depth:2, resolution:3}}}; +Y.prototype.add = function(a, b, c, d) { if (b && (a || 0 === a)) { if (!d && !c && this.reg.has(a)) { return this.update(a, b); @@ -3079,17 +3192,17 @@ W.prototype.add = function(a, b, c, d) { b = this.encoder.encode(b, !d); var e = b.length; if (e) { - for (var f = S(), g = S(), h = this.resolution, k = 0; k < e; k++) { + for (var g = S(), f = S(), h = this.resolution, k = 0; k < e; k++) { var l = b[this.rtl ? e - 1 - k : k], m = l.length; - if (m && (d || !g[l])) { - var q = this.score ? this.score(b, l, k, null, 0) : Vb(h, e, k), p = ""; + if (m && (d || !f[l])) { + var p = this.score ? this.score(b, l, k, null, 0) : Wb(h, e, k), n = ""; switch(this.tokenize) { case "full": if (2 < m) { - q = 0; - for (var n; q < m; q++) { - for (var r = m; r > q; r--) { - p = l.substring(q, r), n = this.rtl ? m - 1 - q : q, n = this.score ? this.score(b, l, k, p, n) : Vb(h, e, k, m, n), Wb(this, g, p, n, a, c); + p = 0; + for (var q; p < m; p++) { + for (var t = m; t > p; t--) { + n = l.substring(p, t), q = this.rtl ? m - 1 - p : p, q = this.score ? this.score(b, l, k, n, q) : Wb(h, e, k, m, q), Xb(this, f, n, q, a, c); } } break; @@ -3097,25 +3210,25 @@ W.prototype.add = function(a, b, c, d) { case "bidirectional": case "reverse": if (1 < m) { - for (r = m - 1; 0 < r; r--) { - p = l[this.rtl ? m - 1 - r : r] + p, n = this.score ? this.score(b, l, k, p, r) : Vb(h, e, k, m, r), Wb(this, g, p, n, a, c); + for (t = m - 1; 0 < t; t--) { + n = l[this.rtl ? m - 1 - t : t] + n, q = this.score ? this.score(b, l, k, n, t) : Wb(h, e, k, m, t), Xb(this, f, n, q, a, c); } - p = ""; + n = ""; } case "forward": if (1 < m) { - for (r = 0; r < m; r++) { - p += l[this.rtl ? m - 1 - r : r], Wb(this, g, p, q, a, c); + for (t = 0; t < m; t++) { + n += l[this.rtl ? m - 1 - t : t], Xb(this, f, n, p, a, c); } break; } default: - if (Wb(this, g, l, q, a, c), d && 1 < e && k < e - 1) { - for (m = S(), p = this.da, q = l, r = Math.min(d + 1, this.rtl ? k + 1 : e - k), n = m[q] = 1; n < r; n++) { - if ((l = b[this.rtl ? e - 1 - k - n : k + n]) && !m[l]) { + if (Xb(this, f, l, p, a, c), d && 1 < e && k < e - 1) { + for (m = S(), n = this.ga, p = l, t = Math.min(d + 1, this.rtl ? k + 1 : e - k), q = m[p] = 1; q < t; q++) { + if ((l = b[this.rtl ? e - 1 - k - q : k + q]) && !m[l]) { m[l] = 1; - var u = this.score ? this.score(b, q, k, l, n - 1) : Vb(p + (e / 2 > p ? 0 : 1), e, k, r - 1, n - 1), x = this.bidirectional && l > q; - Wb(this, f, x ? q : l, u, a, c, x ? l : q); + var u = this.score ? this.score(b, p, k, l, q - 1) : Wb(n + (e / 2 > n ? 0 : 1), e, k, t - 1, q - 1), v = this.bidirectional && l > p; + Xb(this, g, v ? p : l, u, a, c, v ? l : p); } } } @@ -3127,18 +3240,18 @@ W.prototype.add = function(a, b, c, d) { b = ""; } } - this.db && (b || this.commit_task.push({del:a}), this.ca && Tb(this)); + this.db && (b || this.commit_task.push({del:a}), this.ea && Ub(this)); return this; }; -function Wb(a, b, c, d, e, f, g) { - var h = g ? a.ctx : a.map, k; - if (!b[c] || g && !(k = b[c])[g]) { - if (g ? (b = k || (b[c] = S()), b[g] = 1, (k = h.get(g)) ? h = k : h.set(g, h = new Map())) : b[c] = 1, (k = h.get(c)) ? h = k : h.set(c, h = k = []), h = h[d] || (h[d] = []), !f || !h.includes(e)) { +function Xb(a, b, c, d, e, g, f) { + var h = f ? a.ctx : a.map, k; + if (!b[c] || f && !(k = b[c])[f]) { + if (f ? (b = k || (b[c] = S()), b[f] = 1, (k = h.get(f)) ? h = k : h.set(f, h = new Map())) : b[c] = 1, (k = h.get(c)) ? h = k : h.set(c, h = k = []), h = h[d] || (h[d] = []), !g || !h.includes(e)) { if (h.length === Math.pow(2, 31) - 1) { - b = new jb(h); + b = new Wa(h); if (a.fastupdate) { - for (c = A(a.reg.values()), f = c.next(); !f.done; f = c.next()) { - f = f.value, f.includes(h) && (f[f.indexOf(h)] = b); + for (c = C(a.reg.values()), g = c.next(); !g.done; g = c.next()) { + g = g.value, g.includes(h) && (g[g.indexOf(h)] = b); } } k[d] = h = b; @@ -3148,124 +3261,124 @@ function Wb(a, b, c, d, e, f, g) { } } } -function Vb(a, b, c, d, e) { +function Wb(a, b, c, d, e) { return c && 1 < a ? b + (d || 0) <= a ? c + (e || 0) : (a - 1) / (b + (d || 0)) * (c + (e || 0)) + 1 | 0 : 0; } -;W.prototype.search = function(a, b, c) { +;Y.prototype.search = function(a, b, c) { c || (b || "object" !== typeof a ? "object" === typeof b && (c = b, b = 0) : (c = a, a = "")); if (c && c.cache) { return c.cache = !1, a = this.searchCache(a, b, c), c.cache = !0, a; } - var d = [], e = 0, f; + var d = [], e = 0, g; if (c) { a = c.query || a; b = c.limit || b; e = c.offset || 0; - var g = c.context; + var f = c.context; var h = c.suggest; - var k = (f = c.resolve) && c.enrich; + var k = (g = c.resolve) && c.enrich; var l = c.boost; var m = c.resolution; - var q = this.db && c.tag; + var p = this.db && c.tag; } - "undefined" === typeof f && (f = this.resolve); - g = this.depth && !1 !== g; - var p = this.encoder.encode(a, !g); - var n = p.length; - b = b || (f ? 100 : 0); - if (1 === n) { - return Xb.call(this, p[0], "", b, e, f, k, q); + "undefined" === typeof g && (g = this.resolve); + f = this.depth && !1 !== f; + var n = this.encoder.encode(a, !f); + var q = n.length; + b = b || (g ? 100 : 0); + if (1 === q) { + return Yb.call(this, n[0], "", b, e, g, k, p); } - if (2 === n && g && !h) { - return Xb.call(this, p[1], p[0], b, e, f, k, q); + if (2 === q && f && !h) { + return Yb.call(this, n[1], n[0], b, e, g, k, p); } - var r = S(), u = 0; - if (g) { - var x = p[0]; + var t = S(), u = 0; + if (f) { + var v = n[0]; u = 1; } - m || 0 === m || (m = x ? this.da : this.resolution); + m || 0 === m || (m = v ? this.ga : this.resolution); if (this.db) { - if (this.db.search && (c = this.db.search(this, p, b, e, h, f, k, q), !1 !== c)) { + if (this.db.search && (c = this.db.search(this, n, b, e, h, g, k, p), !1 !== c)) { return c; } - var w = this; + var x = this; return function() { - var E, B; - return xa(function(y) { - switch(y.h) { + var y, E; + return wa(function(B) { + switch(B.h) { case 1: - B = E = void 0; + E = y = void 0; case 2: - if (!(u < n)) { - y.h = 4; + if (!(u < q)) { + B.h = 4; break; } - B = p[u]; - if (!B || r[B]) { - y.h = 5; + E = n[u]; + if (!E || t[E]) { + B.h = 5; break; } - r[B] = 1; - return L(y, Yb(w, B, x, 0, 0, !1, !1), 6); + t[E] = 1; + return K(B, Zb(x, E, v, 0, 0, !1, !1), 6); case 6: - E = y.D; - if (E = Zb(E, d, h, m)) { - d = E; - y.h = 4; + y = B.B; + if (y = $b(y, d, h, m)) { + d = y; + B.h = 4; break; } - x && (h && E && d.length || (x = B)); + v && (h && y && d.length || (v = E)); case 5: - h && x && u === n - 1 && !d.length && (m = w.resolution, x = "", u = -1, r = S()); + h && v && u === q - 1 && !d.length && (m = x.resolution, v = "", u = -1, t = S()); u++; - y.h = 2; + B.h = 2; break; case 4: - return y.return($b(d, m, b, e, h, l, f)); + return B.return(ac(d, m, b, e, h, l, g)); } }); }(); } - for (a = c = void 0; u < n; u++) { - if ((a = p[u]) && !r[a]) { - r[a] = 1; - c = Yb(this, a, x, 0, 0, !1, !1); - if (c = Zb(c, d, h, m)) { + for (a = c = void 0; u < q; u++) { + if ((a = n[u]) && !t[a]) { + t[a] = 1; + c = Zb(this, a, v, 0, 0, !1, !1); + if (c = $b(c, d, h, m)) { d = c; break; } - x && (h && c && d.length || (x = a)); + v && (h && c && d.length || (v = a)); } - h && x && u === n - 1 && !d.length && (m = this.resolution, x = "", u = -1, r = S()); + h && v && u === q - 1 && !d.length && (m = this.resolution, v = "", u = -1, t = S()); } - return $b(d, m, b, e, h, l, f); + return ac(d, m, b, e, h, l, g); }; -function $b(a, b, c, d, e, f, g) { +function ac(a, b, c, d, e, g, f) { var h = a.length, k = a; if (1 < h) { - k = yb(a, b, c, d, e, f, g); + k = Ab(a, b, c, d, e, g, f); } else if (1 === h) { - return g ? sb.call(null, a[0], c, d) : new Z(a[0], this); + return f ? sb.call(null, a[0], c, d) : new Z(a[0], this); } - return g ? k : new Z(k, this); + return f ? k : new Z(k, this); } -function Xb(a, b, c, d, e, f, g) { - a = Yb(this, a, b, c, d, e, f, g); +function Yb(a, b, c, d, e, g, f) { + a = Zb(this, a, b, c, d, e, g, f); return this.db ? a.then(function(h) { return e ? h || [] : new Z(h, this); }) : a && a.length ? e ? sb.call(this, a, c, d) : new Z(a, this) : e ? [] : new Z([], this); } -function Zb(a, b, c, d) { +function $b(a, b, c, d) { var e = []; if (a && a.length) { if (a.length <= d) { b.push(a); return; } - for (var f = 0, g; f < d; f++) { - if (g = a[f]) { - e[f] = g; + for (var g = 0, f; g < d; g++) { + if (f = a[g]) { + e[g] = f; } } if (e.length) { @@ -3277,28 +3390,28 @@ function Zb(a, b, c, d) { return e; } } -function Yb(a, b, c, d, e, f, g, h) { +function Zb(a, b, c, d, e, g, f, h) { var k; c && (k = a.bidirectional && b > c) && (k = c, c = b, b = k); if (a.db) { - return a.db.get(b, c, d, e, f, g, h); + return a.db.get(b, c, d, e, g, f, h); } a = c ? (a = a.ctx.get(c)) && a.get(b) : a.map.get(b); return a; } -;function W(a, b) { - if (!this || this.constructor !== W) { - return new W(a); +;function Y(a, b) { + if (!this || this.constructor !== Y) { + return new Y(a); } if (a) { var c = T(a) ? a : a.preset; - c && (Ub[c] || console.warn("Preset not found: " + c), a = Object.assign({}, Ub[c], a)); + c && (Vb[c] || console.warn("Preset not found: " + c), a = Object.assign({}, Vb[c], a)); } else { a = {}; } c = a.context; - var d = !0 === c ? {depth:1} : c || {}, e = T(a.encoder) ? Nb[a.encoder] : a.encode || a.encoder || {}; - this.encoder = e.encode ? e : "object" === typeof e ? new Ma(e) : {encode:e}; + var d = !0 === c ? {depth:1} : c || {}, e = T(a.encoder) ? Ua[a.encoder] : a.encode || a.encoder || {}; + this.encoder = e.encode ? e : "object" === typeof e ? new Ka(e) : {encode:e}; this.resolution = a.resolution || 9; this.tokenize = c = (c = a.tokenize) && "default" !== c && "exact" !== c && c || "strict"; this.depth = "strict" === c && d.depth || 0; @@ -3307,41 +3420,41 @@ function Yb(a, b, c, d, e, f, g, h) { this.score = a.score || null; d && d.depth && "strict" !== this.tokenize && console.warn('Context-Search could not applied, because it is just supported when using the tokenizer "strict".'); (c = a.keystore || 0) && (this.keystore = c); - this.map = c ? new X(c) : new Map(); - this.ctx = c ? new X(c) : new Map(); - this.reg = b || (this.fastupdate ? c ? new X(c) : new Map() : c ? new Y(c) : new Set()); - this.da = d.resolution || 3; + this.map = c ? new Xa(c) : new Map(); + this.ctx = c ? new Xa(c) : new Map(); + this.reg = b || (this.fastupdate ? c ? new Xa(c) : new Map() : c ? new X(c) : new Set()); + this.ga = d.resolution || 3; this.rtl = e.rtl || a.rtl || !1; - this.cache = (c = a.cache || null) && new Pb(c); + this.cache = (c = a.cache || null) && new Na(c); this.resolve = !1 !== a.resolve; if (c = a.db) { this.db = this.mount(c); } - this.ca = !1 !== a.commit; + this.ea = !1 !== a.commit; this.commit_task = []; this.commit_timer = null; this.priority = a.priority || 4; } -v = W.prototype; -v.mount = function(a) { +w = Y.prototype; +w.mount = function(a) { this.commit_timer && (clearTimeout(this.commit_timer), this.commit_timer = null); return a.mount(this); }; -v.commit = function(a, b) { +w.commit = function(a, b) { this.commit_timer && (clearTimeout(this.commit_timer), this.commit_timer = null); return this.db.commit(this, a, b); }; -v.destroy = function() { +w.destroy = function() { this.commit_timer && (clearTimeout(this.commit_timer), this.commit_timer = null); return this.db.destroy(); }; -function Tb(a) { +function Ub(a) { a.commit_timer || (a.commit_timer = setTimeout(function() { a.commit_timer = null; a.db.commit(a, void 0, void 0); }, 1)); } -v.clear = function() { +w.clear = function() { this.map.clear(); this.ctx.clear(); this.reg.clear(); @@ -3349,98 +3462,98 @@ v.clear = function() { this.db && (this.commit_timer && clearTimeout(this.commit_timer), this.commit_timer = null, this.commit_task = [{clear:!0}]); return this; }; -v.append = function(a, b) { +w.append = function(a, b) { return this.add(a, b, !0); }; -v.contain = function(a) { +w.contain = function(a) { return this.db ? this.db.has(a) : this.reg.has(a); }; -v.update = function(a, b) { +w.update = function(a, b) { var c = this, d = this.remove(a); return d && d.then ? d.then(function() { return c.add(a, b); }) : this.add(a, b); }; -v.cleanup = function() { +w.cleanup = function() { if (!this.fastupdate) { return console.info('Cleanup the index isn\'t required when not using "fastupdate".'), this; } - Sb(this.map); - this.depth && Sb(this.ctx); + Tb(this.map); + this.depth && Tb(this.ctx); return this; }; -v.searchCache = Rb; -v.export = function(a, b, c, d) { +w.searchCache = Ma; +w.export = function(a, b, c, d) { c = void 0 === c ? 0 : c; d = void 0 === d ? 0 : d; switch(d) { case 0: var e = "reg"; - var f = eb(this.reg); + var g = Pb(this.reg); break; case 1: e = "cfg"; - f = null; + g = null; break; case 2: e = "map"; - f = ab(this.map, this.reg.size); + g = Lb(this.map, this.reg.size); break; case 3: e = "ctx"; - f = cb(this.ctx, this.reg.size); + g = Nb(this.ctx, this.reg.size); break; default: return; } - return gb.call(this, a, b, e, f, c, d); + return Rb.call(this, a, b, e, g, c, d); }; -v.import = function(a, b) { +w.import = function(a, b) { if (b) { switch("string" === typeof b && (b = JSON.parse(b)), a = a.split("."), "json" === a[a.length - 1] && a.pop(), 3 === a.length && a.shift(), a = 1 < a.length ? a[1] : a[0], a) { case "reg": this.fastupdate = !1; - this.reg = fb(b, this.reg); + this.reg = Qb(b, this.reg); break; case "map": - this.map = bb(b, this.map); + this.map = Mb(b, this.map); break; case "ctx": - this.ctx = db(b, this.ctx); + this.ctx = Ob(b, this.ctx); } } }; -v.serialize = function(a) { +w.serialize = function(a) { a = void 0 === a ? !0 : a; var b = "", c = "", d = ""; if (this.reg.size) { var e; - c = A(this.reg.keys()); - for (var f = c.next(); !f.done; f = c.next()) { - f = f.value, e || (e = typeof f), b += (b ? "," : "") + ("string" === e ? '"' + f + '"' : f); + c = C(this.reg.keys()); + for (var g = c.next(); !g.done; g = c.next()) { + g = g.value, e || (e = typeof g), b += (b ? "," : "") + ("string" === e ? '"' + g + '"' : g); } b = "index.reg=new Set([" + b + "]);"; - c = hb(this.map, e); + c = Sb(this.map, e); c = "index.map=new Map([" + c + "]);"; - f = A(this.ctx.entries()); - for (var g = f.next(); !g.done; g = f.next()) { - var h = g.value; - g = h[0]; - h = hb(h[1], e); + g = C(this.ctx.entries()); + for (var f = g.next(); !f.done; f = g.next()) { + var h = f.value; + f = h[0]; + h = Sb(h[1], e); h = "new Map([" + h + "])"; - h = '["' + g + '",' + h + "]"; + h = '["' + f + '",' + h + "]"; d += (d ? "," : "") + h; } d = "index.ctx=new Map([" + d + "]);"; } return a ? "function inject(index){" + b + c + d + "}" : b + c + d; }; -Ra(W.prototype); -var ac = "undefined" !== typeof window && (window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB), bc = ["map", "ctx", "tag", "reg", "cfg"], cc = S(); -function dc(a, b) { +fb(Y.prototype); +var bc = "undefined" !== typeof window && (window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB), cc = ["map", "ctx", "tag", "reg", "cfg"], dc = S(); +function ec(a, b) { b = void 0 === b ? {} : b; - if (!this || this.constructor !== dc) { - return new dc(a, b); + if (!this || this.constructor !== ec) { + return new ec(a, b); } "object" === typeof a && (b = a, a = a.name); a || console.info("Default storage space was used, because a name was not passed."); @@ -3451,67 +3564,67 @@ function dc(a, b) { this.db = null; this.h = {}; } -v = dc.prototype; -v.mount = function(a) { +w = ec.prototype; +w.mount = function(a) { if (a.index) { return a.mount(this); } a.db = this; return this.open(); }; -v.open = function() { +w.open = function() { if (this.db) { return this.db; } var a = this; navigator.storage && navigator.storage.persist(); - cc[a.id] || (cc[a.id] = []); - cc[a.id].push(a.field); - var b = ac.open(a.id, 1); + dc[a.id] || (dc[a.id] = []); + dc[a.id].push(a.field); + var b = bc.open(a.id, 1); b.onupgradeneeded = function() { - for (var c = a.db = this.result, d = 0, e; d < bc.length; d++) { - e = bc[d]; - for (var f = 0, g; f < cc[a.id].length; f++) { - g = cc[a.id][f], c.objectStoreNames.contains(e + ("reg" !== e ? g ? ":" + g : "" : "")) || c.createObjectStore(e + ("reg" !== e ? g ? ":" + g : "" : "")); + for (var c = a.db = this.result, d = 0, e; d < cc.length; d++) { + e = cc[d]; + for (var g = 0, f; g < dc[a.id].length; g++) { + f = dc[a.id][g], c.objectStoreNames.contains(e + ("reg" !== e ? f ? ":" + f : "" : "")) || c.createObjectStore(e + ("reg" !== e ? f ? ":" + f : "" : "")); } } }; - return a.db = ec(b, function(c) { + return a.db = fc(b, function(c) { a.db = c; a.db.onversionchange = function() { a.close(); }; }); }; -v.close = function() { +w.close = function() { this.db && this.db.close(); this.db = null; }; -v.destroy = function() { - var a = ac.deleteDatabase(this.id); - return ec(a); +w.destroy = function() { + var a = bc.deleteDatabase(this.id); + return fc(a); }; -v.clear = function() { - for (var a = [], b = 0, c; b < bc.length; b++) { - c = bc[b]; - for (var d = 0, e; d < cc[this.id].length; d++) { - e = cc[this.id][d], a.push(c + ("reg" !== c ? e ? ":" + e : "" : "")); +w.clear = function() { + for (var a = [], b = 0, c; b < cc.length; b++) { + c = cc[b]; + for (var d = 0, e; d < dc[this.id].length; d++) { + e = dc[this.id][d], a.push(c + ("reg" !== c ? e ? ":" + e : "" : "")); } } b = this.db.transaction(a, "readwrite"); for (c = 0; c < a.length; c++) { b.objectStore(a[c]).clear(); } - return ec(b); + return fc(b); }; -v.get = function(a, b, c, d, e, f) { +w.get = function(a, b, c, d, e, g) { c = void 0 === c ? 0 : c; d = void 0 === d ? 0 : d; e = void 0 === e ? !0 : e; - f = void 0 === f ? !1 : f; + g = void 0 === g ? !1 : g; a = this.db.transaction((b ? "ctx" : "map") + (this.field ? ":" + this.field : ""), "readonly").objectStore((b ? "ctx" : "map") + (this.field ? ":" + this.field : "")).get(b ? b + ":" + a : a); - var g = this; - return ec(a).then(function(h) { + var f = this; + return fc(a).then(function(h) { var k = []; if (!h || !h.length) { return k; @@ -3525,8 +3638,8 @@ v.get = function(a, b, c, d, e, f) { if (d >= m.length) { d -= m.length; } else { - for (var q = c ? d + Math.min(m.length - d, c) : m.length, p = d; p < q; p++) { - k.push(m[p]); + for (var p = c ? d + Math.min(m.length - d, c) : m.length, n = d; n < p; n++) { + k.push(m[n]); } d = 0; if (k.length === c) { @@ -3535,50 +3648,50 @@ v.get = function(a, b, c, d, e, f) { } } } - return f ? g.enrich(k) : k; + return g ? f.enrich(k) : k; } return h; }); }; -v.tag = function(a, b, c, d) { +w.tag = function(a, b, c, d) { b = void 0 === b ? 0 : b; c = void 0 === c ? 0 : c; d = void 0 === d ? !1 : d; a = this.db.transaction("tag" + (this.field ? ":" + this.field : ""), "readonly").objectStore("tag" + (this.field ? ":" + this.field : "")).get(a); var e = this; - return ec(a).then(function(f) { - if (!f || !f.length || c >= f.length) { + return fc(a).then(function(g) { + if (!g || !g.length || c >= g.length) { return []; } if (!b && !c) { - return f; + return g; } - f = f.slice(c, c + b); - return d ? e.enrich(f) : f; + g = g.slice(c, c + b); + return d ? e.enrich(g) : g; }); }; -v.enrich = function(a) { +w.enrich = function(a) { "object" !== typeof a && (a = [a]); for (var b = this.db.transaction("reg", "readonly").objectStore("reg"), c = [], d = 0; d < a.length; d++) { - c[d] = ec(b.get(a[d])); + c[d] = fc(b.get(a[d])); } return Promise.all(c).then(function(e) { - for (var f = 0; f < e.length; f++) { - e[f] = {id:a[f], doc:e[f] ? JSON.parse(e[f]) : null}; + for (var g = 0; g < e.length; g++) { + e[g] = {id:a[g], doc:e[g] ? JSON.parse(e[g]) : null}; } return e; }); }; -v.has = function(a) { +w.has = function(a) { a = this.db.transaction("reg", "readonly").objectStore("reg").getKey(a); - return ec(a).then(function(b) { + return fc(a).then(function(b) { return !!b; }); }; -v.search = null; -v.info = function() { +w.search = null; +w.info = function() { }; -v.transaction = function(a, b, c) { +w.transaction = function(a, b, c) { a += "reg" !== a ? this.field ? ":" + this.field : "" : ""; var d = this.h[a + ":" + b]; if (d) { @@ -3586,43 +3699,43 @@ v.transaction = function(a, b, c) { } var e = this.db.transaction(a, b); this.h[a + ":" + b] = d = e.objectStore(a); - var f = c.call(this, d); + var g = c.call(this, d); this.h[a + ":" + b] = null; - return ec(e).finally(function() { + return fc(e).finally(function() { e = d = null; - return f; + return g; }); }; -v.commit = function(a, b, c) { - var d = this, e, f, g; - return xa(function(h) { +w.commit = function(a, b, c) { + var d = this, e, g, f; + return wa(function(h) { switch(h.h) { case 1: if (b) { - return L(h, d.clear(), 12); + return K(h, d.clear(), 12); } e = a.commit_task; a.commit_task = []; - f = 0; - g = void 0; + g = 0; + f = void 0; case 4: - if (!(f < e.length)) { + if (!(g < e.length)) { h.h = 6; break; } - g = e[f]; - if (!g.clear) { - e[f] = g.del; + f = e[g]; + if (!f.clear) { + e[g] = f.del; h.h = 5; break; } - return L(h, d.clear(), 8); + return K(h, d.clear(), 8); case 8: b = !0; h.h = 6; break; case 5: - f++; + g++; h.h = 4; break; case 6: @@ -3630,12 +3743,12 @@ v.commit = function(a, b, c) { h.h = 3; break; } - c || (e = e.concat(Ea(a.reg))); + c || (e = e.concat(Da(a.reg))); if (!e.length) { h.h = 10; break; } - return L(h, d.remove(e), 11); + return K(h, d.remove(e), 11); case 11: case 10: h.h = 3; @@ -3643,72 +3756,72 @@ v.commit = function(a, b, c) { case 12: a.commit_task = []; case 3: - return a.reg.size ? L(h, d.transaction("map", "readwrite", function(k) { - for (var l = A(a.map), m = l.next(), q = {}; !m.done; q = {O:void 0, Y:void 0}, m = l.next()) { - m = m.value, q.Y = m[0], q.O = m[1], q.O.length && (b ? k.put(q.O, q.Y) : k.get(q.Y).onsuccess = function(p) { + return a.reg.size ? K(h, d.transaction("map", "readwrite", function(k) { + for (var l = C(a.map), m = l.next(), p = {}; !m.done; p = {P:void 0, Y:void 0}, m = l.next()) { + m = m.value, p.Y = m[0], p.P = m[1], p.P.length && (b ? k.put(p.P, p.Y) : k.get(p.Y).onsuccess = function(n) { return function() { - var n = this.result, r; - if (n && n.length) { - for (var u = Math.max(n.length, p.O.length), x = 0, w; x < u; x++) { - if ((w = p.O[x]) && w.length) { - if ((r = n[x]) && r.length) { - for (var E = 0; E < w.length; E++) { - r.push(w[E]); + var q = this.result, t; + if (q && q.length) { + for (var u = Math.max(q.length, n.P.length), v = 0, x; v < u; v++) { + if ((x = n.P[v]) && x.length) { + if ((t = q[v]) && t.length) { + for (var y = 0; y < x.length; y++) { + t.push(x[y]); } } else { - n[x] = w; + q[v] = x; } - r = 1; + t = 1; } } } else { - n = p.O, r = 1; + q = n.P, t = 1; } - r && k.put(n, p.Y); + t && k.put(q, n.Y); }; - }(q)); + }(p)); } }), 13) : h.return(); case 13: - return L(h, d.transaction("ctx", "readwrite", function(k) { - for (var l = A(a.ctx), m = l.next(), q = {}; !m.done; q = {V:void 0}, m = l.next()) { + return K(h, d.transaction("ctx", "readwrite", function(k) { + for (var l = C(a.ctx), m = l.next(), p = {}; !m.done; p = {W:void 0}, m = l.next()) { m = m.value; - q.V = m[0]; - m = A(m[1]); - for (var p = m.next(), n = {}; !p.done; n = {P:void 0, Z:void 0}, p = m.next()) { - p = p.value, n.Z = p[0], n.P = p[1], n.P.length && (b ? k.put(n.P, q.V + ":" + n.Z) : k.get(q.V + ":" + n.Z).onsuccess = function(r, u) { + p.W = m[0]; + m = C(m[1]); + for (var n = m.next(), q = {}; !n.done; q = {R:void 0, Z:void 0}, n = m.next()) { + n = n.value, q.Z = n[0], q.R = n[1], q.R.length && (b ? k.put(q.R, p.W + ":" + q.Z) : k.get(p.W + ":" + q.Z).onsuccess = function(t, u) { return function() { - var x = this.result, w; - if (x && x.length) { - for (var E = Math.max(x.length, r.P.length), B = 0, y; B < E; B++) { - if ((y = r.P[B]) && y.length) { - if ((w = x[B]) && w.length) { - for (var t = 0; t < y.length; t++) { - w.push(y[t]); + var v = this.result, x; + if (v && v.length) { + for (var y = Math.max(v.length, t.R.length), E = 0, B; E < y; E++) { + if ((B = t.R[E]) && B.length) { + if ((x = v[E]) && x.length) { + for (var r = 0; r < B.length; r++) { + x.push(B[r]); } } else { - x[B] = y; + v[E] = B; } - w = 1; + x = 1; } } } else { - x = r.P, w = 1; + v = t.R, x = 1; } - w && k.put(x, u.V + ":" + r.Z); + x && k.put(v, u.W + ":" + t.Z); }; - }(n, q)); + }(q, p)); } } }), 14); case 14: if (a.store) { - return L(h, d.transaction("reg", "readwrite", function(k) { - for (var l = A(a.store), m = l.next(); !m.done; m = l.next()) { - var q = m.value; - m = q[0]; - q = q[1]; - k.put("object" === typeof q ? JSON.stringify(q) : 1, m); + return K(h, d.transaction("reg", "readwrite", function(k) { + for (var l = C(a.store), m = l.next(); !m.done; m = l.next()) { + var p = m.value; + m = p[0]; + p = p[1]; + k.put("object" === typeof p ? JSON.stringify(p) : 1, m); } }), 16); } @@ -3716,8 +3829,8 @@ v.commit = function(a, b, c) { h.h = 16; break; } - return L(h, d.transaction("reg", "readwrite", function(k) { - for (var l = A(a.reg.keys()), m = l.next(); !m.done; m = l.next()) { + return K(h, d.transaction("reg", "readwrite", function(k) { + for (var l = C(a.reg.keys()), m = l.next(); !m.done; m = l.next()) { k.put(1, m.value); } }), 16); @@ -3726,15 +3839,15 @@ v.commit = function(a, b, c) { h.h = 20; break; } - return L(h, d.transaction("tag", "readwrite", function(k) { - for (var l = A(a.tag), m = l.next(), q = {}; !m.done; q = {X:void 0, ba:void 0}, m = l.next()) { - m = m.value, q.ba = m[0], q.X = m[1], q.X.length && (k.get(q.ba).onsuccess = function(p) { + return K(h, d.transaction("tag", "readwrite", function(k) { + for (var l = C(a.tag), m = l.next(), p = {}; !m.done; p = {X:void 0, da:void 0}, m = l.next()) { + m = m.value, p.da = m[0], p.X = m[1], p.X.length && (k.get(p.da).onsuccess = function(n) { return function() { - var n = this.result; - n = n && n.length ? n.concat(p.X) : p.X; - k.put(n, p.ba); + var q = this.result; + q = q && q.length ? q.concat(n.X) : n.X; + k.put(q, n.da); }; - }(q)); + }(p)); } }), 20); case 20: @@ -3742,44 +3855,44 @@ v.commit = function(a, b, c) { } }); }; -function fc(a, b, c) { - for (var d = a.value, e, f = 0, g = 0, h; g < d.length; g++) { - if (h = c ? d : d[g]) { +function gc(a, b, c) { + for (var d = a.value, e, g = 0, f = 0, h; f < d.length; f++) { + if (h = c ? d : d[f]) { for (var k = 0, l; k < b.length; k++) { if (l = b[k], l = h.indexOf(l), 0 <= l) { if (e = 1, 1 < h.length) { h.splice(l, 1); } else { - d[g] = []; + d[f] = []; break; } } } - f += h.length; + g += h.length; } if (c) { break; } } - f ? e && a.update(d) : a.delete(); + g ? e && a.update(d) : a.delete(); a.continue(); } -v.remove = function(a) { +w.remove = function(a) { "object" !== typeof a && (a = [a]); return Promise.all([this.transaction("map", "readwrite", function(b) { b.openCursor().onsuccess = function() { var c = this.result; - c && fc(c, a); + c && gc(c, a); }; }), this.transaction("ctx", "readwrite", function(b) { b.openCursor().onsuccess = function() { var c = this.result; - c && fc(c, a); + c && gc(c, a); }; }), this.transaction("tag", "readwrite", function(b) { b.openCursor().onsuccess = function() { var c = this.result; - c && fc(c, a, !0); + c && gc(c, a, !0); }; }), this.transaction("reg", "readwrite", function(b) { for (var c = 0; c < a.length; c++) { @@ -3787,7 +3900,7 @@ v.remove = function(a) { } })]); }; -function ec(a, b) { +function fc(a, b) { return new Promise(function(c, d) { a.onsuccess = a.oncomplete = function() { b && b(this.result); @@ -3798,8 +3911,8 @@ function ec(a, b) { a = null; }); } -;var gc = {Index:W, Charset:Nb, Encoder:Ma, Document:pb, Worker:Ya, Resolver:Z, IndexedDB:dc, Language:{}}, hc = "undefined" !== typeof self ? self : "undefined" !== typeof global ? global : self, ic; -(ic = hc.define) && ic.amd ? ic([], function() { - return gc; -}) : "object" === typeof hc.exports ? hc.exports = gc : hc.FlexSearch = gc; +;var hc = {Index:Y, Charset:Ua, Encoder:Ka, Document:pb, Worker:mb, Resolver:Z, IndexedDB:ec, Language:{}}, ic = "undefined" !== typeof self ? self : "undefined" !== typeof global ? global : self, jc; +(jc = ic.define) && jc.amd ? jc([], function() { + return hc; +}) : "object" === typeof ic.exports ? ic.exports = hc : ic.FlexSearch = hc; }(this||self)); diff --git a/dist/flexsearch.es5.min.js b/dist/flexsearch.es5.min.js index 613a4f8..ec8823d 100644 --- a/dist/flexsearch.es5.min.js +++ b/dist/flexsearch.es5.min.js @@ -1,142 +1,144 @@ /**! - * FlexSearch.js v0.8.167 (ES5) + * FlexSearch.js v0.8.200 (ES5) * Author and Copyright: Thomas Wilkerling * Licence: Apache-2.0 * Hosted by Nextapps GmbH * https://github.com/nextapps-de/flexsearch */ -(function _f(self){'use strict';if(typeof module!=='undefined')self=module;else if(typeof process !== 'undefined')self=process;self._factory=_f;var v;function ca(a){var b=0;return function(){return b>>0)+"_",e=0;return b}); -H("Symbol.iterator",function(a){if(a)return a;a=Symbol("Symbol.iterator");for(var b="Array Int8Array Uint8Array Uint8ClampedArray Int16Array Uint16Array Int32Array Uint32Array Float32Array Float64Array".split(" "),c=0;cc&&(c=Math.max(c+e,0));c"a1a".split(c).length; -this.numeric=O(a.numeric,e)}else{try{this.split=O(this.split,Ha)}catch(f){this.split=/\s+/}this.numeric=O(a.numeric,O(this.numeric,!0))}this.prepare=O(a.prepare,null,this.prepare);this.finalize=O(a.finalize,null,this.finalize);c=a.filter;this.filter="function"===typeof c?c:O(c&&new Set(c),null,this.filter);this.dedupe=O(a.dedupe,!0,this.dedupe);this.matcher=O((c=a.matcher)&&new Map(c),null,this.matcher);this.mapper=O((c=a.mapper)&&new Map(c),null,this.mapper);this.stemmer=O((c=a.stemmer)&&new Map(c), -null,this.stemmer);this.replacer=O(a.replacer,null,this.replacer);this.minlength=O(a.minlength,1,this.minlength);this.maxlength=O(a.maxlength,1024,this.maxlength);this.rtl=O(a.rtl,!1,this.rtl);if(this.cache=c=O(a.cache,!0,this.cache))this.D=null,this.T="number"===typeof c?c:2E5,this.B=new Map,this.C=new Map,this.H=this.G=128;this.h="";this.M=null;this.A="";this.N=null;if(this.matcher)for(a=A(this.matcher.keys()),b=a.next();!b.done;b=a.next())this.h+=(this.h?"|":"")+b.value;if(this.stemmer)for(a=A(this.stemmer.keys()), -b=a.next();!b.done;b=a.next())this.A+=(this.A?"|":"")+b.value;return this};v.addStemmer=function(a,b){this.stemmer||(this.stemmer=new Map);this.stemmer.set(a,b);this.A+=(this.A?"|":"")+a;this.N=null;this.cache&&Na(this);return this};v.addFilter=function(a){"function"===typeof a?this.filter=a:(this.filter||(this.filter=new Set),this.filter.add(a));this.cache&&Na(this);return this}; -v.addMapper=function(a,b){if("object"===typeof a)return this.addReplacer(a,b);if(1a.length&&(this.dedupe||this.mapper))return this.addMapper(a,b);this.matcher||(this.matcher=new Map);this.matcher.set(a,b);this.h+=(this.h?"|":"")+a;this.M=null;this.cache&&Na(this);return this}; -v.addReplacer=function(a,b){if("string"===typeof a)return this.addMatcher(a,b);this.replacer||(this.replacer=[]);this.replacer.push(a,b);this.cache&&Na(this);return this}; -v.encode=function(a,b){var c=this;if(this.cache&&a.length<=this.G)if(this.D){if(this.B.has(a))return this.B.get(a)}else this.D=setTimeout(Na,50,this);this.normalize&&("function"===typeof this.normalize?a=this.normalize(a):a=La?a.normalize("NFKD").replace(La,"").toLowerCase():a.toLowerCase());this.prepare&&(a=this.prepare(a));this.numeric&&3this.maxlength)){if(b){if(f[m])continue;f[m]=1}else{if(g===m)continue;g=m}if(d)e.push(m);else if(!this.filter||("function"===typeof this.filter?this.filter(m):!this.filter.has(m))){if(this.cache&&m.length<=this.H)if(this.D){var p=this.C.get(m);if(p||""===p){p&&e.push(p);continue}}else this.D=setTimeout(Na,50,this);if(this.stemmer)for(this.N|| -(this.N=new RegExp("(?!^)("+this.A+")$")),p=void 0;p!==m&&2this.T&&(this.C.clear(),this.H=this.H/1.1|0));if(m){if(m!==q)if(b){if(f[m])continue;f[m]=1}else{if(h===m)continue;h=m}e.push(m)}}}this.finalize&&(e=this.finalize(e)||e);this.cache&&a.length<=this.G&&(this.B.set(a,e),this.B.size>this.T&&(this.B.clear(),this.G=this.G/1.1|0));return e};function Na(a){a.D=null;a.B.clear();a.C.clear()};var Oa,Pa; -function Qa(a){var b,c,d,e,f,g;return xa(function(h){switch(h.h){case 1:a=a.data;b=a.task;c=a.id;d=a.args;switch(b){case "init":Pa=a.options||{};(e=a.factory)?(Function("return "+e)()(self),Oa=new self.FlexSearch.Index(Pa),delete self.FlexSearch):Oa=new W(Pa);postMessage({id:c});break;default:h.h=2;return}h.h=0;break;case 2:"export"===b&&(d[1]?(d[0]=Pa.export,d[2]=0,d[3]=1):d=null);if("import"===b){if(!d[0]){h.h=5;break}return L(h,Pa.import.call(Oa,d[0]),9)}f=d&&Oa[b].apply(Oa,d);if(!f||!f.then){h.h= -5;break}return L(h,f,7);case 7:f=h.D;h.h=5;break;case 9:g=h.D,Oa.import(d[0],g);case 5:postMessage("search"===b?{id:c,msg:f}:{id:c}),h.h=0}})};function Ra(a){Sa.call(a,"add");Sa.call(a,"append");Sa.call(a,"search");Sa.call(a,"update");Sa.call(a,"remove");Sa.call(a,"searchCache")}var Ta,Ua,Va;function Wa(){Ta=Va=0} -function Sa(a){this[a+"Async"]=function(){var b=arguments,c=b[b.length-1];if("function"===typeof c){var d=c;delete b[b.length-1]}Ta?Va||(Va=Date.now()-Ua>=this.priority*this.priority*3):(Ta=setTimeout(Wa,0),Ua=Date.now());if(Va){var e=this;return new Promise(function(g){setTimeout(function(){g(e[a+"Async"].apply(e,b))},0)})}var f=this[a].apply(this,b);c=f.then?f:new Promise(function(g){return g(f)});d&&c.then(d);return c}};var Xa=0; -function Ya(a,b){function c(h){function k(l){l=l.data||l;var m=l.id,q=m&&f.h[m];q&&(q(l.msg),delete f.h[m])}this.worker=h;this.h=S();if(this.worker){e?this.worker.on("message",k):this.worker.onmessage=k;if(a.config)return new Promise(function(l){f.h[++Xa]=function(){l(f);1E9=g.length)b-=g.length;else{b=g[d?"splice":"slice"](b,c);if(g=b.length)if(e=e.length?e.concat(b):b,c-=g,d&&(a.length-=g),!c)break;b=0}return e} -function jb(a){if(!this||this.constructor!==jb)return new jb(a);this.index=a?[a]:[];this.length=a?a.length:0;var b=this;return new Proxy([],{get:function(c,d){if("length"===d)return b.length;if("push"===d)return function(e){b.index[b.index.length-1].push(e);b.length++};if("pop"===d)return function(){if(b.length)return b.length--,b.index[b.index.length-1].pop()};if("indexOf"===d)return function(e){for(var f=0,g=0,h,k;gb?b?a.slice(c,c+b):a.slice(c):a,d?tb.call(this,a):a;for(var e=[],f=0,g=void 0,h=void 0;f=h){c-=h;continue}cb&&(g=g.slice(0,b),h=b);if(!e.length&&h>=b)return d?tb.call(this,g):g;e.push(g);b-=h;if(!b)break}e=1a.length?this.result=a[0]:(this.result=wb(a,c,d,!1,this.h),d=0));return f?this.resolve(c,d,e):this};Z.prototype.and=function(){var a=this.result.length;if(!a){var b=arguments[0];if(b){a=!!b.suggest;var c=b.resolve;var d=b.limit;var e=b.offset;var f=b.enrich&&c}}return a?(a=ub(this,"and",arguments),xb.call(this,a.W,a.$,a.limit,a.offset,a.enrich,a.resolve,a.suggest)):c?this.resolve(d,e,f):this}; -function xb(a,b,c,d,e,f,g){if(b.length){var h=this;return Promise.all(b).then(function(k){a=[];for(var l=0,m=void 0;la.length)this.result=a[0];else{if(b=Ga(a))return this.result=yb(a,b,c,d,g,this.h,f),f?e?tb.call(this.index,this.result):this.result:this;this.result=[]}else g||(this.result=a);return f?this.resolve(c,d,e):this};Z.prototype.xor=function(){var a=ub(this,"xor",arguments);return zb.call(this,a.W,a.$,a.limit,a.offset,a.enrich,a.resolve,a.suggest)}; -function zb(a,b,c,d,e,f,g){if(b.length){var h=this;return Promise.all(b).then(function(k){a=[];for(var l=0,m=void 0;la.length)this.result=a[0];else return this.result=Ab.call(this,a,c,d,f,this.h),f?e?tb.call(this.index,this.result):this.result:this;else g||(this.result=a);return f?this.resolve(c,d,e):this} -function Ab(a,b,c,d,e){for(var f=[],g=S(),h=0,k=0,l;kQ&&(Q=t.length+(t?1:0)),M=t.length+(t?1:0)+ba.length,J+=N,P.push(C.length),C.push({match:ba})),t+=(t?" ":"")+ba)}if(!G)R=D[F],t+=(t?" ":"")+R,k&&C.push({text:R});else if(k&&J>=k)break}J=P.length*(f.length-2);if(n||r||k&&t.length-J>k)if(J=k+J-2*p,F=M-Q,0D&&(D=0)),C=C.length-1){if(K>=C.length){F[z+1]=1;K>=D.length&&(Q[z+1]=1);continue}G-=p}t=C[K].text;var U=r&&V[z]; -if(U)if(0U)if(F[z+1]=1,l)t=t.substring(0,U);else continue;(U-=t.length)||(U=-1);V[z]=U}else{F[z+1]=1;continue}if(G+t.length+1<=k)t=" "+t,M[z]+=t;else if(l)I=k-G-1,0=K){if(0>K){F[z]=1;Q[z]=1;continue}G-=p}t=C[K].text;if(U=n&&R[z])if(0U)if(F[z]=1,l)t=t.substring(t.length-U);else continue;(U-=t.length)||(U=-1);R[z]=U}else{F[z]=1;continue}if(G+ -t.length+1<=k)t+=" ",M[z]=t+M[z];else if(l)I=t.length+1-(k-G),0<=I&&I=D.length-1?I=1:Kc||d?k.slice(d,c+d):k;else{if(ac||d)k=k.slice(d,c+ -d)}else{e=[];for(f=0;fd)d-=g.length;else{if(c&&g.length>c||d)g=g.slice(d,c+d),c-=g.length,d&&(d-=g.length);e.push(g);if(!c)break}k=e}}return k} -function wb(a,b,c,d,e){var f=[],g=S(),h=a.length,k;if(d)for(e=h-1;0<=e;e--){if(k=(d=a[e])&&d.length)for(h=0;hc||d)a=a.slice(d,d+c);e&&(a=tb.call(this,a))}return a} -function tb(a){if(!this||!this.store)return a;for(var b=Array(a.length),c=0,d;cthis.limit&&this.cache.delete(this.cache.keys().next().value)}; -Pb.prototype.get=function(a){var b=this.cache.get(a);b&&this.h!==a&&(this.cache.delete(a),this.cache.set(this.h=a,b));return b};Pb.prototype.remove=function(a){for(var b=A(this.cache),c=b.next();!c.done;c=b.next()){c=c.value;var d=c[0];c[1].includes(a)&&this.cache.delete(d)}};Pb.prototype.clear=function(){this.cache.clear();this.h=""};W.prototype.remove=function(a,b){var c=this.reg.size&&(this.fastupdate?this.reg.get(a):this.reg.has(a));if(c){if(this.fastupdate)for(var d=0,e=void 0,f=void 0;dq;r--)p=l.substring(q,r),n=this.rtl?m-1-q:q,n=this.score?this.score(b,l,k,p,n):Vb(h,e,k,m,n),Wb(this, -g,p,n,a,c);break}case "bidirectional":case "reverse":if(1p?0:1),e,k,r-1,n-1),w=this.bidirectional&& -l>q;Wb(this,f,w?q:l,u,a,c,w?l:q)}}}}this.fastupdate||this.reg.add(a)}else b=""}this.db&&(b||this.commit_task.push({del:a}),this.ca&&Tb(this));return this}; -function Wb(a,b,c,d,e,f,g){var h=g?a.ctx:a.map,k;if(!b[c]||g&&!(k=b[c])[g])if(g?(b=k||(b[c]=S()),b[g]=1,(k=h.get(g))?h=k:h.set(g,h=new Map)):b[c]=1,(k=h.get(c))?h=k:h.set(c,h=k=[]),h=h[d]||(h[d]=[]),!f||!h.includes(e)){if(h.length===Math.pow(2,31)-1){b=new jb(h);if(a.fastupdate)for(c=A(a.reg.values()),f=c.next();!f.done;f=c.next())f=f.value,f.includes(h)&&(f[f.indexOf(h)]=b);k[d]=h=b}h.push(e);a.fastupdate&&((d=a.reg.get(e))?d.push(h):a.reg.set(e,[h]))}} -function Vb(a,b,c,d,e){return c&&1c)&&(k=c,c=b,b=k);if(a.db)return a.db.get(b,c,d,e,f,g,h);a=c?(a=a.ctx.get(c))&&a.get(b):a.map.get(b);return a};function W(a,b){if(!this||this.constructor!==W)return new W(a);if(a){var c=T(a)?a:a.preset;c&&(a=Object.assign({},Ub[c],a))}else a={};c=a.context;var d=!0===c?{depth:1}:c||{},e=T(a.encoder)?Nb[a.encoder]:a.encode||a.encoder||{};this.encoder=e.encode?e:"object"===typeof e?new Ma(e):{encode:e};this.resolution=a.resolution||9;this.tokenize=c=(c=a.tokenize)&&"default"!==c&&"exact"!==c&&c||"strict";this.depth="strict"===c&&d.depth||0;this.bidirectional=!1!==d.bidirectional;this.fastupdate=!!a.fastupdate; -this.score=a.score||null;(c=a.keystore||0)&&(this.keystore=c);this.map=c?new X(c):new Map;this.ctx=c?new X(c):new Map;this.reg=b||(this.fastupdate?c?new X(c):new Map:c?new Y(c):new Set);this.da=d.resolution||3;this.rtl=e.rtl||a.rtl||!1;this.cache=(c=a.cache||null)&&new Pb(c);this.resolve=!1!==a.resolve;if(c=a.db)this.db=this.mount(c);this.ca=!1!==a.commit;this.commit_task=[];this.commit_timer=null;this.priority=a.priority||4}v=W.prototype; -v.mount=function(a){this.commit_timer&&(clearTimeout(this.commit_timer),this.commit_timer=null);return a.mount(this)};v.commit=function(a,b){this.commit_timer&&(clearTimeout(this.commit_timer),this.commit_timer=null);return this.db.commit(this,a,b)};v.destroy=function(){this.commit_timer&&(clearTimeout(this.commit_timer),this.commit_timer=null);return this.db.destroy()};function Tb(a){a.commit_timer||(a.commit_timer=setTimeout(function(){a.commit_timer=null;a.db.commit(a,void 0,void 0)},1))} -v.clear=function(){this.map.clear();this.ctx.clear();this.reg.clear();this.cache&&this.cache.clear();this.db&&(this.commit_timer&&clearTimeout(this.commit_timer),this.commit_timer=null,this.commit_task=[{clear:!0}]);return this};v.append=function(a,b){return this.add(a,b,!0)};v.contain=function(a){return this.db?this.db.has(a):this.reg.has(a)};v.update=function(a,b){var c=this,d=this.remove(a);return d&&d.then?d.then(function(){return c.add(a,b)}):this.add(a,b)}; -v.cleanup=function(){if(!this.fastupdate)return this;Sb(this.map);this.depth&&Sb(this.ctx);return this};v.searchCache=Rb;v.export=function(a,b,c,d){c=void 0===c?0:c;d=void 0===d?0:d;switch(d){case 0:var e="reg";var f=eb(this.reg);break;case 1:e="cfg";f=null;break;case 2:e="map";f=ab(this.map,this.reg.size);break;case 3:e="ctx";f=cb(this.ctx,this.reg.size);break;default:return}return gb.call(this,a,b,e,f,c,d)}; -v.import=function(a,b){if(b)switch("string"===typeof b&&(b=JSON.parse(b)),a=a.split("."),"json"===a[a.length-1]&&a.pop(),3===a.length&&a.shift(),a=1=m.length)d-=m.length;else{for(var q=c?d+Math.min(m.length-d,c): -m.length,p=d;p=f.length)return[];if(!b&&!c)return f;f=f.slice(c,c+b);return d?e.enrich(f):f})}; -v.enrich=function(a){"object"!==typeof a&&(a=[a]);for(var b=this.db.transaction("reg","readonly").objectStore("reg"),c=[],d=0;d>>0)+"_",e=0;return b}); +G("Symbol.iterator",function(a){if(a)return a;a=Symbol("Symbol.iterator");for(var b="Array Int8Array Uint8Array Uint8ClampedArray Int16Array Uint16Array Int32Array Uint32Array Float32Array Float64Array".split(" "),c=0;cc&&(c=Math.max(c+e,0));c"a1a".split(c).length; +this.numeric=P(a.numeric,e)}else{try{this.split=P(this.split,Fa)}catch(g){this.split=/\s+/}this.numeric=P(a.numeric,P(this.numeric,!0))}this.prepare=P(a.prepare,null,this.prepare);this.finalize=P(a.finalize,null,this.finalize);c=a.filter;this.filter="function"===typeof c?c:P(c&&new Set(c),null,this.filter);this.dedupe=P(a.dedupe,!0,this.dedupe);this.matcher=P((c=a.matcher)&&new Map(c),null,this.matcher);this.mapper=P((c=a.mapper)&&new Map(c),null,this.mapper);this.stemmer=P((c=a.stemmer)&&new Map(c), +null,this.stemmer);this.replacer=P(a.replacer,null,this.replacer);this.minlength=P(a.minlength,1,this.minlength);this.maxlength=P(a.maxlength,1024,this.maxlength);this.rtl=P(a.rtl,!1,this.rtl);if(this.cache=c=P(a.cache,!0,this.cache))this.D=null,this.U="number"===typeof c?c:2E5,this.B=new Map,this.C=new Map,this.I=this.H=128;this.h="";this.N=null;this.A="";this.O=null;if(this.matcher)for(a=B(this.matcher.keys()),b=a.next();!b.done;b=a.next())this.h+=(this.h?"|":"")+b.value;if(this.stemmer)for(a=B(this.stemmer.keys()), +b=a.next();!b.done;b=a.next())this.A+=(this.A?"|":"")+b.value;return this};w.addStemmer=function(a,b){this.stemmer||(this.stemmer=new Map);this.stemmer.set(a,b);this.A+=(this.A?"|":"")+a;this.O=null;this.cache&&La(this);return this};w.addFilter=function(a){"function"===typeof a?this.filter=a:(this.filter||(this.filter=new Set),this.filter.add(a));this.cache&&La(this);return this}; +w.addMapper=function(a,b){if("object"===typeof a)return this.addReplacer(a,b);if(1a.length&&(this.dedupe||this.mapper))return this.addMapper(a,b);this.matcher||(this.matcher=new Map);this.matcher.set(a,b);this.h+=(this.h?"|":"")+a;this.N=null;this.cache&&La(this);return this}; +w.addReplacer=function(a,b){if("string"===typeof a)return this.addMatcher(a,b);this.replacer||(this.replacer=[]);this.replacer.push(a,b);this.cache&&La(this);return this}; +w.encode=function(a,b){var c=this;if(this.cache&&a.length<=this.H)if(this.D){if(this.B.has(a))return this.B.get(a)}else this.D=setTimeout(La,50,this);this.normalize&&("function"===typeof this.normalize?a=this.normalize(a):a=Ja?a.normalize("NFKD").replace(Ja,"").toLowerCase():a.toLowerCase());this.prepare&&(a=this.prepare(a));this.numeric&&3this.maxlength)){if(b){if(g[m])continue;g[m]=1}else{if(f===m)continue;f=m}if(d)e.push(m);else if(!this.filter||("function"===typeof this.filter?this.filter(m):!this.filter.has(m))){if(this.cache&&m.length<=this.I)if(this.D){var n=this.C.get(m);if(n||""===n){n&&e.push(n);continue}}else this.D=setTimeout(La,50,this);if(this.stemmer)for(this.O|| +(this.O=new RegExp("(?!^)("+this.A+")$")),n=void 0;n!==m&&2this.U&&(this.C.clear(),this.I=this.I/1.1|0));if(m){if(m!==p)if(b){if(g[m])continue;g[m]=1}else{if(h===m)continue;h=m}e.push(m)}}}this.finalize&&(e=this.finalize(e)||e);this.cache&&a.length<=this.H&&(this.B.set(a,e),this.B.size>this.U&&(this.B.clear(),this.H=this.H/1.1|0));return e};function La(a){a.D=null;a.B.clear();a.C.clear()};function Ma(a,b,c){c||(b||"object"!==typeof a?"object"===typeof b&&(c=b,b=0):c=a);c&&(a=c.query||a,b=c.limit||b);var d=""+(b||0);if(c){var e=c;d+=(e.offset||0)+!!e.context+!!e.suggest+(!1!==e.resolve)+(e.resolution||this.resolution)+(e.boost||0)}a=(""+a).toLowerCase();this.cache||(this.cache=new Na);e=this.cache.get(a+d);if(!e){var g=c&&c.cache;g&&(c.cache=!1);e=this.search(a,b,c);g&&(c.cache=g);this.cache.set(a+d,e)}return e} +function Na(a){this.limit=a&&!0!==a?a:1E3;this.cache=new Map;this.h=""}Na.prototype.set=function(a,b){this.cache.set(this.h=a,b);this.cache.size>this.limit&&this.cache.delete(this.cache.keys().next().value)};Na.prototype.get=function(a){var b=this.cache.get(a);b&&this.h!==a&&(this.cache.delete(a),this.cache.set(this.h=a,b));return b};Na.prototype.remove=function(a){for(var b=B(this.cache),c=b.next();!c.done;c=b.next()){c=c.value;var d=c[0];c[1].includes(a)&&this.cache.delete(d)}}; +Na.prototype.clear=function(){this.cache.clear();this.h=""};var Oa={normalize:!1,numeric:!1,dedupe:!1};var Pa={};var Qa=new Map([["b","p"],["v","f"],["w","f"],["z","s"],["x","s"],["d","t"],["n","m"],["c","k"],["g","k"],["j","k"],["q","k"],["i","e"],["y","e"],["u","o"]]);var Ra=new Map([["ae","a"],["oe","o"],["sh","s"],["kh","k"],["th","t"],["ph","f"],["pf","f"]]),Sa=[/([^aeo])h(.)/g,"$1$2",/([aeo])h([^aeo]|$)/g,"$1$2",/(.)\1+/g,"$1"];var Ta={a:"",e:"",i:"",o:"",u:"",y:"",b:1,f:1,p:1,v:1,c:2,g:2,j:2,k:2,q:2,s:2,x:2,z:2,"\u00df":2,d:3,t:3,l:4,m:5,n:5,r:6};var Ua={Exact:Oa,Default:Pa,Normalize:Pa,LatinBalance:{mapper:Qa},LatinAdvanced:{mapper:Qa,matcher:Ra,replacer:Sa},LatinExtra:{mapper:Qa,replacer:Sa.concat([/(?!^)[aeo]/g,""]),matcher:Ra},LatinSoundex:{dedupe:!1,include:{letter:!0},finalize:function(a){for(var b=0;b=f.length)b-=f.length;else{b=f[d?"splice":"slice"](b,c);if(f=b.length)if(e=e.length?e.concat(b):b,c-=f,d&&(a.length-=f),!c)break;b=0}return e} +function Wa(a){if(!this||this.constructor!==Wa)return new Wa(a);this.index=a?[a]:[];this.length=a?a.length:0;var b=this;return new Proxy([],{get:function(c,d){if("length"===d)return b.length;if("push"===d)return function(e){b.index[b.index.length-1].push(e);b.length++};if("pop"===d)return function(){if(b.length)return b.length--,b.index[b.index.length-1].pop()};if("indexOf"===d)return function(e){for(var g=0,f=0,h,k;f=this.priority*this.priority*3):(hb=setTimeout(kb,0),ib=Date.now());if(jb){var e=this;return new Promise(function(f){setTimeout(function(){f(e[a+"Async"].apply(e,b))},0)})}var g=this[a].apply(this,b);c=g.then?g:new Promise(function(f){return f(g)});d&&c.then(d);return c}};var lb=0; +function mb(a,b){function c(h){function k(l){l=l.data||l;var m=l.id,p=m&&g.h[m];p&&(p(l.msg),delete g.h[m])}this.worker=h;this.h=S();if(this.worker){e?this.worker.on("message",k):this.worker.onmessage=k;if(a.config)return new Promise(function(l){1E9b?a.slice(c,c+b):a,d?tb.call(this,a):a;for(var e=[],g=0,f=void 0,h=void 0;g=h){c-=h;continue}f=f.slice(c,c+b);h=f.length;c=0}h>b&&(f=f.slice(0,b),h=b);if(!e.length&&h>=b)return d?tb.call(this,f):f;e.push(f);b-=h;if(!b)break}e=1a.length?this.result=a[0]:(this.result=yb(a,b,c,!1,this.h),c=0));e&&(this.await=null);return e?this.resolve(b,c,d):this};Z.prototype.and=function(){return ub(this,"and",zb,arguments)};function zb(a,b,c,d,e,g){if(!g&&!this.result.length)return e?this.result:this;if(a.length)if(this.result.length&&a.unshift(this.result),2>a.length)this.result=a[0];else{for(var f=0,h=0,k=void 0,l=void 0;ha.length)this.result=a[0];else{a:{g=c;var f=this.h;for(var h=[],k=S(),l=0,m=0,p;mc||d?k.slice(d,c+d):k;else{if(ac||d)k=k.slice(d, +c+d)}else{e=[];for(g=0;gd)d-=f.length;else{if(c&&f.length>c||d)f=f.slice(d,c+d),c-=f.length,d&&(d-=f.length);e.push(f);if(!c)break}k=e}}return k} +function yb(a,b,c,d,e){var g=[],f=S(),h=a.length,k;if(d)for(e=h-1;0<=e;e--){if(k=(d=a[e])&&d.length)for(h=0;hH&&(H=r.length+(r?1:0)),N=r.length+(r?1:0)+ba.length,Q+=L,U.push(F.length),F.push({match:ba})),r+=(r?" ":"")+ba)}if(!O)R=A[D],r+=(r?" ":"")+R,k&&F.push({text:R});else if(k&&Q>=k)break}Q=U.length*(g.length-2);if(q||t||k&&r.length-Q>k)if(Q=k+Q-2*n,D=N-H,0A&&(A=0)),F=F.length-1){if(J>=F.length){D[z+1]=1;J>=A.length&&(H[z+1]=1);continue}O-=n}r=F[J].text;var V=t&&W[z];if(V)if(0< +V){if(r.length>V)if(D[z+1]=1,l)r=r.substring(0,V);else continue;(V-=r.length)||(V=-1);W[z]=V}else{D[z+1]=1;continue}if(O+r.length+1<=k)r=" "+r,N[z]+=r;else if(l)I=k-O-1,0=J){if(0>J){D[z]=1;H[z]=1;continue}O-=n}r=F[J].text;if(V=q&&R[z])if(0V)if(D[z]=1,l)r=r.substring(r.length-V);else continue;(V-=r.length)||(V=-1);R[z]=V}else{D[z]=1;continue}if(O+ +r.length+1<=k)r+=" ",N[z]=r+N[z];else if(l)I=r.length+1-(k-O),0<=I&&I=A.length-1?I=1:Jc||d)a=a.slice(d,d+c);e&&(a=tb.call(this,a))}return a} +function tb(a){if(!this||!this.store)return a;if(this.db)return this.index.get(this.field[0]).db.enrich(a);for(var b=Array(a.length),c=0,d;cp;t--)n=l.substring(p,t),q=this.rtl?m-1-p:p,q=this.score?this.score(b,l,k,n,q):Wb(h,e,k,m,q),Xb(this, +f,n,q,a,c);break}case "bidirectional":case "reverse":if(1n?0:1),e,k,t-1,q-1),v=this.bidirectional&& +l>p;Xb(this,g,v?p:l,u,a,c,v?l:p)}}}}this.fastupdate||this.reg.add(a)}else b=""}this.db&&(b||this.commit_task.push({del:a}),this.ea&&Ub(this));return this}; +function Xb(a,b,c,d,e,g,f){var h=f?a.ctx:a.map,k;if(!b[c]||f&&!(k=b[c])[f])if(f?(b=k||(b[c]=S()),b[f]=1,(k=h.get(f))?h=k:h.set(f,h=new Map)):b[c]=1,(k=h.get(c))?h=k:h.set(c,h=k=[]),h=h[d]||(h[d]=[]),!g||!h.includes(e)){if(h.length===Math.pow(2,31)-1){b=new Wa(h);if(a.fastupdate)for(c=B(a.reg.values()),g=c.next();!g.done;g=c.next())g=g.value,g.includes(h)&&(g[g.indexOf(h)]=b);k[d]=h=b}h.push(e);a.fastupdate&&((d=a.reg.get(e))?d.push(h):a.reg.set(e,[h]))}} +function Wb(a,b,c,d,e){return c&&1c)&&(k=c,c=b,b=k);if(a.db)return a.db.get(b,c,d,e,g,f,h);a=c?(a=a.ctx.get(c))&&a.get(b):a.map.get(b);return a};function Y(a,b){if(!this||this.constructor!==Y)return new Y(a);if(a){var c=T(a)?a:a.preset;c&&(a=Object.assign({},Vb[c],a))}else a={};c=a.context;var d=!0===c?{depth:1}:c||{},e=T(a.encoder)?Ua[a.encoder]:a.encode||a.encoder||{};this.encoder=e.encode?e:"object"===typeof e?new Ka(e):{encode:e};this.resolution=a.resolution||9;this.tokenize=c=(c=a.tokenize)&&"default"!==c&&"exact"!==c&&c||"strict";this.depth="strict"===c&&d.depth||0;this.bidirectional=!1!==d.bidirectional;this.fastupdate=!!a.fastupdate; +this.score=a.score||null;(c=a.keystore||0)&&(this.keystore=c);this.map=c?new Xa(c):new Map;this.ctx=c?new Xa(c):new Map;this.reg=b||(this.fastupdate?c?new Xa(c):new Map:c?new X(c):new Set);this.ga=d.resolution||3;this.rtl=e.rtl||a.rtl||!1;this.cache=(c=a.cache||null)&&new Na(c);this.resolve=!1!==a.resolve;if(c=a.db)this.db=this.mount(c);this.ea=!1!==a.commit;this.commit_task=[];this.commit_timer=null;this.priority=a.priority||4}w=Y.prototype; +w.mount=function(a){this.commit_timer&&(clearTimeout(this.commit_timer),this.commit_timer=null);return a.mount(this)};w.commit=function(a,b){this.commit_timer&&(clearTimeout(this.commit_timer),this.commit_timer=null);return this.db.commit(this,a,b)};w.destroy=function(){this.commit_timer&&(clearTimeout(this.commit_timer),this.commit_timer=null);return this.db.destroy()};function Ub(a){a.commit_timer||(a.commit_timer=setTimeout(function(){a.commit_timer=null;a.db.commit(a,void 0,void 0)},1))} +w.clear=function(){this.map.clear();this.ctx.clear();this.reg.clear();this.cache&&this.cache.clear();this.db&&(this.commit_timer&&clearTimeout(this.commit_timer),this.commit_timer=null,this.commit_task=[{clear:!0}]);return this};w.append=function(a,b){return this.add(a,b,!0)};w.contain=function(a){return this.db?this.db.has(a):this.reg.has(a)};w.update=function(a,b){var c=this,d=this.remove(a);return d&&d.then?d.then(function(){return c.add(a,b)}):this.add(a,b)}; +w.cleanup=function(){if(!this.fastupdate)return this;Tb(this.map);this.depth&&Tb(this.ctx);return this};w.searchCache=Ma;w.export=function(a,b,c,d){c=void 0===c?0:c;d=void 0===d?0:d;switch(d){case 0:var e="reg";var g=Pb(this.reg);break;case 1:e="cfg";g=null;break;case 2:e="map";g=Lb(this.map,this.reg.size);break;case 3:e="ctx";g=Nb(this.ctx,this.reg.size);break;default:return}return Rb.call(this,a,b,e,g,c,d)}; +w.import=function(a,b){if(b)switch("string"===typeof b&&(b=JSON.parse(b)),a=a.split("."),"json"===a[a.length-1]&&a.pop(),3===a.length&&a.shift(),a=1=m.length)d-=m.length;else{for(var p=c?d+Math.min(m.length-d,c): +m.length,n=d;n=g.length)return[];if(!b&&!c)return g;g=g.slice(c,c+b);return d?e.enrich(g):g})}; +w.enrich=function(a){"object"!==typeof a&&(a=[a]);for(var b=this.db.transaction("reg","readonly").objectStore("reg"),c=[],d=0;d this.maxlength)) { + let f = [], d = x(), h, e, k = this.split || "" === this.split ? a.split(this.split) : [a]; + for (let n = 0, g, w; n < k.length; n++) { + if ((g = w = k[n]) && !(g.length < this.minlength || g.length > this.maxlength)) { if (c) { - if (d[h]) { + if (d[g]) { continue; } - d[h] = 1; + d[g] = 1; } else { - if (g === h) { + if (h === g) { continue; } - g = h; + h = g; } if (b) { - f.push(h); + f.push(g); } else { - if (!this.filter || ("function" === typeof this.filter ? this.filter(h) : !this.filter.has(h))) { - if (this.cache && h.length <= this.o) { + if (!this.filter || ("function" === typeof this.filter ? this.filter(g) : !this.filter.has(g))) { + if (this.cache && g.length <= this.o) { if (this.l) { - var l = this.j.get(h); + var l = this.j.get(g); if (l || "" === l) { l && f.push(l); continue; @@ -219,39 +219,39 @@ r.encode = function(a, c) { if (this.stemmer) { this.u || (this.u = new RegExp("(?!^)(" + this.h + ")$")); let v; - for (; v !== h && 2 < h.length;) { - v = h, h = h.replace(this.u, p => this.stemmer.get(p)); + for (; v !== g && 2 < g.length;) { + v = g, g = g.replace(this.u, p => this.stemmer.get(p)); } } - if (h && (this.mapper || this.dedupe && 1 < h.length)) { + if (g && (this.mapper || this.dedupe && 1 < g.length)) { l = ""; - for (let v = 0, p = "", m, q; v < h.length; v++) { - m = h.charAt(v), m === p && this.dedupe || ((q = this.mapper && this.mapper.get(m)) || "" === q ? q === p && this.dedupe || !(p = q) || (l += q) : l += p = m); + for (let v = 0, p = "", m, q; v < g.length; v++) { + m = g.charAt(v), m === p && this.dedupe || ((q = this.mapper && this.mapper.get(m)) || "" === q ? q === p && this.dedupe || !(p = q) || (l += q) : l += p = m); } - h = l; + g = l; } - this.matcher && 1 < h.length && (this.s || (this.s = new RegExp("(" + this.g + ")", "g")), h = h.replace(this.s, v => this.matcher.get(v))); - if (h && this.replacer) { - for (l = 0; h && l < this.replacer.length; l += 2) { - h = h.replace(this.replacer[l], this.replacer[l + 1]); + this.matcher && 1 < g.length && (this.s || (this.s = new RegExp("(" + this.g + ")", "g")), g = g.replace(this.s, v => this.matcher.get(v))); + if (g && this.replacer) { + for (l = 0; g && l < this.replacer.length; l += 2) { + g = g.replace(this.replacer[l], this.replacer[l + 1]); } } - this.cache && w.length <= this.o && (this.j.set(w, h), this.j.size > this.A && (this.j.clear(), this.o = this.o / 1.1 | 0)); - if (h) { - if (h !== w) { + this.cache && w.length <= this.o && (this.j.set(w, g), this.j.size > this.A && (this.j.clear(), this.o = this.o / 1.1 | 0)); + if (g) { + if (g !== w) { if (c) { - if (d[h]) { + if (d[g]) { continue; } - d[h] = 1; + d[g] = 1; } else { - if (e === h) { + if (e === g) { continue; } - e = h; + e = g; } } - f.push(h); + f.push(g); } } } @@ -271,23 +271,25 @@ function G(a) { return a; } if (1 === a.length) { - return a = a[0], a = b || a.length > c ? c ? a.slice(b, b + c) : a.slice(b) : a; + return a = a[0], a = b || a.length > c ? a.slice(b, b + c) : a; } let f = []; - for (let d = 0, g, e; d < a.length; d++) { - if ((g = a[d]) && (e = g.length)) { + for (let d = 0, h, e; d < a.length; d++) { + if ((h = a[d]) && (e = h.length)) { if (b) { if (b >= e) { b -= e; continue; } - b < e && (g = c ? g.slice(b, b + c) : g.slice(b), e = g.length, b = 0); + h = h.slice(b, b + c); + e = h.length; + b = 0; } - e > c && (g = g.slice(0, c), e = c); + e > c && (h = h.slice(0, c), e = c); if (!f.length && e >= c) { - return g; + return h; } - f.push(g); + f.push(h); c -= e; if (!c) { break; @@ -301,9 +303,9 @@ I.prototype.remove = function(a, c) { const b = this.reg.size && (this.fastupdate ? this.reg.get(a) : this.reg.has(a)); if (b) { if (this.fastupdate) { - for (let f = 0, d, g; f < b.length; f++) { - if ((d = b[f]) && (g = d.length)) { - if (d[g - 1] === a) { + for (let f = 0, d, h; f < b.length; f++) { + if ((d = b[f]) && (h = d.length)) { + if (d[h - 1] === a) { d.pop(); } else { const e = d.indexOf(a); @@ -322,22 +324,26 @@ function J(a, c) { let b = 0; var f = "undefined" === typeof c; if (a.constructor === Array) { - for (let d = 0, g, e; d < a.length; d++) { - if ((g = a[d]) && g.length) { + for (let d = 0, h, e, k; d < a.length; d++) { + if ((h = a[d]) && h.length) { if (f) { - b++; - } else { - if (e = g.indexOf(c), 0 <= e) { - if (1 < g.length) { - g.splice(e, 1); - b++; - break; - } else { - delete a[d]; - } - } else { - b++; + return 1; + } + e = h.indexOf(c); + if (0 <= e) { + if (1 < h.length) { + return h.splice(e, 1), 1; } + delete a[d]; + if (b) { + return 1; + } + k = 1; + } else { + if (k) { + return 1; + } + b++; } } } @@ -358,18 +364,18 @@ I.prototype.add = function(a, c, b, f) { c = this.encoder.encode(c, !f); const n = c.length; if (n) { - const h = x(), w = x(), v = this.resolution; + const g = x(), w = x(), v = this.resolution; for (let p = 0; p < n; p++) { let m = c[this.rtl ? n - 1 - p : p]; var d = m.length; if (d && (f || !w[m])) { - var g = this.score ? this.score(c, m, p, null, 0) : L(v, n, p), e = ""; + var h = this.score ? this.score(c, m, p, null, 0) : L(v, n, p), e = ""; switch(this.tokenize) { case "full": if (2 < d) { for (let q = 0, t; q < d; q++) { - for (g = d; g > q; g--) { - e = m.substring(q, g); + for (h = d; h > q; h--) { + e = m.substring(q, h); t = this.rtl ? d - 1 - q : q; var k = this.score ? this.score(c, m, p, e, t) : L(v, n, p, d, t); M(this, w, e, k, a, b); @@ -390,17 +396,17 @@ I.prototype.add = function(a, c, b, f) { case "forward": if (1 < d) { for (k = 0; k < d; k++) { - e += m[this.rtl ? d - 1 - k : k], M(this, w, e, g, a, b); + e += m[this.rtl ? d - 1 - k : k], M(this, w, e, h, a, b); } break; } default: - if (M(this, w, m, g, a, b), f && 1 < n && p < n - 1) { - for (d = x(), e = this.v, g = m, k = Math.min(f + 1, this.rtl ? p + 1 : n - p), d[g] = 1, l = 1; l < k; l++) { + if (M(this, w, m, h, a, b), f && 1 < n && p < n - 1) { + for (d = x(), e = this.v, h = m, k = Math.min(f + 1, this.rtl ? p + 1 : n - p), d[h] = 1, l = 1; l < k; l++) { if ((m = c[this.rtl ? n - 1 - p - l : p + l]) && !d[m]) { d[m] = 1; - const q = this.score ? this.score(c, g, p, m, l - 1) : L(e + (n / 2 > e ? 0 : 1), n, p, k - 1, l - 1), t = this.bidirectional && m > g; - M(this, h, t ? g : m, q, a, b, t ? m : g); + const q = this.score ? this.score(c, h, p, m, l - 1) : L(e + (n / 2 > e ? 0 : 1), n, p, k - 1, l - 1), t = this.bidirectional && m > h; + M(this, g, t ? h : m, q, a, b, t ? m : h); } } } @@ -412,10 +418,10 @@ I.prototype.add = function(a, c, b, f) { } return this; }; -function M(a, c, b, f, d, g, e) { +function M(a, c, b, f, d, h, e) { let k = e ? a.ctx : a.map, l; if (!c[b] || e && !(l = c[b])[e]) { - e ? (c = l || (c[b] = x()), c[e] = 1, (l = k.get(e)) ? k = l : k.set(e, k = new Map())) : c[b] = 1, (l = k.get(b)) ? k = l : k.set(b, k = []), k = k[f] || (k[f] = []), g && k.includes(d) || (k.push(d), a.fastupdate && ((c = a.reg.get(d)) ? c.push(k) : a.reg.set(d, [k]))); + e ? (c = l || (c[b] = x()), c[e] = 1, (l = k.get(e)) ? k = l : k.set(e, k = new Map())) : c[b] = 1, (l = k.get(b)) ? k = l : k.set(b, k = []), k = k[f] || (k[f] = []), h && k.includes(d) || (k.push(d), a.fastupdate && ((c = a.reg.get(d)) ? c.push(k) : a.reg.set(d, [k]))); } } function L(a, c, b, f, d) { @@ -428,45 +434,45 @@ function L(a, c, b, f, d) { a = b.query || a; c = b.limit || c; d = b.offset || 0; - var g = b.context; + var h = b.context; var e = b.suggest; var k = !0; var l = b.resolution; } "undefined" === typeof k && (k = !0); - g = this.depth && !1 !== g; - a = this.encoder.encode(a, !g); + h = this.depth && !1 !== h; + a = this.encoder.encode(a, !h); b = a.length; c = c || (k ? 100 : 0); if (1 === b) { return e = d, (d = N(this, a[0], "")) && d.length ? H.call(this, d, c, e) : []; } - if (2 === b && g && !e) { + if (2 === b && h && !e) { return e = d, (d = N(this, a[1], a[0])) && d.length ? H.call(this, d, c, e) : []; } k = x(); var n = 0; - if (g) { - var h = a[0]; + if (h) { + var g = a[0]; n = 1; } - l || 0 === l || (l = h ? this.v : this.resolution); + l || 0 === l || (l = g ? this.v : this.resolution); for (let m, q; n < b; n++) { if ((q = a[n]) && !k[q]) { k[q] = 1; - m = N(this, q, h); + m = N(this, q, g); a: { - g = m; + h = m; var w = f, v = e, p = l; let t = []; - if (g && g.length) { - if (g.length <= p) { - w.push(g); + if (h && h.length) { + if (h.length <= p) { + w.push(h); m = void 0; break a; } for (let y = 0, z; y < p; y++) { - if (z = g[y]) { + if (z = h[y]) { t[y] = z; } } @@ -482,26 +488,26 @@ function L(a, c, b, f, d) { f = m; break; } - h && (e && m && f.length || (h = q)); + g && (e && m && f.length || (g = q)); } - e && h && n === b - 1 && !f.length && (l = this.resolution, h = "", n = -1, k = x()); + e && g && n === b - 1 && !f.length && (l = this.resolution, g = "", n = -1, k = x()); } a: { a = f; f = a.length; - h = a; + g = a; if (1 < f) { b: { f = e; - h = a.length; + g = a.length; e = []; b = x(); for (let m = 0, q, t, y, z; m < l; m++) { - for (n = 0; n < h; n++) { + for (n = 0; n < g; n++) { if (y = a[n], m < y.length && (q = y[m])) { - for (g = 0; g < q.length; g++) { - if (t = q[g], (k = b[t]) ? b[t]++ : (k = 0, b[t] = 1), z = e[k] || (e[k] = []), z.push(t), c && k === h - 1 && z.length - d === c) { - h = d ? z.slice(d) : z; + for (h = 0; h < q.length; h++) { + if (t = q[h], (k = b[t]) ? b[t]++ : (k = 0, b[t] = 1), z = e[k] || (e[k] = []), z.push(t), c && k === g - 1 && z.length - d === c) { + g = d ? z.slice(d) : z; break b; } } @@ -515,11 +521,11 @@ function L(a, c, b, f, d) { for (a = [], l = x(), f = e.length, k = f - 1; 0 <= k; k--) { if (b = (f = e[k]) && f.length) { for (n = 0; n < b; n++) { - if (h = f[n], !l[h]) { - if (l[h] = 1, d) { + if (g = f[n], !l[g]) { + if (l[g] = 1, d) { d--; } else { - if (a.push(h), a.length === c) { + if (a.push(g), a.length === c) { break c; } } @@ -529,12 +535,12 @@ function L(a, c, b, f, d) { } } } else { - a = (e = e[0]).length > c || d ? e.slice(d, c + d) : e; + a = (e = e[0]) && c && e.length > c || d ? e.slice(d, c + d) : e; } e = a; } else { - if (a < h) { - h = []; + if (a < g) { + g = []; break b; } e = e[a - 1]; @@ -545,13 +551,13 @@ function L(a, c, b, f, d) { } } } - h = e; + g = e; } } else if (1 === f) { c = H.call(null, a[0], c, d); break a; } - c = h; + c = g; } return c; }; diff --git a/dist/flexsearch.light.min.js b/dist/flexsearch.light.min.js index 592f9dc..30ef504 100644 --- a/dist/flexsearch.light.min.js +++ b/dist/flexsearch.light.min.js @@ -1,26 +1,26 @@ /**! - * FlexSearch.js v0.8.167 (Light) + * FlexSearch.js v0.8.200 (Light) * Author and Copyright: Thomas Wilkerling * Licence: Apache-2.0 * Hosted by Nextapps GmbH * https://github.com/nextapps-de/flexsearch */ -(function(self){'use strict';var r;function u(a,c,b){const f=typeof b,d=typeof a;if("undefined"!==f){if("undefined"!==d){if(b){if("function"===d&&f===d)return function(k){return a(b(k))};c=a.constructor;if(c===b.constructor){if(c===Array)return b.concat(a);if(c===Map){var g=new Map(b);for(var e of a)g.set(e[0],e[1]);return g}if(c===Set){e=new Set(b);for(g of a.values())e.add(g);return e}}}return a}return b}return"undefined"===d?c:a}function x(){return Object.create(null)};const A=/[^\p{L}\p{N}]+/u,B=/(\d{3})/g,C=/(\D)(\d{3})/g,D=/(\d{3})(\D)/g,E=/[\u0300-\u036f]/g;function F(a={}){if(!this||this.constructor!==F)return new F(...arguments);if(arguments.length)for(a=0;a"a1a".split(b).length; +(function(self){'use strict';var r;function u(a,c,b){const f=typeof b,d=typeof a;if("undefined"!==f){if("undefined"!==d){if(b){if("function"===d&&f===d)return function(k){return a(b(k))};c=a.constructor;if(c===b.constructor){if(c===Array)return b.concat(a);if(c===Map){var h=new Map(b);for(var e of a)h.set(e[0],e[1]);return h}if(c===Set){e=new Set(b);for(h of a.values())e.add(h);return e}}}return a}return b}return"undefined"===d?c:a}function x(){return Object.create(null)};const A=/[^\p{L}\p{N}]+/u,B=/(\d{3})/g,C=/(\D)(\d{3})/g,D=/(\d{3})(\D)/g,E=/[\u0300-\u036f]/g;function F(a={}){if(!this||this.constructor!==F)return new F(...arguments);if(arguments.length)for(a=0;a"a1a".split(b).length; this.numeric=u(a.numeric,f)}else{try{this.split=u(this.split,A)}catch(d){this.split=/\s+/}this.numeric=u(a.numeric,u(this.numeric,!0))}this.prepare=u(a.prepare,null,this.prepare);this.finalize=u(a.finalize,null,this.finalize);b=a.filter;this.filter="function"===typeof b?b:u(b&&new Set(b),null,this.filter);this.dedupe=u(a.dedupe,!0,this.dedupe);this.matcher=u((b=a.matcher)&&new Map(b),null,this.matcher);this.mapper=u((b=a.mapper)&&new Map(b),null,this.mapper);this.stemmer=u((b=a.stemmer)&&new Map(b), null,this.stemmer);this.replacer=u(a.replacer,null,this.replacer);this.minlength=u(a.minlength,1,this.minlength);this.maxlength=u(a.maxlength,1024,this.maxlength);this.rtl=u(a.rtl,!1,this.rtl);if(this.cache=b=u(a.cache,!0,this.cache))this.l=null,this.A="number"===typeof b?b:2E5,this.i=new Map,this.j=new Map,this.o=this.m=128;this.g="";this.s=null;this.h="";this.u=null;if(this.matcher)for(const d of this.matcher.keys())this.g+=(this.g?"|":"")+d;if(this.stemmer)for(const d of this.stemmer.keys())this.h+= (this.h?"|":"")+d;return this};r.addStemmer=function(a,c){this.stemmer||(this.stemmer=new Map);this.stemmer.set(a,c);this.h+=(this.h?"|":"")+a;this.u=null;this.cache&&G(this);return this};r.addFilter=function(a){"function"===typeof a?this.filter=a:(this.filter||(this.filter=new Set),this.filter.add(a));this.cache&&G(this);return this}; r.addMapper=function(a,c){if("object"===typeof a)return this.addReplacer(a,c);if(1a.length&&(this.dedupe||this.mapper))return this.addMapper(a,c);this.matcher||(this.matcher=new Map);this.matcher.set(a,c);this.g+=(this.g?"|":"")+a;this.s=null;this.cache&&G(this);return this}; r.addReplacer=function(a,c){if("string"===typeof a)return this.addMatcher(a,c);this.replacer||(this.replacer=[]);this.replacer.push(a,c);this.cache&&G(this);return this}; r.encode=function(a,c){if(this.cache&&a.length<=this.m)if(this.l){if(this.i.has(a))return this.i.get(a)}else this.l=setTimeout(G,50,this);this.normalize&&("function"===typeof this.normalize?a=this.normalize(a):a=E?a.normalize("NFKD").replace(E,"").toLowerCase():a.toLowerCase());this.prepare&&(a=this.prepare(a));this.numeric&&3this.maxlength)){if(c){if(d[h])continue;d[h]=1}else{if(g===h)continue;g=h}if(b)f.push(h);else if(!this.filter||("function"===typeof this.filter?this.filter(h):!this.filter.has(h))){if(this.cache&&h.length<=this.o)if(this.l){var l=this.j.get(h);if(l||""===l){l&&f.push(l);continue}}else this.l=setTimeout(G,50,this);if(this.stemmer){this.u||(this.u=new RegExp("(?!^)("+ -this.h+")$"));let v;for(;v!==h&&2this.stemmer.get(p))}if(h&&(this.mapper||this.dedupe&&1this.matcher.get(v)));if(h&&this.replacer)for(l=0;h&&lthis.A&&(this.j.clear(),this.o=this.o/1.1|0));if(h){if(h!==w)if(c){if(d[h])continue;d[h]=1}else{if(e===h)continue;e=h}f.push(h)}}}this.finalize&&(f=this.finalize(f)||f);this.cache&&a.length<=this.m&&(this.i.set(a,f),this.i.size>this.A&&(this.i.clear(),this.m=this.m/1.1|0));return f};function G(a){a.l=null;a.i.clear();a.j.clear()};function H(a,c,b){if(!a.length)return a;if(1===a.length)return a=a[0],a=b||a.length>c?c?a.slice(b,b+c):a.slice(b):a;let f=[];for(let d=0,g,e;d=e){b-=e;continue}bc&&(g=g.slice(0,c),e=c);if(!f.length&&e>=c)return g;f.push(g);c-=e;if(!c)break}return f=1q;g--){e=m.substring(q,g);t=this.rtl?d-1-q:q;var k=this.score?this.score(c,m,p,e,t):L(v, -n,p,d,t);M(this,w,e,k,a,b)}break}case "bidirectional":case "reverse":if(1e?0: -1),n,p,k-1,l-1),t=this.bidirectional&&m>g;M(this,h,t?g:m,q,a,b,t?m:g)}}}}this.fastupdate||this.reg.add(a)}}return this};function M(a,c,b,f,d,g,e){let k=e?a.ctx:a.map,l;if(!c[b]||e&&!(l=c[b])[e])e?(c=l||(c[b]=x()),c[e]=1,(l=k.get(e))?k=l:k.set(e,k=new Map)):c[b]=1,(l=k.get(b))?k=l:k.set(b,k=[]),k=k[f]||(k[f]=[]),g&&k.includes(d)||(k.push(d),a.fastupdate&&((c=a.reg.get(d))?c.push(k):a.reg.set(d,[k])))}function L(a,c,b,f,d){return b&&1c||d?e.slice(d,c+d):e;e=a}else{if(ac||d)e=e.slice(d,c+d)}h=e}else if(1===f){c= -H.call(null,a[0],c,d);break a}c=h}return c};function N(a,c,b){let f;b&&(f=a.bidirectional&&c>b)&&(f=b,b=c,c=f);a=b?(a=a.ctx.get(b))&&a.get(c):a.map.get(c);return a};function I(a,c){if(!this||this.constructor!==I)return new I(a);if(a){var b="string"===typeof a?a:a.preset;b&&(a=Object.assign({},K[b],a))}else a={};b=a.context;const f=!0===b?{depth:1}:b||{},d=a.encode||a.encoder||{};this.encoder=d.encode?d:"object"===typeof d?new F(d):{encode:d};this.resolution=a.resolution||9;this.tokenize=b=(b=a.tokenize)&&"default"!==b&&"exact"!==b&&b||"strict";this.depth="strict"===b&&f.depth||0;this.bidirectional=!1!==f.bidirectional;this.fastupdate=!!a.fastupdate;this.score= +d=x(),h,e,k=this.split||""===this.split?a.split(this.split):[a];for(let n=0,g,w;nthis.maxlength)){if(c){if(d[g])continue;d[g]=1}else{if(h===g)continue;h=g}if(b)f.push(g);else if(!this.filter||("function"===typeof this.filter?this.filter(g):!this.filter.has(g))){if(this.cache&&g.length<=this.o)if(this.l){var l=this.j.get(g);if(l||""===l){l&&f.push(l);continue}}else this.l=setTimeout(G,50,this);if(this.stemmer){this.u||(this.u=new RegExp("(?!^)("+ +this.h+")$"));let v;for(;v!==g&&2this.stemmer.get(p))}if(g&&(this.mapper||this.dedupe&&1this.matcher.get(v)));if(g&&this.replacer)for(l=0;g&&lthis.A&&(this.j.clear(),this.o=this.o/1.1|0));if(g){if(g!==w)if(c){if(d[g])continue;d[g]=1}else{if(e===g)continue;e=g}f.push(g)}}}this.finalize&&(f=this.finalize(f)||f);this.cache&&a.length<=this.m&&(this.i.set(a,f),this.i.size>this.A&&(this.i.clear(),this.m=this.m/1.1|0));return f};function G(a){a.l=null;a.i.clear();a.j.clear()};function H(a,c,b){if(!a.length)return a;if(1===a.length)return a=a[0],a=b||a.length>c?a.slice(b,b+c):a;let f=[];for(let d=0,h,e;d=e){b-=e;continue}h=h.slice(b,b+c);e=h.length;b=0}e>c&&(h=h.slice(0,c),e=c);if(!f.length&&e>=c)return h;f.push(h);c-=e;if(!c)break}return f=1q;h--){e=m.substring(q,h);t=this.rtl?d-1-q:q;var k=this.score?this.score(c,m,p,e,t):L(v, +n,p,d,t);M(this,w,e,k,a,b)}break}case "bidirectional":case "reverse":if(1e?0: +1),n,p,k-1,l-1),t=this.bidirectional&&m>h;M(this,g,t?h:m,q,a,b,t?m:h)}}}}this.fastupdate||this.reg.add(a)}}return this};function M(a,c,b,f,d,h,e){let k=e?a.ctx:a.map,l;if(!c[b]||e&&!(l=c[b])[e])e?(c=l||(c[b]=x()),c[e]=1,(l=k.get(e))?k=l:k.set(e,k=new Map)):c[b]=1,(l=k.get(b))?k=l:k.set(b,k=[]),k=k[f]||(k[f]=[]),h&&k.includes(d)||(k.push(d),a.fastupdate&&((c=a.reg.get(d))?c.push(k):a.reg.set(d,[k])))}function L(a,c,b,f,d){return b&&1c||d?e.slice(d,c+d):e;e=a}else{if(ac||d)e=e.slice(d,c+d)}g=e}else if(1=== +f){c=H.call(null,a[0],c,d);break a}c=g}return c};function N(a,c,b){let f;b&&(f=a.bidirectional&&c>b)&&(f=b,b=c,c=f);a=b?(a=a.ctx.get(b))&&a.get(c):a.map.get(c);return a};function I(a,c){if(!this||this.constructor!==I)return new I(a);if(a){var b="string"===typeof a?a:a.preset;b&&(a=Object.assign({},K[b],a))}else a={};b=a.context;const f=!0===b?{depth:1}:b||{},d=a.encode||a.encoder||{};this.encoder=d.encode?d:"object"===typeof d?new F(d):{encode:d};this.resolution=a.resolution||9;this.tokenize=b=(b=a.tokenize)&&"default"!==b&&"exact"!==b&&b||"strict";this.depth="strict"===b&&f.depth||0;this.bidirectional=!1!==f.bidirectional;this.fastupdate=!!a.fastupdate;this.score= a.score||null;this.map=new Map;this.ctx=new Map;this.reg=c||(this.fastupdate?new Map:new Set);this.v=f.resolution||3;this.rtl=d.rtl||a.rtl||!1}r=I.prototype;r.clear=function(){this.map.clear();this.ctx.clear();this.reg.clear();return this};r.append=function(a,c){return this.add(a,c,!0)};r.contain=function(a){return this.reg.has(a)};r.update=function(a,c){const b=this,f=this.remove(a);return f&&f.then?f.then(()=>b.add(a,c)):this.add(a,c)}; r.cleanup=function(){if(!this.fastupdate)return this;J(this.map);this.depth&&J(this.ctx);return this};x();const O={Index:I,Charset:null,Encoder:F,Document:null,Worker:null,Resolver:null,IndexedDB:null,Language:{}},P="undefined"!==typeof self?self:"undefined"!==typeof global?global:self;let Q;(Q=P.define)&&Q.amd?Q([],function(){return O}):"object"===typeof P.exports?P.exports=O:P.FlexSearch=O;}(this||self)); diff --git a/dist/flexsearch.light.module.debug.js b/dist/flexsearch.light.module.debug.js index d9f0418..9c408a0 100644 --- a/dist/flexsearch.light.module.debug.js +++ b/dist/flexsearch.light.module.debug.js @@ -1,5 +1,5 @@ /**! - * FlexSearch.js v0.8.167 (Bundle/Debug) + * FlexSearch.js v0.8.200 (Bundle/Debug) * Author and Copyright: Thomas Wilkerling * Licence: Apache-2.0 * Hosted by Nextapps GmbH @@ -22,16 +22,16 @@ function u(a, c, b) { return b.concat(a); } if (c === Map) { - var g = new Map(b); + var h = new Map(b); for (var e of a) { - g.set(e[0], e[1]); + h.set(e[0], e[1]); } - return g; + return h; } if (c === Set) { e = new Set(b); - for (g of a.values()) { - e.add(g); + for (h of a.values()) { + e.add(h); } return e; } @@ -78,7 +78,7 @@ r.assign = function(a) { } try { this.split = new RegExp("[" + (c ? "^" : "") + d + "]+", "u"); - } catch (g) { + } catch (h) { console.error("Your split configuration:", b, "is not supported on this platform. It falls back to using simple whitespace splitter instead: /s+/."), this.split = /\s+/; } } else { @@ -186,27 +186,27 @@ r.encode = function(a, c) { this.prepare && (a = this.prepare(a)); this.numeric && 3 < a.length && (a = a.replace(C, "$1 $2").replace(D, "$1 $2").replace(B, "$1 ")); const b = !(this.dedupe || this.mapper || this.filter || this.matcher || this.stemmer || this.replacer); - let f = [], d = x(), g, e, k = this.split || "" === this.split ? a.split(this.split) : [a]; - for (let n = 0, h, w; n < k.length; n++) { - if ((h = w = k[n]) && !(h.length < this.minlength || h.length > this.maxlength)) { + let f = [], d = x(), h, e, k = this.split || "" === this.split ? a.split(this.split) : [a]; + for (let n = 0, g, w; n < k.length; n++) { + if ((g = w = k[n]) && !(g.length < this.minlength || g.length > this.maxlength)) { if (c) { - if (d[h]) { + if (d[g]) { continue; } - d[h] = 1; + d[g] = 1; } else { - if (g === h) { + if (h === g) { continue; } - g = h; + h = g; } if (b) { - f.push(h); + f.push(g); } else { - if (!this.filter || ("function" === typeof this.filter ? this.filter(h) : !this.filter.has(h))) { - if (this.cache && h.length <= this.o) { + if (!this.filter || ("function" === typeof this.filter ? this.filter(g) : !this.filter.has(g))) { + if (this.cache && g.length <= this.o) { if (this.l) { - var l = this.j.get(h); + var l = this.j.get(g); if (l || "" === l) { l && f.push(l); continue; @@ -218,39 +218,39 @@ r.encode = function(a, c) { if (this.stemmer) { this.u || (this.u = new RegExp("(?!^)(" + this.h + ")$")); let v; - for (; v !== h && 2 < h.length;) { - v = h, h = h.replace(this.u, p => this.stemmer.get(p)); + for (; v !== g && 2 < g.length;) { + v = g, g = g.replace(this.u, p => this.stemmer.get(p)); } } - if (h && (this.mapper || this.dedupe && 1 < h.length)) { + if (g && (this.mapper || this.dedupe && 1 < g.length)) { l = ""; - for (let v = 0, p = "", m, q; v < h.length; v++) { - m = h.charAt(v), m === p && this.dedupe || ((q = this.mapper && this.mapper.get(m)) || "" === q ? q === p && this.dedupe || !(p = q) || (l += q) : l += p = m); + for (let v = 0, p = "", m, q; v < g.length; v++) { + m = g.charAt(v), m === p && this.dedupe || ((q = this.mapper && this.mapper.get(m)) || "" === q ? q === p && this.dedupe || !(p = q) || (l += q) : l += p = m); } - h = l; + g = l; } - this.matcher && 1 < h.length && (this.s || (this.s = new RegExp("(" + this.g + ")", "g")), h = h.replace(this.s, v => this.matcher.get(v))); - if (h && this.replacer) { - for (l = 0; h && l < this.replacer.length; l += 2) { - h = h.replace(this.replacer[l], this.replacer[l + 1]); + this.matcher && 1 < g.length && (this.s || (this.s = new RegExp("(" + this.g + ")", "g")), g = g.replace(this.s, v => this.matcher.get(v))); + if (g && this.replacer) { + for (l = 0; g && l < this.replacer.length; l += 2) { + g = g.replace(this.replacer[l], this.replacer[l + 1]); } } - this.cache && w.length <= this.o && (this.j.set(w, h), this.j.size > this.A && (this.j.clear(), this.o = this.o / 1.1 | 0)); - if (h) { - if (h !== w) { + this.cache && w.length <= this.o && (this.j.set(w, g), this.j.size > this.A && (this.j.clear(), this.o = this.o / 1.1 | 0)); + if (g) { + if (g !== w) { if (c) { - if (d[h]) { + if (d[g]) { continue; } - d[h] = 1; + d[g] = 1; } else { - if (e === h) { + if (e === g) { continue; } - e = h; + e = g; } } - f.push(h); + f.push(g); } } } @@ -270,23 +270,25 @@ function G(a) { return a; } if (1 === a.length) { - return a = a[0], a = b || a.length > c ? c ? a.slice(b, b + c) : a.slice(b) : a; + return a = a[0], a = b || a.length > c ? a.slice(b, b + c) : a; } let f = []; - for (let d = 0, g, e; d < a.length; d++) { - if ((g = a[d]) && (e = g.length)) { + for (let d = 0, h, e; d < a.length; d++) { + if ((h = a[d]) && (e = h.length)) { if (b) { if (b >= e) { b -= e; continue; } - b < e && (g = c ? g.slice(b, b + c) : g.slice(b), e = g.length, b = 0); + h = h.slice(b, b + c); + e = h.length; + b = 0; } - e > c && (g = g.slice(0, c), e = c); + e > c && (h = h.slice(0, c), e = c); if (!f.length && e >= c) { - return g; + return h; } - f.push(g); + f.push(h); c -= e; if (!c) { break; @@ -300,9 +302,9 @@ I.prototype.remove = function(a, c) { const b = this.reg.size && (this.fastupdate ? this.reg.get(a) : this.reg.has(a)); if (b) { if (this.fastupdate) { - for (let f = 0, d, g; f < b.length; f++) { - if ((d = b[f]) && (g = d.length)) { - if (d[g - 1] === a) { + for (let f = 0, d, h; f < b.length; f++) { + if ((d = b[f]) && (h = d.length)) { + if (d[h - 1] === a) { d.pop(); } else { const e = d.indexOf(a); @@ -321,22 +323,26 @@ function J(a, c) { let b = 0; var f = "undefined" === typeof c; if (a.constructor === Array) { - for (let d = 0, g, e; d < a.length; d++) { - if ((g = a[d]) && g.length) { + for (let d = 0, h, e, k; d < a.length; d++) { + if ((h = a[d]) && h.length) { if (f) { - b++; - } else { - if (e = g.indexOf(c), 0 <= e) { - if (1 < g.length) { - g.splice(e, 1); - b++; - break; - } else { - delete a[d]; - } - } else { - b++; + return 1; + } + e = h.indexOf(c); + if (0 <= e) { + if (1 < h.length) { + return h.splice(e, 1), 1; } + delete a[d]; + if (b) { + return 1; + } + k = 1; + } else { + if (k) { + return 1; + } + b++; } } } @@ -357,18 +363,18 @@ I.prototype.add = function(a, c, b, f) { c = this.encoder.encode(c, !f); const n = c.length; if (n) { - const h = x(), w = x(), v = this.resolution; + const g = x(), w = x(), v = this.resolution; for (let p = 0; p < n; p++) { let m = c[this.rtl ? n - 1 - p : p]; var d = m.length; if (d && (f || !w[m])) { - var g = this.score ? this.score(c, m, p, null, 0) : L(v, n, p), e = ""; + var h = this.score ? this.score(c, m, p, null, 0) : L(v, n, p), e = ""; switch(this.tokenize) { case "full": if (2 < d) { for (let q = 0, t; q < d; q++) { - for (g = d; g > q; g--) { - e = m.substring(q, g); + for (h = d; h > q; h--) { + e = m.substring(q, h); t = this.rtl ? d - 1 - q : q; var k = this.score ? this.score(c, m, p, e, t) : L(v, n, p, d, t); M(this, w, e, k, a, b); @@ -389,17 +395,17 @@ I.prototype.add = function(a, c, b, f) { case "forward": if (1 < d) { for (k = 0; k < d; k++) { - e += m[this.rtl ? d - 1 - k : k], M(this, w, e, g, a, b); + e += m[this.rtl ? d - 1 - k : k], M(this, w, e, h, a, b); } break; } default: - if (M(this, w, m, g, a, b), f && 1 < n && p < n - 1) { - for (d = x(), e = this.v, g = m, k = Math.min(f + 1, this.rtl ? p + 1 : n - p), d[g] = 1, l = 1; l < k; l++) { + if (M(this, w, m, h, a, b), f && 1 < n && p < n - 1) { + for (d = x(), e = this.v, h = m, k = Math.min(f + 1, this.rtl ? p + 1 : n - p), d[h] = 1, l = 1; l < k; l++) { if ((m = c[this.rtl ? n - 1 - p - l : p + l]) && !d[m]) { d[m] = 1; - const q = this.score ? this.score(c, g, p, m, l - 1) : L(e + (n / 2 > e ? 0 : 1), n, p, k - 1, l - 1), t = this.bidirectional && m > g; - M(this, h, t ? g : m, q, a, b, t ? m : g); + const q = this.score ? this.score(c, h, p, m, l - 1) : L(e + (n / 2 > e ? 0 : 1), n, p, k - 1, l - 1), t = this.bidirectional && m > h; + M(this, g, t ? h : m, q, a, b, t ? m : h); } } } @@ -411,10 +417,10 @@ I.prototype.add = function(a, c, b, f) { } return this; }; -function M(a, c, b, f, d, g, e) { +function M(a, c, b, f, d, h, e) { let k = e ? a.ctx : a.map, l; if (!c[b] || e && !(l = c[b])[e]) { - e ? (c = l || (c[b] = x()), c[e] = 1, (l = k.get(e)) ? k = l : k.set(e, k = new Map())) : c[b] = 1, (l = k.get(b)) ? k = l : k.set(b, k = []), k = k[f] || (k[f] = []), g && k.includes(d) || (k.push(d), a.fastupdate && ((c = a.reg.get(d)) ? c.push(k) : a.reg.set(d, [k]))); + e ? (c = l || (c[b] = x()), c[e] = 1, (l = k.get(e)) ? k = l : k.set(e, k = new Map())) : c[b] = 1, (l = k.get(b)) ? k = l : k.set(b, k = []), k = k[f] || (k[f] = []), h && k.includes(d) || (k.push(d), a.fastupdate && ((c = a.reg.get(d)) ? c.push(k) : a.reg.set(d, [k]))); } } function L(a, c, b, f, d) { @@ -427,45 +433,45 @@ function L(a, c, b, f, d) { a = b.query || a; c = b.limit || c; d = b.offset || 0; - var g = b.context; + var h = b.context; var e = b.suggest; var k = !0; var l = b.resolution; } "undefined" === typeof k && (k = !0); - g = this.depth && !1 !== g; - a = this.encoder.encode(a, !g); + h = this.depth && !1 !== h; + a = this.encoder.encode(a, !h); b = a.length; c = c || (k ? 100 : 0); if (1 === b) { return e = d, (d = N(this, a[0], "")) && d.length ? H.call(this, d, c, e) : []; } - if (2 === b && g && !e) { + if (2 === b && h && !e) { return e = d, (d = N(this, a[1], a[0])) && d.length ? H.call(this, d, c, e) : []; } k = x(); var n = 0; - if (g) { - var h = a[0]; + if (h) { + var g = a[0]; n = 1; } - l || 0 === l || (l = h ? this.v : this.resolution); + l || 0 === l || (l = g ? this.v : this.resolution); for (let m, q; n < b; n++) { if ((q = a[n]) && !k[q]) { k[q] = 1; - m = N(this, q, h); + m = N(this, q, g); a: { - g = m; + h = m; var w = f, v = e, p = l; let t = []; - if (g && g.length) { - if (g.length <= p) { - w.push(g); + if (h && h.length) { + if (h.length <= p) { + w.push(h); m = void 0; break a; } for (let y = 0, z; y < p; y++) { - if (z = g[y]) { + if (z = h[y]) { t[y] = z; } } @@ -481,26 +487,26 @@ function L(a, c, b, f, d) { f = m; break; } - h && (e && m && f.length || (h = q)); + g && (e && m && f.length || (g = q)); } - e && h && n === b - 1 && !f.length && (l = this.resolution, h = "", n = -1, k = x()); + e && g && n === b - 1 && !f.length && (l = this.resolution, g = "", n = -1, k = x()); } a: { a = f; f = a.length; - h = a; + g = a; if (1 < f) { b: { f = e; - h = a.length; + g = a.length; e = []; b = x(); for (let m = 0, q, t, y, z; m < l; m++) { - for (n = 0; n < h; n++) { + for (n = 0; n < g; n++) { if (y = a[n], m < y.length && (q = y[m])) { - for (g = 0; g < q.length; g++) { - if (t = q[g], (k = b[t]) ? b[t]++ : (k = 0, b[t] = 1), z = e[k] || (e[k] = []), z.push(t), c && k === h - 1 && z.length - d === c) { - h = d ? z.slice(d) : z; + for (h = 0; h < q.length; h++) { + if (t = q[h], (k = b[t]) ? b[t]++ : (k = 0, b[t] = 1), z = e[k] || (e[k] = []), z.push(t), c && k === g - 1 && z.length - d === c) { + g = d ? z.slice(d) : z; break b; } } @@ -514,11 +520,11 @@ function L(a, c, b, f, d) { for (a = [], l = x(), f = e.length, k = f - 1; 0 <= k; k--) { if (b = (f = e[k]) && f.length) { for (n = 0; n < b; n++) { - if (h = f[n], !l[h]) { - if (l[h] = 1, d) { + if (g = f[n], !l[g]) { + if (l[g] = 1, d) { d--; } else { - if (a.push(h), a.length === c) { + if (a.push(g), a.length === c) { break c; } } @@ -528,12 +534,12 @@ function L(a, c, b, f, d) { } } } else { - a = (e = e[0]).length > c || d ? e.slice(d, c + d) : e; + a = (e = e[0]) && c && e.length > c || d ? e.slice(d, c + d) : e; } e = a; } else { - if (a < h) { - h = []; + if (a < g) { + g = []; break b; } e = e[a - 1]; @@ -544,13 +550,13 @@ function L(a, c, b, f, d) { } } } - h = e; + g = e; } } else if (1 === f) { c = H.call(null, a[0], c, d); break a; } - c = h; + c = g; } return c; }; diff --git a/dist/flexsearch.light.module.min.js b/dist/flexsearch.light.module.min.js index c7ffb75..e6b955b 100644 --- a/dist/flexsearch.light.module.min.js +++ b/dist/flexsearch.light.module.min.js @@ -1,27 +1,27 @@ /**! - * FlexSearch.js v0.8.167 (Bundle) + * FlexSearch.js v0.8.200 (Bundle) * Author and Copyright: Thomas Wilkerling * Licence: Apache-2.0 * Hosted by Nextapps GmbH * https://github.com/nextapps-de/flexsearch */ -var r;function u(a,c,b){const f=typeof b,d=typeof a;if("undefined"!==f){if("undefined"!==d){if(b){if("function"===d&&f===d)return function(k){return a(b(k))};c=a.constructor;if(c===b.constructor){if(c===Array)return b.concat(a);if(c===Map){var g=new Map(b);for(var e of a)g.set(e[0],e[1]);return g}if(c===Set){e=new Set(b);for(g of a.values())e.add(g);return e}}}return a}return b}return"undefined"===d?c:a}function x(){return Object.create(null)};const A=/[^\p{L}\p{N}]+/u,B=/(\d{3})/g,C=/(\D)(\d{3})/g,D=/(\d{3})(\D)/g,E=/[\u0300-\u036f]/g;function F(a={}){if(!this||this.constructor!==F)return new F(...arguments);if(arguments.length)for(a=0;a"a1a".split(b).length; +var r;function u(a,c,b){const f=typeof b,d=typeof a;if("undefined"!==f){if("undefined"!==d){if(b){if("function"===d&&f===d)return function(k){return a(b(k))};c=a.constructor;if(c===b.constructor){if(c===Array)return b.concat(a);if(c===Map){var h=new Map(b);for(var e of a)h.set(e[0],e[1]);return h}if(c===Set){e=new Set(b);for(h of a.values())e.add(h);return e}}}return a}return b}return"undefined"===d?c:a}function x(){return Object.create(null)};const A=/[^\p{L}\p{N}]+/u,B=/(\d{3})/g,C=/(\D)(\d{3})/g,D=/(\d{3})(\D)/g,E=/[\u0300-\u036f]/g;function F(a={}){if(!this||this.constructor!==F)return new F(...arguments);if(arguments.length)for(a=0;a"a1a".split(b).length; this.numeric=u(a.numeric,f)}else{try{this.split=u(this.split,A)}catch(d){this.split=/\s+/}this.numeric=u(a.numeric,u(this.numeric,!0))}this.prepare=u(a.prepare,null,this.prepare);this.finalize=u(a.finalize,null,this.finalize);b=a.filter;this.filter="function"===typeof b?b:u(b&&new Set(b),null,this.filter);this.dedupe=u(a.dedupe,!0,this.dedupe);this.matcher=u((b=a.matcher)&&new Map(b),null,this.matcher);this.mapper=u((b=a.mapper)&&new Map(b),null,this.mapper);this.stemmer=u((b=a.stemmer)&&new Map(b), null,this.stemmer);this.replacer=u(a.replacer,null,this.replacer);this.minlength=u(a.minlength,1,this.minlength);this.maxlength=u(a.maxlength,1024,this.maxlength);this.rtl=u(a.rtl,!1,this.rtl);if(this.cache=b=u(a.cache,!0,this.cache))this.l=null,this.A="number"===typeof b?b:2E5,this.i=new Map,this.j=new Map,this.o=this.m=128;this.g="";this.s=null;this.h="";this.u=null;if(this.matcher)for(const d of this.matcher.keys())this.g+=(this.g?"|":"")+d;if(this.stemmer)for(const d of this.stemmer.keys())this.h+= (this.h?"|":"")+d;return this};r.addStemmer=function(a,c){this.stemmer||(this.stemmer=new Map);this.stemmer.set(a,c);this.h+=(this.h?"|":"")+a;this.u=null;this.cache&&G(this);return this};r.addFilter=function(a){"function"===typeof a?this.filter=a:(this.filter||(this.filter=new Set),this.filter.add(a));this.cache&&G(this);return this}; r.addMapper=function(a,c){if("object"===typeof a)return this.addReplacer(a,c);if(1a.length&&(this.dedupe||this.mapper))return this.addMapper(a,c);this.matcher||(this.matcher=new Map);this.matcher.set(a,c);this.g+=(this.g?"|":"")+a;this.s=null;this.cache&&G(this);return this}; r.addReplacer=function(a,c){if("string"===typeof a)return this.addMatcher(a,c);this.replacer||(this.replacer=[]);this.replacer.push(a,c);this.cache&&G(this);return this}; r.encode=function(a,c){if(this.cache&&a.length<=this.m)if(this.l){if(this.i.has(a))return this.i.get(a)}else this.l=setTimeout(G,50,this);this.normalize&&("function"===typeof this.normalize?a=this.normalize(a):a=E?a.normalize("NFKD").replace(E,"").toLowerCase():a.toLowerCase());this.prepare&&(a=this.prepare(a));this.numeric&&3this.maxlength)){if(c){if(d[h])continue;d[h]=1}else{if(g===h)continue;g=h}if(b)f.push(h);else if(!this.filter||("function"===typeof this.filter?this.filter(h):!this.filter.has(h))){if(this.cache&&h.length<=this.o)if(this.l){var l=this.j.get(h);if(l||""===l){l&&f.push(l);continue}}else this.l=setTimeout(G,50,this);if(this.stemmer){this.u||(this.u=new RegExp("(?!^)("+ -this.h+")$"));let v;for(;v!==h&&2this.stemmer.get(p))}if(h&&(this.mapper||this.dedupe&&1this.matcher.get(v)));if(h&&this.replacer)for(l=0;h&&lthis.A&&(this.j.clear(),this.o=this.o/1.1|0));if(h){if(h!==w)if(c){if(d[h])continue;d[h]=1}else{if(e===h)continue;e=h}f.push(h)}}}this.finalize&&(f=this.finalize(f)||f);this.cache&&a.length<=this.m&&(this.i.set(a,f),this.i.size>this.A&&(this.i.clear(),this.m=this.m/1.1|0));return f};function G(a){a.l=null;a.i.clear();a.j.clear()};function H(a,c,b){if(!a.length)return a;if(1===a.length)return a=a[0],a=b||a.length>c?c?a.slice(b,b+c):a.slice(b):a;let f=[];for(let d=0,g,e;d=e){b-=e;continue}bc&&(g=g.slice(0,c),e=c);if(!f.length&&e>=c)return g;f.push(g);c-=e;if(!c)break}return f=1q;g--){e=m.substring(q,g);t=this.rtl?d-1-q:q;var k=this.score?this.score(c,m,p,e,t):L(v, -n,p,d,t);M(this,w,e,k,a,b)}break}case "bidirectional":case "reverse":if(1e?0: -1),n,p,k-1,l-1),t=this.bidirectional&&m>g;M(this,h,t?g:m,q,a,b,t?m:g)}}}}this.fastupdate||this.reg.add(a)}}return this};function M(a,c,b,f,d,g,e){let k=e?a.ctx:a.map,l;if(!c[b]||e&&!(l=c[b])[e])e?(c=l||(c[b]=x()),c[e]=1,(l=k.get(e))?k=l:k.set(e,k=new Map)):c[b]=1,(l=k.get(b))?k=l:k.set(b,k=[]),k=k[f]||(k[f]=[]),g&&k.includes(d)||(k.push(d),a.fastupdate&&((c=a.reg.get(d))?c.push(k):a.reg.set(d,[k])))}function L(a,c,b,f,d){return b&&1c||d?e.slice(d,c+d):e;e=a}else{if(ac||d)e=e.slice(d,c+d)}h=e}else if(1===f){c= -H.call(null,a[0],c,d);break a}c=h}return c};function N(a,c,b){let f;b&&(f=a.bidirectional&&c>b)&&(f=b,b=c,c=f);a=b?(a=a.ctx.get(b))&&a.get(c):a.map.get(c);return a};function I(a,c){if(!this||this.constructor!==I)return new I(a);if(a){var b="string"===typeof a?a:a.preset;b&&(a=Object.assign({},K[b],a))}else a={};b=a.context;const f=!0===b?{depth:1}:b||{},d=a.encode||a.encoder||{};this.encoder=d.encode?d:"object"===typeof d?new F(d):{encode:d};this.resolution=a.resolution||9;this.tokenize=b=(b=a.tokenize)&&"default"!==b&&"exact"!==b&&b||"strict";this.depth="strict"===b&&f.depth||0;this.bidirectional=!1!==f.bidirectional;this.fastupdate=!!a.fastupdate;this.score= +d=x(),h,e,k=this.split||""===this.split?a.split(this.split):[a];for(let n=0,g,w;nthis.maxlength)){if(c){if(d[g])continue;d[g]=1}else{if(h===g)continue;h=g}if(b)f.push(g);else if(!this.filter||("function"===typeof this.filter?this.filter(g):!this.filter.has(g))){if(this.cache&&g.length<=this.o)if(this.l){var l=this.j.get(g);if(l||""===l){l&&f.push(l);continue}}else this.l=setTimeout(G,50,this);if(this.stemmer){this.u||(this.u=new RegExp("(?!^)("+ +this.h+")$"));let v;for(;v!==g&&2this.stemmer.get(p))}if(g&&(this.mapper||this.dedupe&&1this.matcher.get(v)));if(g&&this.replacer)for(l=0;g&&lthis.A&&(this.j.clear(),this.o=this.o/1.1|0));if(g){if(g!==w)if(c){if(d[g])continue;d[g]=1}else{if(e===g)continue;e=g}f.push(g)}}}this.finalize&&(f=this.finalize(f)||f);this.cache&&a.length<=this.m&&(this.i.set(a,f),this.i.size>this.A&&(this.i.clear(),this.m=this.m/1.1|0));return f};function G(a){a.l=null;a.i.clear();a.j.clear()};function H(a,c,b){if(!a.length)return a;if(1===a.length)return a=a[0],a=b||a.length>c?a.slice(b,b+c):a;let f=[];for(let d=0,h,e;d=e){b-=e;continue}h=h.slice(b,b+c);e=h.length;b=0}e>c&&(h=h.slice(0,c),e=c);if(!f.length&&e>=c)return h;f.push(h);c-=e;if(!c)break}return f=1q;h--){e=m.substring(q,h);t=this.rtl?d-1-q:q;var k=this.score?this.score(c,m,p,e,t):L(v, +n,p,d,t);M(this,w,e,k,a,b)}break}case "bidirectional":case "reverse":if(1e?0: +1),n,p,k-1,l-1),t=this.bidirectional&&m>h;M(this,g,t?h:m,q,a,b,t?m:h)}}}}this.fastupdate||this.reg.add(a)}}return this};function M(a,c,b,f,d,h,e){let k=e?a.ctx:a.map,l;if(!c[b]||e&&!(l=c[b])[e])e?(c=l||(c[b]=x()),c[e]=1,(l=k.get(e))?k=l:k.set(e,k=new Map)):c[b]=1,(l=k.get(b))?k=l:k.set(b,k=[]),k=k[f]||(k[f]=[]),h&&k.includes(d)||(k.push(d),a.fastupdate&&((c=a.reg.get(d))?c.push(k):a.reg.set(d,[k])))}function L(a,c,b,f,d){return b&&1c||d?e.slice(d,c+d):e;e=a}else{if(ac||d)e=e.slice(d,c+d)}g=e}else if(1=== +f){c=H.call(null,a[0],c,d);break a}c=g}return c};function N(a,c,b){let f;b&&(f=a.bidirectional&&c>b)&&(f=b,b=c,c=f);a=b?(a=a.ctx.get(b))&&a.get(c):a.map.get(c);return a};function I(a,c){if(!this||this.constructor!==I)return new I(a);if(a){var b="string"===typeof a?a:a.preset;b&&(a=Object.assign({},K[b],a))}else a={};b=a.context;const f=!0===b?{depth:1}:b||{},d=a.encode||a.encoder||{};this.encoder=d.encode?d:"object"===typeof d?new F(d):{encode:d};this.resolution=a.resolution||9;this.tokenize=b=(b=a.tokenize)&&"default"!==b&&"exact"!==b&&b||"strict";this.depth="strict"===b&&f.depth||0;this.bidirectional=!1!==f.bidirectional;this.fastupdate=!!a.fastupdate;this.score= a.score||null;this.map=new Map;this.ctx=new Map;this.reg=c||(this.fastupdate?new Map:new Set);this.v=f.resolution||3;this.rtl=d.rtl||a.rtl||!1}r=I.prototype;r.clear=function(){this.map.clear();this.ctx.clear();this.reg.clear();return this};r.append=function(a,c){return this.add(a,c,!0)};r.contain=function(a){return this.reg.has(a)};r.update=function(a,c){const b=this,f=this.remove(a);return f&&f.then?f.then(()=>b.add(a,c)):this.add(a,c)}; r.cleanup=function(){if(!this.fastupdate)return this;J(this.map);this.depth&&J(this.ctx);return this};x();export default {Index:I,Charset:null,Encoder:F,Document:null,Worker:null,Resolver:null,IndexedDB:null,Language:{}}; export const Index=I;export const Charset=null;export const Encoder=F;export const Document=null;export const Worker=null;export const Resolver=null;export const IndexedDB=null;export const Language={}; \ No newline at end of file diff --git a/dist/module-debug/bundle.js b/dist/module-debug/bundle.js index 970d520..c27d46d 100644 --- a/dist/module-debug/bundle.js +++ b/dist/module-debug/bundle.js @@ -66,16 +66,16 @@ import { KeystoreMap, KeystoreArray, KeystoreSet } from "./keystore.js"; /** @export */Document.prototype.cleanup; /** @export */Document.prototype.addAsync; /** @export */Document.prototype.appendAsync; -/** @export */Document.prototype.searchAsync; -/** @export */Document.prototype.searchCacheAsync; /** @export */Document.prototype.updateAsync; /** @export */Document.prototype.removeAsync; +/** @export */Document.prototype.searchAsync; +/** @export */Document.prototype.searchCacheAsync; +/** @export */Document.prototype.searchCache; /** @export */Document.prototype.mount; /** @export */Document.prototype.commit; /** @export */Document.prototype.destroy; /** @export */Document.prototype.export; /** @export */Document.prototype.import; -/** @export */Document.prototype.searchCache; /** @export */Document.prototype.get; /** @export */Document.prototype.set; @@ -95,6 +95,7 @@ import { KeystoreMap, KeystoreArray, KeystoreSet } from "./keystore.js"; /** @export */Resolver.prototype.xor; /** @export */Resolver.prototype.not; /** @export */Resolver.prototype.result; +/** @export */Resolver.prototype.await; /** @export */StorageInterface.db; /** @export */StorageInterface.id; @@ -218,7 +219,6 @@ import { KeystoreMap, KeystoreArray, KeystoreSet } from "./keystore.js"; /** @export */SearchOptions.context; /** @export */SearchOptions.suggest; /** @export */SearchOptions.resolve; -/** @export */SearchOptions.enrich; /** @export */SearchOptions.cache; /** @export */SearchOptions.resolution; @@ -270,9 +270,13 @@ import { KeystoreMap, KeystoreArray, KeystoreSet } from "./keystore.js"; /** @export */ResolverOptions.query; /** @export */ResolverOptions.limit; /** @export */ResolverOptions.offset; +/** @export */ResolverOptions.boost; /** @export */ResolverOptions.enrich; /** @export */ResolverOptions.resolve; /** @export */ResolverOptions.suggest; +/** @export */ResolverOptions.cache; +/** @export */ResolverOptions.async; +/** @export */ResolverOptions.queue; /** @export */ResolverOptions.and; /** @export */ResolverOptions.or; /** @export */ResolverOptions.xor; diff --git a/dist/module-debug/cache.js b/dist/module-debug/cache.js index 789d1fd..3b8f2db 100644 --- a/dist/module-debug/cache.js +++ b/dist/module-debug/cache.js @@ -1,36 +1,59 @@ import Index from "./index.js"; -import Document from "./document.js"; import { SearchOptions, DocumentSearchOptions } from "./type.js"; /** - * @param {string|SearchOptions|DocumentSearchOptions} query_or_options - * @param {number|SearchOptions|DocumentSearchOptions=} limit_or_options + * @param {string|SearchOptions|DocumentSearchOptions} query + * @param {number|SearchOptions|DocumentSearchOptions=} limit * @param {SearchOptions|DocumentSearchOptions=} options - * @this {Index|Document} + * @this {Index} * @returns {Array|Promise} */ +export function searchCache(query, limit, options) { -export function searchCache(query_or_options, limit_or_options, options) { + if (!options) { + if (!limit && "object" == typeof query) { + options = query; + } else if ("object" == typeof limit) { + options = limit; + limit = 0; + } + } - const query = (limit_or_options ? "" + query_or_options : "object" == typeof query_or_options ? "" + query_or_options.query : query_or_options).toLowerCase(); + if (options) { + query = options.query || query; + limit = options.limit || limit; + } + + let key = "" + (limit || 0); + + if (options) { + + const { + context, + suggest, + offset, + resolve, + boost, + resolution + } = options; + + key += (offset || 0) + !!context + !!suggest + (!1 !== resolve) + (resolution || this.resolution) + (boost || 0); + } + + query = ("" + query).toLowerCase(); if (!this.cache) { this.cache = new CacheClass(); } - let cache = this.cache.get(query); + let cache = this.cache.get(query + key); if (!cache) { - cache = this.search(query_or_options, limit_or_options, options); + const opt_cache = options && options.cache; + opt_cache && (options.cache = !1); + cache = this.search(query, limit, options); + opt_cache && (options.cache = opt_cache); - if (cache.then) { - const self = this; - cache.then(function (cache) { - self.cache.set(query, cache); - return cache; - }); - } - - this.cache.set(query, cache); + this.cache.set(query + key, cache); } return cache; } diff --git a/dist/module-debug/db/indexeddb/index.js b/dist/module-debug/db/indexeddb/index.js index 23e2493..f688473 100644 --- a/dist/module-debug/db/indexeddb/index.js +++ b/dist/module-debug/db/indexeddb/index.js @@ -1,6 +1,3 @@ - -import { PersistentOptions, SearchResults, EnrichedSearchResults } from "../../type.js"; - const VERSION = 1, IndexedDB = "undefined" != typeof window && (window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB), IDBTransaction = "undefined" != typeof window && (window.IDBTransaction || window.webkitIDBTransaction || window.msIDBTransaction), diff --git a/dist/module-debug/db/mongodb/index.js b/dist/module-debug/db/mongodb/index.js index 79b65f9..ec05a19 100644 --- a/dist/module-debug/db/mongodb/index.js +++ b/dist/module-debug/db/mongodb/index.js @@ -140,17 +140,25 @@ MongoDB.prototype.clear = function () { }; function create_result(rows, resolve, enrich) { + const _id = rows[0] && "undefined" != typeof rows[0]._id; if (resolve) { - if (!enrich) for (let i = 0; i < rows.length; i++) { - rows[i] = rows[i].id; + if (!enrich || _id) for (let i = 0, row; i < rows.length; i++) { + row = rows[i]; + if (enrich) { + const id = row._id; + delete row._id; + row.id = id; + } else { + rows[i] = _id ? row._id : row.id; + } } return rows; } else { const arr = []; - for (let i = 0, row; i < rows.length; i++) { + for (let i = 0, row, res; i < rows.length; i++) { row = rows[i]; - arr[row.res] || (arr[row.res] = []); - arr[row.res].push(enrich ? row : row.id); + res = row.res; + (arr[res] || (arr[res] = [])).push(_id ? row._id : row.id); } return arr; } @@ -160,8 +168,17 @@ MongoDB.prototype.get = async function (key, ctx, limit = 0, offset = 0, resolve let rows, params = ctx ? { ctx, key } : { key }; + if (!enrich && !tags) { - rows = await this.db.collection((ctx ? "ctx" : "map") + this.field).find(params, { projection: { _id: 0, res: 1, id: 1 }, limit, skip: offset }).toArray(); + + const stmt = { + projection: { _id: 0, res: 1, id: 1 } + }; + + limit && (stmt.limit = limit); + offset && (stmt.skip = offset); + + rows = await this.db.collection((ctx ? "ctx" : "map") + this.field).find(params, stmt).toArray(); } else { const project = { _id: 0, id: 1 }, stmt = [{ $match: params }]; @@ -204,7 +221,10 @@ MongoDB.prototype.get = async function (key, ctx, limit = 0, offset = 0, resolve stmt.push({ $project: project }); } - stmt.push({ $sort: { res: 1 } }, { $skip: offset }, { $limit: limit }); + stmt.push({ $sort: { res: 1 } }); + + limit && stmt.push({ $limit: limit }); + offset && stmt.push({ $skip: offset }); rows = []; const result = await this.db.collection((ctx ? "ctx" : "map") + this.field).aggregate(stmt); @@ -220,14 +240,26 @@ MongoDB.prototype.get = async function (key, ctx, limit = 0, offset = 0, resolve MongoDB.prototype.tag = async function (tag, limit = 0, offset = 0, enrich = !1) { + let rows; + if (!enrich) { - const rows = await this.db.collection("tag" + this.field).find({ tag }, { projection: { _id: 0, id: 1 }, limit, skip: offset }).toArray(); - return create_result(rows, !0, !1); + const stmt = { + projection: { _id: 0, id: 1 } + }; + + limit && (stmt.limit = limit); + offset && (stmt.skip = offset); + + rows = await this.db.collection("tag" + this.field).find({ tag }, stmt).toArray(); } else { - let rows = []; - const result = await this.db.collection("tag" + this.field).aggregate([{ $match: { tag } }, { $skip: offset }, { $limit: limit }, { $lookup: { + const stmt = [{ $match: { tag } }]; + + limit && stmt.push({ $limit: limit }); + offset && stmt.push({ $skip: offset }); + + stmt.push({ $lookup: { from: "reg", localField: "id", foreignField: "id", @@ -235,15 +267,18 @@ MongoDB.prototype.tag = async function (tag, limit = 0, offset = 0, enrich = !1) } }, { $project: { _id: 0, id: 1, doc: "$doc.doc" } }, { $unwind: { path: "$doc", preserveNullAndEmptyArrays: !0 - } }]); + } }); + + rows = []; + const result = await this.db.collection("tag" + this.field).aggregate(stmt); while (!0) { const row = await result.next(); if (row) rows.push(row);else break; } - - return rows; } + + create_result(rows, !0, enrich); }; MongoDB.prototype.enrich = function (ids) { @@ -323,8 +358,9 @@ MongoDB.prototype.search = async function (flexsearch, query, limit = 100, offse stmt.push({ $match: match }); } - stmt.push({ $sort: suggest ? { count: -1, res: 1 } : { res: 1 } }, { $skip: offset }, { $limit: limit }); - + stmt.push({ $sort: suggest ? { count: -1, res: 1 } : { res: 1 } }); + limit && stmt.push({ $limit: limit }); + offset && stmt.push({ $skip: offset }); stmt.push({ $project: project }); rows = await this.db.collection("ctx" + this.field).aggregate(stmt); @@ -375,8 +411,10 @@ MongoDB.prototype.search = async function (flexsearch, query, limit = 100, offse stmt.push({ $match: match }); } - stmt.push({ $sort: suggest ? { count: -1, res: 1 } : { res: 1 } }, { $skip: offset }, { $limit: limit }); - + stmt.push({ $sort: suggest ? { count: -1, res: 1 } : { res: 1 } + }); + limit && stmt.push({ $limit: limit }); + offset && stmt.push({ $skip: offset }); stmt.push({ $project: project }); rows = await this.db.collection("map" + this.field).aggregate(stmt); @@ -385,21 +423,15 @@ MongoDB.prototype.search = async function (flexsearch, query, limit = 100, offse while (!0) { const row = await rows.next(); if (row) { - if (resolve && !enrich) { - result.push(row._id); - } else { + if (resolve && enrich) { row.id = row._id; delete row._id; - result.push(row); } + result.push(row); } else break; } - if (resolve && !enrich) { - return result; - } else { - return create_result(result, resolve, enrich); - } + return create_result(result, resolve, enrich); }; MongoDB.prototype.info = function () {}; diff --git a/dist/module-debug/db/redis/index.js b/dist/module-debug/db/redis/index.js index ba09779..e34ae3a 100644 --- a/dist/module-debug/db/redis/index.js +++ b/dist/module-debug/db/redis/index.js @@ -88,20 +88,18 @@ RedisDB.prototype.clear = function () { function create_result(range, type, resolve, enrich, resolution) { if (resolve) { - if ("number" === type) { - for (let i = 0, tmp, id; i < range.length; i++) { - tmp = range[i]; - id = "number" === type ? parseInt(tmp.id || tmp, 10) : tmp.id || tmp; - range[i] = enrich ? { id, doc: tmp.doc } : id; - } + for (let i = 0, tmp, id; i < range.length; i++) { + tmp = range[i]; + id = "number" === type ? parseInt(tmp.value || tmp, 10) : tmp.value || tmp; + range[i] = enrich ? { id, doc: tmp.doc } : id; } return range; } else { let result = []; for (let i = 0, tmp, id, score; i < range.length; i++) { tmp = range[i]; - id = "number" === type ? parseInt(tmp.id || tmp, 10) : tmp.id || tmp; - score = resolution - tmp.score; + id = "number" === type ? parseInt(tmp.value || tmp, 10) : tmp.value || tmp; + score = Math.max(resolution - tmp.score, 0); result[score] || (result[score] = []); result[score].push(id); } diff --git a/dist/module-debug/document.js b/dist/module-debug/document.js index c6cff54..d97f0a9 100644 --- a/dist/module-debug/document.js +++ b/dist/module-debug/document.js @@ -472,7 +472,6 @@ Document.prototype.set = function (id, data) { return this; }; - Document.prototype.searchCache = searchCache; diff --git a/dist/module-debug/document/search.js b/dist/module-debug/document/search.js index 0fb7e1d..4bbd266 100644 --- a/dist/module-debug/document/search.js +++ b/dist/module-debug/document/search.js @@ -4,6 +4,7 @@ import { create_object, is_array, is_object, is_string, inherit } from "../commo import { intersect, intersect_union } from "../intersect.js"; import Document from "../document.js"; import Index from "../index.js"; +import WorkerIndex from "../worker.js"; import Resolver from "../resolver.js"; import tick from "../profiler.js"; import { highlight_fields } from "./highlight.js"; @@ -32,7 +33,6 @@ import { highlight_fields } from "./highlight.js"; * > * } */ - Document.prototype.search = function (query, limit, options, _promises) { if (!options) { @@ -45,13 +45,6 @@ Document.prototype.search = function (query, limit, options, _promises) { } } - if (options && options.cache) { - options.cache = !1; - const res = this.searchCache(query, limit, options); - options.cache = !0; - return res; - } - /** @type { * DocumentSearchResults| * EnrichedDocumentSearchResults| @@ -67,6 +60,7 @@ Document.prototype.search = function (query, limit, options, _promises) { merge, suggest, boost, + cache, field, tag, offset, @@ -91,6 +85,7 @@ Document.prototype.search = function (query, limit, options, _promises) { tag = this.tag && options.tag; suggest = options.suggest; resolve = !1 !== options.resolve; + cache = options.cache; if (this.store && options.highlight && !resolve) { console.warn("Highlighting results can only be done on a final resolver task or when calling .resolve({ highlight: ... })"); @@ -244,13 +239,15 @@ Document.prototype.search = function (query, limit, options, _promises) { suggest = inherit(field_options.suggest, suggest); highlight = resolve && this.store && inherit(field_options.highlight, highlight); enrich = !!highlight || resolve && this.store && inherit(field_options.enrich, enrich); + cache = inherit(field_options.cache, cache); } if (_promises) { res = _promises[i]; } else { - let opt = field_options || options, - index = this.index.get(key); + const opt = field_options || options || {}, + opt_enrich = opt.enrich, + index = this.index.get(key); if (tag) { @@ -259,24 +256,24 @@ Document.prototype.search = function (query, limit, options, _promises) { db_tag_search = index.db.support_tag_search; opt.field = field; } - if (!db_tag_search) { + if (!db_tag_search && opt_enrich) { opt.enrich = !1; } } - if (promises) { - promises[i] = index.search(query, limit, opt); - opt && enrich && (opt.enrich = enrich); + res = cache ? index.searchCache(query, limit, opt) : index.search(query, limit, opt); + + opt_enrich && (opt.enrich = opt_enrich); + + if (promises) { + promises[i] = res; continue; - } else { - res = index.search(query, limit, opt); - - opt && enrich && (opt.enrich = enrich); } } - len = res && (resolve ? res.length : res.result.length); + res = res.result || res; + len = res && res.length; if (tag && len) { @@ -375,7 +372,12 @@ Document.prototype.search = function (query, limit, options, _promises) { const self = this; return Promise.all(promises).then(function (result) { - return result.length ? self.search(query, limit, options, result) : result; + + options && (options.resolve = resolve); + if (result.length) { + result = self.search(query, limit, options, result); + } + return result; }); } @@ -383,10 +385,8 @@ Document.prototype.search = function (query, limit, options, _promises) { return resolve ? result : new Resolver(result, this); } if (pluck && (!enrich || !this.store)) { - result = result[0]; - if (!resolve) result.index = this; - return (/** @type {SearchResults|Resolver} */result - ); + result = /** @type {SearchResults|IntermediateSearchResults} */result[0]; + return resolve ? result : new Resolver(result, this); } promises = []; @@ -400,7 +400,7 @@ Document.prototype.search = function (query, limit, options, _promises) { if (enrich && res.length && "undefined" == typeof res[0].doc) { if (!this.db) { - res = apply_enrich.call(this, res); + res = /** @type {EnrichedSearchResults} */apply_enrich.call(this, res); } else { promises.push(res = this.index.get(this.field[0]).db.enrich(res)); @@ -500,17 +500,20 @@ function get_tag(tag, key, limit, offset, enrich) { /** * @param {SearchResults} ids - * @return {EnrichedSearchResults|SearchResults} - * @this {Document|Index|null} + * @return {EnrichedSearchResults|SearchResults|Promise} + * @this {Document|Index|WorkerIndex|null} */ export function apply_enrich(ids) { if (!this || !this.store) return ids; + if (this.db) { + return this.index.get(this.field[0]).db.enrich(ids); + } + /** @type {EnrichedSearchResults} */ const result = Array(ids.length); - for (let x = 0, id; x < ids.length; x++) { id = ids[x]; result[x] = { diff --git a/dist/module-debug/index/remove.js b/dist/module-debug/index/remove.js index d1d28a6..072d647 100644 --- a/dist/module-debug/index/remove.js +++ b/dist/module-debug/index/remove.js @@ -60,24 +60,29 @@ export function remove_index(map, id) { if (is_array(map)) { - for (let x = 0, arr, index; x < map.length; x++) { + for (let x = 0, arr, index, found; x < map.length; x++) { if ((arr = map[x]) && arr.length) { if ("undefined" == typeof id) { - - count++; + return 1; } else { index = arr.indexOf(id); if (0 <= index) { if (1 < arr.length) { arr.splice(index, 1); - count++; - break; + return 1; } else { delete map[x]; + if (count) { + return 1; + } + found = 1; } } else { + if (found) { + return 1; + } count++; } } diff --git a/dist/module-debug/intersect.js b/dist/module-debug/intersect.js index 0b74fb3..2c94d71 100644 --- a/dist/module-debug/intersect.js +++ b/dist/module-debug/intersect.js @@ -119,7 +119,7 @@ export function intersect(arrays, resolution, limit, offset, suggest, boost, res } } else { - result = 1 < result.length ? union(result, limit, offset, resolve, boost) : (result = result[0]).length > limit || offset ? result.slice(offset, limit + offset) : result; + result = 1 < result.length ? union(result, limit, offset, resolve, boost) : (result = result[0]) && limit && result.length > limit || offset ? result.slice(offset, limit + offset) : result; } } diff --git a/dist/module-debug/resolve/and.js b/dist/module-debug/resolve/and.js index 9524427..c31b89b 100644 --- a/dist/module-debug/resolve/and.js +++ b/dist/module-debug/resolve/and.js @@ -1,52 +1,25 @@ import Resolver from "../resolver.js"; import { get_max_len } from "../common.js"; import { intersect } from "../intersect.js"; -import { SearchResults, EnrichedSearchResults, IntermediateSearchResults, ResolverOptions } from "../type.js"; -import { apply_enrich } from "../document/search.js"; +import { SearchResults, EnrichedSearchResults, IntermediateSearchResults } from "../type.js"; -/** @this {Resolver} */ +/** + * @return { + * SearchResults | + * EnrichedSearchResults | + * IntermediateSearchResults | + * Promise | + * Resolver + * } + * @this {Resolver} + */ Resolver.prototype.and = function () { - let execute = this.result.length, - limit, - offset, - enrich, - resolve; - - - if (!execute) { - /** @type {ResolverOptions} */ - const arg = arguments[0]; - if (arg) { - execute = !!arg.suggest; - resolve = arg.resolve; - limit = arg.limit; - offset = arg.offset; - enrich = arg.enrich && resolve; - } - } - - if (execute) { - - const { - final, - promises, - limit, - offset, - enrich, - resolve, - suggest - } = this.handler("and", arguments); - - return return_result.call(this, final, promises, limit, offset, enrich, resolve, suggest); - } - - return resolve ? this.resolve(limit, offset, enrich) : this; + return this.handler("and", return_result, arguments); }; /** * Aggregate the intersection of N raw results * @param {!Array} final - * @param {!Array>} promises * @param {number} limit * @param {number=} offset * @param {boolean=} enrich @@ -62,44 +35,49 @@ Resolver.prototype.and = function () { * } */ -function return_result(final, promises, limit, offset, enrich, resolve, suggest) { +function return_result(final, limit, offset, enrich, resolve, suggest) { - if (promises.length) { - const self = this; - return Promise.all(promises).then(function (result) { - - final = []; - for (let i = 0, tmp; i < result.length; i++) { - if ((tmp = result[i]).length) { - final[i] = tmp; - } - } - - return return_result.call(self, final, [], limit, offset, enrich, resolve, suggest); - }); + if (!suggest && !this.result.length) { + return resolve ? this.result : this; } + let resolved; + if (!final.length) { if (!suggest) { this.result = /** @type {SearchResults|IntermediateSearchResults} */final; } } else { - this.result.length && final.unshift(this.result); if (2 > final.length) { this.result = final[0]; } else { - const resolution = get_max_len(final); + + let resolution = 0; + for (let i = 0, res, len; i < final.length; i++) { + if ((res = final[i]) && (len = res.length)) { + if (resolution < len) { + resolution = len; + } + } else if (!suggest) { + resolution = 0; + break; + } + } + if (!resolution) { this.result = []; } else { this.result = intersect(final, resolution, limit, offset, suggest, this.boostval, resolve); - - return resolve ? enrich ? apply_enrich.call(this.index, /** @type {SearchResults} */this.result) : this.result : this; + resolved = !0; } } } - return resolve ? this.resolve(limit, offset, enrich) : this; + if (resolve) { + this.await = null; + } + + return resolve ? this.resolve(limit, offset, enrich, resolved) : this; } \ No newline at end of file diff --git a/dist/module-debug/resolve/default.js b/dist/module-debug/resolve/default.js index d635438..371b1f1 100644 --- a/dist/module-debug/resolve/default.js +++ b/dist/module-debug/resolve/default.js @@ -3,6 +3,7 @@ import { IntermediateSearchResults, SearchResults, EnrichedSearchResults } from import { apply_enrich } from "../document/search.js"; import Document from "../document.js"; import Index from "../index.js"; +import WorkerIndex from "../worker.js"; /* from -> res[score][id] @@ -16,7 +17,7 @@ import Index from "../index.js"; * @param {number=} offset * @param {boolean=} enrich * @return {SearchResults|EnrichedSearchResults} - * @this {Document|Index} + * @this {Document|Index|WorkerIndex} */ export default function (result, limit, offset, enrich) { @@ -27,8 +28,8 @@ export default function (result, limit, offset, enrich) { if (1 === result.length) { let final = result[0]; - final = offset || final.length > limit ? limit ? final.slice(offset, offset + limit) : final.slice(offset) : final; - return enrich ? apply_enrich.call(this, final) : final; + final = offset || final.length > limit ? final.slice(offset, offset + limit) : final; + return enrich ? /** @type {EnrichedSearchResults} */apply_enrich.call(this, final) : final; } let final = []; @@ -43,14 +44,13 @@ export default function (result, limit, offset, enrich) { continue; } - if (offset < len) { - arr = limit ? arr.slice(offset, offset + limit) : arr.slice(offset); - len = arr.length; - offset = 0; - } + arr = arr.slice(offset, offset + limit); + len = arr.length; + offset = 0; } if (len > limit) { + arr = arr.slice(0, limit); len = limit; } @@ -58,7 +58,7 @@ export default function (result, limit, offset, enrich) { if (!final.length) { if (len >= limit) { - return enrich ? apply_enrich.call(this, arr) : arr; + return enrich ? /** @type {EnrichedSearchResults} */apply_enrich.call(this, arr) : arr; } } diff --git a/dist/module-debug/resolve/handler.js b/dist/module-debug/resolve/handler.js index 32d7980..55a62b7 100644 --- a/dist/module-debug/resolve/handler.js +++ b/dist/module-debug/resolve/handler.js @@ -1,53 +1,68 @@ import Resolver from "../resolver.js"; -import { ResolverOptions, SearchResults, IntermediateSearchResults } from "../type.js"; +import { ResolverOptions, SearchResults, EnrichedSearchResults, IntermediateSearchResults } from "../type.js"; /** - * @param {string} fn - * @param {Array>|Arguments} args + * @param {string} method + * @param {Function} fn + * @param {Array|Arguments} args + * @return { + * SearchResults | + * EnrichedSearchResults | + * IntermediateSearchResults | + * Promise | + * Resolver + * } */ -Resolver.prototype.handler = function (fn, args) { +Resolver.prototype.handler = function (method, fn, args) { - /** @type {ResolverOptions|Promise} */ - let first_argument = args[0]; + /** @type {ResolverOptions} */ + let arg = args[0]; - if (first_argument.then) { - const self = this; - - return Promise.all(args).then(function (args) { - return self[fn].apply(self, args); - }); + if (arg[0] && arg[0].query) { + return this[method].apply(this, arg); } - if (first_argument[0]) { + if ("and" === method || "not" === method) { + let execute = this.result.length || this.await, + resolve; - if (first_argument[0].index) { - return this[fn].apply(this, first_argument); + if (!execute) { + if (!arg.suggest) { + if (1 < args.length) { + arg = args[args.length - 1]; + } + resolve = arg.resolve; + return resolve ? this.await || this.result : this; + } } } - /** @type {SearchResults|IntermediateSearchResults} */ + const self = this; + /** @type {!Array>} */ let final = [], - promises = [], limit = 0, offset = 0, enrich, resolve, suggest, highlight, - highlight_query; - /** @type {Array>} */ + async; + for (let i = 0, query; i < args.length; i++) { - query = /** @type {string|ResolverOptions} */args[i]; + /** @type {ResolverOptions} */ + query = args[i]; + if (query) { let result; + if (query.constructor === Resolver) { - result = query.result; - } else if (query.constructor === Array) { + result = query.await || query.result; + } else if (query.then || query.constructor === Array) { result = query; } else { @@ -57,10 +72,13 @@ Resolver.prototype.handler = function (fn, args) { resolve = query.resolve; highlight = query.highlight && resolve; enrich = highlight || query.enrich && resolve; - let index; + let opt_queue = query.queue, + opt_async = query.async || opt_queue, + index = query.index; - if (query.index) { - this.index = index = query.index; + + if (index) { + this.index || (this.index = index); } else { index = this.index; } @@ -85,47 +103,123 @@ Resolver.prototype.handler = function (fn, args) { } } - query.resolve = !1; + if (opt_queue && (async || this.await)) { + async = 1; - result = index.search(query).result; - query.resolve = resolve; + let resolve; + const idx = this.promises.length, + promise = new Promise(function (_resolve) { + resolve = _resolve; + }); + + + (function (index, query) { + + promise._fn = function () { + query.index = null; + query.resolve = !1; + let result = opt_async ? index.searchAsync(query) : index.search(query); + if (result.then) { + return result.then(function (result) { + self.promises[idx] = result = result.result || result; + resolve(result); + return result; + }); + } + result = result.result || result; + resolve(result); + return result; + }; + })(index, Object.assign({}, /** @type Object */query)); + + this.promises.push(promise); + final[i] = promise; + continue; + } else { + query.resolve = !1; + query.index = null; + + result = opt_async ? index.searchAsync(query) : index.search(query); + + query.resolve = resolve; + query.index = index; + } if (highlight) { - highlight_query = query.query; + query.query; } } else if (query.and) { - result = this.and(query.and); + result = inner_call(query, "and", index); } else if (query.or) { - result = this.or(query.or); - } else if (query.xor) { - result = this.xor(query.xor); + result = inner_call(query, "or", index); } else if (query.not) { - result = this.not(query.not); + result = inner_call(query, "not", index); + } else if (query.xor) { + result = inner_call(query, "xor", index); } else { continue; } } - if (result.then) { - promises.push(result); - } else if (result.length) { - final[i] = result; - } else if (!suggest && ("and" === fn || "xor" === fn)) { - final = []; - break; + if (result.await) { + async = 1; + result = result.await; + } else if (result.then) { + async = 1; + result = result.then(function (result) { + return result.result || result; + }); + } else { + result = result.result || result; } + + final[i] = result; } } - return { - final, - promises, - limit, - offset, - enrich, - resolve, - suggest, - highlight, - highlight_query - }; -}; \ No newline at end of file + if (async && !this.await) { + this.await = new Promise(function (resolve) { + self.return = resolve; + }); + } + + if (async) { + + const promises = Promise.all(final).then(function (final) { + for (let i = 0; i < self.promises.length; i++) { + if (self.promises[i] === promises) { + self.promises[i] = function () { + return fn.call(self, final, limit, offset, enrich, resolve, suggest); + }; + break; + } + } + self.execute(); + }); + this.promises.push(promises); + } else if (this.await) { + + this.promises.push(function () { + return fn.call(self, final, limit, offset, enrich, resolve, suggest); + }); + } else { + + return fn.call(this, final, limit, offset, enrich, resolve, suggest); + } + + return resolve ? this.await || this.result : this; +}; + +function inner_call(query, method, index) { + const args = query[method], + arg = args[0] || args; + + arg.index || (arg.index = index); + + let resolver = new Resolver(arg); + if (1 < args.length) { + resolver = resolver[method].apply(resolver, args.slice(1)); + } + + return resolver; +} \ No newline at end of file diff --git a/dist/module-debug/resolve/not.js b/dist/module-debug/resolve/not.js index cd9070c..8dd9cf5 100644 --- a/dist/module-debug/resolve/not.js +++ b/dist/module-debug/resolve/not.js @@ -1,26 +1,13 @@ import Resolver from "../resolver.js"; import { SearchResults, EnrichedSearchResults, IntermediateSearchResults } from "../type.js"; -import { apply_enrich } from "../document/search.js"; /** @this {Resolver} */ Resolver.prototype.not = function () { - - const { - final, - promises, - limit, - offset, - enrich, - resolve, - suggest - } = this.handler("not", arguments); - - return return_result.call(this, final, promises, limit, offset, enrich, resolve, suggest); + return this.handler("not", return_result, arguments); }; /** * @param {!Array} final - * @param {!Array>} promises * @param {number} limit * @param {number=} offset * @param {boolean=} enrich @@ -35,31 +22,24 @@ Resolver.prototype.not = function () { * Resolver * } */ +function return_result(final, limit, offset, enrich, resolve, suggest) { -function return_result(final, promises, limit, offset, enrich, resolve, suggest) { - - if (promises.length) { - const self = this; - return Promise.all(promises).then(function (result) { - - final = []; - for (let i = 0, tmp; i < result.length; i++) { - if ((tmp = result[i]).length) { - final[i] = tmp; - } - } - - return return_result.call(self, final, [], limit, offset, enrich, resolve, suggest); - }); + if (!suggest && !this.result.length) { + return resolve ? this.result : this; } + let resolved; + if (final.length && this.result.length) { this.result = exclusion.call(this, final, limit, offset, resolve); - } else if (resolve) { - return this.resolve(limit, offset, enrich); + resolved = !0; } - return resolve ? enrich ? apply_enrich.call(this.index, this.result) : this.result : this; + if (resolve) { + this.await = null; + } + + return resolve ? this.resolve(limit, offset, enrich, resolved) : this; } /** @@ -70,7 +50,6 @@ function return_result(final, promises, limit, offset, enrich, resolve, suggest) * @this {Resolver} * @return {SearchResults|IntermediateSearchResults} */ - function exclusion(result, limit, offset, resolve) { /** @type {SearchResults|IntermediateSearchResults} */ diff --git a/dist/module-debug/resolve/or.js b/dist/module-debug/resolve/or.js index 7114d27..3abd217 100644 --- a/dist/module-debug/resolve/or.js +++ b/dist/module-debug/resolve/or.js @@ -4,27 +4,17 @@ import { SearchResults, EnrichedSearchResults, IntermediateSearchResults } from /** @this {Resolver} */ Resolver.prototype.or = function () { - - const { - final, - promises, - limit, - offset, - enrich, - resolve - } = this.handler("or", arguments); - - return return_result.call(this, final, promises, limit, offset, enrich, resolve); + return this.handler("or", return_result, arguments); }; /** * Aggregate the intersection of N raw results * @param {!Array} final - * @param {!Array>} promises * @param {number} limit * @param {number=} offset * @param {boolean=} enrich * @param {boolean=} resolve + * @param {boolean=} suggest * @this {Resolver} * @return { * SearchResults | @@ -35,25 +25,9 @@ Resolver.prototype.or = function () { * } */ -function return_result(final, promises, limit, offset, enrich, resolve) { - - if (promises.length) { - const self = this; - return Promise.all(promises).then(function (result) { - - final = []; - for (let i = 0, tmp; i < result.length; i++) { - if ((tmp = result[i]).length) { - final[i] = tmp; - } - } - - return return_result.call(self, final, [], limit, offset, enrich, resolve); - }); - } +function return_result(final, limit, offset, enrich, resolve) { if (final.length) { - this.result.length && final.push(this.result); if (2 > final.length) { @@ -66,5 +40,9 @@ function return_result(final, promises, limit, offset, enrich, resolve) { } } + if (resolve) { + this.await = null; + } + return resolve ? this.resolve(limit, offset, enrich) : this; } \ No newline at end of file diff --git a/dist/module-debug/resolve/xor.js b/dist/module-debug/resolve/xor.js index 5fdbb40..a940b12 100644 --- a/dist/module-debug/resolve/xor.js +++ b/dist/module-debug/resolve/xor.js @@ -1,28 +1,14 @@ import Resolver from "../resolver.js"; -import default_resolver from "./default.js"; import { create_object } from "../common.js"; import { SearchResults, EnrichedSearchResults, IntermediateSearchResults } from "../type.js"; -import { apply_enrich } from "../document/search.js"; /** @this {Resolver} */ Resolver.prototype.xor = function () { - - const { - final, - promises, - limit, - offset, - enrich, - resolve, - suggest - } = this.handler("xor", arguments); - - return return_result.call(this, final, promises, limit, offset, enrich, resolve, suggest); + return this.handler("xor", return_result, arguments); }; /** * @param {!Array} final - * @param {!Array>} promises * @param {number} limit * @param {number=} offset * @param {boolean=} enrich @@ -38,39 +24,28 @@ Resolver.prototype.xor = function () { * } */ -function return_result(final, promises, limit, offset, enrich, resolve, suggest) { +function return_result(final, limit, offset, enrich, resolve, suggest) { - if (promises.length) { - const self = this; - return Promise.all(promises).then(function (result) { - - final = []; - for (let i = 0, tmp; i < result.length; i++) { - if ((tmp = result[i]).length) { - final[i] = tmp; - } - } - - return return_result.call(self, final, [], limit, offset, enrich, resolve, suggest); - }); - } + let resolved; if (!final.length) { if (!suggest) this.result = /** @type {SearchResults|IntermediateSearchResults} */final; } else { - this.result.length && final.unshift(this.result); if (2 > final.length) { this.result = final[0]; } else { this.result = exclusive.call(this, final, limit, offset, resolve, this.boostval); - - return resolve ? enrich ? apply_enrich.call(this.index, /** @type {SearchResults} */this.result) : this.result : this; + resolved = !0; } } - return resolve ? this.resolve(limit, offset, enrich) : this; + if (resolve) { + this.await = null; + } + + return resolve ? this.resolve(limit, offset, enrich, resolved) : this; } /** diff --git a/dist/module-debug/resolver.js b/dist/module-debug/resolver.js index 1bad4c0..e441a0a 100644 --- a/dist/module-debug/resolver.js +++ b/dist/module-debug/resolver.js @@ -1,41 +1,76 @@ + +import { ResolverOptions, IntermediateSearchResults, SearchResults, EnrichedSearchResults } from "./type.js"; import Index from "./index.js"; import Document from "./document.js"; +import WorkerIndex from "./worker.js"; import default_resolver from "./resolve/default.js"; -import { apply_enrich } from "./document/search.js"; -import { ResolverOptions, IntermediateSearchResults } from "./type.js"; import "./resolve/handler.js"; import "./resolve/or.js"; import "./resolve/and.js"; import "./resolve/xor.js"; import "./resolve/not.js"; -import { highlight_fields } from "./document/highlight.js"; +import { apply_enrich } from "./document/search.js"; /** * @param {IntermediateSearchResults|ResolverOptions=} result - * @param {Index|Document=} index + * @param {Index|Document|WorkerIndex=} index * @return {Resolver} * @constructor */ - export default function Resolver(result, index) { if (!this || this.constructor !== Resolver) { return new Resolver(result, index); } - if (result && result.index) { + let boost = 0, + promises, + _await, + _return; - result.resolve = !1; - this.index = /** @type {Index|Document} */result.index; - this.boostval = result.boost || 0; - this.result = this.index.search(result).result; - return this; + if (result && result.index) { + const options = /** @type {ResolverOptions} */result; + index = options.index; + boost = options.boost || 0; + if (options.query) { + const resolve = options.resolve, + async = options.async || options.queue; + + options.resolve = !1; + options.index = null; + result = async ? index.searchAsync(options) : index.search(options); + options.resolve = resolve; + options.index = index; + result = result.result || result; + } else { + result = []; + } } - /** @type {Index|Document|null} */ + + if (result && result.then) { + const self = this; + result = result.then(function (result) { + self.promises[0] = self.result = result.result || result; + self.execute(); + }); + promises = [result]; + result = []; + _await = new Promise(function (resolve) { + _return = resolve; + }); + } + + /** @type {Index|Document|WorkerIndex|null} */ this.index = index || null; /** @type {IntermediateSearchResults} */ this.result = /** @type {IntermediateSearchResults} */result || []; /** @type {number} */ - this.boostval = 0; + this.boostval = boost; + /** @type {Array|IntermediateSearchResults|Function>} */ + this.promises = promises || []; + /** @type {Promise} */ + this.await = _await || null; + /** @type {Function} */ + this.return = _return || null; } /** @@ -92,29 +127,82 @@ Resolver.prototype.boost = function (boost) { return this; }; +/** + * @param {boolean=} _skip_callback + * @this {Resolver} + */ +Resolver.prototype.execute = function (_skip_callback) { + + let result = this.result; + + for (let i = 0, promise; i < this.promises.length; i++) { + promise = this.promises[i]; + if (promise) { + if ("function" == typeof promise) { + result = promise(); + this.promises[i] = result = result.result || result; + i--; + } else if (promise._fn) { + result = promise._fn(); + this.promises[i] = result = result.result || result; + i--; + } else if (promise.then) { + return this.await; + } + } + } + + const fn = this.return; + this.result = result; + this.promises = []; + this.await = null; + this.return = null; + + _skip_callback || fn(result); + return result; +}; + /** * @param {number|ResolverOptions=} limit * @param {number=} offset * @param {boolean=} enrich - * @param {boolean=} highlight + * @param {boolean=} _resolved */ -Resolver.prototype.resolve = function (limit, offset, enrich) { +Resolver.prototype.resolve = function (limit, offset, enrich, _resolved) { - const index = this.index; - let result = this.result; - this.index = null; - this.result = null; + let result = this.await ? this.execute(!0) : this.result; + + if (result.then) { + const self = this; + return result.then(function () { + return self.resolve(limit, offset, enrich); + }); + } if (result.length) { if ("object" == typeof limit) { enrich = limit.enrich; offset = limit.offset; - limit.highlight; + + + if (limit.highlight) { + console.warn('Highlighting results is not supported within the resolve() method. Instead pass highlight options within the last resolver stage like { query: "...", resolve: true, highlight: ... }.'); + } + limit = limit.limit; } - - result = default_resolver.call(index, result, limit || 100, offset, enrich); + result = _resolved ? enrich ? apply_enrich.call( + /** @type {Document} */this.index, + /** @type {SearchResults} */result) : result : default_resolver.call(this.index, result, limit || 100, offset, enrich); } + const fn = this.return; + this.index = null; + this.result = null; + this.promises = null; + this.await = null; + this.return = null; + + fn && fn(result); return result; }; \ No newline at end of file diff --git a/dist/module-debug/type.js b/dist/module-debug/type.js index 368e9fb..2357d63 100644 --- a/dist/module-debug/type.js +++ b/dist/module-debug/type.js @@ -1,6 +1,8 @@ import Index from "./index.js"; +import Document from "./document.js"; +import WorkerIndex from "./worker.js"; import Encoder from "./encoder.js"; import StorageInterface from "./db/interface.js"; @@ -140,7 +142,7 @@ export let SearchOptions = {}; * resolve: (boolean|undefined), * enrich: (boolean|undefined), * cache: (boolean|undefined), - * tag: (Object|Array|undefined), + * tag: (Object|Array>|undefined), * field: (Array|Array|DocumentSearchOptions|string|undefined), * index: (Array|Array|DocumentSearchOptions|string|undefined), * pluck: (string|DocumentSearchOptions|undefined), @@ -252,13 +254,20 @@ export let PersistentOptions = {}; /** * @typedef {{ - * index: (Index|Document|undefined), + * index: (Index|Document|WorkerIndex|undefined), * query: (string|undefined), + * pluck: (string|undefined), + * field: (string|undefined), * limit: (number|undefined), * offset: (number|undefined), + * boost: (number|undefined), * enrich: (boolean|undefined), + * highlight: (HighlightOptions|string|undefined), * resolve: (boolean|undefined), * suggest: (boolean|undefined), + * cache: (boolean|undefined), + * async: (boolean|undefined), + * queue: (boolean|undefined), * and: (ResolverOptions|Array|undefined), * or: (ResolverOptions|Array|undefined), * xor: (ResolverOptions|Array|undefined), diff --git a/dist/module-debug/worker.js b/dist/module-debug/worker.js index 967d758..a3b8a1f 100644 --- a/dist/module-debug/worker.js +++ b/dist/module-debug/worker.js @@ -1,6 +1,7 @@ import { IndexOptions } from "./type.js"; import { create_object } from "./common.js"; +import { searchCache } from "./cache.js"; import handler from "./worker/handler.js"; import apply_async from "./async.js"; import Encoder from "./encoder.js"; @@ -36,6 +37,8 @@ export default function WorkerIndex(options = /** @type IndexOptions */{}, encod this.resolver = create_object(); if (!this.worker) { + console.warn("Worker is not available on this platform. Please report on Github: https://github.com/nextapps-de/flexsearch/issues"); + return; } @@ -55,12 +58,10 @@ export default function WorkerIndex(options = /** @type IndexOptions */{}, encod if (options.config) { return new Promise(function (resolve) { - + if (1e9 < pid) pid = 0; _self.resolver[++pid] = function () { resolve(_self); - if (1e9 < pid) pid = 0; }; - _self.worker.postMessage({ id: pid, task: "init", @@ -93,13 +94,14 @@ export default function WorkerIndex(options = /** @type IndexOptions */{}, encod register("add"); register("append"); register("search"); -register("searchCache"); register("update"); register("remove"); register("clear"); register("export"); register("import"); +WorkerIndex.prototype.searchCache = searchCache; + apply_async(WorkerIndex.prototype); @@ -122,6 +124,7 @@ function register(key) { args[0] = null; } + if (1e9 < pid) pid = 0; self.resolver[++pid] = resolve; self.worker.postMessage({ task: key, @@ -141,7 +144,5 @@ function register(key) { function create(factory, is_node_js, worker_path) { - let worker = is_node_js ? "undefined" != typeof module ? (0,eval)('new(require("worker_threads")["Worker"])(__dirname+"/worker/node.js")') : import("worker_threads").then(function(worker){return new worker["Worker"](import.meta.dirname+"/../node/node.mjs")}) : factory ? new window.Worker(URL.createObjectURL(new Blob(["onmessage=" + handler.toString()], { type: "text/javascript" }))) : new window.Worker("string" == typeof worker_path ? worker_path : (1, eval)("import.meta.url").replace("/worker.js", "/worker/worker.js").replace("flexsearch.bundle.module.min.js", "module/worker/worker.js"), { type: "module" }); - - return worker; + return is_node_js ? "undefined" != typeof module ? (0,eval)('new(require("worker_threads")["Worker"])(__dirname+"/worker/node.js")') : import("worker_threads").then(function(worker){return new worker["Worker"](import.meta.dirname+"/../node/node.mjs")}) : factory ? new window.Worker(URL.createObjectURL(new Blob(["onmessage=" + handler.toString()], { type: "text/javascript" }))) : new window.Worker("string" == typeof worker_path ? worker_path : (1, eval)("import.meta.url").replace("/worker.js", "/worker/worker.js").replace("flexsearch.bundle.module.min.js", "module/worker/worker.js"), { type: "module" }); } \ No newline at end of file diff --git a/dist/module-debug/worker/handler.js b/dist/module-debug/worker/handler.js index 9b4b8a2..d869496 100644 --- a/dist/module-debug/worker/handler.js +++ b/dist/module-debug/worker/handler.js @@ -72,6 +72,12 @@ export default (async function (data) { if (message && message.then) { message = await message; } + if (message && message.await) { + message = await message.await; + } + if ("search" === task && message.result) { + message = message.result; + } } postMessage("search" === task ? { id: id, msg: message } : { id: id }); diff --git a/dist/module-debug/worker/node.js b/dist/module-debug/worker/node.js index d92e17e..be9c0eb 100644 --- a/dist/module-debug/worker/node.js +++ b/dist/module-debug/worker/node.js @@ -60,6 +60,12 @@ parentPort.on("message", async function (data) { if (message && message.then) { message = await message; } + if (message && message.await) { + message = await message.await; + } + if ("search" === task && message.result) { + message = message.result; + } } parentPort.postMessage("search" === task ? { id: id, msg: message } : { id: id }); diff --git a/dist/module-min/bundle.js b/dist/module-min/bundle.js index cf583b1..250330a 100644 --- a/dist/module-min/bundle.js +++ b/dist/module-min/bundle.js @@ -1 +1 @@ -import{SearchOptions,ContextOptions,DocumentDescriptor,DocumentSearchOptions,FieldOptions,IndexOptions,DocumentOptions,TagOptions,StoreOptions,EncoderOptions,EncoderSplitOptions,PersistentOptions,ResolverOptions,HighlightBoundaryOptions,HighlightEllipsisOptions,HighlightOptions}from"./type.js";import StorageInterface from"./db/interface.js";import Document from"./document.js";import Index from"./index.js";import WorkerIndex from"./worker.js";import Resolver from"./resolver.js";import Encoder from"./encoder.js";import IdxDB from"./db/indexeddb/index.js";import Charset from"./charset.js";import{KeystoreMap,KeystoreArray,KeystoreSet}from"./keystore.js";Index.prototype.add,Index.prototype.append,Index.prototype.search,Index.prototype.update,Index.prototype.remove,Index.prototype.contain,Index.prototype.clear,Index.prototype.cleanup,Index.prototype.searchCache,Index.prototype.addAsync,Index.prototype.appendAsync,Index.prototype.searchAsync,Index.prototype.searchCacheAsync,Index.prototype.updateAsync,Index.prototype.removeAsync,Index.prototype.export,Index.prototype.import,Index.prototype.serialize,Index.prototype.mount,Index.prototype.commit,Index.prototype.destroy,Index.prototype.encoder,Index.prototype.reg,Index.prototype.map,Index.prototype.ctx,Index.prototype.db,Index.prototype.tag,Index.prototype.store,Index.prototype.depth,Index.prototype.bidirectional,Index.prototype.commit_task,Index.prototype.commit_timer,Index.prototype.cache,Index.prototype.bypass,Index.prototype.document,Encoder.prototype.assign,Encoder.prototype.encode,Encoder.prototype.addMatcher,Encoder.prototype.addStemmer,Encoder.prototype.addFilter,Encoder.prototype.addMapper,Encoder.prototype.addReplacer,Document.prototype.add,Document.prototype.append,Document.prototype.search,Document.prototype.update,Document.prototype.remove,Document.prototype.contain,Document.prototype.clear,Document.prototype.cleanup,Document.prototype.addAsync,Document.prototype.appendAsync,Document.prototype.searchAsync,Document.prototype.searchCacheAsync,Document.prototype.updateAsync,Document.prototype.removeAsync,Document.prototype.mount,Document.prototype.commit,Document.prototype.destroy,Document.prototype.export,Document.prototype.import,Document.prototype.searchCache,Document.prototype.get,Document.prototype.set,Document.prototype.field,Document.prototype.index,Document.prototype.reg,Document.prototype.tag,Document.prototype.store,Document.prototype.fastupdate,Resolver.prototype.limit,Resolver.prototype.offset,Resolver.prototype.boost,Resolver.prototype.resolve,Resolver.prototype.or,Resolver.prototype.and,Resolver.prototype.xor,Resolver.prototype.not,Resolver.prototype.result,StorageInterface.db,StorageInterface.id,StorageInterface.support_tag_search,StorageInterface.fastupdate,StorageInterface.prototype.mount,StorageInterface.prototype.open,StorageInterface.prototype.close,StorageInterface.prototype.destroy,StorageInterface.prototype.clear,StorageInterface.prototype.get,StorageInterface.prototype.tag,StorageInterface.prototype.enrich,StorageInterface.prototype.has,StorageInterface.prototype.search,StorageInterface.prototype.info,StorageInterface.prototype.commit,StorageInterface.prototype.remove,KeystoreArray.length,KeystoreMap.size,KeystoreSet.size,Charset.Exact,Charset.Default,Charset.Normalize,Charset.LatinBalance,Charset.LatinAdvanced,Charset.LatinExtra,Charset.LatinSoundex,Charset.CJK,Charset.LatinExact,Charset.LatinDefault,Charset.LatinSimple,Charset.ArabicDefault,Charset.CjkDefault,Charset.CyrillicDefault,IndexOptions.preset,IndexOptions.context,IndexOptions.encoder,IndexOptions.encode,IndexOptions.resolution,IndexOptions.tokenize,IndexOptions.fastupdate,IndexOptions.score,IndexOptions.keystore,IndexOptions.rtl,IndexOptions.cache,IndexOptions.resolve,IndexOptions.db,IndexOptions.worker,IndexOptions.config,IndexOptions.priority,IndexOptions.export,IndexOptions.import,FieldOptions.preset,FieldOptions.context,FieldOptions.encoder,FieldOptions.encode,FieldOptions.resolution,FieldOptions.tokenize,FieldOptions.fastupdate,FieldOptions.score,FieldOptions.keystore,FieldOptions.rtl,FieldOptions.cache,FieldOptions.db,FieldOptions.config,FieldOptions.resolve,FieldOptions.field,FieldOptions.filter,FieldOptions.custom,FieldOptions.worker,DocumentOptions.context,DocumentOptions.encoder,DocumentOptions.encode,DocumentOptions.resolution,DocumentOptions.tokenize,DocumentOptions.fastupdate,DocumentOptions.score,DocumentOptions.keystore,DocumentOptions.rtl,DocumentOptions.cache,DocumentOptions.db,DocumentOptions.doc,DocumentOptions.document,DocumentOptions.worker,DocumentOptions.priority,DocumentOptions.export,DocumentOptions.import,ContextOptions.depth,ContextOptions.bidirectional,ContextOptions.resolution,DocumentDescriptor.field,DocumentDescriptor.index,DocumentDescriptor.tag,DocumentDescriptor.store,DocumentDescriptor.id,TagOptions.field,TagOptions.tag,TagOptions.filter,TagOptions.custom,TagOptions.keystore,TagOptions.db,TagOptions.config,StoreOptions.field,StoreOptions.filter,StoreOptions.custom,StoreOptions.config,SearchOptions.query,SearchOptions.limit,SearchOptions.offset,SearchOptions.context,SearchOptions.suggest,SearchOptions.resolve,SearchOptions.enrich,SearchOptions.cache,SearchOptions.resolution,DocumentSearchOptions.query,DocumentSearchOptions.limit,DocumentSearchOptions.offset,DocumentSearchOptions.context,DocumentSearchOptions.suggest,DocumentSearchOptions.resolve,DocumentSearchOptions.enrich,DocumentSearchOptions.cache,DocumentSearchOptions.resolution,DocumentSearchOptions.tag,DocumentSearchOptions.field,DocumentSearchOptions.index,DocumentSearchOptions.pluck,DocumentSearchOptions.merge,DocumentSearchOptions.highlight,EncoderOptions.rtl,EncoderOptions.dedupe,EncoderOptions.split,EncoderOptions.include,EncoderOptions.exclude,EncoderOptions.prepare,EncoderOptions.finalize,EncoderOptions.filter,EncoderOptions.matcher,EncoderOptions.mapper,EncoderOptions.stemmer,EncoderOptions.replacer,EncoderOptions.minlength,EncoderOptions.maxlength,EncoderOptions.cache,EncoderSplitOptions.letter,EncoderSplitOptions.number,EncoderSplitOptions.symbol,EncoderSplitOptions.punctuation,EncoderSplitOptions.control,EncoderSplitOptions.char,PersistentOptions.name,PersistentOptions.field,PersistentOptions.type,PersistentOptions.db,ResolverOptions.index,ResolverOptions.query,ResolverOptions.limit,ResolverOptions.offset,ResolverOptions.enrich,ResolverOptions.resolve,ResolverOptions.suggest,ResolverOptions.and,ResolverOptions.or,ResolverOptions.xor,ResolverOptions.not,ResolverOptions.pluck,ResolverOptions.field,HighlightBoundaryOptions.before,HighlightBoundaryOptions.after,HighlightBoundaryOptions.total,HighlightEllipsisOptions.template,HighlightEllipsisOptions.pattern,HighlightOptions.template,HighlightOptions.boundary,HighlightOptions.ellipsis,HighlightOptions.clip,HighlightOptions.merge;const FlexSearch={Index:Index,Charset:Charset,Encoder:Encoder,Document:Document,Worker:WorkerIndex,Resolver:Resolver,IndexedDB:IdxDB,Language:{}};{const a="undefined"==typeof self?"undefined"==typeof global?self:global:self;let b;(b=a.define)&&b.amd?b([],function(){return FlexSearch}):"object"==typeof a.exports?a.exports=FlexSearch:a.FlexSearch=FlexSearch}export default FlexSearch;export{Index,Document,Encoder,Charset,WorkerIndex as Worker,Resolver,IdxDB as IndexedDB}; \ No newline at end of file +import{SearchOptions,ContextOptions,DocumentDescriptor,DocumentSearchOptions,FieldOptions,IndexOptions,DocumentOptions,TagOptions,StoreOptions,EncoderOptions,EncoderSplitOptions,PersistentOptions,ResolverOptions,HighlightBoundaryOptions,HighlightEllipsisOptions,HighlightOptions}from"./type.js";import StorageInterface from"./db/interface.js";import Document from"./document.js";import Index from"./index.js";import WorkerIndex from"./worker.js";import Resolver from"./resolver.js";import Encoder from"./encoder.js";import IdxDB from"./db/indexeddb/index.js";import Charset from"./charset.js";import{KeystoreMap,KeystoreArray,KeystoreSet}from"./keystore.js";Index.prototype.add,Index.prototype.append,Index.prototype.search,Index.prototype.update,Index.prototype.remove,Index.prototype.contain,Index.prototype.clear,Index.prototype.cleanup,Index.prototype.searchCache,Index.prototype.addAsync,Index.prototype.appendAsync,Index.prototype.searchAsync,Index.prototype.searchCacheAsync,Index.prototype.updateAsync,Index.prototype.removeAsync,Index.prototype.export,Index.prototype.import,Index.prototype.serialize,Index.prototype.mount,Index.prototype.commit,Index.prototype.destroy,Index.prototype.encoder,Index.prototype.reg,Index.prototype.map,Index.prototype.ctx,Index.prototype.db,Index.prototype.tag,Index.prototype.store,Index.prototype.depth,Index.prototype.bidirectional,Index.prototype.commit_task,Index.prototype.commit_timer,Index.prototype.cache,Index.prototype.bypass,Index.prototype.document,Encoder.prototype.assign,Encoder.prototype.encode,Encoder.prototype.addMatcher,Encoder.prototype.addStemmer,Encoder.prototype.addFilter,Encoder.prototype.addMapper,Encoder.prototype.addReplacer,Document.prototype.add,Document.prototype.append,Document.prototype.search,Document.prototype.update,Document.prototype.remove,Document.prototype.contain,Document.prototype.clear,Document.prototype.cleanup,Document.prototype.addAsync,Document.prototype.appendAsync,Document.prototype.updateAsync,Document.prototype.removeAsync,Document.prototype.searchAsync,Document.prototype.searchCacheAsync,Document.prototype.searchCache,Document.prototype.mount,Document.prototype.commit,Document.prototype.destroy,Document.prototype.export,Document.prototype.import,Document.prototype.get,Document.prototype.set,Document.prototype.field,Document.prototype.index,Document.prototype.reg,Document.prototype.tag,Document.prototype.store,Document.prototype.fastupdate,Resolver.prototype.limit,Resolver.prototype.offset,Resolver.prototype.boost,Resolver.prototype.resolve,Resolver.prototype.or,Resolver.prototype.and,Resolver.prototype.xor,Resolver.prototype.not,Resolver.prototype.result,Resolver.prototype.await,StorageInterface.db,StorageInterface.id,StorageInterface.support_tag_search,StorageInterface.fastupdate,StorageInterface.prototype.mount,StorageInterface.prototype.open,StorageInterface.prototype.close,StorageInterface.prototype.destroy,StorageInterface.prototype.clear,StorageInterface.prototype.get,StorageInterface.prototype.tag,StorageInterface.prototype.enrich,StorageInterface.prototype.has,StorageInterface.prototype.search,StorageInterface.prototype.info,StorageInterface.prototype.commit,StorageInterface.prototype.remove,KeystoreArray.length,KeystoreMap.size,KeystoreSet.size,Charset.Exact,Charset.Default,Charset.Normalize,Charset.LatinBalance,Charset.LatinAdvanced,Charset.LatinExtra,Charset.LatinSoundex,Charset.CJK,Charset.LatinExact,Charset.LatinDefault,Charset.LatinSimple,Charset.ArabicDefault,Charset.CjkDefault,Charset.CyrillicDefault,IndexOptions.preset,IndexOptions.context,IndexOptions.encoder,IndexOptions.encode,IndexOptions.resolution,IndexOptions.tokenize,IndexOptions.fastupdate,IndexOptions.score,IndexOptions.keystore,IndexOptions.rtl,IndexOptions.cache,IndexOptions.resolve,IndexOptions.db,IndexOptions.worker,IndexOptions.config,IndexOptions.priority,IndexOptions.export,IndexOptions.import,FieldOptions.preset,FieldOptions.context,FieldOptions.encoder,FieldOptions.encode,FieldOptions.resolution,FieldOptions.tokenize,FieldOptions.fastupdate,FieldOptions.score,FieldOptions.keystore,FieldOptions.rtl,FieldOptions.cache,FieldOptions.db,FieldOptions.config,FieldOptions.resolve,FieldOptions.field,FieldOptions.filter,FieldOptions.custom,FieldOptions.worker,DocumentOptions.context,DocumentOptions.encoder,DocumentOptions.encode,DocumentOptions.resolution,DocumentOptions.tokenize,DocumentOptions.fastupdate,DocumentOptions.score,DocumentOptions.keystore,DocumentOptions.rtl,DocumentOptions.cache,DocumentOptions.db,DocumentOptions.doc,DocumentOptions.document,DocumentOptions.worker,DocumentOptions.priority,DocumentOptions.export,DocumentOptions.import,ContextOptions.depth,ContextOptions.bidirectional,ContextOptions.resolution,DocumentDescriptor.field,DocumentDescriptor.index,DocumentDescriptor.tag,DocumentDescriptor.store,DocumentDescriptor.id,TagOptions.field,TagOptions.tag,TagOptions.filter,TagOptions.custom,TagOptions.keystore,TagOptions.db,TagOptions.config,StoreOptions.field,StoreOptions.filter,StoreOptions.custom,StoreOptions.config,SearchOptions.query,SearchOptions.limit,SearchOptions.offset,SearchOptions.context,SearchOptions.suggest,SearchOptions.resolve,SearchOptions.cache,SearchOptions.resolution,DocumentSearchOptions.query,DocumentSearchOptions.limit,DocumentSearchOptions.offset,DocumentSearchOptions.context,DocumentSearchOptions.suggest,DocumentSearchOptions.resolve,DocumentSearchOptions.enrich,DocumentSearchOptions.cache,DocumentSearchOptions.resolution,DocumentSearchOptions.tag,DocumentSearchOptions.field,DocumentSearchOptions.index,DocumentSearchOptions.pluck,DocumentSearchOptions.merge,DocumentSearchOptions.highlight,EncoderOptions.rtl,EncoderOptions.dedupe,EncoderOptions.split,EncoderOptions.include,EncoderOptions.exclude,EncoderOptions.prepare,EncoderOptions.finalize,EncoderOptions.filter,EncoderOptions.matcher,EncoderOptions.mapper,EncoderOptions.stemmer,EncoderOptions.replacer,EncoderOptions.minlength,EncoderOptions.maxlength,EncoderOptions.cache,EncoderSplitOptions.letter,EncoderSplitOptions.number,EncoderSplitOptions.symbol,EncoderSplitOptions.punctuation,EncoderSplitOptions.control,EncoderSplitOptions.char,PersistentOptions.name,PersistentOptions.field,PersistentOptions.type,PersistentOptions.db,ResolverOptions.index,ResolverOptions.query,ResolverOptions.limit,ResolverOptions.offset,ResolverOptions.boost,ResolverOptions.enrich,ResolverOptions.resolve,ResolverOptions.suggest,ResolverOptions.cache,ResolverOptions.async,ResolverOptions.queue,ResolverOptions.and,ResolverOptions.or,ResolverOptions.xor,ResolverOptions.not,ResolverOptions.pluck,ResolverOptions.field,HighlightBoundaryOptions.before,HighlightBoundaryOptions.after,HighlightBoundaryOptions.total,HighlightEllipsisOptions.template,HighlightEllipsisOptions.pattern,HighlightOptions.template,HighlightOptions.boundary,HighlightOptions.ellipsis,HighlightOptions.clip,HighlightOptions.merge;const FlexSearch={Index:Index,Charset:Charset,Encoder:Encoder,Document:Document,Worker:WorkerIndex,Resolver:Resolver,IndexedDB:IdxDB,Language:{}};{const a="undefined"==typeof self?"undefined"==typeof global?self:global:self;let b;(b=a.define)&&b.amd?b([],function(){return FlexSearch}):"object"==typeof a.exports?a.exports=FlexSearch:a.FlexSearch=FlexSearch}export default FlexSearch;export{Index,Document,Encoder,Charset,WorkerIndex as Worker,Resolver,IdxDB as IndexedDB}; \ No newline at end of file diff --git a/dist/module-min/cache.js b/dist/module-min/cache.js index 801af41..822927d 100644 --- a/dist/module-min/cache.js +++ b/dist/module-min/cache.js @@ -1 +1 @@ -import Index from"./index.js";import Document from"./document.js";import{SearchOptions,DocumentSearchOptions}from"./type.js";export function searchCache(a,b,c){const d=(b?""+a:"object"==typeof a?""+a.query:a).toLowerCase();this.cache||(this.cache=new CacheClass);let e=this.cache.get(d);if(!e){if(e=this.search(a,b,c),e.then){const a=this;e.then(function(b){return a.cache.set(d,b),b})}this.cache.set(d,e)}return e}export default function CacheClass(a){this.limit=a&&!0!==a?a:1000,this.cache=new Map,this.last=""}CacheClass.prototype.set=function(a,b){this.cache.set(this.last=a,b),this.cache.size>this.limit&&this.cache.delete(this.cache.keys().next().value)},CacheClass.prototype.get=function(a){const b=this.cache.get(a);return b&&this.last!==a&&(this.cache.delete(a),this.cache.set(this.last=a,b)),b},CacheClass.prototype.remove=function(a){for(const b of this.cache){const c=b[0],d=b[1];d.includes(a)&&this.cache.delete(c)}},CacheClass.prototype.clear=function(){this.cache.clear(),this.last=""}; \ No newline at end of file +import Index from"./index.js";import{SearchOptions,DocumentSearchOptions}from"./type.js";export function searchCache(a,b,c){c||(b||"object"!=typeof a?"object"==typeof b&&(c=b,b=0):c=a),c&&(a=c.query||a,b=c.limit||b);let d=""+(b||0);if(c){const{context:a,suggest:b,offset:e,resolve:f,boost:g,resolution:h}=c;d+=(e||0)+!!a+!!b+(!1!==f)+(h||this.resolution)+(g||0)}a=(""+a).toLowerCase(),this.cache||(this.cache=new CacheClass);let e=this.cache.get(a+d);if(!e){const f=c&&c.cache;f&&(c.cache=!1),e=this.search(a,b,c),f&&(c.cache=f),this.cache.set(a+d,e)}return e}export default function CacheClass(a){this.limit=a&&!0!==a?a:1000,this.cache=new Map,this.last=""}CacheClass.prototype.set=function(a,b){this.cache.set(this.last=a,b),this.cache.size>this.limit&&this.cache.delete(this.cache.keys().next().value)},CacheClass.prototype.get=function(a){const b=this.cache.get(a);return b&&this.last!==a&&(this.cache.delete(a),this.cache.set(this.last=a,b)),b},CacheClass.prototype.remove=function(a){for(const b of this.cache){const c=b[0],d=b[1];d.includes(a)&&this.cache.delete(c)}},CacheClass.prototype.clear=function(){this.cache.clear(),this.last=""}; \ No newline at end of file diff --git a/dist/module-min/db/indexeddb/index.js b/dist/module-min/db/indexeddb/index.js index fe439f4..f9dc209 100644 --- a/dist/module-min/db/indexeddb/index.js +++ b/dist/module-min/db/indexeddb/index.js @@ -1 +1 @@ -import{PersistentOptions,SearchResults,EnrichedSearchResults}from"../../type.js";const VERSION=1,IndexedDB="undefined"!=typeof window&&(window.indexedDB||window.mozIndexedDB||window.webkitIndexedDB||window.msIndexedDB),IDBTransaction="undefined"!=typeof window&&(window.IDBTransaction||window.webkitIDBTransaction||window.msIDBTransaction),IDBKeyRange="undefined"!=typeof window&&(window.IDBKeyRange||window.webkitIDBKeyRange||window.msIDBKeyRange),fields=["map","ctx","tag","reg","cfg"];import StorageInterface from"../interface.js";import{create_object,toArray}from"../../common.js";function sanitize(a){return a.toLowerCase().replace(/[^a-z0-9_\-]/g,"")}const Index=create_object();export default function IdxDB(a,b={}){return this&&this.constructor===IdxDB?void("object"==typeof a&&(b=a,a=a.name),!a&&console.info("Default storage space was used, because a name was not passed."),this.id="flexsearch"+(a?":"+sanitize(a):""),this.field=b.field?sanitize(b.field):"",this.type=b.type,this.support_tag_search=!1,this.fastupdate=!1,this.db=null,this.trx={}):new IdxDB(a,b)}IdxDB.prototype.mount=function(a){return a.index?a.mount(this):(a.db=this,this.open())},IdxDB.prototype.open=function(){if(this.db)return this.db;let a=this;navigator.storage&&navigator.storage.persist(),Index[a.id]||(Index[a.id]=[]),Index[a.id].push(a.field);const b=IndexedDB.open(a.id,VERSION);return b.onupgradeneeded=function(){const b=a.db=this.result;for(let c,d=0;d=e.length){d-=e.length;continue}const a=c?d+Math.min(e.length-d,c):e.length;for(let c=d;c=a.length)return[];if(!b&&!c)return a;const e=a.slice(c,c+b);return d?h.enrich(e):e})},IdxDB.prototype.enrich=function(a){"object"!=typeof a&&(a=[a]);const b=this.db.transaction("reg","readonly"),c=b.objectStore("reg"),d=[];for(let b=0;b{a.onsuccess=a.oncomplete=function(){b&&b(this.result),b=null,c(this.result)},a.onerror=a.onblocked=d,a=null})} \ No newline at end of file +const VERSION=1,IndexedDB="undefined"!=typeof window&&(window.indexedDB||window.mozIndexedDB||window.webkitIndexedDB||window.msIndexedDB),IDBTransaction="undefined"!=typeof window&&(window.IDBTransaction||window.webkitIDBTransaction||window.msIDBTransaction),IDBKeyRange="undefined"!=typeof window&&(window.IDBKeyRange||window.webkitIDBKeyRange||window.msIDBKeyRange),fields=["map","ctx","tag","reg","cfg"];import StorageInterface from"../interface.js";import{create_object,toArray}from"../../common.js";function sanitize(a){return a.toLowerCase().replace(/[^a-z0-9_\-]/g,"")}const Index=create_object();export default function IdxDB(a,b={}){return this&&this.constructor===IdxDB?void("object"==typeof a&&(b=a,a=a.name),!a&&console.info("Default storage space was used, because a name was not passed."),this.id="flexsearch"+(a?":"+sanitize(a):""),this.field=b.field?sanitize(b.field):"",this.type=b.type,this.support_tag_search=!1,this.fastupdate=!1,this.db=null,this.trx={}):new IdxDB(a,b)}IdxDB.prototype.mount=function(a){return a.index?a.mount(this):(a.db=this,this.open())},IdxDB.prototype.open=function(){if(this.db)return this.db;let a=this;navigator.storage&&navigator.storage.persist(),Index[a.id]||(Index[a.id]=[]),Index[a.id].push(a.field);const b=IndexedDB.open(a.id,VERSION);return b.onupgradeneeded=function(){const b=a.db=this.result;for(let c,d=0;d=e.length){d-=e.length;continue}const a=c?d+Math.min(e.length-d,c):e.length;for(let c=d;c=a.length)return[];if(!b&&!c)return a;const e=a.slice(c,c+b);return d?h.enrich(e):e})},IdxDB.prototype.enrich=function(a){"object"!=typeof a&&(a=[a]);const b=this.db.transaction("reg","readonly"),c=b.objectStore("reg"),d=[];for(let b=0;b{a.onsuccess=a.oncomplete=function(){b&&b(this.result),b=null,c(this.result)},a.onerror=a.onblocked=d,a=null})} \ No newline at end of file diff --git a/dist/module-min/db/mongodb/index.js b/dist/module-min/db/mongodb/index.js index 9f343dd..1bb8b30 100644 --- a/dist/module-min/db/mongodb/index.js +++ b/dist/module-min/db/mongodb/index.js @@ -1 +1 @@ -import{MongoClient}from"mongodb";const defaults={host:"localhost",port:"27017",user:null,pass:null},VERSION=1,fields=["map","ctx","tag","reg","cfg"];import StorageInterface from"../interface.js";import{toArray}from"../../common.js";function sanitize(a){return a.toLowerCase().replace(/[^a-z0-9_\-]/g,"")}let CLIENT,Index=Object.create(null);export default function MongoDB(a,b={}){return this&&this.constructor===MongoDB?void("object"==typeof a&&(b=a,a=a.name),!a&&console.info("Default storage space was used, because a name was not passed."),this.id="flexsearch"+(a?"-"+sanitize(a):""),this.field=b.field?"-"+sanitize(b.field):"",this.type=b.type||"",this.db=b.db||Index[this.id]||CLIENT||null,this.trx=!1,this.support_tag_search=!0,Object.assign(defaults,b),this.db&&delete defaults.db):new MongoDB(a,b)}MongoDB.prototype.mount=function(a){return a.index?a.mount(this):(a.db=this,this.open())};async function createCollection(a,b,c){"map"===b?(await a.createCollection("map"+c),await a.collection("map"+c).createIndex({key:1}),await a.collection("map"+c).createIndex({id:1})):"ctx"===b?(await a.createCollection("ctx"+c),await a.collection("ctx"+c).createIndex({ctx:1,key:1}),await a.collection("ctx"+c).createIndex({id:1})):"tag"===b?(await a.createCollection("tag"+c),await a.collection("tag"+c).createIndex({tag:1}),await a.collection("tag"+c).createIndex({id:1})):"reg"===b?(await a.createCollection("reg"),await a.collection("reg").createIndex({id:1})):"cfg"===b?await a.createCollection("cfg"+c):void 0}MongoDB.prototype.open=async function(){if(!this.db&&!(this.db=Index[this.id])&&!(this.db=CLIENT)){let a=defaults.url;a||(a=defaults.user?`mongodb://${defaults.user}:${defaults.pass}@${defaults.host}:${defaults.port}`:`mongodb://${defaults.host}:${defaults.port}`),this.db=CLIENT=new MongoClient(a),await this.db.connect()}this.db.db&&(this.db=Index[this.id]=this.db.db(this.id));const a=await this.db.listCollections().toArray();for(let b,c=0;cl;k.push({ctx:d?j:l,key:d?l:j}),l=j}const m={_id:1};f||(m.res=1),g&&(m.doc=1);const n=[{$match:{$or:k}},{$group:{_id:"$id",count:{$sum:1},res:e?{$sum:"$res"}:{$sum:"$res"}}}];if(e||n.push({$match:{count:b.length-1}}),g&&(m.doc="$doc.doc",n.push({$lookup:{from:"reg",localField:"_id",foreignField:"id",as:"doc"}},{$unwind:{path:"$doc",preserveNullAndEmptyArrays:!0}})),h){const a={};for(let b=0,c=1;bl;k.push({ctx:d?j:l,key:d?l:j}),l=j}const m={_id:1};f||(m.res=1),g&&(m.doc=1);const n=[{$match:{$or:k}},{$group:{_id:"$id",count:{$sum:1},res:e?{$sum:"$res"}:{$sum:"$res"}}}];if(e||n.push({$match:{count:b.length-1}}),g&&(m.doc="$doc.doc",n.push({$lookup:{from:"reg",localField:"_id",foreignField:"id",as:"doc"}},{$unwind:{path:"$doc",preserveNullAndEmptyArrays:!0}})),h){const a={};for(let b=0,c=1;bconsole.error(a)).connect()},RedisDB.prototype.close=async function(){return DB&&(await this.db.disconnect()),this.db=DB=null,this},RedisDB.prototype.destroy=function(){return this.clear()},RedisDB.prototype.clear=function(){return this.db.unlink([this.id+"map"+this.field,this.id+"ctx"+this.field,this.id+"tag"+this.field,this.id+"cfg"+this.field,this.id+"doc",this.id+"reg"])};function create_result(a,b,c,d,e){if(c){if("number"===b)for(let c,e,f=0;f=a.length)return[];if(!b&&!c)return a;const f=a.slice(c,c+b);return d?e.enrich(f):f})},RedisDB.prototype.enrich=function(a){return"object"!=typeof a&&(a=[a]),this.db.hmGet(this.id+"doc","number"===this.type?a.map(a=>""+a):a).then(function(b){for(let c=0;ce,k.push(c+(f?d:e)+":"+(f?e:d)),l.push(1),e=d}else{const a=this.id+"map"+this.field+":";for(let c=0;cconsole.error(a)).connect()},RedisDB.prototype.close=async function(){return DB&&(await this.db.disconnect()),this.db=DB=null,this},RedisDB.prototype.destroy=function(){return this.clear()},RedisDB.prototype.clear=function(){return this.db.unlink([this.id+"map"+this.field,this.id+"ctx"+this.field,this.id+"tag"+this.field,this.id+"cfg"+this.field,this.id+"doc",this.id+"reg"])};function create_result(a,b,c,d,e){if(c){for(let c,e,f=0;f=a.length)return[];if(!b&&!c)return a;const f=a.slice(c,c+b);return d?e.enrich(f):f})},RedisDB.prototype.enrich=function(a){return"object"!=typeof a&&(a=[a]),this.db.hmGet(this.id+"doc","number"===this.type?a.map(a=>""+a):a).then(function(b){for(let c=0;ce,k.push(c+(f?d:e)+":"+(f?e:d)),l.push(1),e=d}else{const a=this.id+"map"+this.field+":";for(let c=0;cc||d)&&(f=f.slice(d,d+c)),e&&(f=apply_enrich.call(this,f))),f}export function apply_enrich(a){if(!this||!this.store)return a;const b=Array(a.length);for(let c,d=0;dc||d)&&(f=f.slice(d,d+c)),e&&(f=apply_enrich.call(this,f))),f}export function apply_enrich(a){if(!this||!this.store)return a;if(this.db)return this.index.get(this.field[0]).db.enrich(a);const b=Array(a.length);for(let c,d=0;dc||d)&&(k=k.slice(d,c+d));else{const a=[];for(let b,e=0;ed){d-=b.length;continue}if((c&&b.length>c||d)&&(b=b.slice(d,c+d),c-=b.length,d&&(d-=b.length)),a.push(b),!c)break}k=a}}else k=1c||d?k.slice(d,c+d):k;return k}export function union(a,b,c,d,e){const f=[],g=create_object();let h,l,m,n=a.length;if(!d)for(let d,j=n-1,i=0;0<=j;j--){d=a[j];for(let a=0;ac||d)&&(k=k.slice(d,c+d));else{const a=[];for(let b,e=0;ed){d-=b.length;continue}if((c&&b.length>c||d)&&(b=b.slice(d,c+d),c-=b.length,d&&(d-=b.length)),a.push(b),!c)break}k=a}}else k=1c||d?k.slice(d,c+d):k;return k}export function union(a,b,c,d,e){const f=[],g=create_object();let h,l,m,n=a.length;if(!d)for(let d,j=n-1,i=0;0<=j;j--){d=a[j];for(let a=0;aa.length)this.result=a[0];else{const b=get_max_len(a);if(!b)this.result=[];else return this.result=intersect(a,b,c,d,g,this.boostval,f),f?e?apply_enrich.call(this.index,this.result):this.result:this}return f?this.resolve(c,d,e):this} \ No newline at end of file +import Resolver from"../resolver.js";import{get_max_len}from"../common.js";import{intersect}from"../intersect.js";import{SearchResults,EnrichedSearchResults,IntermediateSearchResults}from"../type.js";Resolver.prototype.and=function(){return this.handler("and",return_result,arguments)};function return_result(a,b,c,d,e,f){if(!f&&!this.result.length)return e?this.result:this;let g;if(!a.length)f||(this.result=a);else if(this.result.length&&a.unshift(this.result),2>a.length)this.result=a[0];else{let d=0;for(let b,c,e=0;eb?b?e.slice(c,c+b):e.slice(c):e,d?apply_enrich.call(this,e):e}let e=[];for(let f,g,h=0;h=g){c-=g;continue}cb&&(f=f.slice(0,b),g=b),!e.length&&g>=b)return d?apply_enrich.call(this,f):f;if(e.push(f),b-=g,!b)break}return e=1b?e.slice(c,c+b):e,d?apply_enrich.call(this,e):e}let e=[];for(let f,g,h=0;h=g){c-=g;continue}f=f.slice(c,c+b),g=f.length,c=0}if(g>b&&(f=f.slice(0,b),g=b),!e.length&&g>=b)return d?apply_enrich.call(this,f):f;if(e.push(f),b-=g,!b)break}return e=1a.length?this.result=a[0]:(this.result=union(a,c,d,!1,this.boostval),d=0)),f?this.resolve(c,d,e):this} \ No newline at end of file +import Resolver from"../resolver.js";import{union}from"../intersect.js";import{SearchResults,EnrichedSearchResults,IntermediateSearchResults}from"../type.js";Resolver.prototype.or=function(){return this.handler("or",return_result,arguments)};function return_result(a,b,c,d,e){return a.length&&(this.result.length&&a.push(this.result),2>a.length?this.result=a[0]:(this.result=union(a,b,c,!1,this.boostval),c=0)),e&&(this.await=null),e?this.resolve(b,c,d):this} \ No newline at end of file diff --git a/dist/module-min/resolve/xor.js b/dist/module-min/resolve/xor.js index bc29c14..2545057 100644 --- a/dist/module-min/resolve/xor.js +++ b/dist/module-min/resolve/xor.js @@ -1 +1 @@ -import Resolver from"../resolver.js";import default_resolver from"./default.js";import{create_object}from"../common.js";import{SearchResults,EnrichedSearchResults,IntermediateSearchResults}from"../type.js";import{apply_enrich}from"../document/search.js";Resolver.prototype.xor=function(){const{final:a,promises:b,limit:c,offset:d,enrich:e,resolve:f,suggest:g}=this.handler("xor",arguments);return return_result.call(this,a,b,c,d,e,f,g)};function return_result(a,b,c,d,e,f,g){if(b.length){const h=this;return Promise.all(b).then(function(b){a=[];for(let c,d=0;da.length)this.result=a[0];else return this.result=exclusive.call(this,a,c,d,f,this.boostval),f?e?apply_enrich.call(this.index,this.result):this.result:this;return f?this.resolve(c,d,e):this}function exclusive(a,b,c,d,e){const f=[],g=create_object();let h=0;for(let f,j=0;ja.length?this.result=a[0]:(this.result=exclusive.call(this,a,b,c,e,this.boostval),g=!0)):!f&&(this.result=a),e&&(this.await=null),e?this.resolve(b,c,d,g):this}function exclusive(a,b,c,d,e){const f=[],g=create_object();let h=0;for(let f,j=0;j|Promise} */ +export function searchCache(query, limit, options) { -export function searchCache(query_or_options, limit_or_options, options) { + if (!options) { + if (!limit && "object" == typeof query) { + options = query; + } else if ("object" == typeof limit) { + options = limit; + limit = 0; + } + } - const query = (limit_or_options ? "" + query_or_options : "object" == typeof query_or_options ? "" + query_or_options.query : query_or_options).toLowerCase(); + if (options) { + query = options.query || query; + limit = options.limit || limit; + } + + let key = "" + (limit || 0); + + if (options) { + + const { + context, + suggest, + offset, + resolve, + boost, + resolution + } = options; + + key += (offset || 0) + !!context + !!suggest + (!1 !== resolve) + (resolution || this.resolution) + (boost || 0); + } + + query = ("" + query).toLowerCase(); if (!this.cache) { this.cache = new CacheClass(); } - let cache = this.cache.get(query); + let cache = this.cache.get(query + key); if (!cache) { - cache = this.search(query_or_options, limit_or_options, options); + const opt_cache = options && options.cache; + opt_cache && (options.cache = !1); + cache = this.search(query, limit, options); + opt_cache && (options.cache = opt_cache); - if (cache.then) { - const self = this; - cache.then(function (cache) { - self.cache.set(query, cache); - return cache; - }); - } - - this.cache.set(query, cache); + this.cache.set(query + key, cache); } return cache; } diff --git a/dist/module/db/indexeddb/index.js b/dist/module/db/indexeddb/index.js index 23e2493..f688473 100644 --- a/dist/module/db/indexeddb/index.js +++ b/dist/module/db/indexeddb/index.js @@ -1,6 +1,3 @@ - -import { PersistentOptions, SearchResults, EnrichedSearchResults } from "../../type.js"; - const VERSION = 1, IndexedDB = "undefined" != typeof window && (window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB), IDBTransaction = "undefined" != typeof window && (window.IDBTransaction || window.webkitIDBTransaction || window.msIDBTransaction), diff --git a/dist/module/db/mongodb/index.js b/dist/module/db/mongodb/index.js index 79b65f9..ec05a19 100644 --- a/dist/module/db/mongodb/index.js +++ b/dist/module/db/mongodb/index.js @@ -140,17 +140,25 @@ MongoDB.prototype.clear = function () { }; function create_result(rows, resolve, enrich) { + const _id = rows[0] && "undefined" != typeof rows[0]._id; if (resolve) { - if (!enrich) for (let i = 0; i < rows.length; i++) { - rows[i] = rows[i].id; + if (!enrich || _id) for (let i = 0, row; i < rows.length; i++) { + row = rows[i]; + if (enrich) { + const id = row._id; + delete row._id; + row.id = id; + } else { + rows[i] = _id ? row._id : row.id; + } } return rows; } else { const arr = []; - for (let i = 0, row; i < rows.length; i++) { + for (let i = 0, row, res; i < rows.length; i++) { row = rows[i]; - arr[row.res] || (arr[row.res] = []); - arr[row.res].push(enrich ? row : row.id); + res = row.res; + (arr[res] || (arr[res] = [])).push(_id ? row._id : row.id); } return arr; } @@ -160,8 +168,17 @@ MongoDB.prototype.get = async function (key, ctx, limit = 0, offset = 0, resolve let rows, params = ctx ? { ctx, key } : { key }; + if (!enrich && !tags) { - rows = await this.db.collection((ctx ? "ctx" : "map") + this.field).find(params, { projection: { _id: 0, res: 1, id: 1 }, limit, skip: offset }).toArray(); + + const stmt = { + projection: { _id: 0, res: 1, id: 1 } + }; + + limit && (stmt.limit = limit); + offset && (stmt.skip = offset); + + rows = await this.db.collection((ctx ? "ctx" : "map") + this.field).find(params, stmt).toArray(); } else { const project = { _id: 0, id: 1 }, stmt = [{ $match: params }]; @@ -204,7 +221,10 @@ MongoDB.prototype.get = async function (key, ctx, limit = 0, offset = 0, resolve stmt.push({ $project: project }); } - stmt.push({ $sort: { res: 1 } }, { $skip: offset }, { $limit: limit }); + stmt.push({ $sort: { res: 1 } }); + + limit && stmt.push({ $limit: limit }); + offset && stmt.push({ $skip: offset }); rows = []; const result = await this.db.collection((ctx ? "ctx" : "map") + this.field).aggregate(stmt); @@ -220,14 +240,26 @@ MongoDB.prototype.get = async function (key, ctx, limit = 0, offset = 0, resolve MongoDB.prototype.tag = async function (tag, limit = 0, offset = 0, enrich = !1) { + let rows; + if (!enrich) { - const rows = await this.db.collection("tag" + this.field).find({ tag }, { projection: { _id: 0, id: 1 }, limit, skip: offset }).toArray(); - return create_result(rows, !0, !1); + const stmt = { + projection: { _id: 0, id: 1 } + }; + + limit && (stmt.limit = limit); + offset && (stmt.skip = offset); + + rows = await this.db.collection("tag" + this.field).find({ tag }, stmt).toArray(); } else { - let rows = []; - const result = await this.db.collection("tag" + this.field).aggregate([{ $match: { tag } }, { $skip: offset }, { $limit: limit }, { $lookup: { + const stmt = [{ $match: { tag } }]; + + limit && stmt.push({ $limit: limit }); + offset && stmt.push({ $skip: offset }); + + stmt.push({ $lookup: { from: "reg", localField: "id", foreignField: "id", @@ -235,15 +267,18 @@ MongoDB.prototype.tag = async function (tag, limit = 0, offset = 0, enrich = !1) } }, { $project: { _id: 0, id: 1, doc: "$doc.doc" } }, { $unwind: { path: "$doc", preserveNullAndEmptyArrays: !0 - } }]); + } }); + + rows = []; + const result = await this.db.collection("tag" + this.field).aggregate(stmt); while (!0) { const row = await result.next(); if (row) rows.push(row);else break; } - - return rows; } + + create_result(rows, !0, enrich); }; MongoDB.prototype.enrich = function (ids) { @@ -323,8 +358,9 @@ MongoDB.prototype.search = async function (flexsearch, query, limit = 100, offse stmt.push({ $match: match }); } - stmt.push({ $sort: suggest ? { count: -1, res: 1 } : { res: 1 } }, { $skip: offset }, { $limit: limit }); - + stmt.push({ $sort: suggest ? { count: -1, res: 1 } : { res: 1 } }); + limit && stmt.push({ $limit: limit }); + offset && stmt.push({ $skip: offset }); stmt.push({ $project: project }); rows = await this.db.collection("ctx" + this.field).aggregate(stmt); @@ -375,8 +411,10 @@ MongoDB.prototype.search = async function (flexsearch, query, limit = 100, offse stmt.push({ $match: match }); } - stmt.push({ $sort: suggest ? { count: -1, res: 1 } : { res: 1 } }, { $skip: offset }, { $limit: limit }); - + stmt.push({ $sort: suggest ? { count: -1, res: 1 } : { res: 1 } + }); + limit && stmt.push({ $limit: limit }); + offset && stmt.push({ $skip: offset }); stmt.push({ $project: project }); rows = await this.db.collection("map" + this.field).aggregate(stmt); @@ -385,21 +423,15 @@ MongoDB.prototype.search = async function (flexsearch, query, limit = 100, offse while (!0) { const row = await rows.next(); if (row) { - if (resolve && !enrich) { - result.push(row._id); - } else { + if (resolve && enrich) { row.id = row._id; delete row._id; - result.push(row); } + result.push(row); } else break; } - if (resolve && !enrich) { - return result; - } else { - return create_result(result, resolve, enrich); - } + return create_result(result, resolve, enrich); }; MongoDB.prototype.info = function () {}; diff --git a/dist/module/db/redis/index.js b/dist/module/db/redis/index.js index ba09779..e34ae3a 100644 --- a/dist/module/db/redis/index.js +++ b/dist/module/db/redis/index.js @@ -88,20 +88,18 @@ RedisDB.prototype.clear = function () { function create_result(range, type, resolve, enrich, resolution) { if (resolve) { - if ("number" === type) { - for (let i = 0, tmp, id; i < range.length; i++) { - tmp = range[i]; - id = "number" === type ? parseInt(tmp.id || tmp, 10) : tmp.id || tmp; - range[i] = enrich ? { id, doc: tmp.doc } : id; - } + for (let i = 0, tmp, id; i < range.length; i++) { + tmp = range[i]; + id = "number" === type ? parseInt(tmp.value || tmp, 10) : tmp.value || tmp; + range[i] = enrich ? { id, doc: tmp.doc } : id; } return range; } else { let result = []; for (let i = 0, tmp, id, score; i < range.length; i++) { tmp = range[i]; - id = "number" === type ? parseInt(tmp.id || tmp, 10) : tmp.id || tmp; - score = resolution - tmp.score; + id = "number" === type ? parseInt(tmp.value || tmp, 10) : tmp.value || tmp; + score = Math.max(resolution - tmp.score, 0); result[score] || (result[score] = []); result[score].push(id); } diff --git a/dist/module/document.js b/dist/module/document.js index 0176e91..3b7f8ab 100644 --- a/dist/module/document.js +++ b/dist/module/document.js @@ -468,7 +468,6 @@ Document.prototype.set = function (id, data) { return this; }; - Document.prototype.searchCache = searchCache; diff --git a/dist/module/document/search.js b/dist/module/document/search.js index b66ca6f..01ac810 100644 --- a/dist/module/document/search.js +++ b/dist/module/document/search.js @@ -4,6 +4,7 @@ import { create_object, is_array, is_object, is_string, inherit } from "../commo import { intersect, intersect_union } from "../intersect.js"; import Document from "../document.js"; import Index from "../index.js"; +import WorkerIndex from "../worker.js"; import Resolver from "../resolver.js"; import tick from "../profiler.js"; import { highlight_fields } from "./highlight.js"; @@ -32,7 +33,6 @@ import { highlight_fields } from "./highlight.js"; * > * } */ - Document.prototype.search = function (query, limit, options, _promises) { if (!options) { @@ -45,13 +45,6 @@ Document.prototype.search = function (query, limit, options, _promises) { } } - if (options && options.cache) { - options.cache = !1; - const res = this.searchCache(query, limit, options); - options.cache = !0; - return res; - } - /** @type { * DocumentSearchResults| * EnrichedDocumentSearchResults| @@ -67,6 +60,7 @@ Document.prototype.search = function (query, limit, options, _promises) { merge, suggest, boost, + cache, field, tag, offset, @@ -91,6 +85,7 @@ Document.prototype.search = function (query, limit, options, _promises) { tag = this.tag && options.tag; suggest = options.suggest; resolve = !1 !== options.resolve; + cache = options.cache; highlight = resolve && this.store && options.highlight; enrich = !!highlight || resolve && this.store && options.enrich; @@ -226,13 +221,15 @@ Document.prototype.search = function (query, limit, options, _promises) { suggest = inherit(field_options.suggest, suggest); highlight = resolve && this.store && inherit(field_options.highlight, highlight); enrich = !!highlight || resolve && this.store && inherit(field_options.enrich, enrich); + cache = inherit(field_options.cache, cache); } if (_promises) { res = _promises[i]; } else { - let opt = field_options || options, - index = this.index.get(key); + const opt = field_options || options || {}, + opt_enrich = opt.enrich, + index = this.index.get(key); if (tag) { @@ -241,24 +238,24 @@ Document.prototype.search = function (query, limit, options, _promises) { db_tag_search = index.db.support_tag_search; opt.field = field; } - if (!db_tag_search) { + if (!db_tag_search && opt_enrich) { opt.enrich = !1; } } - if (promises) { - promises[i] = index.search(query, limit, opt); - opt && enrich && (opt.enrich = enrich); + res = cache ? index.searchCache(query, limit, opt) : index.search(query, limit, opt); + + opt_enrich && (opt.enrich = opt_enrich); + + if (promises) { + promises[i] = res; continue; - } else { - res = index.search(query, limit, opt); - - opt && enrich && (opt.enrich = enrich); } } - len = res && (resolve ? res.length : res.result.length); + res = res.result || res; + len = res && res.length; if (tag && len) { @@ -353,7 +350,12 @@ Document.prototype.search = function (query, limit, options, _promises) { const self = this; return Promise.all(promises).then(function (result) { - return result.length ? self.search(query, limit, options, result) : result; + + options && (options.resolve = resolve); + if (result.length) { + result = self.search(query, limit, options, result); + } + return result; }); } @@ -361,10 +363,8 @@ Document.prototype.search = function (query, limit, options, _promises) { return resolve ? result : new Resolver(result, this); } if (pluck && (!enrich || !this.store)) { - result = result[0]; - if (!resolve) result.index = this; - return (/** @type {SearchResults|Resolver} */result - ); + result = /** @type {SearchResults|IntermediateSearchResults} */result[0]; + return resolve ? result : new Resolver(result, this); } promises = []; @@ -378,7 +378,7 @@ Document.prototype.search = function (query, limit, options, _promises) { if (enrich && res.length && "undefined" == typeof res[0].doc) { if (!this.db) { - res = apply_enrich.call(this, res); + res = /** @type {EnrichedSearchResults} */apply_enrich.call(this, res); } else { promises.push(res = this.index.get(this.field[0]).db.enrich(res)); @@ -478,17 +478,20 @@ function get_tag(tag, key, limit, offset, enrich) { /** * @param {SearchResults} ids - * @return {EnrichedSearchResults|SearchResults} - * @this {Document|Index|null} + * @return {EnrichedSearchResults|SearchResults|Promise} + * @this {Document|Index|WorkerIndex|null} */ export function apply_enrich(ids) { if (!this || !this.store) return ids; + if (this.db) { + return this.index.get(this.field[0]).db.enrich(ids); + } + /** @type {EnrichedSearchResults} */ const result = Array(ids.length); - for (let x = 0, id; x < ids.length; x++) { id = ids[x]; result[x] = { diff --git a/dist/module/index/remove.js b/dist/module/index/remove.js index d1d28a6..072d647 100644 --- a/dist/module/index/remove.js +++ b/dist/module/index/remove.js @@ -60,24 +60,29 @@ export function remove_index(map, id) { if (is_array(map)) { - for (let x = 0, arr, index; x < map.length; x++) { + for (let x = 0, arr, index, found; x < map.length; x++) { if ((arr = map[x]) && arr.length) { if ("undefined" == typeof id) { - - count++; + return 1; } else { index = arr.indexOf(id); if (0 <= index) { if (1 < arr.length) { arr.splice(index, 1); - count++; - break; + return 1; } else { delete map[x]; + if (count) { + return 1; + } + found = 1; } } else { + if (found) { + return 1; + } count++; } } diff --git a/dist/module/intersect.js b/dist/module/intersect.js index 0b74fb3..2c94d71 100644 --- a/dist/module/intersect.js +++ b/dist/module/intersect.js @@ -119,7 +119,7 @@ export function intersect(arrays, resolution, limit, offset, suggest, boost, res } } else { - result = 1 < result.length ? union(result, limit, offset, resolve, boost) : (result = result[0]).length > limit || offset ? result.slice(offset, limit + offset) : result; + result = 1 < result.length ? union(result, limit, offset, resolve, boost) : (result = result[0]) && limit && result.length > limit || offset ? result.slice(offset, limit + offset) : result; } } diff --git a/dist/module/resolve/and.js b/dist/module/resolve/and.js index 9524427..c31b89b 100644 --- a/dist/module/resolve/and.js +++ b/dist/module/resolve/and.js @@ -1,52 +1,25 @@ import Resolver from "../resolver.js"; import { get_max_len } from "../common.js"; import { intersect } from "../intersect.js"; -import { SearchResults, EnrichedSearchResults, IntermediateSearchResults, ResolverOptions } from "../type.js"; -import { apply_enrich } from "../document/search.js"; +import { SearchResults, EnrichedSearchResults, IntermediateSearchResults } from "../type.js"; -/** @this {Resolver} */ +/** + * @return { + * SearchResults | + * EnrichedSearchResults | + * IntermediateSearchResults | + * Promise | + * Resolver + * } + * @this {Resolver} + */ Resolver.prototype.and = function () { - let execute = this.result.length, - limit, - offset, - enrich, - resolve; - - - if (!execute) { - /** @type {ResolverOptions} */ - const arg = arguments[0]; - if (arg) { - execute = !!arg.suggest; - resolve = arg.resolve; - limit = arg.limit; - offset = arg.offset; - enrich = arg.enrich && resolve; - } - } - - if (execute) { - - const { - final, - promises, - limit, - offset, - enrich, - resolve, - suggest - } = this.handler("and", arguments); - - return return_result.call(this, final, promises, limit, offset, enrich, resolve, suggest); - } - - return resolve ? this.resolve(limit, offset, enrich) : this; + return this.handler("and", return_result, arguments); }; /** * Aggregate the intersection of N raw results * @param {!Array} final - * @param {!Array>} promises * @param {number} limit * @param {number=} offset * @param {boolean=} enrich @@ -62,44 +35,49 @@ Resolver.prototype.and = function () { * } */ -function return_result(final, promises, limit, offset, enrich, resolve, suggest) { +function return_result(final, limit, offset, enrich, resolve, suggest) { - if (promises.length) { - const self = this; - return Promise.all(promises).then(function (result) { - - final = []; - for (let i = 0, tmp; i < result.length; i++) { - if ((tmp = result[i]).length) { - final[i] = tmp; - } - } - - return return_result.call(self, final, [], limit, offset, enrich, resolve, suggest); - }); + if (!suggest && !this.result.length) { + return resolve ? this.result : this; } + let resolved; + if (!final.length) { if (!suggest) { this.result = /** @type {SearchResults|IntermediateSearchResults} */final; } } else { - this.result.length && final.unshift(this.result); if (2 > final.length) { this.result = final[0]; } else { - const resolution = get_max_len(final); + + let resolution = 0; + for (let i = 0, res, len; i < final.length; i++) { + if ((res = final[i]) && (len = res.length)) { + if (resolution < len) { + resolution = len; + } + } else if (!suggest) { + resolution = 0; + break; + } + } + if (!resolution) { this.result = []; } else { this.result = intersect(final, resolution, limit, offset, suggest, this.boostval, resolve); - - return resolve ? enrich ? apply_enrich.call(this.index, /** @type {SearchResults} */this.result) : this.result : this; + resolved = !0; } } } - return resolve ? this.resolve(limit, offset, enrich) : this; + if (resolve) { + this.await = null; + } + + return resolve ? this.resolve(limit, offset, enrich, resolved) : this; } \ No newline at end of file diff --git a/dist/module/resolve/default.js b/dist/module/resolve/default.js index d635438..371b1f1 100644 --- a/dist/module/resolve/default.js +++ b/dist/module/resolve/default.js @@ -3,6 +3,7 @@ import { IntermediateSearchResults, SearchResults, EnrichedSearchResults } from import { apply_enrich } from "../document/search.js"; import Document from "../document.js"; import Index from "../index.js"; +import WorkerIndex from "../worker.js"; /* from -> res[score][id] @@ -16,7 +17,7 @@ import Index from "../index.js"; * @param {number=} offset * @param {boolean=} enrich * @return {SearchResults|EnrichedSearchResults} - * @this {Document|Index} + * @this {Document|Index|WorkerIndex} */ export default function (result, limit, offset, enrich) { @@ -27,8 +28,8 @@ export default function (result, limit, offset, enrich) { if (1 === result.length) { let final = result[0]; - final = offset || final.length > limit ? limit ? final.slice(offset, offset + limit) : final.slice(offset) : final; - return enrich ? apply_enrich.call(this, final) : final; + final = offset || final.length > limit ? final.slice(offset, offset + limit) : final; + return enrich ? /** @type {EnrichedSearchResults} */apply_enrich.call(this, final) : final; } let final = []; @@ -43,14 +44,13 @@ export default function (result, limit, offset, enrich) { continue; } - if (offset < len) { - arr = limit ? arr.slice(offset, offset + limit) : arr.slice(offset); - len = arr.length; - offset = 0; - } + arr = arr.slice(offset, offset + limit); + len = arr.length; + offset = 0; } if (len > limit) { + arr = arr.slice(0, limit); len = limit; } @@ -58,7 +58,7 @@ export default function (result, limit, offset, enrich) { if (!final.length) { if (len >= limit) { - return enrich ? apply_enrich.call(this, arr) : arr; + return enrich ? /** @type {EnrichedSearchResults} */apply_enrich.call(this, arr) : arr; } } diff --git a/dist/module/resolve/handler.js b/dist/module/resolve/handler.js index add2d9b..cd58493 100644 --- a/dist/module/resolve/handler.js +++ b/dist/module/resolve/handler.js @@ -1,53 +1,68 @@ import Resolver from "../resolver.js"; -import { ResolverOptions, SearchResults, IntermediateSearchResults } from "../type.js"; +import { ResolverOptions, SearchResults, EnrichedSearchResults, IntermediateSearchResults } from "../type.js"; /** - * @param {string} fn - * @param {Array>|Arguments} args + * @param {string} method + * @param {Function} fn + * @param {Array|Arguments} args + * @return { + * SearchResults | + * EnrichedSearchResults | + * IntermediateSearchResults | + * Promise | + * Resolver + * } */ -Resolver.prototype.handler = function (fn, args) { +Resolver.prototype.handler = function (method, fn, args) { - /** @type {ResolverOptions|Promise} */ - let first_argument = args[0]; + /** @type {ResolverOptions} */ + let arg = args[0]; - if (first_argument.then) { - const self = this; - - return Promise.all(args).then(function (args) { - return self[fn].apply(self, args); - }); + if (arg[0] && arg[0].query) { + return this[method].apply(this, arg); } - if (first_argument[0]) { + if ("and" === method || "not" === method) { + let execute = this.result.length || this.await, + resolve; - if (first_argument[0].index) { - return this[fn].apply(this, first_argument); + if (!execute) { + if (!arg.suggest) { + if (1 < args.length) { + arg = args[args.length - 1]; + } + resolve = arg.resolve; + return resolve ? this.await || this.result : this; + } } } - /** @type {SearchResults|IntermediateSearchResults} */ + const self = this; + /** @type {!Array>} */ let final = [], - promises = [], limit = 0, offset = 0, enrich, resolve, suggest, highlight, - highlight_query; - /** @type {Array>} */ + async; + for (let i = 0, query; i < args.length; i++) { - query = /** @type {string|ResolverOptions} */args[i]; + /** @type {ResolverOptions} */ + query = args[i]; + if (query) { let result; + if (query.constructor === Resolver) { - result = query.result; - } else if (query.constructor === Array) { + result = query.await || query.result; + } else if (query.then || query.constructor === Array) { result = query; } else { @@ -57,10 +72,13 @@ Resolver.prototype.handler = function (fn, args) { resolve = query.resolve; highlight = query.highlight && resolve; enrich = highlight || query.enrich && resolve; - let index; + let opt_queue = query.queue, + opt_async = query.async || opt_queue, + index = query.index; - if (query.index) { - this.index = index = query.index; + + if (index) { + this.index || (this.index = index); } else { index = this.index; } @@ -74,47 +92,123 @@ Resolver.prototype.handler = function (fn, args) { } } - query.resolve = !1; + if (opt_queue && (async || this.await)) { + async = 1; - result = index.search(query).result; - query.resolve = resolve; + let resolve; + const idx = this.promises.length, + promise = new Promise(function (_resolve) { + resolve = _resolve; + }); + + + (function (index, query) { + + promise._fn = function () { + query.index = null; + query.resolve = !1; + let result = opt_async ? index.searchAsync(query) : index.search(query); + if (result.then) { + return result.then(function (result) { + self.promises[idx] = result = result.result || result; + resolve(result); + return result; + }); + } + result = result.result || result; + resolve(result); + return result; + }; + })(index, Object.assign({}, /** @type Object */query)); + + this.promises.push(promise); + final[i] = promise; + continue; + } else { + query.resolve = !1; + query.index = null; + + result = opt_async ? index.searchAsync(query) : index.search(query); + + query.resolve = resolve; + query.index = index; + } if (highlight) { - highlight_query = query.query; + query.query; } } else if (query.and) { - result = this.and(query.and); + result = inner_call(query, "and", index); } else if (query.or) { - result = this.or(query.or); - } else if (query.xor) { - result = this.xor(query.xor); + result = inner_call(query, "or", index); } else if (query.not) { - result = this.not(query.not); + result = inner_call(query, "not", index); + } else if (query.xor) { + result = inner_call(query, "xor", index); } else { continue; } } - if (result.then) { - promises.push(result); - } else if (result.length) { - final[i] = result; - } else if (!suggest && ("and" === fn || "xor" === fn)) { - final = []; - break; + if (result.await) { + async = 1; + result = result.await; + } else if (result.then) { + async = 1; + result = result.then(function (result) { + return result.result || result; + }); + } else { + result = result.result || result; } + + final[i] = result; } } - return { - final, - promises, - limit, - offset, - enrich, - resolve, - suggest, - highlight, - highlight_query - }; -}; \ No newline at end of file + if (async && !this.await) { + this.await = new Promise(function (resolve) { + self.return = resolve; + }); + } + + if (async) { + + const promises = Promise.all(final).then(function (final) { + for (let i = 0; i < self.promises.length; i++) { + if (self.promises[i] === promises) { + self.promises[i] = function () { + return fn.call(self, final, limit, offset, enrich, resolve, suggest); + }; + break; + } + } + self.execute(); + }); + this.promises.push(promises); + } else if (this.await) { + + this.promises.push(function () { + return fn.call(self, final, limit, offset, enrich, resolve, suggest); + }); + } else { + + return fn.call(this, final, limit, offset, enrich, resolve, suggest); + } + + return resolve ? this.await || this.result : this; +}; + +function inner_call(query, method, index) { + const args = query[method], + arg = args[0] || args; + + arg.index || (arg.index = index); + + let resolver = new Resolver(arg); + if (1 < args.length) { + resolver = resolver[method].apply(resolver, args.slice(1)); + } + + return resolver; +} \ No newline at end of file diff --git a/dist/module/resolve/not.js b/dist/module/resolve/not.js index cd9070c..8dd9cf5 100644 --- a/dist/module/resolve/not.js +++ b/dist/module/resolve/not.js @@ -1,26 +1,13 @@ import Resolver from "../resolver.js"; import { SearchResults, EnrichedSearchResults, IntermediateSearchResults } from "../type.js"; -import { apply_enrich } from "../document/search.js"; /** @this {Resolver} */ Resolver.prototype.not = function () { - - const { - final, - promises, - limit, - offset, - enrich, - resolve, - suggest - } = this.handler("not", arguments); - - return return_result.call(this, final, promises, limit, offset, enrich, resolve, suggest); + return this.handler("not", return_result, arguments); }; /** * @param {!Array} final - * @param {!Array>} promises * @param {number} limit * @param {number=} offset * @param {boolean=} enrich @@ -35,31 +22,24 @@ Resolver.prototype.not = function () { * Resolver * } */ +function return_result(final, limit, offset, enrich, resolve, suggest) { -function return_result(final, promises, limit, offset, enrich, resolve, suggest) { - - if (promises.length) { - const self = this; - return Promise.all(promises).then(function (result) { - - final = []; - for (let i = 0, tmp; i < result.length; i++) { - if ((tmp = result[i]).length) { - final[i] = tmp; - } - } - - return return_result.call(self, final, [], limit, offset, enrich, resolve, suggest); - }); + if (!suggest && !this.result.length) { + return resolve ? this.result : this; } + let resolved; + if (final.length && this.result.length) { this.result = exclusion.call(this, final, limit, offset, resolve); - } else if (resolve) { - return this.resolve(limit, offset, enrich); + resolved = !0; } - return resolve ? enrich ? apply_enrich.call(this.index, this.result) : this.result : this; + if (resolve) { + this.await = null; + } + + return resolve ? this.resolve(limit, offset, enrich, resolved) : this; } /** @@ -70,7 +50,6 @@ function return_result(final, promises, limit, offset, enrich, resolve, suggest) * @this {Resolver} * @return {SearchResults|IntermediateSearchResults} */ - function exclusion(result, limit, offset, resolve) { /** @type {SearchResults|IntermediateSearchResults} */ diff --git a/dist/module/resolve/or.js b/dist/module/resolve/or.js index 7114d27..3abd217 100644 --- a/dist/module/resolve/or.js +++ b/dist/module/resolve/or.js @@ -4,27 +4,17 @@ import { SearchResults, EnrichedSearchResults, IntermediateSearchResults } from /** @this {Resolver} */ Resolver.prototype.or = function () { - - const { - final, - promises, - limit, - offset, - enrich, - resolve - } = this.handler("or", arguments); - - return return_result.call(this, final, promises, limit, offset, enrich, resolve); + return this.handler("or", return_result, arguments); }; /** * Aggregate the intersection of N raw results * @param {!Array} final - * @param {!Array>} promises * @param {number} limit * @param {number=} offset * @param {boolean=} enrich * @param {boolean=} resolve + * @param {boolean=} suggest * @this {Resolver} * @return { * SearchResults | @@ -35,25 +25,9 @@ Resolver.prototype.or = function () { * } */ -function return_result(final, promises, limit, offset, enrich, resolve) { - - if (promises.length) { - const self = this; - return Promise.all(promises).then(function (result) { - - final = []; - for (let i = 0, tmp; i < result.length; i++) { - if ((tmp = result[i]).length) { - final[i] = tmp; - } - } - - return return_result.call(self, final, [], limit, offset, enrich, resolve); - }); - } +function return_result(final, limit, offset, enrich, resolve) { if (final.length) { - this.result.length && final.push(this.result); if (2 > final.length) { @@ -66,5 +40,9 @@ function return_result(final, promises, limit, offset, enrich, resolve) { } } + if (resolve) { + this.await = null; + } + return resolve ? this.resolve(limit, offset, enrich) : this; } \ No newline at end of file diff --git a/dist/module/resolve/xor.js b/dist/module/resolve/xor.js index 5fdbb40..a940b12 100644 --- a/dist/module/resolve/xor.js +++ b/dist/module/resolve/xor.js @@ -1,28 +1,14 @@ import Resolver from "../resolver.js"; -import default_resolver from "./default.js"; import { create_object } from "../common.js"; import { SearchResults, EnrichedSearchResults, IntermediateSearchResults } from "../type.js"; -import { apply_enrich } from "../document/search.js"; /** @this {Resolver} */ Resolver.prototype.xor = function () { - - const { - final, - promises, - limit, - offset, - enrich, - resolve, - suggest - } = this.handler("xor", arguments); - - return return_result.call(this, final, promises, limit, offset, enrich, resolve, suggest); + return this.handler("xor", return_result, arguments); }; /** * @param {!Array} final - * @param {!Array>} promises * @param {number} limit * @param {number=} offset * @param {boolean=} enrich @@ -38,39 +24,28 @@ Resolver.prototype.xor = function () { * } */ -function return_result(final, promises, limit, offset, enrich, resolve, suggest) { +function return_result(final, limit, offset, enrich, resolve, suggest) { - if (promises.length) { - const self = this; - return Promise.all(promises).then(function (result) { - - final = []; - for (let i = 0, tmp; i < result.length; i++) { - if ((tmp = result[i]).length) { - final[i] = tmp; - } - } - - return return_result.call(self, final, [], limit, offset, enrich, resolve, suggest); - }); - } + let resolved; if (!final.length) { if (!suggest) this.result = /** @type {SearchResults|IntermediateSearchResults} */final; } else { - this.result.length && final.unshift(this.result); if (2 > final.length) { this.result = final[0]; } else { this.result = exclusive.call(this, final, limit, offset, resolve, this.boostval); - - return resolve ? enrich ? apply_enrich.call(this.index, /** @type {SearchResults} */this.result) : this.result : this; + resolved = !0; } } - return resolve ? this.resolve(limit, offset, enrich) : this; + if (resolve) { + this.await = null; + } + + return resolve ? this.resolve(limit, offset, enrich, resolved) : this; } /** diff --git a/dist/module/resolver.js b/dist/module/resolver.js index 1bad4c0..1b7055f 100644 --- a/dist/module/resolver.js +++ b/dist/module/resolver.js @@ -1,41 +1,76 @@ + +import { ResolverOptions, IntermediateSearchResults, SearchResults, EnrichedSearchResults } from "./type.js"; import Index from "./index.js"; import Document from "./document.js"; +import WorkerIndex from "./worker.js"; import default_resolver from "./resolve/default.js"; -import { apply_enrich } from "./document/search.js"; -import { ResolverOptions, IntermediateSearchResults } from "./type.js"; import "./resolve/handler.js"; import "./resolve/or.js"; import "./resolve/and.js"; import "./resolve/xor.js"; import "./resolve/not.js"; -import { highlight_fields } from "./document/highlight.js"; +import { apply_enrich } from "./document/search.js"; /** * @param {IntermediateSearchResults|ResolverOptions=} result - * @param {Index|Document=} index + * @param {Index|Document|WorkerIndex=} index * @return {Resolver} * @constructor */ - export default function Resolver(result, index) { if (!this || this.constructor !== Resolver) { return new Resolver(result, index); } - if (result && result.index) { + let boost = 0, + promises, + _await, + _return; - result.resolve = !1; - this.index = /** @type {Index|Document} */result.index; - this.boostval = result.boost || 0; - this.result = this.index.search(result).result; - return this; + if (result && result.index) { + const options = /** @type {ResolverOptions} */result; + index = options.index; + boost = options.boost || 0; + if (options.query) { + const resolve = options.resolve, + async = options.async || options.queue; + + options.resolve = !1; + options.index = null; + result = async ? index.searchAsync(options) : index.search(options); + options.resolve = resolve; + options.index = index; + result = result.result || result; + } else { + result = []; + } } - /** @type {Index|Document|null} */ + + if (result && result.then) { + const self = this; + result = result.then(function (result) { + self.promises[0] = self.result = result.result || result; + self.execute(); + }); + promises = [result]; + result = []; + _await = new Promise(function (resolve) { + _return = resolve; + }); + } + + /** @type {Index|Document|WorkerIndex|null} */ this.index = index || null; /** @type {IntermediateSearchResults} */ this.result = /** @type {IntermediateSearchResults} */result || []; /** @type {number} */ - this.boostval = 0; + this.boostval = boost; + /** @type {Array|IntermediateSearchResults|Function>} */ + this.promises = promises || []; + /** @type {Promise} */ + this.await = _await || null; + /** @type {Function} */ + this.return = _return || null; } /** @@ -92,29 +127,77 @@ Resolver.prototype.boost = function (boost) { return this; }; +/** + * @param {boolean=} _skip_callback + * @this {Resolver} + */ +Resolver.prototype.execute = function (_skip_callback) { + + let result = this.result; + + for (let i = 0, promise; i < this.promises.length; i++) { + promise = this.promises[i]; + if (promise) { + if ("function" == typeof promise) { + result = promise(); + this.promises[i] = result = result.result || result; + i--; + } else if (promise._fn) { + result = promise._fn(); + this.promises[i] = result = result.result || result; + i--; + } else if (promise.then) { + return this.await; + } + } + } + + const fn = this.return; + this.result = result; + this.promises = []; + this.await = null; + this.return = null; + + _skip_callback || fn(result); + return result; +}; + /** * @param {number|ResolverOptions=} limit * @param {number=} offset * @param {boolean=} enrich - * @param {boolean=} highlight + * @param {boolean=} _resolved */ -Resolver.prototype.resolve = function (limit, offset, enrich) { +Resolver.prototype.resolve = function (limit, offset, enrich, _resolved) { - const index = this.index; - let result = this.result; - this.index = null; - this.result = null; + let result = this.await ? this.execute(!0) : this.result; + + if (result.then) { + const self = this; + return result.then(function () { + return self.resolve(limit, offset, enrich); + }); + } if (result.length) { if ("object" == typeof limit) { enrich = limit.enrich; offset = limit.offset; - limit.highlight; + limit = limit.limit; } - - result = default_resolver.call(index, result, limit || 100, offset, enrich); + result = _resolved ? enrich ? apply_enrich.call( + /** @type {Document} */this.index, + /** @type {SearchResults} */result) : result : default_resolver.call(this.index, result, limit || 100, offset, enrich); } + const fn = this.return; + this.index = null; + this.result = null; + this.promises = null; + this.await = null; + this.return = null; + + fn && fn(result); return result; }; \ No newline at end of file diff --git a/dist/module/type.js b/dist/module/type.js index 368e9fb..2357d63 100644 --- a/dist/module/type.js +++ b/dist/module/type.js @@ -1,6 +1,8 @@ import Index from "./index.js"; +import Document from "./document.js"; +import WorkerIndex from "./worker.js"; import Encoder from "./encoder.js"; import StorageInterface from "./db/interface.js"; @@ -140,7 +142,7 @@ export let SearchOptions = {}; * resolve: (boolean|undefined), * enrich: (boolean|undefined), * cache: (boolean|undefined), - * tag: (Object|Array|undefined), + * tag: (Object|Array>|undefined), * field: (Array|Array|DocumentSearchOptions|string|undefined), * index: (Array|Array|DocumentSearchOptions|string|undefined), * pluck: (string|DocumentSearchOptions|undefined), @@ -252,13 +254,20 @@ export let PersistentOptions = {}; /** * @typedef {{ - * index: (Index|Document|undefined), + * index: (Index|Document|WorkerIndex|undefined), * query: (string|undefined), + * pluck: (string|undefined), + * field: (string|undefined), * limit: (number|undefined), * offset: (number|undefined), + * boost: (number|undefined), * enrich: (boolean|undefined), + * highlight: (HighlightOptions|string|undefined), * resolve: (boolean|undefined), * suggest: (boolean|undefined), + * cache: (boolean|undefined), + * async: (boolean|undefined), + * queue: (boolean|undefined), * and: (ResolverOptions|Array|undefined), * or: (ResolverOptions|Array|undefined), * xor: (ResolverOptions|Array|undefined), diff --git a/dist/module/worker.js b/dist/module/worker.js index 967d758..146adce 100644 --- a/dist/module/worker.js +++ b/dist/module/worker.js @@ -1,6 +1,7 @@ import { IndexOptions } from "./type.js"; import { create_object } from "./common.js"; +import { searchCache } from "./cache.js"; import handler from "./worker/handler.js"; import apply_async from "./async.js"; import Encoder from "./encoder.js"; @@ -55,12 +56,10 @@ export default function WorkerIndex(options = /** @type IndexOptions */{}, encod if (options.config) { return new Promise(function (resolve) { - + if (1e9 < pid) pid = 0; _self.resolver[++pid] = function () { resolve(_self); - if (1e9 < pid) pid = 0; }; - _self.worker.postMessage({ id: pid, task: "init", @@ -93,13 +92,14 @@ export default function WorkerIndex(options = /** @type IndexOptions */{}, encod register("add"); register("append"); register("search"); -register("searchCache"); register("update"); register("remove"); register("clear"); register("export"); register("import"); +WorkerIndex.prototype.searchCache = searchCache; + apply_async(WorkerIndex.prototype); @@ -122,6 +122,7 @@ function register(key) { args[0] = null; } + if (1e9 < pid) pid = 0; self.resolver[++pid] = resolve; self.worker.postMessage({ task: key, @@ -141,7 +142,5 @@ function register(key) { function create(factory, is_node_js, worker_path) { - let worker = is_node_js ? "undefined" != typeof module ? (0,eval)('new(require("worker_threads")["Worker"])(__dirname+"/worker/node.js")') : import("worker_threads").then(function(worker){return new worker["Worker"](import.meta.dirname+"/../node/node.mjs")}) : factory ? new window.Worker(URL.createObjectURL(new Blob(["onmessage=" + handler.toString()], { type: "text/javascript" }))) : new window.Worker("string" == typeof worker_path ? worker_path : (1, eval)("import.meta.url").replace("/worker.js", "/worker/worker.js").replace("flexsearch.bundle.module.min.js", "module/worker/worker.js"), { type: "module" }); - - return worker; + return is_node_js ? "undefined" != typeof module ? (0,eval)('new(require("worker_threads")["Worker"])(__dirname+"/worker/node.js")') : import("worker_threads").then(function(worker){return new worker["Worker"](import.meta.dirname+"/../node/node.mjs")}) : factory ? new window.Worker(URL.createObjectURL(new Blob(["onmessage=" + handler.toString()], { type: "text/javascript" }))) : new window.Worker("string" == typeof worker_path ? worker_path : (1, eval)("import.meta.url").replace("/worker.js", "/worker/worker.js").replace("flexsearch.bundle.module.min.js", "module/worker/worker.js"), { type: "module" }); } \ No newline at end of file diff --git a/dist/module/worker/handler.js b/dist/module/worker/handler.js index 2deff6f..1d26f1c 100644 --- a/dist/module/worker/handler.js +++ b/dist/module/worker/handler.js @@ -64,6 +64,12 @@ export default (async function (data) { if (message && message.then) { message = await message; } + if (message && message.await) { + message = await message.await; + } + if ("search" === task && message.result) { + message = message.result; + } } postMessage("search" === task ? { id: id, msg: message } : { id: id }); diff --git a/dist/module/worker/node.js b/dist/module/worker/node.js index d92e17e..be9c0eb 100644 --- a/dist/module/worker/node.js +++ b/dist/module/worker/node.js @@ -60,6 +60,12 @@ parentPort.on("message", async function (data) { if (message && message.then) { message = await message; } + if (message && message.await) { + message = await message.await; + } + if ("search" === task && message.result) { + message = message.result; + } } parentPort.postMessage("search" === task ? { id: id, msg: message } : { id: id }); diff --git a/dist/node/node.js b/dist/node/node.js index a29bff4..8033bcd 100644 --- a/dist/node/node.js +++ b/dist/node/node.js @@ -67,6 +67,12 @@ parentPort.on("message", async function(data){ if(message && message.then){ message = await message; } + if(message && message.await){ + message = await message.await; + } + if(task === "search" && message.result){ + message = message.result; + } } parentPort.postMessage( diff --git a/dist/node/node.mjs b/dist/node/node.mjs index 11b90d9..95e8b89 100644 --- a/dist/node/node.mjs +++ b/dist/node/node.mjs @@ -69,6 +69,12 @@ parentPort.on("message", async function(data){ if(message && message.then){ message = await message; } + if(message && message.await){ + message = await message.await; + } + if(task === "search" && message.result){ + message = message.result; + } } parentPort.postMessage( diff --git a/doc/0.8.0.md b/doc/0.8.0.md deleted file mode 100644 index a28dfd3..0000000 --- a/doc/0.8.0.md +++ /dev/null @@ -1,1857 +0,0 @@ -# FlexSearch v0.8 (Preview) - -```bash -npm install flexsearch@latest -``` - -## What's New - -- Persistent indexes support for: `IndexedDB` (Browser), `Redis`, `SQLite`, `Postgres`, `MongoDB`, `Clickhouse` -- Enhanced language customization via the new `Encoder` class -- Result Highlighting -- Query performance achieve results up to 4.5 times faster compared to the previous generation v0.7.x by also improving the quality of results -- Enhanced support for larger indexes or larger result sets -- Improved offset and limit processing achieve up to 100 times faster traversal performance through large datasets -- Support for larger In-Memory index with extended key size (the defaults maximum keystore limit is: 2^24) -- Greatly enhanced performance of the whole text encoding pipeline -- Improved indexing of numeric content (Triplets) -- Intermediate result sets and `Resolver` -- Basic Resolver: `and`, `or`, `xor`, `not`, `limit`, `offset`, `boost`, `resolve` -- Improved charset collection -- New charset preset `soundex` which further reduces memory consumption by also increasing "fuzziness" -- Performance gain when polling tasks to the index by using "Event-Loop-Caches" -- Up to 100 times faster deletion/replacement when not using the additional "fastupdate" register -- Regex Pre-Compilation (transforms hundreds of regex rules into just a few) -- Extended support for multiple tags (DocumentIndex) -- Custom Fields ("Virtual Fields") -- Custom Filter -- Custom Score Function -- Added French language preset (stop-word filter, stemmer) -- Enhanced Worker Support -- Export / Import index in chunks -- Improved Build System + Bundler (Supported: CommonJS, ESM, Global Namespace), also the import of language packs are now supported for Node.js -- Full covering index.d.ts type definitions -- Fast-Boot Serialization optimized for Server-Side-Rendering (PHP, Python, Ruby, Rust, Java, Go, Node.js, ...) - -Compare Benchmark: [0.7.0](https://nextapps-de.github.io/flexsearch/test/flexsearch-0.7.0/) vs. [0.8.0](https://nextapps-de.github.io/flexsearch/test/flexsearch-0.8.0/) - -## Persistent Indexes - -FlexSearch provides a new Storage Adapter where indexes are delegated through persistent storages. - -Supported: - -- [IndexedDB (Browser)](src/db/indexeddb/) -- [Redis](src/db/redis/) -- [SQLite](src/db/sqlite/) -- [Postgres](src/db/postgres/) -- [MongoDB](src/db/mongodb/) -- [Clickhouse](src/db/clickhouse/) - -The `.export()` and `.import()` methods are still available for non-persistent In-Memory indexes. - -All search capabilities are available on persistent indexes like: -- Context-Search -- Suggestions -- Cursor-based Queries (Limit/Offset) -- Scoring (supports a resolution of up to 32767 slots) -- Document-Search -- Partial Search -- Multi-Tag-Search -- Boost Fields -- Custom Encoder -- Resolver -- Tokenizer (Strict, Forward, Reverse, Full) -- Document Store (incl. enrich results) -- Worker Threads to run in parallel -- Auto-Balanced Cache (top queries + last queries) - -All persistent variants are optimized for larger sized indexes under heavy workload. Almost every task will be streamlined to run in batch/parallel, getting the most out of the selected database engine. Whereas the InMemory index can't share their data between different nodes when running in a cluster, every persistent storage can handle this by default. - -### Examples Node.js - -- [nodejs-commonjs](../example/nodejs-commonjs): - - [basic](../example/nodejs-commonjs/basic) - - [basic-suggestion](../example/nodejs-commonjs/basic-suggestion) - - [basic-persistent](../example/nodejs-commonjs/basic-persistent) - - [basic-resolver](../example/nodejs-commonjs/basic-resolver) - - [basic-worker](../example/nodejs-commonjs/basic-worker) - - [basic-worker-extern-config](../example/nodejs-commonjs/basic-worker-extern-config) - - [basic-export-import](../example/nodejs-commonjs/basic-export-import) - - [document](../example/nodejs-commonjs/document) - - [document-persistent](../example/nodejs-commonjs/document-persistent) - - [document-worker](../example/nodejs-commonjs/document-worker) - - [document-worker-extern-config](../example/nodejs-commonjs/document-worker-extern-config) - - [document-export-import](../example/nodejs-commonjs/document-export-import) - - [language-pack](../example/nodejs-commonjs/language-pack) -- [nodejs-esm](../example/nodejs-esm): - - [basic](../example/nodejs-esm/basic) - - [basic-suggestion](../example/nodejs-esm/basic-suggestion) - - [basic-persistent](../example/nodejs-esm/basic-persistent) - - [basic-resolver](../example/nodejs-esm/basic-resolver) - - [basic-worker](../example/nodejs-esm/basic-worker) - - [basic-worker-extern-config](../example/nodejs-esm/basic-worker-extern-config) - - [basic-export-import](../example/nodejs-esm/basic-export-import) - - [document](../example/nodejs-esm/document) - - [document-persistent](../example/nodejs-esm/document-persistent) - - [document-worker](../example/nodejs-esm/document-worker) - - [document-worker-extern-config](../example/nodejs-esm/document-worker-extern-config) - - [document-export-import](../example/nodejs-esm/document-export-import) - - [language-pack](../example/nodejs-esm/language-pack) - -### Examples Browser - -- [browser-legacy](../example/browser-legacy): - - [basic](../example/browser-legacy/basic) - - [basic-suggestion](../example/browser-legacy/basic-suggestion) - - [basic-persistent](../example/browser-legacy/basic-persistent) - - [basic-resolver](../example/browser-legacy/basic-resolver) - - [basic-worker](../example/browser-legacy/basic-worker) - - [document](../example/browser-legacy/document) - - [document-highlighting](../example/browser-legacy/document-highlighting) - - [document-persistent](../example/browser-legacy/document-persistent) - - [document-worker](../example/browser-legacy/document-worker) - - [language-pack](../example/browser-legacy/language-pack) -- [browser-module](../example/browser-module): - - [basic](../example/browser-module/basic) - - [basic-suggestion](../example/browser-module/basic-suggestion) - - [basic-persistent](../example/browser-module/basic-persistent) - - [basic-resolver](../example/browser-module/basic-resolver) - - [basic-worker](../example/browser-module/basic-worker) - - [basic-worker-extern-config](../example/browser-module/basic-worker-extern-config) - - [document](../example/browser-module/document) - - [document-highlighting](../example/browser-module/document-highlighting) - - [document-persistent](../example/browser-module/document-persistent) - - [document-worker](../example/browser-module/document-worker) - - [document-worker-extern-config](../example/browser-module/document-worker-extern-config) - - [language-pack](../example/browser-module/language-pack) - -```js -import FlexSearchIndex from "./index.d.ts"; -import Database from "./db/indexeddb/index.js"; -// create an index -const index = new FlexSearchIndex(); -// create db instance with optional prefix -const db = new Database("my-store"); -// mount and await before transfering data -await flexsearch.mount(db); - -// update the index as usual -index.add(1, "content..."); -index.update(2, "content..."); -index.remove(3); - -// changes are automatically committed by default -// when you need to wait for the task completion, then you -// can use the commit method explicitely: -await index.commit(); -``` - -Alternatively mount a store by index creation: - -```js -const index = new FlexSearchIndex({ - db: new Storage("my-store") -}); - -// await for the db response before access the first time -await index.db; -// apply changes to the index -// ... -``` - -Query against a persistent storage just as usual: - -```js -const result = await index.search("gulliver"); -``` - -Auto-Commit is enabled by default and will process changes asynchronously in batch. -You can fully disable the auto-commit feature and perform them manually: - -```js -const index = new FlexSearchIndex({ - db: new Storage("my-store"), - commit: false -}); -// update the index -index.add(1, "content..."); -index.update(2, "content..."); -index.remove(3); - -// transfer all changes to the db -await index.commit(); -``` - -You can call the commit method manually also when `commit: true` option was set. - -### Benchmark - -The benchmark was measured in "terms per second". - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
StoreAddSearch 1Search NReplaceRemoveNot FoundScaling
terms per secterms per secterms per secterms per secterms per secterms per sec
IndexedDB123,29883,82362,37057,410171,053425,744No
Redis1,566,091201,534859,463117,013129,595875,526Yes
Sqlite269,81229,627129,735174,4451,406,553122,566No
Postgres354,89424,32976,189324,5463,702,64750,305Yes
MongoDB515,93819,68481,558243,353485,19267,751Yes
Clickhouse1,436,99211,50722,196931,0263,276,84716,644Yes
- -__Search 1:__ Single term query
-__Search N:__ Multi term query (Context-Search) - -The benchmark was executed against a single client. - -## Encoder - -Search capabilities highly depends on language processing. The old workflow wasn't really practicable. The new Encoder class is a huge improvement and fully replaces the encoding part. Some FlexSearch options was moved to the new `Encoder` instance. - -New Encoding Pipeline: -1. charset normalization -2. custom preparation -3. split into terms (apply includes/excludes) -4. filter (pre-filter) -5. matcher (substitute terms) -6. stemmer (substitute term endings) -7. filter (post-filter) -8. replace chars (mapper) -9. custom regex (replacer) -10. letter deduplication -11. apply finalize - -### Example - -```js -const encoder = new Encoder({ - normalize: true, - dedupe: true, - cache: true, - include: { - letter: true, - number: true, - symbol: false, - punctuation: false, - control: false, - char: "@" - } -}); -``` - -You can use an `include` __instead__ of an `exclude` definition: - -```js -const encoder = new Encoder({ - exclude: { - letter: false, - number: false, - symbol: true, - punctuation: true, - control: true - } -}); -``` - -Instead of using `include` or `exclude` you can pass a regular expression to the field `split`: - -```js -const encoder = new Encoder({ - split: /\s+/ -}); -``` - -> The definitions `include` and `exclude` is a replacement for `split`. You can just define one of those 3. - -Adding custom functions to the encoder pipeline: - -```js -const encoder = new Encoder({ - normalize: function(str){ - return str.toLowerCase(); - }, - prepare: function(str){ - return str.replace(/&/g, " and "); - }, - finalize: function(arr){ - return arr.filter(term => term.length > 2); - } -}); -``` - -Assign encoder to an index: - -```js -const index = new Index({ - encoder: encoder -}); -``` - -Define language specific transformations: - -```js -const encoder = new Encoder({ - replacer: [ - /[´`’ʼ]/g, "'" - ], - filter: new Set([ - "and", - ]), - matcher: new Map([ - ["xvi", "16"] - ]), - stemmer: new Map([ - ["ly", ""] - ]), - mapper: new Map([ - ["é", "e"] - ]) -}); -``` - -Or use predefined language and extend it with custom options: - -```js -import EnglishBookPreset from "./lang/en.js"; -const encoder = new Encoder(EnglishBookPreset, { - filter: false -}); -``` - -Equivalent: - -```js -import EnglishBookPreset from "./lang/en.js"; -const encoder = new Encoder(EnglishBookPreset); -encoder.assign({ filter: false }); -``` - -Assign extensions to the encoder instance: - -```js -import LatinEncoderPreset from "./charset/latin/simple.js"; -import EnglishBookPreset from "./lang/en.js"; -// stack definitions to the encoder instance -const encoder = new Encoder() - .assign(LatinEncoderPreset) - .assign(EnglishBookPreset) -// override preset options ... - .assign({ minlength: 3 }); -// assign further presets ... -``` - -> When adding extension to the encoder every previously assigned configuration is still intact, very much like Mixins, also when assigning custom functions. - -Add custom transformations to an existing index: - -```js -import LatinEncoderPreset from "./charset/latin/default.js"; -const encoder = new Encoder(LatinEncoderPreset); -encoder.addReplacer(/[´`’ʼ]/g, "'"); -encoder.addFilter("and"); -encoder.addMatcher("xvi", "16"); -encoder.addStemmer("ly", ""); -encoder.addMapper("é", "e"); -``` - -Shortcut for just assigning one encoder configuration to an index: - -```js -import LatinEncoderPreset from "./charset/latin/default.js"; -const index = new Index({ - encoder: LatinEncoderPreset -}); -``` - -## Resolver - -Retrieve an unresolved result: - -```js -const raw = index.search("a short query", { - resolve: false -}); -``` - -You can apply and chain different resolver methods to the raw result, e.g.: - -```js -raw.and( ... ) - .and( ... ) - .boost(2) - .or( ... , ... ) - .limit(100) - .xor( ... ) - .not( ... ) - // final resolve - .resolve({ - limit: 10, - offset: 0, - enrich: true - }); -``` - -The default resolver: - -```js -const raw = index.search("a short query", { - resolve: false -}); -const result = raw.resolve(); -``` - -Or use declaration style: - -```js -import Resolver from "./resolver.js"; -const raw = new Resolver({ - index: index, - query: "a short query" -}); -const result = raw.resolve(); -``` - -### Chainable Boolean Operations - -The basic concept explained: - -```js -// 1. get one or multiple unresolved results -const raw1 = index.search("a short query", { - resolve: false -}); -const raw2 = index.search("another query", { - resolve: false, - boost: 2 -}); - -// 2. apply and chain resolver operations -const raw3 = raw1.and(raw2, /* ... */); -// you can access the aggregated result by raw3.result -console.log("The aggregated result is:", raw3.result) -// apply further operations ... - -// 3. resolve final result -const result = raw3.resolve({ - limit: 100, - offset: 0 -}); -console.log("The final result is:", result) -``` - -Use inline queries: - -```js -const result = index.search("further query", { - // set resolve to false on the first query - resolve: false, - boost: 2 -}) -.or( // union - index.search("a query") - .and( // intersection - index.search("another query", { - boost: 2 - }) - ) -) -.not( // exclusion - index.search("some query") -) -// resolve the result -.resolve({ - limit: 100, - offset: 0 -}); -``` - -```js -import Resolver from "./resolver.js"; -const result = new Resolver({ - index: index, - query: "further query", - boost: 2 -}) -.or({ - and: [{ // inner expression - index: index, - query: "a query" - },{ - index: index, - query: "another query", - boost: 2 - }] -}) -.not({ // exclusion - index: index, - query: "some query" -}) -.resolve({ - limit: 100, - offset: 0 -}); -``` - -When all queries are made against the same index, you can skip the index in every declaration followed after initially calling `new Resolve()`: - -```js -import Resolver from "./resolver.js"; -const result = new Resolver({ - index: index, - query: "a query" -}) -.and({ query: "another query", boost: 2 }) -.or ({ query: "further query", boost: 2 }) -.not({ query: "some query" }) -.resolve(100); -``` - - - -### Custom Resolver - -```js -function CustomResolver(raw){ - // console.log(raw) - let output; - // generate output ... - return output; -} - -const result = index.search("a short query", { - resolve: CustomResolver -}); -``` - -## Result Highlighting - -Result highlighting could be just enabled when using Document-Index with enabled Data-Store. Also when you just want to add id-content-pairs you'll need to use a DocumentIndex for this feature (just define a simple document descriptor as shown below). - -```js -// create the document index -const index = new Document({ - document: { - store: true, - index: [{ - field: "title", - tokenize: "forward", - encoder: Charset.LatinBalance - }] - } -}); - -// add data -index.add({ - "id": 1, - "title": "Carmencita" -}); -index.add({ - "id": 2, - "title": "Le clown et ses chiens" -}); - -// perform a query -const result = index.search({ - query: "karmen or clown or not found", - suggest: true, - // set enrich to true (required) - enrich: true, - // highlight template - // $1 is a placeholder for the matched partial - highlight: "$1" -}); -``` - -The result will look like: - -```js -[{ - "field": "title", - "result": [{ - "id": 1, - "doc": { - "id": 1, - "title": "Carmencita" - }, - "highlight": "Carmencita" - },{ - "id": 2, - "doc": { - "id": 2, - "title": "Le clown et ses chiens" - }, - "highlight": "Le clown et ses chiens" - } - ] -}] -``` - -## Big In-Memory Keystores - -The default maximum keystore limit for the In-Memory index is 2^24 of distinct terms/partials being stored (so-called "cardinality"). An additional register could be enabled and is dividing the index into self-balanced partitions. - -```js -const index = new FlexSearchIndex({ - // e.g. set keystore range to 8-Bit: - // 2^8 * 2^24 = 2^32 keys total - keystore: 8 -}); -``` - -You can theoretically store up to 2^88 keys (64-Bit address range). - -The internal ID arrays scales automatically when limit of 2^31 has reached by using Proxy. - -> Persistent storages has no keystore limit by default. You should not enable keystore when using persistent indexes, as long as you do not stress the buffer too hard before calling `index.commit()`. - -## Multi-Tag-Search - -Assume this document schema (a dataset from IMDB): -```js -{ - "tconst": "tt0000001", - "titleType": "short", - "primaryTitle": "Carmencita", - "originalTitle": "Carmencita", - "isAdult": 0, - "startYear": "1894", - "endYear": "", - "runtimeMinutes": "1", - "genres": [ - "Documentary", - "Short" - ] -} -``` - -An appropriate document descriptor could look like: -```js -import LatinEncoder from "./charset/latin/simple.js"; - -const flexsearch = new Document({ - encoder: LatinEncoder, - resolution: 3, - document: { - id: "tconst", - //store: true, // document store - index: [{ - field: "primaryTitle", - tokenize: "forward" - },{ - field: "originalTitle", - tokenize: "forward" - }], - tag: [ - "startYear", - "genres" - ] - } -}); -``` -The field contents of `primaryTitle` and `originalTitle` are encoded by the forward tokenizer. The field contents of `startYear` and `genres` are added as tags. - -Get all entries of a specific tag: -```js -const result = flexsearch.search({ - //enrich: true, // enrich documents - tag: { "genres": "Documentary" }, - limit: 1000, - offset: 0 -}); -``` - -Get entries of multiple tags (intersection): -```js -const result = flexsearch.search({ - //enrich: true, // enrich documents - tag: { - "genres": ["Documentary", "Short"], - "startYear": "1894" - } -}); -``` - -Combine tags with queries (intersection): -```js -const result = flexsearch.search({ - query: "Carmen", // forward tokenizer - tag: { - "genres": ["Documentary", "Short"], - "startYear": "1894" - } -}); -``` - -Alternative declaration: -```js -const result = flexsearch.search("Carmen", { - tag: [{ - field: "genres", - tag: ["Documentary", "Short"] - },{ - field: "startYear", - tag: "1894" - }] -}); -``` - -## Filter Fields (Index / Tags / Datastore) - -```js -const flexsearch = new Document({ - document: { - id: "id", - index: [{ - // custom field: - field: "somefield", - filter: function(data){ - // return false to filter out - // return anything else to keep - return true; - } - }], - tag: [{ - field: "city", - filter: function(data){ - // return false to filter out - // return anything else to keep - return true; - } - }], - store: [{ - field: "anotherfield", - filter: function(data){ - // return false to filter out - // return anything else to keep - return true; - } - }] - } -}); -``` - -## Custom Fields (Index / Tags / Datastore) - -Dataset example: -```js -{ - "id": 10001, - "firstname": "John", - "lastname": "Doe", - "city": "Berlin", - "street": "Alexanderplatz", - "number": "1a", - "postal": "10178" -} -``` - -You can apply custom fields derived from data or by anything else: -```js -const flexsearch = new Document({ - document: { - id: "id", - index: [{ - // custom field: - field: "fullname", - custom: function(data){ - // return custom string - return data.firstname + " " + - data.lastname; - } - },{ - // custom field: - field: "location", - custom: function(data){ - return data.street + " " + - data.number + ", " + - data.postal + " " + - data.city; - } - }], - tag: [{ - // existing field - field: "city" - },{ - // custom field: - field: "category", - custom: function(data){ - let tags = []; - // push one or multiple tags - // .... - return tags; - } - }], - store: [{ - field: "anotherfield", - custom: function(data){ - // return a falsy value to filter out - // return anything else as to keep in store - return data; - } - }] - } -}); -``` - -> Filter is also available in custom functions when returning `false`. - -Perform a query against the custom field as usual: -```js -const result = flexsearch.search({ - query: "10178 Berlin Alexanderplatz", - field: "location" -}); -``` - -```js -const result = flexsearch.search({ - query: "john doe", - tag: { "city": "Berlin" } -}); -``` - -## Custom Score Function - -```js -const index = new FlexSearchIndex({ - resolution: 10, - score: function(content, term, term_index, partial, partial_index){ - // you'll need to return a number between 0 and "resolution" - // score is starting from 0, which is the highest score - // for a resolution of 10 you can return 0 - 9 - // ... - return 3; - } -}); -``` - -A common situation is you have some predefined labels which are related to some kind of order, e.g. the importance or priority. A priority label could be `high`, `moderate`, `low` so you can derive the scoring from those properties. Another example is when you have something already ordered and you would like to keep this order as relevance. - -Probably you won't need the parameters passed to the score function. But when needed here are the parameters from the score function explained: - -1. `content` is the whole content as an array of terms (encoded) -2. `term` is the current term which is actually processed (encoded) -3. `term_index` is the index of the term in the content array -4. `partial` is the current partial of a term which is actually processed -5. `partial_index` is the index position of the partial within the term - -Partials params are empty when using tokenizer `strict`. Let's take an example by using the tokenizer `full`. - -The content: "This is an ex[amp]()le of partial encoding"
-The highlighting part marks the partial which is actually processed. Then your score function will called by passing these parameters: - -```js -function score(content, term, term_index, partial, partial_index){ - content = ["this", "is", "an", "example", "of", "partial", "encoding"] - term = "example" - term_index = 3 - partial = "amp" - partial_index = 2 -} -``` - -## Merge Document Results - -By default, the result set of Field-Search has a structure grouped by field names: -```js -[{ - field: "fieldname-1", - result: [{ - id: 1001, - doc: {/* stored document */} - }] -},{ - field: "fieldname-2", - result: [{ - id: 1001, - doc: {/* stored document */} - }] -},{ - field: "fieldname-3", - result: [{ - id: 1002, - doc: {/* stored document */} - }] -}] -``` - -By passing the search option `merge: true` the result set will be merged into (group by id): -```js -[{ - id: 1001, - doc: {/* stored document */} - field: ["fieldname-1", "fieldname-2"] -},{ - id: 1002, - doc: {/* stored document */} - field: ["fieldname-3"] -}] -``` - -## Extern Worker Configuration - -When using Worker by __also__ assign custom functions to the options e.g.: - -- Custom Encoder -- Custom Encoder methods (normalize, prepare, finalize) -- Custom Score (function) -- Custom Filter (function) -- Custom Fields (function) - -... then you'll need to move your __field configuration__ into a file which exports the configuration as a `default` export. The field configuration is not the whole Document-Descriptor. - -When not using custom functions in combination with Worker you can skip this part. - -Since every field resolves into a dedicated Worker, also every field which includes custom functions should have their own configuration file accordingly. - -Let's take this document descriptor: - -```js -{ - document: { - index: [{ - // this is the field configuration - // ----> - field: "custom_field", - custom: function(data){ - return "custom field content"; - } - // <------ - }] - } -}; -``` - -The configuration which needs to be available as a default export is: - -```js -{ - field: "custom_field", - custom: function(data){ - return "custom field content"; - } -}; -``` - -You're welcome to make some suggestions how to improve the handling of extern configuration. - -### Example Node.js: - -An extern configuration for one WorkerIndex, let's assume it is located in `./custom_field.js`: -```js -const { Charset } = require("flexsearch"); -const { LatinSimple } = Charset; -// it requires a default export: -module.exports = { - encoder: LatinSimple, - tokenize: "forward", - // custom function: - custom: function(data){ - return "custom field content"; - } -}; -``` - -Create Worker Index along the configuration above: -```js -const { Document } = require("flexsearch"); -const flexsearch = new Document({ - worker: true, - document: { - index: [{ - // the field name needs to be set here - field: "custom_field", - // path to your config from above: - config: "./custom_field.js", - }] - } -}); -``` - -### Browser (ESM) - -An extern configuration for one WorkerIndex, let's assume it is located in `./custom_field.js`: -```js -import { Charset } from "./dist/flexsearch.bundle.module.min.js"; -const { LatinSimple } = Charset; -// it requires a default export: -export default { - encoder: LatinSimple, - tokenize: "forward", - // custom function: - custom: function(data){ - return "custom field content"; - } -}; -``` - -Create Worker Index with the configuration above: -```js -import { Document } from "./dist/flexsearch.bundle.module.min.js"; -// you will need to await for the response! -const flexsearch = await new Document({ - worker: true, - document: { - index: [{ - // the field name needs to be set here - field: "custom_field", - // Absolute URL to your config from above: - config: "http://localhost/custom_field.js" - }] - } -}); -``` - -Here it needs the __absolute URL__, because the WorkerIndex context is from type `Blob` and you can't use relative URLs starting from this context. - -### Test Case - -As a test the whole IMDB data collection was indexed, containing of: - -JSON Documents: 9,273,132
-Fields: 83,458,188
-Tokens: 128,898,832
- -The used index configuration has 2 fields (using bidirectional context of `depth: 1`), 1 custom field, 2 tags and a full datastore of all input json documents. - -A non-Worker Document index requires 181 seconds to index all contents.
-The Worker index just takes 32 seconds to index them all, by processing every field and tag in parallel. For such large content it is a quite impressive result. - -### CSP-friendly Worker (Browser) - -When just using worker by passing the option `worker: true`, the worker will be created by code generation under the hood. This might have issues when using strict CSP settings. - -You can overcome this issue by passing the filepath to the worker file like `worker: "./worker.js"`. The original worker file is located at `src/worker/worker.js`. - -## Fuzzy-Search - -Fuzzysearch describes a basic concept of how making queries more tolerant. FlexSearch provides several methods to achieve fuzziness: - -1. Use a tokenizer: `forward`, `reverse` or `full` -2. Don't forget to use any of the builtin encoder `simple` > `balance` > `advanced` > `extra` > `soundex` (sorted by fuzziness) -3. Use one of the language specific presets e.g. `/lang/en.js` for en-US specific content -4. Enable suggestions by passing the search option `suggest: true` - -Additionally, you can apply custom `Mapper`, `Replacer`, `Stemmer`, `Filter` or by assigning a custom `normalize(str)`, `prepare(str)` or `finalize(arr)` function to the Encoder. - -### Compare Fuzzy-Search Encoding - -Original term which was indexed: "Struldbrugs" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Encoder:LatinExactLatinDefaultLatinSimpleLatinBalanceLatinAdvancedLatinExtraLatinSoundex
Index Size3.1 Mb1.9 Mb1.8 Mb1.7 Mb1.6 Mb1.1 Mb0.7 Mb
Struldbrugs
struldbrugs
strũldbrųĝgs
strultbrooks
shtruhldbrohkz
zdroltbrykz
struhlbrogger
- -The index size was measured after indexing the book "Gulliver's Travels". - -### Custom Encoder - -Since it is very simple to create a custom Encoder, you are welcome to create your own. -e.g. -```js -function customEncoder(content){ - const tokens = []; - // split content into terms/tokens - // apply your changes to each term/token - // you will need to return an Array of terms/tokens - // so just iterate through the input string and - // push tokens to the array - // ... - return tokens; -} - -const index = new Index({ - // set to strict when your tokenization was already done - tokenize: "strict", - encode: customEncoder -}); -``` - -If you get some good results please feel free to share your encoder. - -## Fast-Boot Serialization for Server-Side-Rendering (PHP, Python, Ruby, Rust, Java, Go, Node.js, ...) - -> This is an experimental feature with limited support which probably might drop in future release. You're welcome to give some feedback. - -When using Server-Side-Rendering you can create a different export which instantly boot up. Especially when using Server-side rendered content, this could help to restore a __static__ index on page load. Document-Indexes aren't supported yet for this method. - -> When your index is too large you should use the default export/import mechanism. - -As the first step populate the FlexSearch index with your contents. - -You have two options: - -### 1. Create a function as string - -```js -const fn_string = index.serialize(); -``` - -The contents of `fn_string` is a valid Javascript-Function declared as `inject(index)`. Store it or place this somewhere in your code. - -This function basically looks like: - -```js -function inject(index){ - index.reg = new Set([/* ... */]); - index.map = new Map([/* ... */]); - index.ctx = new Map([/* ... */]); -} -``` - -You can save this function by e.g. `fs.writeFileSync("inject.js", fn_string);` or place it as string in your SSR-generated markup. - -After creating the index on client side just call the inject method like: - -```js -const index = new Index({/* use same configuration! */}); -inject(index); -``` - -That's it. - -> You'll need to use the same configuration as you used before the export. Any changes on the configuration needs to be re-indexed. - -### 2. Create just a function body as string - -Alternatively you can use lazy function declaration by passing `false` to the serialize function: - -```js -const fn_body = index.serialize(false); -``` - -You will get just the function body which looks like: - -```js -index.reg = new Set([/* ... */]); -index.map = new Map([/* ... */]); -index.ctx = new Map([/* ... */]); -``` - -Now you can place this in your code directly (name your index as `index`), or you can also create an inject function from it, e.g.: - -```js -const inject = new Function("index", fn_body); -``` - -This function is callable like the above example: - -```js -const index = new Index(); -inject(index); -``` - -## Load Library (Node.js, ESM, Legacy Browser) - - - -```bash -npm install flexsearch -``` -The **_dist_** folder are located in: `node_modules/flexsearch/dist/` - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
BuildFileCDN
flexsearch.bundle.debug.jsDownloadhttps://cdn.jsdelivr.net/gh/nextapps-de/flexsearch@0.8.1/dist/flexsearch.bundle.debug.js
flexsearch.bundle.min.jsDownloadhttps://cdn.jsdelivr.net/gh/nextapps-de/flexsearch@0.8.1/dist/flexsearch.bundle.min.js
flexsearch.bundle.module.debug.jsDownloadhttps://cdn.jsdelivr.net/gh/nextapps-de/flexsearch@0.8.1/dist/flexsearch.bundle.module.debug.js
flexsearch.bundle.module.min.jsDownloadhttps://cdn.jsdelivr.net/gh/nextapps-de/flexsearch@0.8.1/dist/flexsearch.bundle.module.min.js
flexsearch.es5.debug.jsDownloadhttps://cdn.jsdelivr.net/gh/nextapps-de/flexsearch@0.8.1/dist/flexsearch.es5.debug.js
flexsearch.es5.min.jsDownloadhttps://cdn.jsdelivr.net/gh/nextapps-de/flexsearch@0.8.1/dist/flexsearch.es5.min.js
flexsearch.light.debug.jsDownloadhttps://cdn.jsdelivr.net/gh/nextapps-de/flexsearch@0.8.1/dist/flexsearch.light.debug.js
flexsearch.light.min.jsDownloadhttps://cdn.jsdelivr.net/gh/nextapps-de/flexsearch@0.8.1/dist/flexsearch.light.min.js
flexsearch.light.module.debug.jsDownloadhttps://cdn.jsdelivr.net/gh/nextapps-de/flexsearch@0.8.1/dist/flexsearch.light.module.debug.js
flexsearch.light.module.min.jsDownloadhttps://cdn.jsdelivr.net/gh/nextapps-de/flexsearch@0.8.1/dist/flexsearch.light.module.min.js
Javascript Modules (ESM)Downloadhttps://github.com/nextapps-de/flexsearch/tree/0.8.1/dist/module
Javascript Modules Minified (ESM)Downloadhttps://github.com/nextapps-de/flexsearch/tree/0.8.1/dist/module-min
Javascript Modules Debug (ESM)Downloadhttps://github.com/nextapps-de/flexsearch/tree/0.8.1/dist/module-debug
flexsearch.custom.jsRead more about "Custom Build"
- -> All debug versions are providing debug information through the console and gives you helpful advices on certain situations. Do not use them in production, since they are special builds containing extra debugging processes which noticeably reduce performance. - -The abbreviations used at the end of the filenames indicates: - -- `bundle` All features included, FlexSearch is available on `window.FlexSearch` -- `light` Only basic features are included, FlexSearch is available on `window.FlexSearch` -- `es5` bundle has support for EcmaScript5, FlexSearch is available on `window.FlexSearch` -- `module` indicates that this bundle is a Javascript module (ESM), FlexSearch members are available by `import { Index, Document, Worker, Encoder, Charset } from "./flexsearch.bundle.module.min.js"` or alternatively using the default export `import FlexSearch from "./flexsearch.bundle.module.min.js"` -- `min` bundle is minified -- `debug` bundle has enabled debug mode and contains additional code just for debugging purposes (do not use for production) - -### Non-Module Bundles (ES5 Legacy) - -> Non-Module Bundles export all their features to the public namespace "FlexSearch" e.g. `window.FlexSearch.Index` or `window.FlexSearch.Document`. - -Load the bundle by a script tag: - -```html - - -``` - -FlexSearch Members are accessible on: -```js -var Index = window.FlexSearch.Index; -var Document = window.FlexSearch.Document; -var Encoder = window.FlexSearch.Encoder; -var Charset = window.FlexSearch.Charset; -var Resolver = window.FlexSearch.Resolver; -var Worker = window.FlexSearch.Worker; -var IdxDB = window.FlexSearch.IndexedDB; -// only exported by non-module builds: -var Language = window.FlexSearch.Language; -``` - -Load language packs: - -```html - - - - - - - -``` - -### Module (ESM) - -When using modules you can choose from 2 variants: `flexsearch.xxx.module.min.js` has all features bundled ready for production, whereas the folder `/dist/module/` export all the features in the same structure as the source code but here compiler flags was resolved. - -Also, for each variant there exist: -1. A debug version for the development -2. A pre-compiled minified version for production - -Use the bundled version exported as a module (default export): - -```html - -``` - -Or import FlexSearch members separately by: - -```html - -``` - -Use non-bundled modules: - -```html - -``` - -Language packs are accessible via: - -```js -import EnglishEncoderPreset from "./dist/module/lang/en.js"; -import GermanEncoderPreset from "./dist/module/lang/de.js"; -import FrenchEncoderPreset from "./dist/module/lang/fr.js"; -``` - -Also, pre-compiled non-bundled production-ready modules are located in `dist/module-min/`, whereas the debug version is located at `dist/module-debug/`. - -You can also load modules via CDN: - -```html - -``` - -### Node.js - -Install FlexSearch via NPM: - -```npm -npm install flexsearch -``` - -Use the default export: - -```js -const FlexSearch = require("flexsearch"); -const index = new FlexSearch.Index(/* ... */); -``` - -Or require FlexSearch members separately by: - -```js -const { Index, Document, Encoder, Charset, Resolver, Worker, IdxDB } = require("flexsearch"); -const index = new Index(/* ... */); -``` - -When you are using ESM in Node.js then just use the Modules explained one section above. - -Language packs are accessible via: - -```js -const EnglishEncoderPreset = require("flexsearch/lang/en"); -const GermanEncoderPreset = require("flexsearch/lang/de"); -const FrenchEncoderPreset = require("flexsearch/lang/fr"); -``` - -Persistent Connectors are accessible via: - -```js -const Postgres = require("flexsearch/db/postgres"); -const Sqlite = require("flexsearch/db/sqlite"); -const MongoDB = require("flexsearch/db/mongodb"); -const Redis = require("flexsearch/db/redis"); -const Clickhouse = require("flexsearch/db/clickhouse"); -``` - - - -## Custom Builds - -The `/src/` folder of this repository requires some compilation to resolve the build flags. Those are your options: - -- Closure Compiler (Advanced Compilation) (used by this library here) -- Babel + Plugin `babel-plugin-conditional-compile` (used by this library here) - -You can't resolve build flags with: - -- Webpack -- esbuild -- rollup -- Terser - -These are some of the basic builds located in the `/dist/` folder: - -```bash -npm run build:bundle -npm run build:light -npm run build:module -npm run build:es5 -``` - -Perform a custom build (UMD bundle) by passing build flags: - -```bash -npm run build:custom SUPPORT_DOCUMENT=true SUPPORT_TAGS=true LANGUAGE_OUT=ECMASCRIPT5 POLYFILL=true -``` - -Perform a custom build in ESM module format: - -```bash -npm run build:custom RELEASE=custom.module SUPPORT_DOCUMENT=true SUPPORT_TAGS=true -``` - -Perform a debug build: - -```bash -npm run build:custom DEBUG=true SUPPORT_DOCUMENT=true SUPPORT_TAGS=true -``` - -> On custom builds each build flag will be set to `false` by default when not passed. - -The custom build will be saved to `dist/flexsearch.custom.xxxx.min.js` or when format is module to `dist/flexsearch.custom.module.xxxx.min.js` (the "xxxx" is a hash based on the used build flags). - - - -### Supported Build Flags - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
FlagValuesInfo

Feature Flags
SUPPORT_WORKERtrue, false
SUPPORT_ENCODERtrue, false
SUPPORT_CHARSETtrue, false
SUPPORT_CACHEtrue, false
SUPPORT_ASYNCtrue, falseAsynchronous Rendering (support Promises)
SUPPORT_STOREtrue, false
SUPPORT_SUGGESTIONtrue, false
SUPPORT_SERIALIZEtrue, false
SUPPORT_DOCUMENTtrue, false
SUPPORT_TAGStrue, false
SUPPORT_PERSISTENTtrue, false
SUPPORT_KEYSTOREtrue, false
SUPPORT_COMPRESSIONtrue, false
SUPPORT_RESOLVERtrue, false

Compiler Flags
DEBUGtrue, falseOutput debug information to the console (default: false)
RELEASE




custom
custom.module
bundle
bundle.module
es5
light
compact
POLYFILLtrue, falseInclude Polyfills (based on LANGUAGE_OUT)
PROFILERtrue, falseJust used for automatic performance tests
LANGUAGE_OUT










ECMASCRIPT3
ECMASCRIPT5
ECMASCRIPT_2015
ECMASCRIPT_2016
ECMASCRIPT_2017
ECMASCRIPT_2018
ECMASCRIPT_2019
ECMASCRIPT_2020
ECMASCRIPT_2021
ECMASCRIPT_2022
ECMASCRIPT_NEXT
STABLE
Target language
- -### Misc - -A formula to determine a well-balanced value for the `resolution` is: $2*floor(\sqrt{content.length})$ where content is the value pushed by `index.add()`. Here the maximum length of all contents should be used. - -## Import / Export (In-Memory) - -> Persistent-Indexes and Worker-Indexes don't support Import/Export. - -Export an `Index` or `Document-Index` to the folder `/export/`: - -```js -import { promises as fs } from "fs"; - -await index.export(async function(key, data){ - await fs.writeFile("./export/" + key, data, "utf8"); -}); -``` - -Import from folder `/export/` into an `Index` or `Document-Index`: - -```js -const index = new Index({/* keep old config and place it here */}); - -const files = await fs.readdir("./export/"); -for(let i = 0; i < files.length; i++){ - const data = await fs.readFile("./export/" + files[i], "utf8"); - await index.import(files[i], data); -} -``` - -> You'll need to use the same configuration as you used before the export. Any changes on the configuration needs to be re-indexed. - -## Migration - -- The index option property "minlength" has moved to the Encoder Class -- The index option flag "optimize" was removed -- The index option flag "lang" was replaced by the Encoder Class `.assign()` -- Boost cannot apply upfront anymore when indexing, instead you can use the boost property on a query dynamically -- All definitions of the old text encoding process was replaced by similar definitions (Array changed to Set, Object changed to Map). You can use of the helper methods like `.addMatcher(char_match, char_replace)` which adds everything properly. -- The default value for `fastupdate` is set to `false` by default when not passed via options -- The method `index.encode()` has moved to `index.encoder.encode()` -- The options `charset` and `lang` was removed from index (replaced by `Encoder.assign({...})`) -- Every charset collection (files in folder `/lang/**.js`) is now exported as a config object (instead of a function). This config needs to be created by passing to the constructor `new Encoder(config)` or can be added to an existing instance via `encoder.assign(config)`. The reason was to keep the default encoder configuration when having multiple document indexes. -- The property `bool` from DocumentOptions was removed (replaced by `Resolver`) -- The static methods `FlexSearch.registerCharset()` and `FlexSearch.registerLanguage()` was removed, those collections are now exported to `FlexSearch.Charset` which can be accessed as module `import { Charset } from "flexsearch"` and language packs are now applied by `encoder.assign()` -- Instead of e.g. "latin:simple" the Charset collection is exported as a module and has to be imported by e.g. `import LatinSimple from "./charset.js"` and then assigned to an existing Encoder by `encoder.assign(LatinSimple)` or by creation `encoder = new Encoder(LatinSimple)` -- You can import language packs by `dist/module/lang/*` when using ESM and by `const EnglishPreset = require("flexsearch/lang/en");` when using CommonJS (Node.js) -- The method `index.append()` is now deprecated and will be removed in the near future, because it isn't consistent and leads into unexpected behavior when not used properly. You should only use `index.add()` to push contents to the index. -- ~~Using the `async` variants like `.searchAsync` is now deprecated (but still works), asynchronous responses will always return from Worker-Index and from Persistent-Index, everything else will return a non-promised result. Having both types of methods looks like the developers can choose between them, but they can't.~~ -- Any of your exports from versions below v0.8 are not compatible to import into v0.8 - -## What's next? - -Unfortunately, not everything could be finished and needs to be done in the upcoming version. - -- ~~The `Resolver` currently does not support Document-Indexes, there is still some work to do.~~ -- Config serialization for persistent indexes (store configuration, check migrations, import and restore field configurations) -- Tooling for persistent indexes (list all tables, remove tables) diff --git a/doc/resolver.md b/doc/resolver.md index 99a117a..787e8f3 100644 --- a/doc/resolver.md +++ b/doc/resolver.md @@ -35,11 +35,12 @@ const raw = index.search("a short query", { const result = raw.resolve(); ``` -Or use declaration style: +Alternatively you can create a `Resolver` by passing an initial query: ```js -import Resolver from "./resolver.js"; -const raw = new Resolver({ +import { Resolver } from "flexsearch"; +const raw = new Resolver({ + // pass the index is required when query was set index: index, query: "a short query" }); @@ -74,25 +75,26 @@ const result = raw3.resolve({ console.log("The final result is:", result) ``` -Use inline queries: +Chain operations and nest inline queries: ```js const result = index.search("further query", { // set resolve to false on the first query resolve: false, + // boost the first query boost: 2 }) -.or( // union - index.search("a query") - .and( // intersection - index.search("another query", { - boost: 2 - }) - ) -) -.not( // exclusion - index.search("some query") -) +.or({ + // nested expression + and: [{ + query: "a query" + },{ + query: "another query" + }] +}) +.not({ + query: "some query" +}) // resolve the result .resolve({ limit: 100, @@ -100,24 +102,27 @@ const result = index.search("further query", { }); ``` +Alternatively you can create a `Resolver` by passing an initial query: + ```js -import Resolver from "./resolver.js"; +import { Resolver } from "flexsearch"; const result = new Resolver({ + // pass the index is required when query was set index: index, query: "further query", boost: 2 }) .or({ - and: [{ // inner expression - index: index, + and: [{ query: "a query" },{ + // you can bind a different index for this + // query when IDs are from same source index: index, - query: "another query", - boost: 2 + query: "another query" }] }) -.not({ // exclusion +.not({ index: index, query: "some query" }) @@ -127,67 +132,83 @@ const result = new Resolver({ }); ``` -When all queries are made against the same index, you can skip the index in every declaration followed after initially calling `new Resolve()`: +When all queries are made against the same index, you can skip the index in every resolver stage followed after initially calling `new Resolve({ index: ... })`: ```js -import Resolver from "./resolver.js"; +import { Resolver } from "flexsearch"; const result = new Resolver({ index: index, query: "a query" }) -.and({ query: "another query", boost: 2 }) +.and({ query: "another query" }) .or ({ query: "further query", boost: 2 }) .not({ query: "some query" }) .resolve(100); ``` +### Using Cached Queries + +```js +import { Resolver } from "flexsearch"; +const result = new Resolver({ + index: index, + query: "a query", + cache: true +}) +.and({ + query: "another query", + cache: true +}) +.resolve(100); +``` + +### Using Async Queries (incl. Runtime Balancer) + +All async tasks will run in parallel, balanced by the runtime observer: + +```js +import { Resolver } from "flexsearch"; +const resolver = new Resolver({ + index: index, + query: "a query", + async: true +}) +.and({ + query: "another query", + async: true +}) +.or({ + query: "some query", + async: true +}); +const result = await resolver.resolve(100); +``` + +### Queuing Async Queries + +All queued tasks will run consecutively, also balanced by the runtime observer: + +```js +import { Resolver } from "flexsearch"; +const resolver = await new Resolver({ + index: index, + query: "a query", + async: true +}) +.and({ + query: "another query", + queue: true +}) +.or({ + query: "some query", + queue: true +}) +.resolve(100); +``` + +When tasks are processed consecutively, it will skip specific resolver stages when there is no result expected. + - ### Custom Resolver ```js @@ -202,3 +223,4 @@ const result = index.search("a short query", { resolve: CustomResolver }); ``` +--> diff --git a/example/browser-legacy/basic-persistent/index.html b/example/browser-legacy/basic-persistent/index.html index 1526ea1..6266f90 100644 --- a/example/browser-legacy/basic-persistent/index.html +++ b/example/browser-legacy/basic-persistent/index.html @@ -6,7 +6,7 @@ Example: browser-legacy-basic-persistent - + + + + + + + + + \ No newline at end of file diff --git a/example/browser-legacy/document-worker/index.html b/example/browser-legacy/document-worker/index.html index 2d0efb7..1937d90 100644 --- a/example/browser-legacy/document-worker/index.html +++ b/example/browser-legacy/document-worker/index.html @@ -6,7 +6,7 @@ Example: browser-legacy-document-worker - + + + - + + + \ No newline at end of file diff --git a/example/browser-module/document-worker-extern-config/config.originalTitle.js b/example/browser-module/document-worker-extern-config/config.originalTitle.js index 3451a16..72a645b 100644 --- a/example/browser-module/document-worker-extern-config/config.originalTitle.js +++ b/example/browser-module/document-worker-extern-config/config.originalTitle.js @@ -1,5 +1,5 @@ -import { Encoder, Charset } from "https://cdn.jsdelivr.net/gh/nextapps-de/flexsearch@0.8.1/dist/flexsearch.bundle.module.min.js"; -import EnglishPreset from "https://cdn.jsdelivr.net/gh/nextapps-de/flexsearch@0.8.1/dist/module/lang/en.js"; +import { Encoder, Charset } from "https://cdn.jsdelivr.net/gh/nextapps-de/flexsearch@0.8.2/dist/flexsearch.bundle.module.min.js"; +import EnglishPreset from "https://cdn.jsdelivr.net/gh/nextapps-de/flexsearch@0.8.2/dist/module/lang/en.js"; export default { tokenize: "forward", diff --git a/example/browser-module/document-worker-extern-config/config.primaryTitle.js b/example/browser-module/document-worker-extern-config/config.primaryTitle.js index 3451a16..72a645b 100644 --- a/example/browser-module/document-worker-extern-config/config.primaryTitle.js +++ b/example/browser-module/document-worker-extern-config/config.primaryTitle.js @@ -1,5 +1,5 @@ -import { Encoder, Charset } from "https://cdn.jsdelivr.net/gh/nextapps-de/flexsearch@0.8.1/dist/flexsearch.bundle.module.min.js"; -import EnglishPreset from "https://cdn.jsdelivr.net/gh/nextapps-de/flexsearch@0.8.1/dist/module/lang/en.js"; +import { Encoder, Charset } from "https://cdn.jsdelivr.net/gh/nextapps-de/flexsearch@0.8.2/dist/flexsearch.bundle.module.min.js"; +import EnglishPreset from "https://cdn.jsdelivr.net/gh/nextapps-de/flexsearch@0.8.2/dist/module/lang/en.js"; export default { tokenize: "forward", diff --git a/example/browser-module/document/index.html b/example/browser-module/document/index.html index f06ff63..eb4cb3c 100644 --- a/example/browser-module/document/index.html +++ b/example/browser-module/document/index.html @@ -8,7 +8,7 @@