diff --git a/dist/flexsearch.bundle.debug.js b/dist/flexsearch.bundle.debug.js index 1d99cb2..2e0fdab 100644 --- a/dist/flexsearch.bundle.debug.js +++ b/dist/flexsearch.bundle.debug.js @@ -1,5 +1,5 @@ /**! - * FlexSearch.js v0.8.155 (Bundle/Debug) + * FlexSearch.js v0.8.156 (Bundle/Debug) * Author and Copyright: Thomas Wilkerling * Licence: Apache-2.0 * Hosted by Nextapps GmbH @@ -50,17 +50,17 @@ function A() { function D(a) { return "string" === typeof a; } -function G(a) { +function aa(a) { return "object" === typeof a; } -function aa(a) { +function ba(a) { const c = []; for (const b of a.keys()) { c.push(b); } return c; } -function ba(a, c) { +function ca(a, c) { if (D(c)) { a = a[c]; } else { @@ -70,17 +70,17 @@ function ba(a, c) { } return a; } -function ca(a) { +function da(a) { let c = 0; for (let b = 0, e; b < a.length; b++) { (e = a[b]) && c < e.length && (c = e.length); } return c; } -;const da = /[^\p{L}\p{N}]+/u, ea = /(\d{3})/g, fa = /(\D)(\d{3})/g, ha = /(\d{3})(\D)/g, ia = /[\u0300-\u036f]/g; -function ja(a = {}) { - if (!this || this.constructor !== ja) { - return new ja(...arguments); +;const ea = /[^\p{L}\p{N}]+/u, fa = /(\d{3})/g, ha = /(\D)(\d{3})/g, ia = /(\d{3})(\D)/g, ja = /[\u0300-\u036f]/g; +function G(a = {}) { + if (!this || this.constructor !== G) { + return new G(...arguments); } if (arguments.length) { for (a = 0; a < arguments.length; a++) { @@ -90,7 +90,7 @@ function ja(a = {}) { this.assign(a); } } -t = ja.prototype; +t = G.prototype; t.assign = function(a) { this.normalize = z(a.normalize, !0, this.normalize); let c = a.include, b = c || a.exclude || a.split, e; @@ -118,7 +118,7 @@ t.assign = function(a) { this.numeric = z(a.numeric, e); } else { try { - this.split = z(this.split, da); + this.split = z(this.split, ea); } catch (d) { console.warn("This platform does not support unicode regex. It falls back to using simple whitespace splitter instead: /s+/."), this.split = /\s+/; } @@ -213,9 +213,9 @@ t.encode = function(a) { this.H = setTimeout(H, 50, this); } } - this.normalize && ("function" === typeof this.normalize ? a = this.normalize(a) : a = ia ? a.normalize("NFKD").replace(ia, "").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(fa, "$1 $2").replace(ha, "$1 $2").replace(ea, "$1 ")); + this.numeric && 3 < a.length && (a = a.replace(ha, "$1 $2").replace(ia, "$1 $2").replace(fa, "$1 ")); const c = !(this.dedupe || this.mapper || this.filter || this.matcher || this.stemmer || this.replacer); let b = [], e = this.split || "" === this.split ? a.split(this.split) : a; for (let f = 0, g, h; f < e.length; f++) { @@ -661,7 +661,7 @@ function Ba(a) { return b; } ;U.prototype.add = function(a, c, b) { - G(a) && (c = a, a = ba(c, this.key)); + aa(a) && (c = a, a = ca(c, this.key)); if (c && (a || 0 === a)) { if (!b && this.reg.has(a)) { return this.update(a, c); @@ -693,7 +693,7 @@ function Ba(a) { continue; } f.constructor === String && (f = "" + f); - f = ba(c, f); + f = ca(c, f); } if (d && f) { D(f) && (f = [f]); @@ -1032,7 +1032,7 @@ function La(a, c, b, e, d, f, g) { if (this.result.length && a.unshift(this.result), 2 > a.length) { this.result = a[0]; } else { - if (c = ca(a)) { + if (c = da(a)) { return this.result = Fa(a, c, b, e, g, this.h, f), f ? d ? V.call(this.index, this.result) : this.result : this; } this.result = []; @@ -1214,7 +1214,7 @@ W.prototype.resolve = function(a, c, b) { }; A(); U.prototype.search = function(a, c, b, e) { - b || (!c && G(a) ? (b = a, a = "") : G(c) && (b = c, c = 0)); + b || (!c && aa(a) ? (b = a, a = "") : aa(c) && (b = c, c = 0)); let d = []; var f = []; let g; @@ -1439,7 +1439,7 @@ function Ra(a, c, b, e, d) { h = m.tokenize; l !== f && (f = l, g = f.encode(a)); for (let r = 0; r < p.length; r++) { - let q = "", u = ba(p[r].doc, n).split(/\s+/); + let q = "", u = ca(p[r].doc, n).split(/\s+/); for (let v = 0, w, x; v < u.length; v++) { w = u[v]; x = l.encode(w); @@ -1524,7 +1524,7 @@ function V(a) { this.store = b && (e ? new S(e) : new Map()); this.cache = (b = a.cache || null) && new X(b); a.cache = !1; - this.worker = a.worker; + this.worker = a.worker || !1; this.priority = a.priority || 4; this.index = Ua.call(this, a, c); this.tag = null; @@ -1547,19 +1547,29 @@ function V(a) { } if (this.worker) { this.fastupdate = !1; - a = []; - for (const d of this.index.values()) { - d.then && a.push(d); + const d = []; + for (const f of this.index.values()) { + f.then && d.push(f); } - if (a.length) { - const d = this; - return Promise.all(a).then(function(f) { - let g = 0; - for (const h of d.index.entries()) { - const k = h[0]; - h[1].then && d.index.set(k, f[g++]); + if (d.length) { + const f = this; + return Promise.all(d).then(function(g) { + const h = new Map(); + let k = 0; + for (const m of f.index.entries()) { + const n = m[0]; + var l = m[1]; + if (l.then) { + l = d[k].encoder || {}; + let p = h.get(l); + p || (p = l.encode ? l : new G(l), h.set(l, p)); + l = g[k]; + l.encoder = p; + f.index.set(n, l); + k++; + } } - return d; + return f; }); } } else { @@ -1573,28 +1583,30 @@ t.mount = function(a) { } let c = this.field; if (this.tag) { - for (let d = 0, f; d < this.R.length; d++) { - f = this.R[d]; + for (let f = 0, g; f < this.R.length; f++) { + g = this.R[f]; var b = void 0; - this.index.set(f, b = new N({}, this.reg)); + this.index.set(g, b = new N({}, this.reg)); c === this.field && (c = c.slice(0)); - c.push(f); - b.tag = this.tag.get(f); + c.push(g); + b.tag = this.tag.get(g); } } b = []; const e = {db:a.db, type:a.type, fastupdate:a.fastupdate}; - for (let d = 0, f, g; d < c.length; d++) { - e.field = g = c[d]; - f = this.index.get(g); - const h = new a.constructor(a.id, e); - h.id = a.id; - b[d] = h.mount(f); - f.document = !0; - d ? f.bypass = !0 : f.store = this.store; + 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, e); + k.id = a.id; + b[f] = k.mount(g); + g.document = !0; + f ? g.bypass = !0 : g.store = this.store; } - this.db = !0; - return Promise.all(b); + const d = this; + return this.db = Promise.all(b).then(function() { + d.db = !0; + }); }; t.commit = async function(a, c) { const b = []; @@ -1618,9 +1630,10 @@ function Ua(a, c) { for (let d = 0, f, g; d < e.length; d++) { f = e[d]; D(f) || (g = f, f = f.field); - g = G(g) ? Object.assign({}, a, g) : a; + g = aa(g) ? Object.assign({}, a, g) : a; if (this.worker) { const h = new P(g); + h.encoder = g.encoder; b.set(f, h); } this.worker || b.set(f, new N(g, this.reg)); @@ -1652,7 +1665,7 @@ t.update = function(a, c) { return this.remove(a).add(a, c); }; t.remove = function(a) { - G(a) && (a = ba(a, this.key)); + aa(a) && (a = ca(a, this.key)); for (var c of this.index.values()) { c.remove(a, !0); } @@ -1702,7 +1715,7 @@ t.get = function(a) { }) : this.store.get(a) || null; }; t.set = function(a, c) { - "object" === typeof a && (c = a, a = ba(c, this.key)); + "object" === typeof a && (c = a, a = ca(c, this.key)); this.store.set(a, c); return this; }; @@ -2077,7 +2090,7 @@ function ib(a, c, b, e, d, f, g, h) { } b = a.context; const e = !0 === b ? {depth:1} : b || {}, d = D(a.encoder) ? bb[a.encoder] : a.encode || a.encoder || {}; - this.encoder = d.encode ? d : "object" === typeof d ? new ja(d) : {encode:d}; + this.encoder = d.encode ? d : "object" === typeof d ? new G(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; @@ -2210,7 +2223,7 @@ t.serialize = function(a = !0) { la(N.prototype); const lb = "undefined" !== typeof window && (window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB), mb = ["map", "ctx", "tag", "reg", "cfg"], Y = A(); function nb(a, c = {}) { - if (!this) { + if (!this || this.constructor !== nb) { return new nb(a, c); } "object" === typeof a && (c = a, a = a.name); @@ -2224,7 +2237,7 @@ function nb(a, c = {}) { } t = nb.prototype; t.mount = function(a) { - if (!a.encoder) { + if (a.index) { return a.mount(this); } a.db = this; @@ -2376,7 +2389,7 @@ t.commit = async function(a, c, b) { e[d] = f.del; } } - c || (b || (e = e.concat(aa(a.reg))), e.length && await this.remove(e)); + c || (b || (e = e.concat(ba(a.reg))), e.length && await this.remove(e)); } a.reg.size && (await this.transaction("map", "readwrite", function(e) { for (const d of a.map) { @@ -2511,7 +2524,7 @@ function Z(a, c) { a = null; }); } -;const pb = {Index:N, Charset:bb, Encoder:ja, Document:U, Worker:P, Resolver:W, IndexedDB:nb, Language:{}}, qb = "undefined" !== typeof self ? self : "undefined" !== typeof global ? global : self; +;const pb = {Index:N, Charset:bb, Encoder:G, Document:U, Worker:P, Resolver:W, IndexedDB:nb, Language:{}}, qb = "undefined" !== typeof self ? self : "undefined" !== typeof global ? global : self; let sb; (sb = qb.define) && sb.amd ? sb([], function() { return pb; diff --git a/dist/flexsearch.bundle.min.js b/dist/flexsearch.bundle.min.js index cc0af81..634a2b5 100644 --- a/dist/flexsearch.bundle.min.js +++ b/dist/flexsearch.bundle.min.js @@ -1,19 +1,19 @@ /**! - * FlexSearch.js v0.8.155 (Bundle) + * FlexSearch.js v0.8.156 (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 t;function z(a,b,c){const e=typeof c,d=typeof a;if("undefined"!==e){if("undefined"!==d){if(c){if("function"===d&&e===d)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"===d?b:a}function A(){return Object.create(null)}function D(a){return"string"===typeof a} -function G(a){return"object"===typeof a}function aa(a){const b=[];for(const c of a.keys())b.push(c);return b}function H(a,b){if(D(b))a=a[b];else for(let c=0;a&&c"a1a".split(c).length; -this.numeric=z(a.numeric,e)}else{try{this.split=z(this.split,ca)}catch(d){this.split=/\s+/}this.numeric=z(a.numeric,z(this.numeric,!0))}this.prepare=z(a.prepare,null,this.prepare);this.finalize=z(a.finalize,null,this.finalize);c=a.filter;this.filter="function"===typeof c?c:z(c&&new Set(c),null,this.filter);this.dedupe=z(a.dedupe,!0,this.dedupe);this.matcher=z((c=a.matcher)&&new Map(c),null,this.matcher);this.mapper=z((c=a.mapper)&&new Map(c),null,this.mapper);this.stemmer=z((c=a.stemmer)&&new Map(c), +this.numeric=z(a.numeric,e)}else{try{this.split=z(this.split,da)}catch(d){this.split=/\s+/}this.numeric=z(a.numeric,z(this.numeric,!0))}this.prepare=z(a.prepare,null,this.prepare);this.finalize=z(a.finalize,null,this.finalize);c=a.filter;this.filter="function"===typeof c?c:z(c&&new Set(c),null,this.filter);this.dedupe=z(a.dedupe,!0,this.dedupe);this.matcher=z((c=a.matcher)&&new Map(c),null,this.matcher);this.mapper=z((c=a.mapper)&&new Map(c),null,this.mapper);this.stemmer=z((c=a.stemmer)&&new Map(c), null,this.stemmer);this.replacer=z(a.replacer,null,this.replacer);this.minlength=z(a.minlength,1,this.minlength);this.maxlength=z(a.maxlength,1024,this.maxlength);this.rtl=z(a.rtl,!1,this.rtl);if(this.cache=c=z(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 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};t.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&&I(this);return this};t.addFilter=function(a){"function"===typeof a?this.filter=a:(this.filter||(this.filter=new Set),this.filter.add(a));this.cache&&I(this);return this}; t.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&&I(this);return this}; t.addReplacer=function(a,b){if("string"===typeof a)return this.addMatcher(a,b);this.replacer||(this.replacer=[]);this.replacer.push(a,b);this.cache&&I(this);return this}; -t.encode=function(a){if(this.cache&&a.length<=this.K)if(this.H){if(this.B.has(a))return this.B.get(a)}else this.H=setTimeout(I,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(b)c.push(g);else if(!this.filter||("function"===typeof this.filter?this.filter(g):!this.filter.has(g))){if(this.cache&&g.length<=this.L)if(this.H){var d=this.G.get(g);if(d||""===d){d&&c.push(d);continue}}else this.H=setTimeout(I,50,this);this.stemmer&&2this.stemmer.get(k))); if(g&&(this.mapper||this.dedupe&&1this.matcher.get(k)));if(g&&this.replacer)for(d=0;g&&dthis.S&& (this.G.clear(),this.L=this.L/1.1|0));g&&c.push(g)}this.finalize&&(c=this.finalize(c)||c);this.cache&&a.length<=this.K&&(this.B.set(a,c),this.B.size>this.S&&(this.B.clear(),this.K=this.K/1.1|0));return c};function I(a){a.H=null;a.B.clear();a.G.clear()};let K,ja;async function ka(a){a=a.data;var b=a.task;const c=a.id;let e=a.args;switch(b){case "init":ja=a.options||{};(b=a.factory)?(Function("return "+b)()(self),K=new self.FlexSearch.Index(ja),delete self.FlexSearch):K=new M(ja);postMessage({id:c});break;default:let d;"export"===b&&(e[1]?(e[0]=ja.export,e[2]=0,e[3]=1):e=null);"import"===b?e[0]&&(a=await ja.import.call(K,e[0]),K.import(e[0],a)):(d=e&&K[b].apply(K,e))&&d.then&&(d=await d);postMessage("search"===b?{id:c,msg:d}:{id:c})}};function la(a){ma.call(a,"add");ma.call(a,"append");ma.call(a,"search");ma.call(a,"update");ma.call(a,"remove")}let na,oa,pa;function qa(){na=pa=0} @@ -31,15 +31,15 @@ if("includes"===e)return function(d){for(let f=0;fc||e?k.slice(e,c+e):k;else{if(ac||e)k=k.slice(e,c+e)}else{d= [];for(let n=0,p;ne)e-=p.length;else{if(p.length>c||e)p=p.slice(e,c+e),c-=p.length,e&&(e-=p.length);d.push(p);if(!c)break}k=1b?b?a.slice(c,c+b):a.slice(c):a,e?V.call(this,a):a;let d=[];for(let f=0,g,h;f=h){c-=h;continue}cb&&(g=g.slice(0,b),h=b);if(!d.length&&h>=b)return e?V.call(this,g):g;d.push(g);b-=h;if(!b)break}d=1a.length?this.result=a[0]:(this.result=Ga(a,c,e,!1,this.h),e=0));return f?this.resolve(c,e,d):this};W.prototype.and=function(){let a=this.result.length,b,c,e,d;if(!a){const f=arguments[0];f&&(a=!!f.suggest,d=f.resolve,b=f.limit,c=f.offset,e=f.enrich&&d)}if(a){const {O:f,P:g,limit:h,offset:k,enrich:l,resolve:m,suggest:n}=Ja(this,"and",arguments);return La.call(this,f,g,h,k,l,m,n)}return d?this.resolve(b,c,e):this}; -function La(a,b,c,e,d,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=ba(a))return this.result=Fa(a,b,c,e,g,this.h,f),f?d?V.call(this.index,this.result):this.result:this;this.result=[]}else g||(this.result=a);return f?this.resolve(c,e,d):this};W.prototype.xor=function(){const {O:a,P:b,limit:c,offset:e,enrich:d,resolve:f,suggest:g}=Ja(this,"xor",arguments);return Ma.call(this,a,b,c,e,d,f,g)}; +function La(a,b,c,e,d,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=ca(a))return this.result=Fa(a,b,c,e,g,this.h,f),f?d?V.call(this.index,this.result):this.result:this;this.result=[]}else g||(this.result=a);return f?this.resolve(c,e,d):this};W.prototype.xor=function(){const {O:a,P:b,limit:c,offset:e,enrich:d,resolve:f,suggest:g}=Ja(this,"xor",arguments);return Ma.call(this,a,b,c,e,d,f,g)}; function Ma(a,b,c,e,d,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=Na.call(this,a,c,e,f,this.h),f?d?V.call(this.index,this.result):this.result:this;else g||(this.result=a);return f?this.resolve(c,e,d):this} function Na(a,b,c,e,d){const f=[],g=A();let h=0;for(let k=0,l;kc||e)a=a.slice(e,e+c);d&&(a=V.call(this,a));return a}}function V(a){if(!this||!this.store)return a;const b=Array(a.length);for(let c=0,e;cc||e)a=a.slice(e,e+c);d&&(a=V.call(this,a));return a}}function V(a){if(!this||!this.store)return a;const b=Array(a.length);for(let c=0,e;cthis.limit&&this.cache.delete(this.cache.keys().next().value)}; @@ -72,14 +72,14 @@ function gb(a,b,c,e,d,f,g){let h=g?a.ctx:a.map,k;if(!b[c]||g&&!(k=b[c])[g])if(g? function fb(a,b,c,e,d){return c&&1c)&&(k=c,c=b,b=k);if(a.db)return a.db.get(b,c,e,d,f,g,h);a=c?(a=a.ctx.get(c))&&a.get(b):a.map.get(b);return a};function M(a,b){if(!this||this.constructor!==M)return new M(a);if(a){var c=D(a)?a:a.preset;c&&(a=Object.assign({},eb[c],a))}else a={};c=a.context;const e=!0===c?{depth:1}:c||{},d=D(a.encoder)?bb[a.encoder]:a.encode||a.encoder||{};this.encoder=d.encode?d:"object"===typeof d?new ia(d):{encode:d};this.resolution=a.resolution||9;this.tokenize=c=(c=a.tokenize)&&"default"!==c&&"exact"!==c&&c||"strict";this.depth="strict"===c&&e.depth||0;this.bidirectional=!1!==e.bidirectional;this.fastupdate=!!a.fastupdate; +function jb(a,b,c,e){let d=[];if(a&&a.length){if(a.length<=e){b.push(a);return}for(let f=0,g;fc)&&(k=c,c=b,b=k);if(a.db)return a.db.get(b,c,e,d,f,g,h);a=c?(a=a.ctx.get(c))&&a.get(b):a.map.get(b);return a};function M(a,b){if(!this||this.constructor!==M)return new M(a);if(a){var c=D(a)?a:a.preset;c&&(a=Object.assign({},eb[c],a))}else a={};c=a.context;const e=!0===c?{depth:1}:c||{},d=D(a.encoder)?bb[a.encoder]:a.encode||a.encoder||{};this.encoder=d.encode?d:"object"===typeof d?new H(d):{encode:d};this.resolution=a.resolution||9;this.tokenize=c=(c=a.tokenize)&&"default"!==c&&"exact"!==c&&c||"strict";this.depth="strict"===c&&e.depth||0;this.bidirectional=!1!==e.bidirectional;this.fastupdate=!!a.fastupdate; this.score=a.score||null;(c=a.keystore||0)&&(this.keystore=c);this.map=c?new S(c):new Map;this.ctx=c?new S(c):new Map;this.reg=b||(this.fastupdate?c?new S(c):new Map:c?new T(c):new Set);this.U=e.resolution||3;this.rtl=d.rtl||a.rtl||!1;this.cache=(c=a.cache||null)&&new X(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}t=M.prototype; t.mount=function(a){this.commit_timer&&(clearTimeout(this.commit_timer),this.commit_timer=null);return a.mount(this)};t.commit=function(a,b){this.commit_timer&&(clearTimeout(this.commit_timer),this.commit_timer=null);return this.db.commit(this,a,b)};t.destroy=function(){this.commit_timer&&(clearTimeout(this.commit_timer),this.commit_timer=null);return this.db.destroy()};function db(a){a.commit_timer||(a.commit_timer=setTimeout(function(){a.commit_timer=null;a.db.commit(a,void 0,void 0)},1))} t.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};t.append=function(a,b){return this.add(a,b,!0)};t.contain=function(a){return this.db?this.db.has(a):this.reg.has(a)};t.update=function(a,b){const c=this,e=this.remove(a);return e&&e.then?e.then(()=>c.add(a,b)):this.add(a,b)}; t.cleanup=function(){if(!this.fastupdate)return this;cb(this.map);this.depth&&cb(this.ctx);return this};t.searchCache=Va;t.export=function(a,b,c=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=sa(this.map,this.reg.size);break;case 3:d="ctx";f=ua(this.ctx,this.reg.size);break;default:return}return ya.call(this,a,b,d,f,c,e)}; t.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){e-=m.length;continue}const n=c?e+Math.min(m.length-e,c):m.length;for(let p=e;p{a.onsuccess=a.oncomplete=function(){b&&b(this.result);b=null;c(this.result)};a.onerror=a.onblocked=e;a=null})};const pb={Index:M,Charset:bb,Encoder:ia,Document:U,Worker:P,Resolver:W,IndexedDB:nb,Language:{}},rb="undefined"!==typeof self?self:"undefined"!==typeof global?global:self;let sb;(sb=rb.define)&&sb.amd?sb([],function(){return pb}):"object"===typeof rb.exports?rb.exports=pb:rb.FlexSearch=pb;}(this||self)); +function Z(a,b){return new Promise((c,e)=>{a.onsuccess=a.oncomplete=function(){b&&b(this.result);b=null;c(this.result)};a.onerror=a.onblocked=e;a=null})};const pb={Index:M,Charset:bb,Encoder:H,Document:U,Worker:P,Resolver:W,IndexedDB:nb,Language:{}},rb="undefined"!==typeof self?self:"undefined"!==typeof global?global:self;let sb;(sb=rb.define)&&sb.amd?sb([],function(){return pb}):"object"===typeof rb.exports?rb.exports=pb:rb.FlexSearch=pb;}(this||self)); diff --git a/dist/flexsearch.bundle.module.debug.js b/dist/flexsearch.bundle.module.debug.js index 1582ae8..fe1f0cd 100644 --- a/dist/flexsearch.bundle.module.debug.js +++ b/dist/flexsearch.bundle.module.debug.js @@ -1,5 +1,5 @@ /**! - * FlexSearch.js v0.8.155 (Bundle/Module/Debug) + * FlexSearch.js v0.8.156 (Bundle/Module/Debug) * Author and Copyright: Thomas Wilkerling * Licence: Apache-2.0 * Hosted by Nextapps GmbH @@ -49,17 +49,17 @@ function A() { function D(a) { return "string" === typeof a; } -function G(a) { +function aa(a) { return "object" === typeof a; } -function aa(a) { +function ba(a) { const c = []; for (const b of a.keys()) { c.push(b); } return c; } -function ba(a, c) { +function ca(a, c) { if (D(c)) { a = a[c]; } else { @@ -69,17 +69,17 @@ function ba(a, c) { } return a; } -function ca(a) { +function da(a) { let c = 0; for (let b = 0, e; b < a.length; b++) { (e = a[b]) && c < e.length && (c = e.length); } return c; } -;const da = /[^\p{L}\p{N}]+/u, ea = /(\d{3})/g, fa = /(\D)(\d{3})/g, ha = /(\d{3})(\D)/g, ia = /[\u0300-\u036f]/g; -function ja(a = {}) { - if (!this || this.constructor !== ja) { - return new ja(...arguments); +;const ea = /[^\p{L}\p{N}]+/u, fa = /(\d{3})/g, ha = /(\D)(\d{3})/g, ia = /(\d{3})(\D)/g, ja = /[\u0300-\u036f]/g; +function G(a = {}) { + if (!this || this.constructor !== G) { + return new G(...arguments); } if (arguments.length) { for (a = 0; a < arguments.length; a++) { @@ -89,7 +89,7 @@ function ja(a = {}) { this.assign(a); } } -t = ja.prototype; +t = G.prototype; t.assign = function(a) { this.normalize = z(a.normalize, !0, this.normalize); let c = a.include, b = c || a.exclude || a.split, e; @@ -117,7 +117,7 @@ t.assign = function(a) { this.numeric = z(a.numeric, e); } else { try { - this.split = z(this.split, da); + this.split = z(this.split, ea); } catch (d) { console.warn("This platform does not support unicode regex. It falls back to using simple whitespace splitter instead: /s+/."), this.split = /\s+/; } @@ -212,9 +212,9 @@ t.encode = function(a) { this.H = setTimeout(H, 50, this); } } - this.normalize && ("function" === typeof this.normalize ? a = this.normalize(a) : a = ia ? a.normalize("NFKD").replace(ia, "").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(fa, "$1 $2").replace(ha, "$1 $2").replace(ea, "$1 ")); + this.numeric && 3 < a.length && (a = a.replace(ha, "$1 $2").replace(ia, "$1 $2").replace(fa, "$1 ")); const c = !(this.dedupe || this.mapper || this.filter || this.matcher || this.stemmer || this.replacer); let b = [], e = this.split || "" === this.split ? a.split(this.split) : a; for (let f = 0, g, h; f < e.length; f++) { @@ -660,7 +660,7 @@ function Ba(a) { return b; } ;U.prototype.add = function(a, c, b) { - G(a) && (c = a, a = ba(c, this.key)); + aa(a) && (c = a, a = ca(c, this.key)); if (c && (a || 0 === a)) { if (!b && this.reg.has(a)) { return this.update(a, c); @@ -692,7 +692,7 @@ function Ba(a) { continue; } f.constructor === String && (f = "" + f); - f = ba(c, f); + f = ca(c, f); } if (d && f) { D(f) && (f = [f]); @@ -1031,7 +1031,7 @@ function La(a, c, b, e, d, f, g) { if (this.result.length && a.unshift(this.result), 2 > a.length) { this.result = a[0]; } else { - if (c = ca(a)) { + if (c = da(a)) { return this.result = Fa(a, c, b, e, g, this.h, f), f ? d ? V.call(this.index, this.result) : this.result : this; } this.result = []; @@ -1213,7 +1213,7 @@ W.prototype.resolve = function(a, c, b) { }; A(); U.prototype.search = function(a, c, b, e) { - b || (!c && G(a) ? (b = a, a = "") : G(c) && (b = c, c = 0)); + b || (!c && aa(a) ? (b = a, a = "") : aa(c) && (b = c, c = 0)); let d = []; var f = []; let g; @@ -1438,7 +1438,7 @@ function Ra(a, c, b, e, d) { h = m.tokenize; l !== f && (f = l, g = f.encode(a)); for (let r = 0; r < p.length; r++) { - let q = "", u = ba(p[r].doc, n).split(/\s+/); + let q = "", u = ca(p[r].doc, n).split(/\s+/); for (let v = 0, w, x; v < u.length; v++) { w = u[v]; x = l.encode(w); @@ -1523,7 +1523,7 @@ function V(a) { this.store = b && (e ? new S(e) : new Map()); this.cache = (b = a.cache || null) && new X(b); a.cache = !1; - this.worker = a.worker; + this.worker = a.worker || !1; this.priority = a.priority || 4; this.index = Ua.call(this, a, c); this.tag = null; @@ -1546,19 +1546,29 @@ function V(a) { } if (this.worker) { this.fastupdate = !1; - a = []; - for (const d of this.index.values()) { - d.then && a.push(d); + const d = []; + for (const f of this.index.values()) { + f.then && d.push(f); } - if (a.length) { - const d = this; - return Promise.all(a).then(function(f) { - let g = 0; - for (const h of d.index.entries()) { - const k = h[0]; - h[1].then && d.index.set(k, f[g++]); + if (d.length) { + const f = this; + return Promise.all(d).then(function(g) { + const h = new Map(); + let k = 0; + for (const m of f.index.entries()) { + const n = m[0]; + var l = m[1]; + if (l.then) { + l = d[k].encoder || {}; + let p = h.get(l); + p || (p = l.encode ? l : new G(l), h.set(l, p)); + l = g[k]; + l.encoder = p; + f.index.set(n, l); + k++; + } } - return d; + return f; }); } } else { @@ -1572,28 +1582,30 @@ t.mount = function(a) { } let c = this.field; if (this.tag) { - for (let d = 0, f; d < this.R.length; d++) { - f = this.R[d]; + for (let f = 0, g; f < this.R.length; f++) { + g = this.R[f]; var b = void 0; - this.index.set(f, b = new N({}, this.reg)); + this.index.set(g, b = new N({}, this.reg)); c === this.field && (c = c.slice(0)); - c.push(f); - b.tag = this.tag.get(f); + c.push(g); + b.tag = this.tag.get(g); } } b = []; const e = {db:a.db, type:a.type, fastupdate:a.fastupdate}; - for (let d = 0, f, g; d < c.length; d++) { - e.field = g = c[d]; - f = this.index.get(g); - const h = new a.constructor(a.id, e); - h.id = a.id; - b[d] = h.mount(f); - f.document = !0; - d ? f.bypass = !0 : f.store = this.store; + 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, e); + k.id = a.id; + b[f] = k.mount(g); + g.document = !0; + f ? g.bypass = !0 : g.store = this.store; } - this.db = !0; - return Promise.all(b); + const d = this; + return this.db = Promise.all(b).then(function() { + d.db = !0; + }); }; t.commit = async function(a, c) { const b = []; @@ -1617,9 +1629,10 @@ function Ua(a, c) { for (let d = 0, f, g; d < e.length; d++) { f = e[d]; D(f) || (g = f, f = f.field); - g = G(g) ? Object.assign({}, a, g) : a; + g = aa(g) ? Object.assign({}, a, g) : a; if (this.worker) { const h = new P(g); + h.encoder = g.encoder; b.set(f, h); } this.worker || b.set(f, new N(g, this.reg)); @@ -1651,7 +1664,7 @@ t.update = function(a, c) { return this.remove(a).add(a, c); }; t.remove = function(a) { - G(a) && (a = ba(a, this.key)); + aa(a) && (a = ca(a, this.key)); for (var c of this.index.values()) { c.remove(a, !0); } @@ -1701,7 +1714,7 @@ t.get = function(a) { }) : this.store.get(a) || null; }; t.set = function(a, c) { - "object" === typeof a && (c = a, a = ba(c, this.key)); + "object" === typeof a && (c = a, a = ca(c, this.key)); this.store.set(a, c); return this; }; @@ -2076,7 +2089,7 @@ function ib(a, c, b, e, d, f, g, h) { } b = a.context; const e = !0 === b ? {depth:1} : b || {}, d = D(a.encoder) ? bb[a.encoder] : a.encode || a.encoder || {}; - this.encoder = d.encode ? d : "object" === typeof d ? new ja(d) : {encode:d}; + this.encoder = d.encode ? d : "object" === typeof d ? new G(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; @@ -2209,7 +2222,7 @@ t.serialize = function(a = !0) { la(N.prototype); const lb = "undefined" !== typeof window && (window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB), mb = ["map", "ctx", "tag", "reg", "cfg"], Y = A(); function nb(a, c = {}) { - if (!this) { + if (!this || this.constructor !== nb) { return new nb(a, c); } "object" === typeof a && (c = a, a = a.name); @@ -2223,7 +2236,7 @@ function nb(a, c = {}) { } t = nb.prototype; t.mount = function(a) { - if (!a.encoder) { + if (a.index) { return a.mount(this); } a.db = this; @@ -2375,7 +2388,7 @@ t.commit = async function(a, c, b) { e[d] = f.del; } } - c || (b || (e = e.concat(aa(a.reg))), e.length && await this.remove(e)); + c || (b || (e = e.concat(ba(a.reg))), e.length && await this.remove(e)); } a.reg.size && (await this.transaction("map", "readwrite", function(e) { for (const d of a.map) { @@ -2510,6 +2523,6 @@ function Z(a, c) { a = null; }); } -;export default {Index:N, Charset:bb, Encoder:ja, Document:U, Worker:P, Resolver:W, IndexedDB:nb, Language:{}}; +;export default {Index:N, Charset:bb, Encoder:G, Document:U, Worker:P, Resolver:W, IndexedDB:nb, Language:{}}; -export const Index=N;export const Charset=bb;export const Encoder=ja;export const Document=U;export const Worker=P;export const Resolver=W;export const IndexedDB=nb;export const Language={}; \ No newline at end of file +export const Index=N;export const Charset=bb;export const Encoder=G;export const Document=U;export const Worker=P;export const Resolver=W;export const IndexedDB=nb;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 2d51e5d..89c2bdb 100644 --- a/dist/flexsearch.bundle.module.min.js +++ b/dist/flexsearch.bundle.module.min.js @@ -1,19 +1,19 @@ /**! - * FlexSearch.js v0.8.155 (Bundle/Module) + * FlexSearch.js v0.8.156 (Bundle/Module) * Author and Copyright: Thomas Wilkerling * Licence: Apache-2.0 * Hosted by Nextapps GmbH * https://github.com/nextapps-de/flexsearch */ var t;function z(a,b,c){const e=typeof c,d=typeof a;if("undefined"!==e){if("undefined"!==d){if(c){if("function"===d&&e===d)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"===d?b:a}function A(){return Object.create(null)}function D(a){return"string"===typeof a} -function G(a){return"object"===typeof a}function aa(a){const b=[];for(const c of a.keys())b.push(c);return b}function H(a,b){if(D(b))a=a[b];else for(let c=0;a&&c"a1a".split(c).length; -this.numeric=z(a.numeric,e)}else{try{this.split=z(this.split,ca)}catch(d){this.split=/\s+/}this.numeric=z(a.numeric,z(this.numeric,!0))}this.prepare=z(a.prepare,null,this.prepare);this.finalize=z(a.finalize,null,this.finalize);c=a.filter;this.filter="function"===typeof c?c:z(c&&new Set(c),null,this.filter);this.dedupe=z(a.dedupe,!0,this.dedupe);this.matcher=z((c=a.matcher)&&new Map(c),null,this.matcher);this.mapper=z((c=a.mapper)&&new Map(c),null,this.mapper);this.stemmer=z((c=a.stemmer)&&new Map(c), +this.numeric=z(a.numeric,e)}else{try{this.split=z(this.split,da)}catch(d){this.split=/\s+/}this.numeric=z(a.numeric,z(this.numeric,!0))}this.prepare=z(a.prepare,null,this.prepare);this.finalize=z(a.finalize,null,this.finalize);c=a.filter;this.filter="function"===typeof c?c:z(c&&new Set(c),null,this.filter);this.dedupe=z(a.dedupe,!0,this.dedupe);this.matcher=z((c=a.matcher)&&new Map(c),null,this.matcher);this.mapper=z((c=a.mapper)&&new Map(c),null,this.mapper);this.stemmer=z((c=a.stemmer)&&new Map(c), null,this.stemmer);this.replacer=z(a.replacer,null,this.replacer);this.minlength=z(a.minlength,1,this.minlength);this.maxlength=z(a.maxlength,1024,this.maxlength);this.rtl=z(a.rtl,!1,this.rtl);if(this.cache=c=z(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 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};t.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&&I(this);return this};t.addFilter=function(a){"function"===typeof a?this.filter=a:(this.filter||(this.filter=new Set),this.filter.add(a));this.cache&&I(this);return this}; t.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&&I(this);return this}; t.addReplacer=function(a,b){if("string"===typeof a)return this.addMatcher(a,b);this.replacer||(this.replacer=[]);this.replacer.push(a,b);this.cache&&I(this);return this}; -t.encode=function(a){if(this.cache&&a.length<=this.K)if(this.H){if(this.B.has(a))return this.B.get(a)}else this.H=setTimeout(I,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(b)c.push(g);else if(!this.filter||("function"===typeof this.filter?this.filter(g):!this.filter.has(g))){if(this.cache&&g.length<=this.L)if(this.H){var d=this.G.get(g);if(d||""===d){d&&c.push(d);continue}}else this.H=setTimeout(I,50,this);this.stemmer&&2this.stemmer.get(k))); if(g&&(this.mapper||this.dedupe&&1this.matcher.get(k)));if(g&&this.replacer)for(d=0;g&&dthis.S&& (this.G.clear(),this.L=this.L/1.1|0));g&&c.push(g)}this.finalize&&(c=this.finalize(c)||c);this.cache&&a.length<=this.K&&(this.B.set(a,c),this.B.size>this.S&&(this.B.clear(),this.K=this.K/1.1|0));return c};function I(a){a.H=null;a.B.clear();a.G.clear()};let K,ja;async function ka(a){a=a.data;var b=a.task;const c=a.id;let e=a.args;switch(b){case "init":ja=a.options||{};(b=a.factory)?(Function("return "+b)()(self),K=new self.FlexSearch.Index(ja),delete self.FlexSearch):K=new M(ja);postMessage({id:c});break;default:let d;"export"===b&&(e[1]?(e[0]=ja.export,e[2]=0,e[3]=1):e=null);"import"===b?e[0]&&(a=await ja.import.call(K,e[0]),K.import(e[0],a)):(d=e&&K[b].apply(K,e))&&d.then&&(d=await d);postMessage("search"===b?{id:c,msg:d}:{id:c})}};function la(a){ma.call(a,"add");ma.call(a,"append");ma.call(a,"search");ma.call(a,"update");ma.call(a,"remove")}let na,oa,pa;function qa(){na=pa=0} @@ -31,15 +31,15 @@ if("includes"===e)return function(d){for(let f=0;fc||e?k.slice(e,c+e):k;else{if(ac||e)k=k.slice(e,c+e)}else{d= [];for(let n=0,p;ne)e-=p.length;else{if(p.length>c||e)p=p.slice(e,c+e),c-=p.length,e&&(e-=p.length);d.push(p);if(!c)break}k=1b?b?a.slice(c,c+b):a.slice(c):a,e?V.call(this,a):a;let d=[];for(let f=0,g,h;f=h){c-=h;continue}cb&&(g=g.slice(0,b),h=b);if(!d.length&&h>=b)return e?V.call(this,g):g;d.push(g);b-=h;if(!b)break}d=1a.length?this.result=a[0]:(this.result=Ga(a,c,e,!1,this.h),e=0));return f?this.resolve(c,e,d):this};W.prototype.and=function(){let a=this.result.length,b,c,e,d;if(!a){const f=arguments[0];f&&(a=!!f.suggest,d=f.resolve,b=f.limit,c=f.offset,e=f.enrich&&d)}if(a){const {O:f,P:g,limit:h,offset:k,enrich:l,resolve:m,suggest:n}=Ja(this,"and",arguments);return La.call(this,f,g,h,k,l,m,n)}return d?this.resolve(b,c,e):this}; -function La(a,b,c,e,d,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=ba(a))return this.result=Fa(a,b,c,e,g,this.h,f),f?d?V.call(this.index,this.result):this.result:this;this.result=[]}else g||(this.result=a);return f?this.resolve(c,e,d):this};W.prototype.xor=function(){const {O:a,P:b,limit:c,offset:e,enrich:d,resolve:f,suggest:g}=Ja(this,"xor",arguments);return Ma.call(this,a,b,c,e,d,f,g)}; +function La(a,b,c,e,d,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=ca(a))return this.result=Fa(a,b,c,e,g,this.h,f),f?d?V.call(this.index,this.result):this.result:this;this.result=[]}else g||(this.result=a);return f?this.resolve(c,e,d):this};W.prototype.xor=function(){const {O:a,P:b,limit:c,offset:e,enrich:d,resolve:f,suggest:g}=Ja(this,"xor",arguments);return Ma.call(this,a,b,c,e,d,f,g)}; function Ma(a,b,c,e,d,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=Na.call(this,a,c,e,f,this.h),f?d?V.call(this.index,this.result):this.result:this;else g||(this.result=a);return f?this.resolve(c,e,d):this} function Na(a,b,c,e,d){const f=[],g=A();let h=0;for(let k=0,l;kc||e)a=a.slice(e,e+c);d&&(a=V.call(this,a));return a}}function V(a){if(!this||!this.store)return a;const b=Array(a.length);for(let c=0,e;cc||e)a=a.slice(e,e+c);d&&(a=V.call(this,a));return a}}function V(a){if(!this||!this.store)return a;const b=Array(a.length);for(let c=0,e;cthis.limit&&this.cache.delete(this.cache.keys().next().value)}; @@ -72,14 +72,14 @@ function gb(a,b,c,e,d,f,g){let h=g?a.ctx:a.map,k;if(!b[c]||g&&!(k=b[c])[g])if(g? function fb(a,b,c,e,d){return c&&1c)&&(k=c,c=b,b=k);if(a.db)return a.db.get(b,c,e,d,f,g,h);a=c?(a=a.ctx.get(c))&&a.get(b):a.map.get(b);return a};function M(a,b){if(!this||this.constructor!==M)return new M(a);if(a){var c=D(a)?a:a.preset;c&&(a=Object.assign({},eb[c],a))}else a={};c=a.context;const e=!0===c?{depth:1}:c||{},d=D(a.encoder)?bb[a.encoder]:a.encode||a.encoder||{};this.encoder=d.encode?d:"object"===typeof d?new ia(d):{encode:d};this.resolution=a.resolution||9;this.tokenize=c=(c=a.tokenize)&&"default"!==c&&"exact"!==c&&c||"strict";this.depth="strict"===c&&e.depth||0;this.bidirectional=!1!==e.bidirectional;this.fastupdate=!!a.fastupdate; +function jb(a,b,c,e){let d=[];if(a&&a.length){if(a.length<=e){b.push(a);return}for(let f=0,g;fc)&&(k=c,c=b,b=k);if(a.db)return a.db.get(b,c,e,d,f,g,h);a=c?(a=a.ctx.get(c))&&a.get(b):a.map.get(b);return a};function M(a,b){if(!this||this.constructor!==M)return new M(a);if(a){var c=D(a)?a:a.preset;c&&(a=Object.assign({},eb[c],a))}else a={};c=a.context;const e=!0===c?{depth:1}:c||{},d=D(a.encoder)?bb[a.encoder]:a.encode||a.encoder||{};this.encoder=d.encode?d:"object"===typeof d?new H(d):{encode:d};this.resolution=a.resolution||9;this.tokenize=c=(c=a.tokenize)&&"default"!==c&&"exact"!==c&&c||"strict";this.depth="strict"===c&&e.depth||0;this.bidirectional=!1!==e.bidirectional;this.fastupdate=!!a.fastupdate; this.score=a.score||null;(c=a.keystore||0)&&(this.keystore=c);this.map=c?new S(c):new Map;this.ctx=c?new S(c):new Map;this.reg=b||(this.fastupdate?c?new S(c):new Map:c?new T(c):new Set);this.U=e.resolution||3;this.rtl=d.rtl||a.rtl||!1;this.cache=(c=a.cache||null)&&new X(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}t=M.prototype; t.mount=function(a){this.commit_timer&&(clearTimeout(this.commit_timer),this.commit_timer=null);return a.mount(this)};t.commit=function(a,b){this.commit_timer&&(clearTimeout(this.commit_timer),this.commit_timer=null);return this.db.commit(this,a,b)};t.destroy=function(){this.commit_timer&&(clearTimeout(this.commit_timer),this.commit_timer=null);return this.db.destroy()};function db(a){a.commit_timer||(a.commit_timer=setTimeout(function(){a.commit_timer=null;a.db.commit(a,void 0,void 0)},1))} t.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};t.append=function(a,b){return this.add(a,b,!0)};t.contain=function(a){return this.db?this.db.has(a):this.reg.has(a)};t.update=function(a,b){const c=this,e=this.remove(a);return e&&e.then?e.then(()=>c.add(a,b)):this.add(a,b)}; t.cleanup=function(){if(!this.fastupdate)return this;cb(this.map);this.depth&&cb(this.ctx);return this};t.searchCache=Va;t.export=function(a,b,c=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=sa(this.map,this.reg.size);break;case 3:d="ctx";f=ua(this.ctx,this.reg.size);break;default:return}return ya.call(this,a,b,d,f,c,e)}; t.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){e-=m.length;continue}const n=c?e+Math.min(m.length-e,c):m.length;for(let p=e;p{a.onsuccess=a.oncomplete=function(){b&&b(this.result);b=null;c(this.result)};a.onerror=a.onblocked=e;a=null})};export default {Index:M,Charset:bb,Encoder:ia,Document:U,Worker:P,Resolver:W,IndexedDB:ob,Language:{}}; -export const Index=M;export const Charset=bb;export const Encoder=ia;export const Document=U;export const Worker=P;export const Resolver=W;export const IndexedDB=ob;export const Language={}; \ No newline at end of file +function Z(a,b){return new Promise((c,e)=>{a.onsuccess=a.oncomplete=function(){b&&b(this.result);b=null;c(this.result)};a.onerror=a.onblocked=e;a=null})};export default {Index:M,Charset:bb,Encoder:H,Document:U,Worker:P,Resolver:W,IndexedDB:ob,Language:{}}; +export const Index=M;export const Charset=bb;export const Encoder=H;export const Document=U;export const Worker=P;export const Resolver=W;export const IndexedDB=ob;export const Language={}; \ No newline at end of file diff --git a/dist/flexsearch.compact.debug.js b/dist/flexsearch.compact.debug.js index 1e0c607..cf43ce2 100644 --- a/dist/flexsearch.compact.debug.js +++ b/dist/flexsearch.compact.debug.js @@ -1,5 +1,5 @@ /**! - * FlexSearch.js v0.8.155 (Bundle/Debug) + * FlexSearch.js v0.8.156 (Bundle/Debug) * Author and Copyright: Thomas Wilkerling * Licence: Apache-2.0 * Hosted by Nextapps GmbH diff --git a/dist/flexsearch.compact.min.js b/dist/flexsearch.compact.min.js index b6d0bd7..b1f4b55 100644 --- a/dist/flexsearch.compact.min.js +++ b/dist/flexsearch.compact.min.js @@ -1,5 +1,5 @@ /**! - * FlexSearch.js v0.8.155 (Bundle) + * FlexSearch.js v0.8.156 (Bundle) * Author and Copyright: Thomas Wilkerling * Licence: Apache-2.0 * Hosted by Nextapps GmbH diff --git a/dist/flexsearch.compact.module.debug.js b/dist/flexsearch.compact.module.debug.js index d2ccf7c..bb9daca 100644 --- a/dist/flexsearch.compact.module.debug.js +++ b/dist/flexsearch.compact.module.debug.js @@ -1,5 +1,5 @@ /**! - * FlexSearch.js v0.8.155 (Bundle/Debug) + * FlexSearch.js v0.8.156 (Bundle/Debug) * Author and Copyright: Thomas Wilkerling * Licence: Apache-2.0 * Hosted by Nextapps GmbH diff --git a/dist/flexsearch.compact.module.min.js b/dist/flexsearch.compact.module.min.js index c35cfba..5763a6c 100644 --- a/dist/flexsearch.compact.module.min.js +++ b/dist/flexsearch.compact.module.min.js @@ -1,5 +1,5 @@ /**! - * FlexSearch.js v0.8.155 (Bundle) + * FlexSearch.js v0.8.156 (Bundle) * Author and Copyright: Thomas Wilkerling * Licence: Apache-2.0 * Hosted by Nextapps GmbH diff --git a/dist/flexsearch.es5.debug.js b/dist/flexsearch.es5.debug.js index 40dfdfe..08ed8c4 100644 --- a/dist/flexsearch.es5.debug.js +++ b/dist/flexsearch.es5.debug.js @@ -1,5 +1,5 @@ /**! - * FlexSearch.js v0.8.155 (ES5/Debug) + * FlexSearch.js v0.8.156 (ES5/Debug) * Author and Copyright: Thomas Wilkerling * Licence: Apache-2.0 * Hosted by Nextapps GmbH @@ -1172,9 +1172,9 @@ function Ia(a) { Ka.call(a, "update"); Ka.call(a, "remove"); } -var Ma, Na, Oa; +var La, Na, Oa; function Pa() { - Ma = Oa = 0; + La = Oa = 0; } function Ka(a) { this[a + "Async"] = function() { @@ -1183,7 +1183,7 @@ function Ka(a) { var d = c; delete b[b.length - 1]; } - Ma ? Oa || (Oa = Date.now() - Na >= this.priority * this.priority * 3) : (Ma = setTimeout(Pa, 0), Na = Date.now()); + La ? Oa || (Oa = Date.now() - Na >= this.priority * this.priority * 3) : (La = setTimeout(Pa, 0), Na = Date.now()); if (Oa) { var e = this; return new Promise(function(f) { @@ -2233,10 +2233,10 @@ V.prototype.search = function(a, b, c, d) { if (p) { if (this.db) { x.tag = p; - var La = v.db.support_tag_search; + var Ma = v.db.support_tag_search; x.field = n; } - La || (x.enrich = !1); + Ma || (x.enrich = !1); } if (r) { r[z] = v.search(a, b, x); @@ -2251,7 +2251,7 @@ V.prototype.search = function(a, b, c, d) { E = []; N = 0; if (this.db && d) { - if (!La) { + if (!Ma) { for (H = n.length; H < d.length; H++) { var L = d[H]; if (L && L.length) { @@ -2296,7 +2296,7 @@ V.prototype.search = function(a, b, c, d) { } } if (r) { - if (this.db && p && p.length && !La) { + if (this.db && p && p.length && !Ma) { for (f = 0; f < p.length; f += 2) { g = this.index.get(p[f]); if (!g) { @@ -2439,7 +2439,7 @@ function W(a) { this.store = c && (d ? new T(d) : new Map()); this.cache = (c = a.cache || null) && new Y(c); a.cache = !1; - this.worker = a.worker; + this.worker = a.worker || !1; this.priority = a.priority || 4; this.index = xb.call(this, a, b); this.tag = null; @@ -2463,20 +2463,21 @@ function W(a) { } if (this.worker) { this.fastupdate = !1; - a = []; - c = y(this.index.values()); - for (b = c.next(); !b.done; b = c.next()) { - b = b.value, b.then && a.push(b); + var g = []; + a = y(this.index.values()); + for (c = a.next(); !c.done; c = a.next()) { + c = c.value, c.then && g.push(c); } - if (a.length) { - var g = this; - return Promise.all(a).then(function(f) { - for (var h = 0, k = y(g.index.entries()), l = k.next(); !l.done; l = k.next()) { - l = l.value; - var m = l[0]; - l[1].then && g.index.set(m, f[h++]); + if (g.length) { + var f = this; + return Promise.all(g).then(function(h) { + for (var k = new Map(), l = 0, m = y(f.index.entries()), n = m.next(); !n.done; n = m.next()) { + var p = n.value; + n = p[0]; + var q = p[1]; + q.then && (q = g[l].encoder || {}, p = k.get(q), p || (p = q.encode ? q : new Ga(q), k.set(q, p)), q = h[l], q.encoder = p, f.index.set(n, q), l++); } - return g; + return f; }); } } else { @@ -2490,9 +2491,9 @@ u.mount = function(a) { } var b = this.field; if (this.tag) { - for (var c = 0, d; c < this.aa.length; c++) { + for (var c = 0, d = void 0; c < this.aa.length; c++) { d = this.aa[c]; - var e; + var e = void 0; this.index.set(d, e = new O({}, this.reg)); b === this.field && (b = b.slice(0)); b.push(d); @@ -2502,7 +2503,8 @@ u.mount = function(a) { c = []; d = {db:a.db, type:a.type, fastupdate:a.fastupdate}; e = 0; - for (var g; e < b.length; e++) { + 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); @@ -2511,8 +2513,10 @@ u.mount = function(a) { g.document = !0; e ? g.bypass = !0 : g.store = this.store; } - this.db = !0; - return Promise.all(c); + var h = this; + return this.db = Promise.all(c).then(function() { + h.db = !0; + }); }; u.commit = function(a, b) { var c = this, d, e, g, f; @@ -2544,6 +2548,7 @@ function xb(a, b) { f = xa(f) ? Object.assign({}, a, f) : a; if (this.worker) { var h = new Ra(f); + h.encoder = f.encoder; c.set(g, h); } this.worker || c.set(g, new O(f, this.reg)); @@ -3166,7 +3171,7 @@ Ja(O.prototype); var Pb = "undefined" !== typeof window && (window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB), Qb = ["map", "ctx", "tag", "reg", "cfg"], Rb = I(); function Sb(a, b) { b = void 0 === b ? {} : b; - if (!this) { + if (!this || this.constructor !== Sb) { return new Sb(a, b); } "object" === typeof a && (b = a, a = a.name); @@ -3180,7 +3185,7 @@ function Sb(a, b) { } u = Sb.prototype; u.mount = function(a) { - if (!a.encoder) { + if (a.index) { return a.mount(this); } a.db = this; diff --git a/dist/flexsearch.es5.min.js b/dist/flexsearch.es5.min.js index 908708d..0509d57 100644 --- a/dist/flexsearch.es5.min.js +++ b/dist/flexsearch.es5.min.js @@ -1,5 +1,5 @@ /**! - * FlexSearch.js v0.8.155 (ES5) + * FlexSearch.js v0.8.156 (ES5) * Author and Copyright: Thomas Wilkerling * Licence: Apache-2.0 * Hosted by Nextapps GmbH @@ -18,8 +18,8 @@ C("Promise",function(a){function b(f){this.A=0;this.B=void 0;this.h=[];this.H=!1 f[h];f[h]=null;try{k()}catch(l){this.C(l)}}}this.h=null};c.prototype.C=function(f){this.B(function(){throw f;})};b.prototype.C=function(){function f(l){return function(m){k||(k=!0,l.call(h,m))}}var h=this,k=!1;return{resolve:f(this.fa),reject:f(this.D)}};b.prototype.fa=function(f){if(f===this)this.D(new TypeError("A Promise cannot resolve to itself"));else if(f instanceof b)this.ha(f);else{a:switch(typeof f){case "object":var h=null!=f;break a;case "function":h=!0;break a;default:h=!1}h?this.ea(f): this.G(f)}};b.prototype.ea=function(f){var h=void 0;try{h=f.then}catch(k){this.D(k);return}"function"==typeof h?this.ia(h,f):this.G(f)};b.prototype.D=function(f){this.M(2,f)};b.prototype.G=function(f){this.M(1,f)};b.prototype.M=function(f,h){if(0!=this.A)throw Error("Cannot settle("+f+", "+h+"): Promise already settled in state"+this.A);this.A=f;this.B=h;2===this.A&&this.ga();this.N()};b.prototype.ga=function(){var f=this;e(function(){if(f.T()){var h=B.console;"undefined"!==typeof h&&h.error(f.B)}}, 1)};b.prototype.T=function(){if(this.H)return!1;var f=B.CustomEvent,h=B.Event,k=B.dispatchEvent;if("undefined"===typeof k)return!0;"function"===typeof f?f=new f("unhandledrejection",{cancelable:!0}):"function"===typeof h?f=new h("unhandledrejection",{cancelable:!0}):(f=B.document.createEvent("CustomEvent"),f.initCustomEvent("unhandledrejection",!1,!0,f));f.promise=this;f.reason=this.B;return k(f)};b.prototype.N=function(){if(null!=this.h){for(var f=0;fa.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&&K(this);return this}; u.addReplacer=function(a,b){if("string"===typeof a)return this.addMatcher(a,b);this.replacer||(this.replacer=[]);this.replacer.push(a,b);this.cache&&K(this);return this}; u.encode=function(a){var b=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(K,50,this);this.normalize&&("function"===typeof this.normalize?a=this.normalize(a):a=Fa?a.normalize("NFKD").replace(Fa,"").toLowerCase():a.toLowerCase());this.prepare&&(a=this.prepare(a));this.numeric&&3this.maxlength))if(c)d.push(f);else if(!this.filter||("function"===typeof this.filter?this.filter(f):!this.filter.has(f))){if(this.cache&&f.length<=this.H)if(this.D){var k=this.C.get(f);if(k||""===k){k&&d.push(k);continue}}else this.D=setTimeout(K,50,this);this.stemmer&&2this.maxlength))if(c)d.push(f);else if(!this.filter||("function"===typeof this.filter?this.filter(f):!this.filter.has(f))){if(this.cache&&f.length<=this.H)if(this.D){var k=this.C.get(f);if(k||""===k){k&&d.push(k);continue}}else this.D=setTimeout(K,50,this);this.stemmer&&2this.T&&(this.C.clear(),this.H=this.H/1.1|0));f&&d.push(f)}this.finalize&&(d=this.finalize(d)||d);this.cache&&a.length<=this.G&&(this.B.set(a,d),this.B.size>this.T&&(this.B.clear(),this.G=this.G/1.1|0));return d};function K(a){a.D=null;a.B.clear();a.C.clear()};var Ha,Ia; function Ja(a){var b,c,d,e,g,f;return ta(function(h){switch(h.h){case 1:a=a.data;b=a.task;c=a.id;d=a.args;switch(b){case "init":Ia=a.options||{};(e=a.factory)?(Function("return "+e)()(self),Ha=new self.FlexSearch.Index(Ia),delete self.FlexSearch):Ha=new M(Ia);postMessage({id:c});break;default:h.h=2;return}h.h=0;break;case 2:"export"===b&&(d[1]?(d[0]=Ia.export,d[2]=0,d[3]=1):d=null);if("import"===b){if(!d[0]){h.h=5;break}return D(h,Ia.import.call(Ha,d[0]),9)}g=d&&Ha[b].apply(Ha,d);if(!g||!g.then){h.h= -5;break}return D(h,g,7);case 7:g=h.D;h.h=5;break;case 9:f=h.D,Ha.import(d[0],f);case 5:postMessage("search"===b?{id:c,msg:g}:{id:c}),h.h=0}})};function Ka(a){Ma.call(a,"add");Ma.call(a,"append");Ma.call(a,"search");Ma.call(a,"update");Ma.call(a,"remove")}var Na,Oa,Pa;function Qa(){Na=Pa=0} -function Ma(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]}Na?Pa||(Pa=Date.now()-Oa>=this.priority*this.priority*3):(Na=setTimeout(Qa,0),Oa=Date.now());if(Pa){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 Ra=0; +5;break}return D(h,g,7);case 7:g=h.D;h.h=5;break;case 9:f=h.D,Ha.import(d[0],f);case 5:postMessage("search"===b?{id:c,msg:g}:{id:c}),h.h=0}})};function Ka(a){La.call(a,"add");La.call(a,"append");La.call(a,"search");La.call(a,"update");La.call(a,"remove")}var Na,Oa,Pa;function Qa(){Na=Pa=0} +function La(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]}Na?Pa||(Pa=Date.now()-Oa>=this.priority*this.priority*3):(Na=setTimeout(Qa,0),Oa=Date.now());if(Pa){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 Ra=0; function Sa(a){function b(f){function h(k){k=k.data||k;var l=k.id,m=l&&e.h[l];m&&(m(k.msg),delete e.h[l])}this.worker=f;this.h=I();if(this.worker){d?this.worker.on("message",h):this.worker.onmessage=h;if(a.config)return new Promise(function(k){e.h[++Ra]=function(){k(e);1E9c||d?k.slice(d,c+d):k;else{if(ac||d)k=k.slice(d,c+ +f.R;if(h&&!h(b))continue;f.constructor===String&&(f=""+f);f=za(b,f)}if(g&&f){J(f)&&(f=[f]);h=0;for(var k,l=void 0;hc||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(f.length>c||d)f=f.slice(d,c+d),c-=f.length,d&&(d-=f.length);e.push(f);if(!c)break}k=1b?b?a.slice(c,c+b):a.slice(c):a,d?V.call(this,a):a;for(var e=[],g=0,f=void 0,h=void 0;g=h){c-=h;continue}cb&&(f=f.slice(0,b),h=b);if(!e.length&&h>=b)return d?V.call(this,f):f;e.push(f);b-=h;if(!b)break}e=1a.length?this.result=a[0]:(this.result=kb(a,c,d,!1,this.h),d=0));return g?this.resolve(c,d,e):this};W.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 g=b.enrich&&c}}return a?(a=nb(this,"and",arguments),pb.call(this,a.W,a.$,a.limit,a.offset,a.enrich,a.resolve,a.suggest)):c?this.resolve(d,e,g):this}; +function kb(a,b,c,d,e){var g=[],f=I(),h=a.length,k;if(d)for(e=h-1;0<=e;e--){if(k=(d=a[e])&&d.length)for(h=0;hb?b?a.slice(c,c+b):a.slice(c):a,d?V.call(this,a):a;for(var e=[],g=0,f=void 0,h=void 0;g=h){c-=h;continue}cb&&(f=f.slice(0,b),h=b);if(!e.length&&h>=b)return d?V.call(this,f):f;e.push(f);b-=h;if(!b)break}e=1a.length?this.result=a[0]:(this.result=kb(a,c,d,!1,this.h),d=0));return g?this.resolve(c,d,e):this};W.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 g=b.enrich&&c}}return a?(a=nb(this,"and",arguments),pb.call(this,a.W,a.$,a.limit,a.offset,a.enrich,a.resolve,a.suggest)):c?this.resolve(d,e,g):this}; function pb(a,b,c,d,e,g,f){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=Aa(a))return this.result=jb(a,b,c,d,f,this.h,g),g?e?V.call(this.index,this.result):this.result:this;this.result=[]}else f||(this.result=a);return g?this.resolve(c,d,e):this};W.prototype.xor=function(){var a=nb(this,"xor",arguments);return qb.call(this,a.W,a.$,a.limit,a.offset,a.enrich,a.resolve,a.suggest)}; function qb(a,b,c,d,e,g,f){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=rb.call(this,a,c,d,g,this.h),g?e?V.call(this.index,this.result):this.result:this;else f||(this.result=a);return g?this.resolve(c,d,e):this} -function rb(a,b,c,d,e){for(var g=[],f=I(),h=0,k=0,l;kc||d)a=a.slice(d,d+c);e&&(a=V.call(this,a));return a}}function V(a){if(!this||!this.store)return a;for(var b=Array(a.length),c=0,d;cc||d)a=a.slice(d,d+c);e&&(a=V.call(this,a));return a}}function V(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)}; X.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};X.prototype.remove=function(a){for(var b=y(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)}};X.prototype.clear=function(){this.cache.clear();this.h=""};var Ab={normalize:!1,numeric:!1,dedupe:!1};var Bb={};var Cb=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 Db=new Map([["ae","a"],["oe","o"],["sh","s"],["kh","k"],["th","t"],["ph","f"],["pf","f"]]),Eb=[/([^aeo])h(.)/g,"$1$2",/([aeo])h([^aeo]|$)/g,"$1$2",/(.)\1+/g,"$1"];var Fb={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 Gb={Exact:Ab,Default:Bb,Normalize:Bb,LatinBalance:{mapper:Cb},LatinAdvanced:{mapper:Cb,matcher:Db,replacer:Eb},LatinExtra:{mapper:Cb,replacer:Eb.concat([/(?!^)[aeo]/g,""]),matcher:Db},LatinSoundex:{dedupe:!1,include:{letter:!0},finalize:function(a){for(var b=0;be.length)e.pop();else{var g=e.indexOf(a);g===c.length-1?e.pop():e.splice(g,1)}}else Hb(this.map,a),this.depth&&Hb(this.ctx,a);b||this.reg.delete(a)}this.db&&(this.commit_task.push({del:a}),this.ca&&Ib(this));this.cache&&this.cache.remove(a);return this}; -function Hb(a,b){var c=0,d="undefined"===typeof b;if(a.constructor===Array)for(var e=0,g=void 0,f;en;r--)p=l.substring(n,r),q=this.rtl?m-1-n:n,q=this.score?this.score(b,l,k,p,q):Kb(h,d,k,m,q),Lb(this,g,p, -q,a,c);break}case "bidirectional":case "reverse":if(1p?0:1),d,k,r-1,q-1),t=this.bidirectional&& +function Hb(a,b){var c=0,d="undefined"===typeof b;if(a.constructor===Array)for(var e=0,g=void 0,f;en;r--)q=l.substring(n,r),p=this.rtl?m-1-n:n,p=this.score?this.score(b,l,k,q,p):Kb(h,d,k,m,p),Lb(this,g,q, +p,a,c);break}case "bidirectional":case "reverse":if(1q?0:1),d,k,r-1,p-1),t=this.bidirectional&& l>n;Lb(this,e,t?n:l,w,a,c,t?l:n)}}}}this.fastupdate||this.reg.add(a)}else b=""}this.db&&(b||this.commit_task.push({del:a}),this.ca&&Ib(this));return this}; function Lb(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]=I()),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 R(h);if(a.fastupdate)for(c=y(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 Kb(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 M(a,b){if(!this||this.constructor!==M)return new M(a);if(a){var c=J(a)?a:a.preset;c&&(a=Object.assign({},Jb[c],a))}else a={};c=a.context;var d=!0===c?{depth:1}:c||{},e=J(a.encoder)?Gb[a.encoder]:a.encode||a.encoder||{};this.encoder=e.encode?e:"object"===typeof e?new Ga(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 S(c):new Map;this.ctx=c?new S(c):new Map;this.reg=b||(this.fastupdate?c?new S(c):new Map:c?new T(c):new Set);this.da=d.resolution||3;this.rtl=e.rtl||a.rtl||!1;this.cache=(c=a.cache||null)&&new X(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}u=M.prototype; u.mount=function(a){this.commit_timer&&(clearTimeout(this.commit_timer),this.commit_timer=null);return a.mount(this)};u.commit=function(a,b){this.commit_timer&&(clearTimeout(this.commit_timer),this.commit_timer=null);return this.db.commit(this,a,b)};u.destroy=function(){this.commit_timer&&(clearTimeout(this.commit_timer),this.commit_timer=null);return this.db.destroy()};function Ib(a){a.commit_timer||(a.commit_timer=setTimeout(function(){a.commit_timer=null;a.db.commit(a,void 0,void 0)},1))} @@ -116,18 +116,19 @@ u.cleanup=function(){if(!this.fastupdate)return this;Hb(this.map);this.depth&&Hb u.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 n=c?d+Math.min(m.length-d,c):m.length, -p=d;p=g.length)return[];if(!b&&!c)return g;g=g.slice(c,c+b);return d?e.enrich(g):g})}; +q=d;q=g.length)return[];if(!b&&!c)return g;g=g.slice(c,c+b);return d?e.enrich(g):g})}; u.enrich=function(a){"object"!==typeof a&&(a=[a]);for(var b=this.db.transaction("reg","readonly").objectStore("reg"),c=[],d=0;d} - * @export */ this.index = parse_descriptor.call(this, options, document); @@ -113,13 +108,27 @@ export default function Document(options) { } if (promises.length) { const self = this; - return Promise.all(promises).then(function (promises) { + return Promise.all(promises).then(function (result) { + const encoder_last = new Map(); let count = 0; for (const item of self.index.entries()) { - const key = /** @type {string} */item[0], - index = item[1]; + const key = /** @type {string} */item[0]; + let index = item[1]; + if (index.then) { + // make encoder available for result highlighting + let opt = promises[count].encoder || {}, + encoder = encoder_last.get(opt); + // handle shared encoders - index.then && self.index.set(key, promises[count++]); + if (!encoder) { + encoder = opt.encode ? opt : new Encoder(opt); + encoder_last.set(opt, encoder); + } + index = result[count]; + index.encoder = encoder; + self.index.set(key, index); + count++; + } } return self; }); @@ -127,7 +136,6 @@ export default function Document(options) { } else { if (options.db) { this.fastupdate = !1; - // think about to return the promise here // actually it can be awaited on "await index.db" this.mount(options.db); } @@ -136,7 +144,7 @@ export default function Document(options) { /** * @param {!StorageInterface} db - * @return {Promise>} + * @return {Promise} */ Document.prototype.mount = function (db) { if (this.worker) { @@ -195,9 +203,10 @@ Document.prototype.mount = function (db) { } } - //this.async = true; - this.db = !0; - return Promise.all(promises); + const self = this; + return this.db = Promise.all(promises).then(function () { + self.db = !0; + }); }; Document.prototype.commit = async function (replace, append) { @@ -250,6 +259,8 @@ function parse_descriptor(options, document) { if (this.worker) { const worker = new WorkerIndex(opt); if (worker) { + // assign encoder for result highlighting + worker.encoder = opt.encoder; // worker could be a promise // it needs to be resolved and swapped later index.set(key, worker); diff --git a/dist/module-debug/serialize.js b/dist/module-debug/serialize.js index e92750c..2fb84aa 100644 --- a/dist/module-debug/serialize.js +++ b/dist/module-debug/serialize.js @@ -232,7 +232,8 @@ export function importIndex(key, data) { case "reg": // fast update isn't supported by export/import - this.fastupdate = /* suggest */ /* append: */ /* enrich */ /* resolve: */!1; + this.fastupdate = /* suggest */ + /* append: */ /* enrich */ /* resolve: */!1; this.reg = json_to_reg( /** @type {Array} */data, this.reg); break; diff --git a/dist/module-min/db/clickhouse/index.js b/dist/module-min/db/clickhouse/index.js index 9e59122..2e9ce6e 100644 --- a/dist/module-min/db/clickhouse/index.js +++ b/dist/module-min/db/clickhouse/index.js @@ -1,4 +1,4 @@ -import{ClickHouse}from"clickhouse";import StorageInterface from"../interface.js";import{concat,toArray}from"../../common.js";const defaults={host:"http://localhost",port:"8123",debug:!1,basicAuth:null,isUseGzip:!1,trimQuery:!1,usePost:!1,format:"json",raw:!1,config:{output_format_json_quote_64bit_integers:0,enable_http_compression:0,database:"default"}},VERSION=1,fields=["map","ctx","tag","reg","cfg"],types={text:"String",char:"String",varchar:"String",string:"String",number:"Int32",numeric:"Int32",integer:"Int32",smallint:"Int16",tinyint:"Int8",mediumint:"Int32",int:"Int32",int8:"Int8",uint8:"UInt8",int16:"Int16",uint16:"UInt16",int32:"Int32",uint32:"UInt32",int64:"Int64",uint64:"UInt64",bigint:"Int64"};function sanitize(a){return a.toLowerCase().replace(/[^a-z0-9_]/g,"")}let Index;export default function ClickhouseDB(a,b={}){if(!this)return new ClickhouseDB(a,b);if("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?types[b.type.toLowerCase()]:"String",!this.type)throw new Error("Unknown type of ID '"+b.type+"'");this.support_tag_search=!0,this.db=Index||(Index=b.db||null),Object.assign(defaults,b),b.database&&(defaults.config.database=b.database),this.db&&delete defaults.db}ClickhouseDB.prototype.mount=function(a){return a.encoder?(defaults.resolution=Math.max(a.resolution,a.resolution_ctx),a.db=this,this.open()):a.mount(this)},ClickhouseDB.prototype.open=async function(){this.db||(this.db=Index||(Index=new ClickHouse(defaults)));const a=await this.db.query(` +import{ClickHouse}from"clickhouse";import StorageInterface from"../interface.js";import{concat,toArray}from"../../common.js";const defaults={host:"http://localhost",port:"8123",debug:!1,basicAuth:null,isUseGzip:!1,trimQuery:!1,usePost:!1,format:"json",raw:!1,config:{output_format_json_quote_64bit_integers:0,enable_http_compression:0,database:"default"}},VERSION=1,fields=["map","ctx","tag","reg","cfg"],types={text:"String",char:"String",varchar:"String",string:"String",number:"Int32",numeric:"Int32",integer:"Int32",smallint:"Int16",tinyint:"Int8",mediumint:"Int32",int:"Int32",int8:"Int8",uint8:"UInt8",int16:"Int16",uint16:"UInt16",int32:"Int32",uint32:"UInt32",int64:"Int64",uint64:"UInt64",bigint:"Int64"};function sanitize(a){return a.toLowerCase().replace(/[^a-z0-9_]/g,"")}let Index;export default function ClickhouseDB(a,b={}){if(!this||this.constructor!==ClickhouseDB)return new ClickhouseDB(a,b);if("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?types[b.type.toLowerCase()]:"String",!this.type)throw new Error("Unknown type of ID '"+b.type+"'");this.support_tag_search=!0,this.db=Index||(Index=b.db||null),Object.assign(defaults,b),b.database&&(defaults.config.database=b.database),this.db&&delete defaults.db}ClickhouseDB.prototype.mount=function(a){return a.index?a.mount(this):(defaults.resolution=Math.max(a.resolution,a.resolution_ctx),a.db=this,this.open())},ClickhouseDB.prototype.open=async function(){this.db||(this.db=Index||(Index=new ClickHouse(defaults)));const a=await this.db.query(` SELECT 1 FROM system.databases WHERE name = '${this.id}'; `).toPromise();a&&a.length||(await this.db.query(` CREATE DATABASE IF NOT EXISTS ${this.id}; diff --git a/dist/module-min/db/indexeddb/index.js b/dist/module-min/db/indexeddb/index.js index a435512..fe439f4 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?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.encoder?(a.db=this,this.open()):a.mount(this)},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 +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 diff --git a/dist/module-min/db/mongodb/index.js b/dist/module-min/db/mongodb/index.js index 0889c88..9f343dd 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?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.encoder?(a.db=this,this.open()):a.mount(this)};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,this.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){if(c){for(let c,d,e=0;e=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",a).then(function(b){for(let c=0;ce,j.push(c+(f?d:e)+":"+(f?e:d)),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){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",a).then(function(b){for(let c=0;ce,j.push(c+(f?d:e)+":"+(f?e:d)),e=d}else{const a=this.id+"map"+this.field+":";for(let c=0;c} - * @export */ this.index = parse_descriptor.call(this, options, document); @@ -113,13 +108,27 @@ export default function Document(options) { } if (promises.length) { const self = this; - return Promise.all(promises).then(function (promises) { + return Promise.all(promises).then(function (result) { + const encoder_last = new Map(); let count = 0; for (const item of self.index.entries()) { - const key = /** @type {string} */item[0], - index = item[1]; + const key = /** @type {string} */item[0]; + let index = item[1]; + if (index.then) { + // make encoder available for result highlighting + let opt = promises[count].encoder || {}, + encoder = encoder_last.get(opt); + // handle shared encoders - index.then && self.index.set(key, promises[count++]); + if (!encoder) { + encoder = opt.encode ? opt : new Encoder(opt); + encoder_last.set(opt, encoder); + } + index = result[count]; + index.encoder = encoder; + self.index.set(key, index); + count++; + } } return self; }); @@ -127,7 +136,6 @@ export default function Document(options) { } else { if (options.db) { this.fastupdate = !1; - // think about to return the promise here // actually it can be awaited on "await index.db" this.mount(options.db); } @@ -136,7 +144,7 @@ export default function Document(options) { /** * @param {!StorageInterface} db - * @return {Promise>} + * @return {Promise} */ Document.prototype.mount = function (db) { @@ -191,9 +199,10 @@ Document.prototype.mount = function (db) { } } - //this.async = true; - this.db = !0; - return Promise.all(promises); + const self = this; + return this.db = Promise.all(promises).then(function () { + self.db = !0; + }); }; Document.prototype.commit = async function (replace, append) { @@ -246,6 +255,8 @@ function parse_descriptor(options, document) { if (this.worker) { const worker = new WorkerIndex(opt); if (worker) { + // assign encoder for result highlighting + worker.encoder = opt.encoder; // worker could be a promise // it needs to be resolved and swapped later index.set(key, worker); diff --git a/dist/module/serialize.js b/dist/module/serialize.js index e92750c..2fb84aa 100644 --- a/dist/module/serialize.js +++ b/dist/module/serialize.js @@ -232,7 +232,8 @@ export function importIndex(key, data) { case "reg": // fast update isn't supported by export/import - this.fastupdate = /* suggest */ /* append: */ /* enrich */ /* resolve: */!1; + this.fastupdate = /* suggest */ + /* append: */ /* enrich */ /* resolve: */!1; this.reg = json_to_reg( /** @type {Array} */data, this.reg); break; diff --git a/package-lock.json b/package-lock.json index 1709b69..913339a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "flexsearch", - "version": "0.8.155", + "version": "0.8.156", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "flexsearch", - "version": "0.8.155", + "version": "0.8.156", "funding": [ { "type": "github", diff --git a/package.json b/package.json index 6fe8271..98c2043 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "public": true, "preferGlobal": false, "name": "flexsearch", - "version": "0.8.155", + "version": "0.8.156", "description": "Next-Generation full-text search library for Browser and Node.js", "homepage": "https://github.com/nextapps-de/flexsearch/", "author": "Thomas Wilkerling", diff --git a/src/db/clickhouse/index.js b/src/db/clickhouse/index.js index 8c913e9..9ee8840 100644 --- a/src/db/clickhouse/index.js +++ b/src/db/clickhouse/index.js @@ -55,7 +55,7 @@ let Index; */ export default function ClickhouseDB(name, config = {}){ - if(!this){ + if(!this || this.constructor !== ClickhouseDB){ return new ClickhouseDB(name, config); } if(typeof name === "object"){ @@ -82,7 +82,7 @@ export default function ClickhouseDB(name, config = {}){ ClickhouseDB.prototype.mount = function(flexsearch){ //if(flexsearch.constructor === Document){ - if(!flexsearch.encoder){ + if(flexsearch.index){ return flexsearch.mount(this); } defaults.resolution = Math.max(flexsearch.resolution, flexsearch.resolution_ctx); diff --git a/src/db/indexeddb/index.js b/src/db/indexeddb/index.js index 6842441..b44841d 100644 --- a/src/db/indexeddb/index.js +++ b/src/db/indexeddb/index.js @@ -50,7 +50,7 @@ const Index = create_object(); */ export default function IdxDB(name, config = {}){ - if(!this){ + if(!this || this.constructor !== IdxDB){ return new IdxDB(name, config); } if(typeof name === "object"){ @@ -71,7 +71,7 @@ export default function IdxDB(name, config = {}){ IdxDB.prototype.mount = function(flexsearch){ //if(flexsearch.constructor === Document){ - if(!flexsearch.encoder){ + if(flexsearch.index){ return flexsearch.mount(this); } flexsearch.db = this; diff --git a/src/db/mongodb/index.js b/src/db/mongodb/index.js index 39d41c9..7d8b8d2 100644 --- a/src/db/mongodb/index.js +++ b/src/db/mongodb/index.js @@ -23,7 +23,7 @@ let Index = Object.create(null); */ export default function MongoDB(name, config = {}){ - if(!this){ + if(!this || this.constructor !== MongoDB){ return new MongoDB(name, config); } if(typeof name === "object"){ @@ -49,7 +49,7 @@ export default function MongoDB(name, config = {}){ MongoDB.prototype.mount = function(flexsearch){ //if(flexsearch.constructor === Document){ - if(!flexsearch.encoder){ + if(flexsearch.index){ return flexsearch.mount(this); } flexsearch.db = this; diff --git a/src/db/postgres/index.js b/src/db/postgres/index.js index 5c6f17d..fc55ef7 100644 --- a/src/db/postgres/index.js +++ b/src/db/postgres/index.js @@ -48,7 +48,7 @@ let DB, TRX; */ export default function PostgresDB(name, config = {}){ - if(!this){ + if(!this || this.constructor !== PostgresDB){ return new PostgresDB(name, config); } if(typeof name === "object"){ @@ -70,7 +70,7 @@ export default function PostgresDB(name, config = {}){ PostgresDB.prototype.mount = function(flexsearch){ //if(flexsearch.constructor === Document){ - if(!flexsearch.encoder){ + if(flexsearch.index){ return flexsearch.mount(this); } flexsearch.db = this; diff --git a/src/db/redis/index.js b/src/db/redis/index.js index 3ddf01c..7ee0a5e 100644 --- a/src/db/redis/index.js +++ b/src/db/redis/index.js @@ -22,7 +22,7 @@ let DB, TRX; */ export default function RedisDB(name, config = {}){ - if(!this){ + if(!this || this.constructor !== RedisDB){ return new RedisDB(name, config); } if(typeof name === "object"){ @@ -49,7 +49,7 @@ export default function RedisDB(name, config = {}){ RedisDB.prototype.mount = function(flexsearch){ //if(flexsearch.constructor === Document){ - if(!flexsearch.encoder){ + if(flexsearch.index){ return flexsearch.mount(this); } flexsearch.db = this; diff --git a/src/db/sqlite/index.js b/src/db/sqlite/index.js index 9f83589..fe2dc84 100644 --- a/src/db/sqlite/index.js +++ b/src/db/sqlite/index.js @@ -43,7 +43,7 @@ const Index = Object.create(null); */ export default function SqliteDB(name, config = {}){ - if(!this){ + if(!this || this.constructor !== SqliteDB){ return new SqliteDB(name, config); } if(typeof name === "object"){ @@ -68,7 +68,7 @@ export default function SqliteDB(name, config = {}){ SqliteDB.prototype.mount = function(flexsearch){ //if(flexsearch.constructor === Document){ - if(!flexsearch.encoder){ + if(flexsearch.index){ return flexsearch.mount(this); } flexsearch.db = this; diff --git a/src/document.js b/src/document.js index b298167..8546bfb 100644 --- a/src/document.js +++ b/src/document.js @@ -20,10 +20,18 @@ import { } from "./config.js"; // <-- COMPILER BLOCK -import { IndexOptions, DocumentOptions, DocumentDescriptor, FieldOptions, StoreOptions } from "./type.js"; +import { + IndexOptions, + DocumentOptions, + DocumentDescriptor, + FieldOptions, + StoreOptions, + EncoderOptions +} from "./type.js"; import StorageInterface from "./db/interface.js"; import Index from "./index.js"; import WorkerIndex from "./worker.js"; +import Encoder from "./encoder.js"; import Cache, { searchCache } from "./cache.js"; import { is_string, is_object, parse_simple } from "./common.js"; import apply_async from "./async.js"; @@ -81,21 +89,15 @@ export default function Document(options){ } if(SUPPORT_WORKER){ - this.worker = options.worker; + this.worker = options.worker || false; } if(SUPPORT_ASYNC){ this.priority = options.priority || 4; } - // if(SUPPORT_ASYNC){ - // // this switch is used by recall of promise callbacks - // this.async = false; - // } - /** * @type {Map} - * @export */ this.index = parse_descriptor.call(this, options, document); @@ -146,12 +148,28 @@ export default function Document(options){ } if(promises.length){ const self = this; - return Promise.all(promises).then(function(promises){ + return Promise.all(promises).then(function(result){ + const encoder_last = new Map(); let count = 0; for(const item of self.index.entries()){ const key = /** @type {string} */ (item[0]); - const index = item[1]; - index.then && self.index.set(key, promises[count++]); + let index = item[1]; + if(index.then){ + // make encoder available for result highlighting + let opt = promises[count].encoder || {}; + // handle shared encoders + let encoder = encoder_last.get(opt); + if(!encoder){ + encoder = opt.encode + ? opt + : new Encoder(opt); + encoder_last.set(opt, encoder); + } + index = result[count]; + index.encoder = encoder; + self.index.set(key, index); + count++; + } } return self; }); @@ -160,7 +178,6 @@ export default function Document(options){ else if(SUPPORT_PERSISTENT){ if(options.db){ this.fastupdate = false; - // think about to return the promise here // actually it can be awaited on "await index.db" this.mount(options.db); } @@ -171,7 +188,7 @@ if(SUPPORT_PERSISTENT){ /** * @param {!StorageInterface} db - * @return {Promise>} + * @return {Promise} */ Document.prototype.mount = function(db){ @@ -232,9 +249,10 @@ if(SUPPORT_PERSISTENT){ } } - //this.async = true; - this.db = true; - return Promise.all(promises); + const self = this; + return this.db = Promise.all(promises).then(function(){ + self.db = true; + }); }; Document.prototype.commit = async function(replace, append){ @@ -292,6 +310,8 @@ function parse_descriptor(options, document){ if(SUPPORT_WORKER && this.worker){ const worker = new WorkerIndex(opt); if(worker){ + // assign encoder for result highlighting + worker.encoder = opt.encoder; // worker could be a promise // it needs to be resolved and swapped later index.set(key, worker); diff --git a/test/highlight.js b/test/highlight.js index dd2b2bf..b470ebd 100644 --- a/test/highlight.js +++ b/test/highlight.js @@ -9,10 +9,11 @@ const build_light = env && env.includes(".light"); const build_compact = env && env.includes(".compact"); const build_esm = !env || env.startsWith("module"); const Charset = _Charset || (await import("../src/charset.js")).default; +import SQLite from "flexsearch/db/sqlite"; if(!build_light) describe("Result Highlighting", function(){ - it("Should have been indexed properly", function(){ + it("Should have been highlighted results properly (Document)", function(){ // some test data const data = [{ @@ -25,6 +26,7 @@ if(!build_light) describe("Result Highlighting", function(){ // create the document index const index = new Document({ + cache: true, document: { store: true, index: [{ @@ -41,7 +43,7 @@ if(!build_light) describe("Result Highlighting", function(){ } // perform a query - const result = index.search({ + let result = index.searchCache({ query: "karmen or clown or not found", suggest: true, // set enrich to true (required) @@ -60,9 +62,147 @@ if(!build_light) describe("Result Highlighting", function(){ doc: data[1], highlight: 'Le clown et ses chiens' }]); + + // perform a query on cache + result = index.searchCache({ + 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" + }); + + expect(result[0].result).to.eql([{ + id: 1, + doc: data[0], + highlight: 'Carmencita' + },{ + id: 2, + doc: data[1], + highlight: 'Le clown et ses chiens' + }]); + + // perform a query using pluck + result = index.search({ + query: "karmen or clown or not found", + suggest: true, + // set enrich to true (required) + enrich: true, + pluck: "title", + // highlight template + // $1 is a placeholder for the matched partial + highlight: "$1" + }); + + expect(result).to.eql([{ + id: 1, + doc: data[0], + highlight: 'Carmencita' + },{ + id: 2, + doc: data[1], + highlight: 'Le clown et ses chiens' + }]); }); - it("Should have been indexed properly (#480)", function(){ + if(!build_compact) it("Should have been highlighted results properly (Document Worker)", async function(){ + + // some test data + const data = [{ + "id": 1, + "title": "Carmencita" + },{ + "id": 2, + "title": "Le clown et ses chiens" + }]; + + // create the document index + const index = await new Document({ + cache: true, + worker: true, + document: { + store: true, + index: [{ + field: "title", + tokenize: "forward", + encoder: Charset.LatinBalance + }] + } + }); + + // add test data + for(let i = 0; i < data.length; i++){ + await index.add(data[i]); + } + + // perform a query + let result = await index.searchCache({ + 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" + }); + + expect(result[0].result).to.eql([{ + id: 1, + doc: data[0], + highlight: 'Carmencita' + },{ + id: 2, + doc: data[1], + highlight: 'Le clown et ses chiens' + }]); + + // perform a query on cache + result = await index.searchCache({ + 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" + }); + + expect(result[0].result).to.eql([{ + id: 1, + doc: data[0], + highlight: 'Carmencita' + },{ + id: 2, + doc: data[1], + highlight: 'Le clown et ses chiens' + }]); + + // perform a query using pluck + result = await index.search({ + query: "karmen or clown or not found", + suggest: true, + // set enrich to true (required) + enrich: true, + pluck: "title", + // highlight template + // $1 is a placeholder for the matched partial + highlight: "$1" + }); + + expect(result).to.eql([{ + id: 1, + doc: data[0], + highlight: 'Carmencita' + },{ + id: 2, + doc: data[1], + highlight: 'Le clown et ses chiens' + }]); + }); + + it("Should have been highlighted results properly (#480)", function(){ const index = new Document({ document: { @@ -104,7 +244,6 @@ if(!build_light) describe("Result Highlighting", function(){ highlight: "$1" }); - expect(result.length).to.equal(2); expect(result[0]).to.eql({ field: "title", diff --git a/test/persistent.js b/test/persistent.js index e12ccdc..09ce98a 100644 --- a/test/persistent.js +++ b/test/persistent.js @@ -368,4 +368,147 @@ export default function(DB, DBClass){ index.db.close(); } }); -} \ No newline at end of file + + it("Result Highlighting", async function(){ + + // some test data + const data = [{ + "id": 1, + "title": "Carmencita" + },{ + "id": 2, + "title": "Le clown et ses chiens" + }]; + + // create the document index + const index = new Document({ + cache: true, + db: new DB("test-highlight", { + type: "Integer" + }), + document: { + store: true, + index: [{ + field: "title", + tokenize: "forward", + encoder: Charset.LatinBalance + }] + } + }); + + await index.db; + + // add test data + for(let i = 0; i < data.length; i++){ + index.add(data[i]); + } + + await index.commit(); + + // perform a query + let result = await index.searchCache({ + 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" + }); + + // todo Redis has slightly different sorting by aggregation + if(result[0].result[0].id === 1){ + expect(result[0].result).to.eql([{ + id: 1, + doc: data[0], + highlight: 'Carmencita' + },{ + id: 2, + doc: data[1], + highlight: 'Le clown et ses chiens' + }]); + } + else{ + expect(result[0].result).to.eql([{ + id: 2, + doc: data[1], + highlight: 'Le clown et ses chiens' + },{ + id: 1, + doc: data[0], + highlight: 'Carmencita' + }]); + } + + // perform a query on cache + result = await index.searchCache({ + 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" + }); + + // todo Redis has slightly different sorting by aggregation + if(result[0].result[0].id === 1){ + expect(result[0].result).to.eql([{ + id: 1, + doc: data[0], + highlight: 'Carmencita' + }, { + id: 2, + doc: data[1], + highlight: 'Le clown et ses chiens' + }]); + } + else{ + expect(result[0].result).to.eql([{ + id: 2, + doc: data[1], + highlight: 'Le clown et ses chiens' + },{ + id: 1, + doc: data[0], + highlight: 'Carmencita' + }]); + } + + // perform a query using pluck + result = await index.search({ + query: "karmen or clown or not found", + suggest: true, + // set enrich to true (required) + enrich: true, + pluck: "title", + // highlight template + // $1 is a placeholder for the matched partial + highlight: "$1" + }); + + // todo Redis has slightly different sorting by aggregation + if(result[0].id === 1){ + expect(result).to.eql([{ + id: 1, + doc: data[0], + highlight: 'Carmencita' + },{ + id: 2, + doc: data[1], + highlight: 'Le clown et ses chiens' + }]); + } + else{ + expect(result).to.eql([{ + id: 2, + doc: data[1], + highlight: 'Le clown et ses chiens' + },{ + id: 1, + doc: data[0], + highlight: 'Carmencita' + }]); + } + }); +}