1
0
mirror of https://github.com/nextapps-de/flexsearch.git synced 2025-08-16 19:14:16 +02:00

added support for workers

This commit is contained in:
Thomas Wilkerling
2021-05-19 12:22:01 +02:00
parent 41398573ca
commit 9c7541d993
15 changed files with 268 additions and 199 deletions

View File

@@ -5,27 +5,31 @@
* Licence: Apache-2.0
* https://github.com/nextapps-de/flexsearch
*/
(function(){'use strict';var q;var t=/[\W_]+/;function aa(a){if(a=a.toLowerCase())if(a&&this.B&&(a=B(a,this.B)),this.D&&1<a.length&&(a=B(a,this.D)),a&&(t||""===t)&&(a=a.split(t),this.filter)){for(var b=this.filter,c=a.length,d=[],e=0,h=0;e<c;e++){var f=a[e];f&&!b[f]&&(d[h++]=f)}a=d}return a};function C(a){for(var b=Array(a),c=0;c<a;c++)b[c]=D();return b}function D(){return Object.create(null)}function ba(a,b){return b.length-a.length};function ca(a){for(var b=D(),c=0,d=a.length;c<d;c++)b[a[c]]=1;return b}function F(a,b){for(var c=Object.keys(a),d=c.length,e=[],h="",f=0,g=0,k,l;g<d;g++)k=c[g],(l=a[k])?(e[f++]=new RegExp(b?"(?!\\b)"+k+"(\\b|_)":k,"g"),e[f++]=l):h+=(h?"|":"")+k;h&&(e[f++]=new RegExp(b?"(?!\\b)("+h+")(\\b|_)":"("+h+")","g"),e[f]="");return e}function B(a,b){for(var c=0,d=b.length;c<d&&(a=a.replace(b[c],b[c+1]),a);c+=2);return a};var G={},H={};var I=Promise;Object.assign||(Object.assign=function(){for(var a=arguments,b=a.length,c=a[0],d=1,e,h,f;d<b;d++){e=a[d];h=Object.keys(e);f=h.length;for(var g=0,k;g<f;g++)k=h[g],c[k]=e[k]}return c});if(!I){var J=function(a){this.g=null;var b=this;a(function(c){b.g&&(b.g(c),b=b.g=null)})};J.prototype.then=function(a){this.g=a};I=J};function K(a,b,c){return L.call(this,this.add,arguments)}function M(a,b,c){return L.call(this,this.append,arguments)}function N(a,b,c){return L.call(this,this.search,arguments)}function P(a,b,c){return L.call(this,this.update,arguments)}function Q(a,b){return L.call(this,this.remove,arguments)}function L(a,b){for(var c,d=0;d<b.length;d++)if("function"===typeof b[d]){c=b[d];delete b[d];break}var e=this;d=new I(function(h){setTimeout(function(){h(a.apply(e,b))})});return c?(d.then(c),this):d};function da(a,b,c,d){var e=a.length,h=[],f=D(),g=0;d&&(d=[]);for(var k=0;k<e;k++){for(var l=a[k],n=l.length,r=D(),p=!k,m=0;m<n;m++){var u=l[m],w=u.length;if(w){d&&(d[m]=[]);for(var v=0,x=0,y;v<w;v++)if(y=u[v],!k)r[y]=1;else if(f[y]){if(k===e-1)if(c)c--;else{if(h[g++]=y,g===b)return h}else d&&x<b&&(d[m][x++]=y),r[y]=1;p=!0}}}if(!p&&!d)return[];f=r}if(d)for(a=d.length-1;0<=a;a--)if(e=(c=d[a])&&c.length){if(g+e>=b)return h.concat(c.slice(0,b-g));h=h.concat(c);g+=e}return h};function R(a){this.h=!0!==a&&a;this.cache=D();this.g=[]}function ea(a,b,c){"object"===typeof a&&(a=a.query);var d=this.cache.get(a);d||(d=this.search(a,b,c),this.cache.set(a,d));return d}R.prototype.set=function(a,b){if(!this.cache[a]){var c=this.g.length;c===this.h?delete this.cache[this.g[c-1]]:c++;for(--c;0<c;c--)this.g[c]=this.g[c-1];this.g[0]=a}this.cache[a]=b};
R.prototype.get=function(a){var b=this.cache[a];if(this.h&&b&&(a=this.g.indexOf(a))){var c=this.g[a-1];this.g[a-1]=this.g[a];this.g[a]=c}return b};var fa={memory:{charset:"latin:extra",u:3,s:3,i:!1,l:"memory"},performance:{threshold:8,s:3,context:{depth:1,v:!0}},match:{charset:"latin:extra",G:"full",u:3},score:{charset:"latin:advanced",threshold:1,context:{depth:3,v:!0}},"default":{u:3,threshold:0,depth:3}};function ha(a){if("string"===typeof a)a=fa[a];else{var b=a.preset;b&&(a=Object.assign({},fa[b],a))}return a};function ia(a,b,c,d,e,h){setTimeout(function(){var f=a(c,JSON.stringify(h));f&&f.then?f.then(function(){b.export(a,b,c,d,e+1)}):b.export(a,b,c,d,e+1)})};function S(a,b){if(!(this instanceof S))return new S(a);if(a){a=ha(a);var c=a.charset;var d=a.lang;"string"===typeof c&&(-1===c.indexOf(":")&&(c+=":default"),c=H[c]);"string"===typeof d&&(d=G[d])}else a={};var e,h=a.context||{};this.encode=a.encode||c&&c.encode||aa;this.register=b||D();var f=a.resolution||9;var g=a.threshold||0;g>=f&&(g=f-1);this.u=f;this.threshold=g;this.A=b=c&&c.G||a.tokenize||"strict";this.depth="strict"===b&&h.depth;this.v=T(h.bidirectional,!0);this.l=e="memory"===a.optimize;
this.i=T(a.fastupdate,!0);this.s=a.minlength||1;this.g=e?C(f-g):D();f=h.resolution||f;g=h.threshold||g;g>=f&&(g=f-1);this.j=f;this.m=g;this.h=e?C(f-g):D();this.C=c&&c.C||a.rtl;this.B=(b=a.matcher||d&&d.B)&&F(b,!1);this.D=(b=a.stemmer||d&&d.D)&&F(b,!0);this.filter=(b=a.filter||d&&d.filter)&&ca(b);this.cache=(b=a.cache)&&new R(b);this.o=(b=a.worker)&&new Worker("worker.js",{type:"module"});b&&(this.o.onmessage=function(k){console.log(k.data)},this.o.postMessage({H:"register",options:a}))}
function T(a,b){return"undefined"!==typeof a?a:b}q=S.prototype;q.append=function(a,b){return this.add(a,b,!0)};
q.add=function(a,b,c){if(this.register[a]&&!c)return this.update(a,b);if(b&&(a||0===a)){b=this.encode(b);var d=b.length;if(d){for(var e=this.depth,h=this.u-this.threshold,f=D(),g=D(),k=0;k<d;k++){var l=b[this.C?d-1-k:k],n=l.length;if(l&&n>=this.s&&(e||!f[l])){var r=Math.min(this.u/d*k|0,k);if(r<h){var p="";switch(this.A){case "full":if(3<n){for(var m=0;m<n;m++){var u=m?Math.min(r/2+this.u/n*m/2|0,r+m):r;if(u<h)for(var w=n;w>m;w--)p=l.substring(m,w),p.length>=this.s&&U(this,f,p,u,a,c)}break}case "reverse":if(2<
n){for(m=n-1;0<m;m--)p=l[m]+p,p.length>=this.s&&U(this,f,p,r,a,c);p=""}case "forward":if(1<n)for(m=0;m<n;m++)p+=l[m],p.length>=this.s&&U(this,f,p,r,a,c);break;default:if(U(this,f,l,r,a,c),e&&1<d&&k<d-1)for(n=this.j-this.m,r=D(),p=l,m=Math.min(e+1,d-k),u=r[p]=1;u<m;u++)if((l=b[this.C?d-1-k-u:k+u])&&l.length>=this.s&&!r[l]){if(r[l]=1,w=Math.min((this.j-m)/d*k+u|0,k+(u-1)),w<n){var v=this.v&&l>p;U(this,g,v?p:l,w,a,c,v?l:p)}}else m=Math.min(m+1,d-k)}}}}this.i||(this.register[a]=1)}}return this};
function U(a,b,c,d,e,h,f){var g=f?a.h:a.g;if(!b[c]||f&&!b[c][f])a.l&&(g=g[d]),f?(b[c]||(b[c]=D()),b[c][f]=1,g=g[f]||(g[f]=D())):b[c]=1,g=g[c]||(g[c]=[]),a.l||(g=g[d]||(g[d]=[])),h&&-1!==g.indexOf(e)||(g[g.length]=e,a.i&&(a=a.register[e]||(a.register[e]=[]),a[a.length]=g))}
q.search=function(a,b,c){"object"===typeof a?(c=a,a=c.query):"object"===typeof b&&(c=b);var d=[],e=this.threshold,h=0;if(c){b=c.limit;h=c.offset||0;e=T(c.threshold,e);var f=c.context;var g=c.suggest}if(a){a=this.encode(a);var k=a.length;if(1<k){c=D();for(var l=[],n=0,r=0,p;n<k;n++)if((p=a[n])&&p.length>=this.s&&!c[p])if(this.l||g||this.g[p])l[r++]=p,c[p]=1;else return d;a=l;k=a.length}}if(!k)return d;b||(b=100);c=this.u-e;e=this.j-e;f=this.depth&&1<k&&!1!==f;l=0;if(f){var m=a[0];l=1}else 1<k&&a.sort(ba);
for(;l<k;l++){r=a[l];f?(n=ja(this,d,g,e,b,2===k,r,m),g&&!1===n&&d.length||(m=r)):n=ja(this,d,g,c,b,1===k,r);if(n)return n;if(g&&l===k-1){n=d.length;if(!n){if(f){f=0;l=-1;continue}return d}if(1===n)return d=d[0],d=1===d.length?d[0]:[].concat.apply([],d),d.length>b?d.slice(0,b):d}}return da(d,b,h,g)};function ka(a,b,c,d){c?(d=d&&b>c,a=(a=a[d?b:c])&&a[d?c:b]):a=a[b];return a}
function ja(a,b,c,d,e,h,f,g){var k=[],l=g?a.h:a.g;a.l||(l=ka(l,f,g,a.v));if(l){var n=0;d=Math.min(l.length,d);for(var r=0,p=0,m;r<d&&!(m=l[r],a.l&&(m=ka(m,f,g,a.v)),m&&(k[n++]=m,h&&(p+=m.length,p>=e)));r++);if(n){if(h)return k=1===n?k[0]:[].concat.apply([],k),k.length>e?k.slice(0,e):k;b[b.length]=k;return}}return!c&&k}q.contain=function(a){return!!this.register[a]};q.update=function(a,b){return this.remove(a).add(a,b)};
q.remove=function(a,b){var c=this.register[a];if(c){if(this.i)for(var d=0,e;d<c.length;d++)e=c[d],e.splice(e.indexOf(a),1);else V(this.g,a,this.u-this.threshold,this.l),this.depth&&V(this.h,a,this.j-this.m,this.l);b||delete this.register[a];if(this.cache)for(b=this.cache,c=0;c<b.g.length;c++)e=b.g[c],d=b.cache[e],-1!==d.indexOf(a)&&(b.g.splice(c--,1),delete b.cache[e])}return this};
function V(a,b,c,d,e){var h=0;if(a.constructor===Array)if(e)b=a.indexOf(b),-1!==b?1<a.length&&(a.splice(b,1),h++):h++;else{e=Math.min(a.length,c);for(var f=0,g;f<e;f++)if(g=a[f])h=V(g,b,c,d,e),d||h||delete a[f]}else for(f in a)(h=V(a[f],b,c,d,e))||delete a[f];return h}q.searchCache=ea;q.addAsync=K;q.appendAsync=M;q.searchAsync=N;q.updateAsync=P;q.removeAsync=Q;
q.export=function(a,b,c,d,e){switch(e||(e=0)){case 0:var h="reg";if(this.i){var f=D();for(var g in this.register)f[g]=1}else f=this.register;break;case 1:h="cfg";f={doc:0,opt:this.l?1:0};break;case 2:h="map";f=this.g;break;case 3:h="ctx";f=this.h;break;default:return}ia(a,b||this,c?c+"."+h:h,d,e,f);return!0};
q.import=function(a,b){if(b)switch("string"===typeof b&&(b=JSON.parse(b)),a){case "cfg":this.l=!!b.opt;break;case "reg":this.i=!1;this.register=b;break;case "map":this.g=b;break;case "ctx":this.h=b}};function W(a){if(!(this instanceof W))return new W(a);var b;a||(a={});this.F=[];this.g=[];this.m=[];this.register=D();this.key=(b=a.key)&&X(b,this.m)||"id";b=a.fastupdate;this.i="undefined"!==typeof b?b:!0;this.store=(b=a.store)&&D();this.o=b&&!0!==b&&[];this.A=(b=a.tag)&&X(b,this.m);this.h=b&&D();this.cache=(b=a.cache)&&new R(b);a.cache=!1;b=a;a=D();var c=b.doc;if("string"===typeof c)c=[c];else if(c.constructor!==Array){var d=c;c=Object.keys(c)}for(var e=0,h,f;e<c.length;e++)h=c[e],d?(f=d[h],f="object"===
typeof f?Object.assign({},b,f):b):f=b,a[h]=new S(f,this.register),this.F[e]=X(h,this.m),this.g[e]=h;if(this.o)for(d=b.store,"string"===typeof d&&(d=[d]),b=0;b<d.length;b++)this.o[b]=X(d[b],this.m);this.j=a}function X(a,b){for(var c=a.split(":"),d=0,e=0;e<c.length;e++)a=c[e],0<=a.indexOf("[]")&&(a=a.substring(0,a.length-2))&&(b[d]=!0),a&&(c[d++]=a);d<c.length&&(c.length=d);return 1<d?c:c[0]}function la(a,b){if("string"===typeof b)a=a[b];else for(var c=0;a&&c<b.length;c++)a=a[b[c]];return a}
function Y(a,b,c,d,e){a=a[e];if(d===c.length-1)b[e]=a;else if(a)if(a.constructor===Array)for(b=b[e]=Array(a.length),e=0;e<a.length;e++)Y(a,b,c,d,e);else b=b[e]||(b[e]=D()),e=c[++d],Y(a,b,c,d,e)}function Z(a,b,c,d,e,h,f,g){a=a[f];if(d===b.length-1){if(a.constructor===Array){if(c[d]){for(b=0;b<a.length;b++)e.add(h,a[b],!0);return}a=a.join(" ")}e.add(h,a,g)}else if(a)if(a.constructor===Array)for(f=0;f<a.length;f++)Z(a,b,c,d,e,h,f,g);else f=b[++d],Z(a,b,c,d,e,h,f,g)}q=W.prototype;
q.add=function(a,b,c){"object"===typeof a&&(b=a,a=la(b,this.key));if(b&&(a||0===a)){if(this.register[a])return this.update(a,b);for(var d=0,e,h;d<this.g.length;d++)h=this.g[d],e=this.F[d],"string"===typeof e&&(e=[e]),Z(b,e,this.m,0,this.j[h],a,e[0],c);if(this.A){d=la(b,this.A);e=D();"string"===typeof d&&(d=[d]);h=0;for(var f;h<d.length;h++)if(f=d[h],!e[f]&&(e[f]=1,f=this.h[f]||(this.h[f]=[]),!c||-1===f.indexOf(a)))if(f[f.length]=a,this.i){var g=this.register[a]||(this.register[a]=[]);g[g.length]=
f}}if(this.store){if(this.o){var k=D();for(c=0;c<this.o.length;c++)d=this.o[c],"string"===typeof d?k[d]=b[d]:Y(b,k,d,0,d[0])}this.store[a]=k||b}}return this};q.append=function(a,b){return this.add(a,b,!0)};q.update=function(a,b){return this.remove(a).add(a,b)};
q.remove=function(a){"object"===typeof a&&(a=a[this.key]);if(this.register[a]){for(var b=0;b<this.g.length&&(this.j[this.g[b]].remove(a,!0),!this.i);b++);if(this.A&&!this.i)for(var c in this.h){b=this.h[c];var d=b.indexOf(a);-1!==d&&(1<b.length?b.splice(d,1):delete this.h[c])}this.store&&delete this.store[a];delete this.register[a]}return this};
q.search=function(a,b,c){"object"===typeof a?(c=a,a=c.query):"object"===typeof b&&(c=b);var d=[],e=[],h,f=0;if(c){var g=(h=c.pluck)||c.field;var k=c.tag;var l=this.store&&c.enrich;var n="and"===c.bool;b=c.limit;var r=c.offset;if(g)if("string"===typeof g)g=[g];else if(g.constructor!==Array){var p=g;g=Object.keys(g)}if(k&&("string"===typeof k&&(k=[k]),!a)){for(e=0;e<k.length;e++){h=k[e];a=b||100;c=r||0;if((p=(g=this.h[h])&&g.length-c)&&0<p){if(p>a||c)g=g.slice(c,c+a);l&&(g=ma.call(this,g));h={tag:h,
result:g}}else h=void 0;h&&(d[d.length]=h,f++)}return f?d:[]}}g||(g=this.g);n=n&&(1<g.length||k&&1<k.length);for(var m=0,u,w,v;m<g.length;m++){w=g[m];u=this.j[w].search(a,b,p?p[w]:c);v=u.length;if(k&&v){var x=[],y=0;n&&(x[0]=[u]);for(var A=0,z;A<k.length;A++)if(v=k[A],v=(z=this.h[v])&&z.length)y++,x[x.length]=n?[z]:z;if(y){if(n)x=da(x,b||100,r||0);else{v=u;u=D();y=D();A=[];for(z=0;z<v.length;z++)u[v[z]]=1;for(v=0;v<x.length;v++){z=x[v];for(var O=0,E;O<z.length;O++)E=z[O],u[E]&&!y[E]&&(y[E]=1,A[A.length]=
E)}x=A}u=x;v=u.length}}if(v)e[f]=w,d[f++]=u;else if(n)return[]}if(!f)return[];if(h&&(!l||!this.store))return d[0];for(b=0;b<e.length;b++){k=d[b];k.length&&l&&(k=ma.call(this,k));if(h)return k;d[b]={field:e[b],result:k}}return d};function ma(a){for(var b=Array(a.length),c=0,d;c<a.length;c++)d=a[c],b[c]={key:d,doc:this.store[d]};return b}q.contain=function(a){return!!this.register[a]};q.get=function(a){return this.store[a]};q.set=function(a,b){this.store[a]=b;return this};q.searchCache=ea;
q.addAsync=K;q.appendAsync=M;q.searchAsync=N;q.updateAsync=P;q.removeAsync=Q;q.export=function(a,b,c,d,e){e||(e=0);d||(d=0);if(d<this.g.length){var h=this.g[d],f=this.j[h];b=this;setTimeout(function(){f.export(a,b,e?h.replace(":","-"):"",d,e++)||(d++,e=1,b.export(a,b,h,d,e))})}else{switch(e){case 1:c="tag";var g=this.h;break;case 2:c="store";g=this.store;break;default:return}ia(a,this,c,d,e,g)}};
q.import=function(a,b){if(b)switch("string"===typeof b&&(b=JSON.parse(b)),a){case "tag":this.h=b;break;case "reg":this.i=!1;this.register=b;a=0;for(var c;a<this.g.length;a++)c=this.j[this.g[a]],c.register=b,c.i=!1;break;case "store":this.store=b;break;default:a=a.split("."),c=a[0],a=a[1],c&&a&&this.j[c].import(a,b)}};window.FlexSearch={Index:S,Document:W,registerCharset:function(a,b){H[a]=b},registerLanguage:function(a,b){G[a]=b}};}).call(this);
(function(){'use strict';var t;function aa(a){function b(c){return a.next(c)}function d(c){return a.throw(c)}return new Promise(function(c,e){function f(g){g.done?c(g.value):Promise.resolve(g.value).then(b,d).then(f,e)}f(a.next())})};const x=/[\W_]+/;function ba(a){if(a=a.toLowerCase())if(a&&this.B&&(a=z(a,this.B)),this.F&&1<a.length&&(a=z(a,this.F)),a&&(x||""===x)&&(a=a.split(x),this.filter)){var b=this.filter;const d=a.length,c=[];for(let e=0,f=0;e<d;e++){const g=a[e];g&&!b[g]&&(c[f++]=g)}a=c}return a};function B(a){const b=Array(a);for(let d=0;d<a;d++)b[d]=E();return b}function E(){return Object.create(null)}function ca(a,b){return b.length-a.length};function F(a,b){const d=Object.keys(a),c=d.length,e=[];let f="",g=0;for(let h=0,k,l;h<c;h++)k=d[h],(l=a[k])?(e[g++]=new RegExp(b?"(?!\\b)"+k+"(\\b|_)":k,"g"),e[g++]=l):f+=(f?"|":"")+k;f&&(e[g++]=new RegExp(b?"(?!\\b)("+f+")(\\b|_)":"("+f+")","g"),e[g]="");return e}function z(a,b){for(let d=0,c=b.length;d<c&&(a=a.replace(b[d],b[d+1]),a);d+=2);return a};const G={},da={};let K=Promise;Object.assign||(Object.assign=function(){const a=arguments,b=a.length,d=a[0];for(let c=1,e,f,g;c<b;c++){e=a[c];f=Object.keys(e);g=f.length;for(let h=0,k;h<g;h++)k=f[h],d[k]=e[k]}return d});if(!K){function a(b){this.g=null;const d=this;b(function(c){d.g&&d.g(c)})}a.prototype.then=function(b){this.g=b};K=a};function ea(a){L(a,"add");L(a,"append");L(a,"search");L(a,"update");L(a,"remove")}function L(a,b){a[b+"Async"]=function(){const d=this,c=arguments;var e=c[c.length-1];let f;"function"===typeof e&&(f=e,delete c[c.length-1]);e=new K(function(g){setTimeout(function(){const h=d[b];h.async=!0;g(h.apply(d,c));h.async=!1})});return f?(e.then(f),this):e}};function fa(a,b,d,c){const e=a.length;let f=[],g=E(),h=0;c&&(c=[]);for(let k=0;k<e;k++){const l=a[k],u=l.length,m=E();let q=!k;for(let n=0;n<u;n++){const w=l[n],r=w.length;if(r){c&&(c[n]=[]);for(let y=0,v=0,p;y<r;y++)if(p=w[y],!k)m[p]=1;else if(g[p]){if(k===e-1)if(d)d--;else{if(f[h++]=p,h===b)return f}else c&&v<b&&(c[n][v++]=p),m[p]=1;q=!0}}}if(!q&&!c)return[];g=m}if(c)for(let k=c.length-1,l,u;0<=k;k--)if(u=(l=c[k])&&l.length){if(h+u>=b)return f.concat(l.slice(0,b-h));f=f.concat(l);h+=u}return f}
;function M(a){this.h=!0!==a&&a;this.cache=E();this.g=[]}function ha(a,b,d){"object"===typeof a&&(a=a.query);let c=this.cache.get(a);c||(c=this.search(a,b,d),this.cache.set(a,c));return c}M.prototype.set=function(a,b){if(!this.cache[a]){var d=this.g.length;d===this.h?delete this.cache[this.g[d-1]]:d++;for(--d;0<d;d--)this.g[d]=this.g[d-1];this.g[0]=a}this.cache[a]=b};
M.prototype.get=function(a){const b=this.cache[a];if(this.h&&b&&(a=this.g.indexOf(a))){const d=this.g[a-1];this.g[a-1]=this.g[a];this.g[a]=d}return b};const ia={memory:{charset:"latin:extra",s:3,o:3,i:!1,l:"memory"},performance:{threshold:8,o:3,context:{depth:1,v:!0}},match:{charset:"latin:extra",I:"full",s:3},score:{charset:"latin:advanced",threshold:1,context:{depth:3,v:!0}},"default":{s:3,threshold:0,depth:3}};function ja(a,b,d,c,e,f){setTimeout(function(){const g=a(d,JSON.stringify(f));g&&g.then?g.then(function(){b.export(a,b,d,c,e+1)}):b.export(a,b,d,c,e+1)})};function N(a,b){if(!(this instanceof N))return new N(a);var d;if(a){"string"===typeof a?a=ia[a]:(d=a.preset)&&(a=Object.assign({},ia[d],a));d=a.charset;var c=a.lang;"string"===typeof d&&(-1===d.indexOf(":")&&(d+=":default"),d=da[d]);"string"===typeof c&&(c=G[c])}else a={};let e,f,g,h=a.context||{};this.encode=a.encode||d&&d.encode||ba;this.register=b||E();e=a.resolution||9;f=a.threshold||0;f>=e&&(f=e-1);this.s=e;this.threshold=f;this.u=b=d&&d.I||a.tokenize||"strict";this.depth="strict"===b&&h.depth;
this.v=P(h.bidirectional,!0);this.l=g="memory"===a.optimize;this.i=P(a.fastupdate,!0);this.o=a.minlength||1;this.g=g?B(e-f):E();e=h.resolution||e;f=h.threshold||f;f>=e&&(f=e-1);this.j=e;this.m=f;this.h=g?B(e-f):E();this.D=d&&d.D||a.rtl;this.B=(b=a.matcher||c&&c.B)&&F(b,!1);this.F=(b=a.stemmer||c&&c.F)&&F(b,!0);if(d=b=a.filter||c&&c.filter){d=b;c=E();for(let k=0,l=d.length;k<l;k++)c[d[k]]=1;d=c}this.filter=d;this.cache=(b=a.cache)&&new M(b)}function P(a,b){return"undefined"!==typeof a?a:b}t=N.prototype;
t.append=function(a,b){return this.add(a,b,!0)};
t.add=function(a,b,d){if(this.register[a]&&!d)return this.update(a,b);if(b&&(a||0===a)){b=this.encode(b);const l=b.length;if(l){const u=this.depth,m=this.s-this.threshold,q=E(),n=E();for(let w=0;w<l;w++){let r=b[this.D?l-1-w:w];var c=r.length;if(r&&c>=this.o&&(u||!q[r])){var e=Math.min(this.s/l*w|0,w);if(e<m){var f="";switch(this.u){case "full":if(3<c){for(var g=0;g<c;g++){var h=g?Math.min(e/2+this.s/c*g/2|0,e+g):e;if(h<m)for(var k=c;k>g;k--)f=r.substring(g,k),f.length>=this.o&&Q(this,q,f,h,a,d)}break}case "reverse":if(2<
c){for(g=c-1;0<g;g--)f=r[g]+f,f.length>=this.o&&Q(this,q,f,e,a,d);f=""}case "forward":if(1<c)for(g=0;g<c;g++)f+=r[g],f.length>=this.o&&Q(this,q,f,e,a,d);break;default:if(Q(this,q,r,e,a,d),u&&1<l&&w<l-1)for(c=this.j-this.m,e=E(),f=r,g=Math.min(u+1,l-w),e[f]=1,h=1;h<g;h++)if((r=b[this.D?l-1-w-h:w+h])&&r.length>=this.o&&!e[r]){if(e[r]=1,k=Math.min((this.j-g)/l*w+h|0,w+(h-1)),k<c){const y=this.v&&r>f;Q(this,n,y?f:r,k,a,d,y?r:f)}}else g=Math.min(g+1,l-w)}}}}this.i||(this.register[a]=1)}}return this};
function Q(a,b,d,c,e,f,g){let h=g?a.h:a.g;if(!b[d]||g&&!b[d][g])a.l&&(h=h[c]),g?(b[d]||(b[d]=E()),b[d][g]=1,h=h[g]||(h[g]=E())):b[d]=1,h=h[d]||(h[d]=[]),a.l||(h=h[c]||(h[c]=[])),f&&-1!==h.indexOf(e)||(h[h.length]=e,a.i&&(a=a.register[e]||(a.register[e]=[]),a[a.length]=h))}
t.search=function(a,b,d){"object"===typeof a?(d=a,a=d.query):"object"===typeof b&&(d=b);let c=[],e;var f=this.threshold;let g,h=0;if(d){b=d.limit;h=d.offset||0;f=P(d.threshold,f);var k=d.context;g=d.suggest}if(a&&(a=this.encode(a),e=a.length,1<e)){d=E();var l=[];for(let m=0,q=0,n;m<e;m++)if((n=a[m])&&n.length>=this.o&&!d[n])if(this.l||g||this.g[n])l[q++]=n,d[n]=1;else return c;a=l;e=a.length}if(!e)return c;b||(b=100);d=this.s-f;f=this.j-f;k=this.depth&&1<e&&!1!==k;l=0;let u;k?(u=a[0],l=1):1<e&&a.sort(ca);
for(let m,q;l<e;l++){q=a[l];k?(m=ka(this,c,g,f,b,2===e,q,u),g&&!1===m&&c.length||(u=q)):m=ka(this,c,g,d,b,1===e,q);if(m)return m;if(g&&l===e-1){let n=c.length;if(!n){if(k){k=0;l=-1;continue}return c}if(1===n)return c=c[0],c=1===c.length?c[0]:[].concat.apply([],c),c.length>b?c.slice(0,b):c}}return fa(c,b,h,g)};function la(a,b,d,c){d?(c=c&&b>d,a=(a=a[c?b:d])&&a[c?d:b]):a=a[b];return a}
function ka(a,b,d,c,e,f,g,h){let k=[],l=h?a.h:a.g;a.l||(l=la(l,g,h,a.v));if(l){let u=0;c=Math.min(l.length,c);for(let m=0,q=0,n;m<c&&!(n=l[m],a.l&&(n=la(n,g,h,a.v)),n&&(k[u++]=n,f&&(q+=n.length,q>=e)));m++);if(u){if(f)return k=1===u?k[0]:[].concat.apply([],k),k.length>e?k.slice(0,e):k;b[b.length]=k;return}}return!d&&k}t.contain=function(a){return!!this.register[a]};t.update=function(a,b){return this.remove(a).add(a,b)};
t.remove=function(a,b){const d=this.register[a];if(d){if(this.i)for(let c=0,e;c<d.length;c++)e=d[c],e.splice(e.indexOf(a),1);else R(this.g,a,this.s-this.threshold,this.l),this.depth&&R(this.h,a,this.j-this.m,this.l);b||delete this.register[a];if(this.cache){b=this.cache;for(let c=0,e,f;c<b.g.length;c++)f=b.g[c],e=b.cache[f],-1!==e.indexOf(a)&&(b.g.splice(c--,1),delete b.cache[f])}}return this};
function R(a,b,d,c,e){let f=0;if(a.constructor===Array)if(e)b=a.indexOf(b),-1!==b?1<a.length&&(a.splice(b,1),f++):f++;else{e=Math.min(a.length,d);for(let g=0,h;g<e;g++)if(h=a[g])f=R(h,b,d,c,e),c||f||delete a[g]}else for(let g in a)(f=R(a[g],b,d,c,e))||delete a[g];return f}t.searchCache=ha;ea(N.prototype);
N.prototype.export=function(a,b,d,c,e){let f,g;switch(e||(e=0)){case 0:f="reg";if(this.i){g=E();for(let h in this.register)g[h]=1}else g=this.register;break;case 1:f="cfg";g={doc:0,opt:this.l?1:0};break;case 2:f="map";g=this.g;break;case 3:f="ctx";g=this.h;break;default:return}ja(a,b||this,d?d+"."+f:f,c,e,g);return!0};
N.prototype.import=function(a,b){if(b)switch("string"===typeof b&&(b=JSON.parse(b)),a){case "cfg":this.l=!!b.opt;break;case "reg":this.i=!1;this.register=b;break;case "map":this.g=b;break;case "ctx":this.h=b}};let ma=0;function S(a){if("object"===typeof a){var b=a;a=0}"function"===typeof b.encode&&(b.encode=b.encode.toString());const d=this;this.id=a||ma++;this.h=null;this.g=new Worker("worker.js",{type:"module"});this.g.onmessage=function(c){d.h(c.data)};this.g.postMessage({H:"create",options:b})}T("add");T("append");T("search");T("update");T("remove");
function T(a){S.prototype[a]=S.prototype[a+"Async"]=function(){const b=this,d=[].slice.call(arguments);var c=d[d.length-1];let e;"function"===typeof c&&(e=c,d.splice(d.length-1,1));c=new K(function(f){b.g.postMessage({H:a,J:d});"search"===a?b.h=f:f()});return e?(c.then(e),this):c}};function W(a){if(!(this instanceof W))return new W(a);var b;a||(a={});this.G=[];this.g=[];this.m=[];this.register=E();this.key=(b=a.key)&&X(b,this.m)||"id";b=a.fastupdate;this.i="undefined"!==typeof b?b:!0;this.store=(b=a.store)&&E();this.u=b&&!0!==b&&[];this.A=(b=a.tag)&&X(b,this.m);this.h=b&&E();this.cache=(b=a.cache)&&new M(b);a.cache=!1;this.C=a.worker;this.async=!1;b=a;a=E();let d=b.doc;if("string"===typeof d)d=[d];else if(d.constructor!==Array){var c=d;d=Object.keys(d)}for(let e=0,f,g;e<d.length;e++)f=
d[e],c?(g=c[f],g="object"===typeof g?Object.assign({},b,g):b):g=b,a[f]=this.C?new S(g):new N(g,this.register),this.G[e]=X(f,this.m),this.g[e]=f;if(this.u)for(c=b.store,"string"===typeof c&&(c=[c]),b=0;b<c.length;b++)this.u[b]=X(c[b],this.m);this.j=a}function X(a,b){const d=a.split(":");let c=0;for(let e=0;e<d.length;e++)a=d[e],0<=a.indexOf("[]")&&(a=a.substring(0,a.length-2))&&(b[c]=!0),a&&(d[c++]=a);c<d.length&&(d.length=c);return 1<c?d:d[0]}
function na(a,b){if("string"===typeof b)a=a[b];else for(let d=0;a&&d<b.length;d++)a=a[b[d]];return a}function Y(a,b,d,c,e){a=a[e];if(c===d.length-1)b[e]=a;else if(a)if(a.constructor===Array)for(b=b[e]=Array(a.length),e=0;e<a.length;e++)Y(a,b,d,c,e);else b=b[e]||(b[e]=E()),e=d[++c],Y(a,b,d,c,e)}
function Z(a,b,d,c,e,f,g,h){a=a[g];if(c===b.length-1){if(a.constructor===Array){if(d[c]){for(b=0;b<a.length;b++)e.add(f,a[b],!0);return}a=a.join(" ")}e.add(f,a,h)}else if(a)if(a.constructor===Array)for(g=0;g<a.length;g++)Z(a,b,d,c,e,f,g,h);else g=b[++c],Z(a,b,d,c,e,f,g,h)}t=W.prototype;
t.add=function(a,b,d){"object"===typeof a&&(b=a,a=na(b,this.key));if(b&&(a||0===a)){if(this.register[a])return this.update(a,b);for(let c=0,e,f;c<this.g.length;c++)f=this.g[c],e=this.G[c],"string"===typeof e&&(e=[e]),Z(b,e,this.m,0,this.j[f],a,e[0],d);if(this.A){let c=na(b,this.A),e=E();"string"===typeof c&&(c=[c]);for(let f=0,g,h;f<c.length;f++)if(g=c[f],!e[g]&&(e[g]=1,h=this.h[g]||(this.h[g]=[]),!d||-1===h.indexOf(a)))if(h[h.length]=a,this.i){const k=this.register[a]||(this.register[a]=[]);k[k.length]=
h}}if(this.store){let c;if(this.u){c=E();for(let e=0,f;e<this.u.length;e++)f=this.u[e],"string"===typeof f?c[f]=b[f]:Y(b,c,f,0,f[0])}this.store[a]=c||b}}return this};t.append=function(a,b){return this.add(a,b,!0)};t.update=function(a,b){return this.remove(a).add(a,b)};
t.remove=function(a){"object"===typeof a&&(a=a[this.key]);if(this.register[a]){for(var b=0;b<this.g.length&&(this.j[this.g[b]].remove(a,!0),!this.i);b++);if(this.A&&!this.i)for(let d in this.h){b=this.h[d];const c=b.indexOf(a);-1!==c&&(1<b.length?b.splice(c,1):delete this.h[d])}this.store&&delete this.store[a];delete this.register[a]}return this};
t.search=function(a,b,d){const c=this;return aa(function*(){"object"===typeof a?(d=a,a=d.query):"object"===typeof b&&(d=b);let e=[];var f=[],g;let h;let k;let l,u=0;if(d){var m=(g=d.pluck)||d.field;k=d.tag;h=c.store&&d.enrich;var q="and"===d.bool;b=d.limit;l=d.offset;if(m)if("string"===typeof m)m=[m];else if(m.constructor!==Array){var n=m;m=Object.keys(m)}if(k&&("string"===typeof k&&(k=[k]),!a)){for(let v=0,p;v<k.length;v++){f=k[v];g=b||100;m=l||0;if((q=(n=c.h[f])&&n.length-m)&&0<q){if(q>g||m)n=n.slice(m,
m+g);h&&(n=oa.call(c,n));p={tag:f,result:n}}else p=void 0;p&&(e[e.length]=p,u++)}return u?e:[]}}m||(m=c.g);q=q&&(1<m.length||k&&1<k.length);let w=[];if(c.C||c.async){for(let v=0,p;v<m.length;v++)p=m[v],w[v]=c.j[p][c.async?"searchAsync":"search"](a,b,n?n[p]:d);w=yield Promise.all(w)}for(let v=0,p,O,H;v<m.length;v++){O=m[v];p=c.C||c.async?w[v]:c.j[O].search(a,b,n?n[O]:d);H=p.length;if(k&&H){var r=[],y=0;q&&(r[0]=[p]);for(let D=0,I,A;D<k.length;D++)if(I=k[D],H=(A=c.h[I])&&A.length)y++,r[r.length]=q?
[A]:A;if(y){if(q)y=fa(r,b||100,l||0);else{y=p;const D=E(),I=E(),A=[];for(let C=0;C<y.length;C++)D[y[C]]=1;for(let C=0,U;C<r.length;C++){U=r[C];for(let V=0,J;V<U.length;V++)J=U[V],D[J]&&!I[J]&&(I[J]=1,A[A.length]=J)}y=A}p=y;H=p.length}}if(H)f[u]=O,e[u++]=p;else if(q)return[]}if(!u)return[];if(g&&(!h||!c.store))return e[0];for(let v=0,p;v<f.length;v++){p=e[v];p.length&&h&&(p=oa.call(c,p));if(g)return p;e[v]={field:f[v],result:p}}return e}())};
function oa(a){const b=Array(a.length);for(let d=0,c;d<a.length;d++)c=a[d],b[d]={key:c,doc:this.store[c]};return b}t.contain=function(a){return!!this.register[a]};t.get=function(a){return this.store[a]};t.set=function(a,b){this.store[a]=b;return this};t.searchCache=ha;ea(W.prototype);
W.prototype.export=function(a,b,d,c,e){e||(e=0);c||(c=0);if(c<this.g.length){const f=this.g[c],g=this.j[f];b=this;setTimeout(function(){g.export(a,b,e?f.replace(":","-"):"",c,e++)||(c++,e=1,b.export(a,b,f,c,e))})}else{let f;switch(e){case 1:d="tag";f=this.h;break;case 2:d="store";f=this.store;break;default:return}ja(a,this,d,c,e,f)}};
W.prototype.import=function(a,b){if(b)switch("string"===typeof b&&(b=JSON.parse(b)),a){case "tag":this.h=b;break;case "reg":this.i=!1;this.register=b;for(let c=0,e;c<this.g.length;c++)e=this.j[this.g[c]],e.register=b,e.i=!1;break;case "store":this.store=b;break;default:a=a.split(".");const d=a[0];a=a[1];d&&a&&this.j[d].import(a,b)}};window.FlexSearch={Index:N,Document:W,registerCharset:function(a,b){da[a]=b},registerLanguage:function(a,b){G[a]=b}};}).call(this);

View File

@@ -66,8 +66,8 @@ Source Code v0.7.0-pre-alpha available here:<br>
What is not included yet but comes soon?
- Worker (almost done)
- Offset-Pagination (not implemented yet)
- ~~Worker (almost done)~~
- Offset-Pagination (almost done)
- ~~Export/Import (not implemented yet)~~
- Engines (almost done)
- ~~Tags (almost done)~~
@@ -80,6 +80,7 @@ What will be dropped?
- Where-Clause
- Index Information `index.info()`
- Paging Cursor (gets replaced by offset)
- Inline Worker (was replaced by native workers)
## Builtin Profiles
@@ -288,7 +289,7 @@ Assuming our document has a data structure like this:
}
```
Old syntax FlexSearch v0.6.3 (___not supported anymore___):
Old syntax FlexSearch v0.6.3 (___not supported anymore!___):
```js
const index = new Document({
@@ -983,8 +984,10 @@ One important change is how workers divided their tasks and how contents are dis
Let us take an example. Assuming you have 4 workers and you will add 4 contents to the index, then each content is delegated to one worker (a perfect balance but index becomes a partial index).
Old syntax FlexSearch v0.6.3 (___not supported anymore!___):
```js
const index = new Index({ worker: 4 });
const index = new FlexSearch({ worker: 4 });
index.add(1, "some")
.add(2, "content")
.add(3, "to")
@@ -1003,9 +1006,9 @@ The issue starts when you query a term. Each of the worker has to resolve the se
The new worker model from v0.7.0 is divided into "fields" from the document (1 worker = 1 field index). This way the worker becomes able to solve tasks (subtasks) completely. The downside of this paradigm is they might not have been perfect balanced in storing contents (fields may have different length of contents). On the other hand there is no indication that balancing the storage gives any advantage (they all require the same amount in total).
```js
const index = new Index({
const index = new Document({
doc: ["tag", "name", "title", "text"],
worker: 4
worker: true
});
index.add({
@@ -1028,7 +1031,22 @@ Worker 4: { 1: "some", 2: "content", 3: "to", 4: "index" }
When you perform a field search through all fields then this task is perfectly balanced through all workers, which can solve their subtasks independently.
The main thread has to solve a last intersection calculation as before. On this step it needs to apply "bool" and "paging" logic, also "suggestions". I'm thinking about to move the workload from the main tread to another worker, so all computations will perform in background completely, the index from the main thread just holds the configuration and its document store (when using store).
### WorkerIndex (Adapter)
Above we have seen that documents will create worker automatically for each field. You can also create a WorkerIndex directly (same like using `Index` instead of `Document`).
```js
import WorkerIndex from "./adapter.js";
const index = new WorkerIndex(options);
index.add(1, "some")
.add(2, "content")
.add(3, "to")
.add(4, "index");
```
Such a WorkerIndex works pretty much the same as a created instance of `Index`.
> A WorkerIndex only support the `async` variant of all methods. That means when you call `index.search()` on a WorkerIndex this will perform also in async the same way as `index.searchAsync()` will do.
## Export / Import

81
src/adapter.js Normal file
View File

@@ -0,0 +1,81 @@
import { promise as Promise } from "./polyfill.js";
let counter = 0;
/**
* @param {number|string|Object} id
* @param {Object=} options
* @constructor
*/
function WorkerAdapter(id, options){
if(typeof id === "object"){
options = id;
id = 0;
}
if(typeof options["encode"] === "function"){
options["encode"] = options["encode"].toString();
}
const self = this;
this.id = id || counter++;
this.resolver = null;
this.worker = new Worker("worker.js", { type: "module" });
this.worker.onmessage = function(e){ self.resolver(e["data"]/*["results"]*/) };
this.worker.postMessage({ task: "create", /*id: this.id,*/ options: options });
}
export default WorkerAdapter;
register("add");
register("append");
register("search");
register("update");
register("remove");
function register(key){
WorkerAdapter.prototype[key] =
WorkerAdapter.prototype[key + "Async"] = function(){
const self = this;
const args = [].slice.call(arguments);
const arg = args[args.length - 1];
let callback;
if(typeof arg === "function"){
callback = arg;
args.splice(args.length - 1, 1);
}
const promise = new Promise(function(resolve){
self.worker.postMessage({ task: key, /*id: this.id,*/ args: args });
if(key === "search"){
self.resolver = resolve;
}
else{
resolve();
}
});
if(callback){
promise.then(callback);
return this;
}
else{
return promise;
}
};
}

View File

@@ -2,106 +2,49 @@ import Index from "./index.js";
import Document from "./document.js";
import { promise as Promise } from "./polyfill.js";
/**
* @param {Function=} callback
*/
export default function(prototype){
export function addAsync(id, content, callback){
return caller.call(
/** @type {Document|Index} */ (this),
/** @type {Document|Index} */ (this).add,
arguments
);
register(prototype, "add");
register(prototype, "append");
register(prototype, "search");
register(prototype, "update");
register(prototype, "remove");
}
/**
* @param {Function=} callback
*/
function register(prototype, key){
export function appendAsync(id, content, callback){
prototype[key + "Async"] = function(){
return caller.call(
/** @type {Document|Index} */ (this),
/** @type {Document|Index} */ (this).append,
arguments
);
}
const self = this;
const args = /*[].slice.call*/(arguments);
const arg = args[args.length - 1];
let callback;
/**
* @param {!string} query
* @param {number|Object|Function=} options
* @param {Function=} callback
*/
if(typeof arg === "function"){
export function searchAsync(query, options, callback){
return caller.call(
/** @type {Document|Index} */ (this),
/** @type {Document|Index} */ (this).search,
arguments
);
}
/**
* @param {Function=} callback
*/
export function updateAsync(id, content, callback){
return caller.call(
/** @type {Document|Index} */ (this),
/** @type {Document|Index} */ (this).update,
arguments
);
}
/**
* @param {Function=} callback
*/
export function removeAsync(id, callback){
return caller.call(
/** @type {Document|Index} */ (this),
/** @type {Document|Index} */ (this).remove,
arguments
);
}
function caller(method, args){
let callback;
for(let i = 0; i < args.length; i++){
if(typeof args[i] === "function"){
callback = args[i];
delete args[i];
break;
callback = arg;
delete args[args.length - 1];
}
}
const self = /** @type {Document|Index} */ (this);
const promise = new Promise(function(resolve){
const promise = new Promise(function(resolve){
setTimeout(function(){
// Promises are bullshit, they will block the main thread
setTimeout(function(){
resolve(method.apply(self, args));
const fn = self[key];
fn.async = true;
resolve(fn.apply(self, args));
fn.async = false;
});
});
});
if(callback){
if(callback){
promise.then(callback);
return this;
}
else{
promise.then(callback);
return this;
}
else{
return promise;
}
return promise;
}
};
}

View File

@@ -27,6 +27,7 @@ export default CacheClass;
* @param {!string} query
* @param {number|Object=} limit
* @param {Object=} options
* @this {Document|Index}
* @returns {Array<number|string>}
*/
@@ -37,12 +38,12 @@ export function searchCache(query, limit, options){
query = query["query"];
}
let cache = /** @type {Document|Index} */ (this).cache.get(query);
let cache = this.cache.get(query);
if(!cache){
cache = /** @type {Document|Index} */ (this).search(query, limit, options);
/** @type {Document|Index} */ (this).cache.set(query, cache);
cache = this.search(query, limit, options);
this.cache.set(query, cache);
}
return cache;

View File

@@ -39,6 +39,3 @@ export const SUPPORT_SERIALIZE = true;
/** @define {boolean} */
export const SUPPORT_DOCUMENT = true;
/** @define {boolean} */
export const SUPPORT_PAGINATION = true;

View File

@@ -39,6 +39,3 @@ export const SUPPORT_SERIALIZE = true;
/** @define {boolean} */
export const SUPPORT_DOCUMENT = true;
/** @define {boolean} */
export const SUPPORT_PAGINATION = true;

View File

@@ -39,6 +39,3 @@ export const SUPPORT_SERIALIZE = false;
/** @define {boolean} */
export const SUPPORT_DOCUMENT = true;
/** @define {boolean} */
export const SUPPORT_PAGINATION = false;

View File

@@ -39,6 +39,3 @@ export const SUPPORT_SERIALIZE = false;
/** @define {boolean} */
export const SUPPORT_DOCUMENT = false;
/** @define {boolean} */
export const SUPPORT_PAGINATION = false;

View File

@@ -6,13 +6,24 @@
* https://github.com/nextapps-de/flexsearch
*/
import { SUPPORT_ASYNC, SUPPORT_CACHE, SUPPORT_SERIALIZE, SUPPORT_STORE, SUPPORT_TAGS } from "./config.js";
import {
SUPPORT_ASYNC,
SUPPORT_CACHE,
SUPPORT_SERIALIZE,
SUPPORT_STORE,
SUPPORT_TAGS,
SUPPORT_WORKER
} from "./config.js";
import Index from "./index.js";
import Cache, { searchCache } from "./cache.js";
import { create_object } from "./common.js";
import { addAsync, appendAsync, removeAsync, searchAsync, updateAsync } from "./async.js";
import apply_async from "./async.js";
import { intersect, intersect_union } from "./intersect.js";
import { exportDocument, importDocument } from "./serialize.js";
import WorkerAdapter from "./adapter.js";
/**
* @param {Object=} options
@@ -56,6 +67,16 @@ function Document(options){
options["cache"] = false;
}
if(SUPPORT_WORKER){
this.worker = options["worker"];
}
if(SUPPORT_ASYNC){
this.async = false;
}
this.index = parse_descriptor.call(this, options);
}
@@ -100,7 +121,15 @@ function parse_descriptor(options){
item = options;
}
index[key] = new Index(item, this.register);
if(this.worker){
index[key] = new WorkerAdapter(item);
}
else{
index[key] = new Index(item, this.register);
}
this.tree[i] = parse_tree(key, this.marker);
this.field[i] = key;
}
@@ -425,7 +454,7 @@ Document.prototype.remove = function(id){
return this;
};
Document.prototype.search = function(query, limit, options){
Document.prototype.search = async function(query, limit, options){
if(typeof query === "object"){
@@ -495,15 +524,38 @@ Document.prototype.search = function(query, limit, options){
field || (field = this.field);
bool = bool && ((field.length > 1) || (tag && (tag.length > 1)));
let async_res = [];
// use Promise.all to get a change of processing requests in parallel
if(this.worker || this.async){
for(let i = 0, key; i < field.length; i++){
key = field[i];
async_res[i] = this.index[key][this.async ? "searchAsync" : "search"](query, limit, field_options ? field_options[key] : options);
}
async_res = await Promise.all(async_res);
}
// TODO solve this in one loop below
for(let i = 0, res, key, len; i < field.length; i++){
key = field[i];
// inherit options also when search? it is just for laziness, Object.assign() has a cost
if(this.worker || this.async){
res = async_res[i];
}
else{
// inherit options also when search? it is just for laziness, Object.assign() has a cost
res = this.index[key].search(query, limit, field_options ? field_options[key] : options);
}
res = this.index[key].search(query, limit, field_options ? field_options[key] : options);
len = res.length;
if(tag && len){
@@ -675,11 +727,7 @@ if(SUPPORT_CACHE){
if(SUPPORT_ASYNC){
Document.prototype.addAsync = addAsync;
Document.prototype.appendAsync = appendAsync;
Document.prototype.searchAsync = searchAsync;
Document.prototype.updateAsync = updateAsync;
Document.prototype.removeAsync = removeAsync;
apply_async(Document.prototype);
}
if(SUPPORT_SERIALIZE){

View File

@@ -7,18 +7,20 @@
*/
import {
SUPPORT_PRESET,
SUPPORT_CACHE,
SUPPORT_ASYNC,
SUPPORT_WORKER,
SUPPORT_SUGGESTION,
SUPPORT_SERIALIZE
} from "./config.js";
import { encode as default_encoder } from "./lang/latin/default.js";
import { create_object, create_object_array, concat, sort_by_length_down } from "./common.js";
import { pipeline, init_stemmer_or_matcher, init_filter } from "./lang.js";
import { global_lang, global_charset } from "./global.js";
import { addAsync, appendAsync, removeAsync, searchAsync, updateAsync } from "./async.js";
import apply_async from "./async.js";
import { intersect } from "./intersect.js";
import Cache, { searchCache } from "./cache.js";
import apply_preset from "./presets.js";
@@ -116,18 +118,6 @@ function Index(options, _register){
this.cache = (tmp = options["cache"]) && new Cache(tmp);
}
if(SUPPORT_WORKER){
/** @private */
this.worker = (tmp = options["worker"]) && new Worker("worker.js", { type: "module" });
if(tmp){
this.worker.onmessage = function(e){ console.log(e.data) };
this.worker.postMessage({ task: "register", options: options });
}
}
}
export default Index;
@@ -775,11 +765,7 @@ if(SUPPORT_CACHE){
if(SUPPORT_ASYNC){
Index.prototype.addAsync = addAsync;
Index.prototype.appendAsync = appendAsync;
Index.prototype.searchAsync = searchAsync;
Index.prototype.updateAsync = updateAsync;
Index.prototype.removeAsync = removeAsync;
apply_async(Index.prototype);
}
if(SUPPORT_SERIALIZE){

View File

@@ -51,20 +51,17 @@ if(POLYFILL){
this.callback = null;
let self = this;
const self = this;
//setTimeout(function(){
fn(function(val){
fn(function(val){
if(self.callback){
if(self.callback){
self.callback(val);
self.callback = null;
self = null;
}
});
//});
self.callback(val);
// self.callback = null;
// self = null;
}
});
}
/**

View File

@@ -55,4 +55,4 @@ window["FlexSearch"] = {
"Document": SUPPORT_DOCUMENT ? Document : null,
"registerCharset": registerCharset,
"registerLanguage": registerLanguage
}
};

View File

@@ -8,43 +8,46 @@ onmessage = function(event) {
switch(data["task"]){
case "register":
case "create":
const options = data["options"] || {};
options["cache"] = false;
// options["async"] = false;
// options["worker"] = false;
id = data["id"];
if(typeof options["encode"] === "string"){
options["encode"] = Function(options["encode"]);
}
index = new Index(options);
break;
case "search":
const results = index.search(data["query"], data);
postMessage({ id, results });
break;
case "add":
index.add(data["id"], data["content"]);
index.add.apply(index, data["args"]);
break;
case "append":
index.append.apply(index, data["args"]);
break;
case "search":
const results = index.search.apply(index, data["args"]);
//postMessage({ id: id, results: results });
postMessage(results);
break;
case "update":
index.update(data["id"], data["content"]);
index.update.apply(index, data["args"]);
break;
case "remove":
index.remove(data["id"]);
index.remove.apply(index, data["args"]);
break;
// case "clear":
//
// index.clear();
// break;
}
};

View File

@@ -59,7 +59,7 @@ var options = (function(argv){
if(index !== "RELEASE"){
flag_str += " --define='" + index + "=" + val + "'";
//flag_str += " --define='" + index + "=" + val + "'";
}
arr[index] = val;
@@ -111,8 +111,8 @@ let parameter = (function(opt){
//jscomp_error: "newCheckTypesExtraChecks",
generate_exports: true,
export_local_property_definitions: true,
language_in: "ECMASCRIPT6_STRICT",
language_out: language_out || "ECMASCRIPT5_STRICT",
language_in: "ECMASCRIPT_2017",
language_out: language_out || "ECMASCRIPT6_STRICT",
process_closure_primitives: true,
summary_detail_level: 3,
warning_level: "VERBOSE",