diff --git a/dist/flexsearch.compact.js b/dist/flexsearch.compact.js index 2f18d91..311fe85 100644 --- a/dist/flexsearch.compact.js +++ b/dist/flexsearch.compact.js @@ -1,24 +1,27 @@ /**! - * FlexSearch.js v0.7.0-pre-alpha (Compact) + * FlexSearch.js v0.7.0-beta (Compact) * Copyright 2019 Nextapps GmbH * Author: Thomas Wilkerling * Licence: Apache-2.0 * https://github.com/nextapps-de/flexsearch */ -(function(){var r;function t(a){for(var b=Array(a),c=0;c=b)return e.concat(d.slice(0,b-f));e=e.concat(d);f+=h}return e};var O={memory:{charset:"latin:extra",m:3,j:3,u:!1,l:"memory"},performance:{threshold:8,j:3,context:{depth:1,s:!0}},match:{charset:"latin:extra",D:"full",m:3},score:{charset:"latin:advanced",threshold:1,context:{depth:3,s:!0}},"default":{m:3,threshold:0,depth:3}};function P(a,b){if(!(this instanceof P))return new P(a);var c;if(a){"string"===typeof a?a=O[a]:(c=a.preset)&&(a=Object.assign({},O[c],a));c=a.charset;var d=a.lang;"string"===typeof c&&(-1===c.indexOf(":")&&(c+=":default"),c=F[c]);"string"===typeof d&&(d=E[d])}else a={};var e,h=a.context||{};this.encode=a.encode||c&&c.encode||B;this.register=b||x();var f=a.resolution||9;var g=a.threshold||0;g>=f&&(g=f-1);this.m=f;this.threshold=g;this.v=b=c&&c.D||a.tokenize||"strict";this.depth="strict"===b&&h.depth; -this.s=Q(h.bidirectional,!0);this.l=e="memory"===a.optimize;this.u=Q(a.fastupdate,!0);this.j=a.minlength||1;this.g=e?t(f-g):x();f=h.resolution||f;g=h.threshold||g;g>=f&&(g=f-1);this.h=f;this.o=g;this.i=e?t(f-g):x();this.B=c&&c.B||a.rtl;this.A=(b=a.matcher||d&&d.A)&&D(b,!1);this.C=(b=a.stemmer||d&&d.C)&&D(b,!0);if(a=b=a.filter||d&&d.filter){a=b;c=x();d=0;for(b=a.length;d=this.j&&(e||!f[k])){var m=Math.min(this.m/d*l|0,l);if(mq;v--)p=k.substring(q,v),p.length>=this.j&&R(this,f,p,u,a,c)}break}case "reverse":if(2< -n){for(q=n-1;0=this.j&&R(this,f,p,m,a,c);p=""}case "forward":if(1=this.j&&R(this,f,p,m,a,c);break;default:if(R(this,f,k,m,a,c),e&&1=this.j&&!m[k]){if(m[k]=1,v=Math.min((this.h-q)/d*l+u|0,l+(u-1)),vp;R(this,g,y?p:k,v,a,c,y?k:p)}}else q=Math.min(q+1,d-l)}}}}this.u||(this.register[a]=1)}}return this}; -function R(a,b,c,d,e,h,f){var g=f?a.i:a.g;if(!b[c]||f&&!b[c][f])a.l&&(g=g[d]),f?(b[c]||(b[c]=x()),b[c][f]=1,g=g[f]||(g[f]=x())):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.u&&(a=a.register[e]||(a.register[e]=[]),a[a.length]=g))} -r.search=function(a,b,c){"object"===typeof a?(c=a,a=c.query):"object"===typeof b&&(c=b);var d=[],e=this.threshold;if(c){b=c.limit;e=Q(c.threshold,e);var h=c.context;var f=c.suggest}if(a){a=this.encode(a);var g=a.length;if(1=this.j&&!c[m])if(this.l||f||this.g[m])l[n++]=m,c[m]=1;else return d;a=l;g=a.length}}if(!g)return d;b||(b=100);c=this.m-e;e=this.h-e;h=this.depth&&1b?d.slice(0,b):d}}return N(d,b,f)};function T(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 S(a,b,c,d,e,h,f,g){var l=[],k=g?a.i:a.g;a.l||(k=T(k,f,g,a.s));if(k){var n=0;d=Math.min(k.length,d);for(var m=0,p=0,q;m=e)));m++);if(n){if(h)return l=1===n?l[0]:[].concat.apply([],l),l.length>e?l.slice(0,e):l;b[b.length]=l;return}}return!c&&l}r.contain=function(a){return!!this.register[a]};r.update=function(a,b){return this.remove(a).add(a,b)}; -r.remove=function(a,b){var c=this.register[a];if(c){if(this.u)for(var d=0,e;d=b)return g.concat(k.slice(0,b-h));g=g.concat(k);h+=r}return g} +;const ka={memory:{charset:"latin:extra",m:3,j:3,s:!1,l:"memory"},performance:{threshold:8,j:3,context:{depth:1,u:!0}},match:{charset:"latin:extra",H:"full",m:3},score:{charset:"latin:advanced",threshold:1,context:{depth:3,u:!0}},"default":{m:3,threshold:0,depth:3}};function O(a,b){if(!(this instanceof O))return new O(a);var d;let c;a?(H(a)?a=ka[a]:(d=a.preset)&&(a=Object.assign({},ka[d],a)),d=a.charset,c=a.lang,H(d)&&(-1===d.indexOf(":")&&(d+=":default"),d=fa[d]),H(c)&&(c=ea[c])):a={};let e,g,f,h=a.context||{};this.encode=a.encode||d&&d.encode||ba;this.register=b||G();e=a.resolution||9;g=a.threshold||0;g>=e&&(g=e-1);this.m=e;this.threshold=g;this.v=b=d&&d.H||a.tokenize||"strict";this.depth="strict"===b&&h.depth;this.u=A(h.bidirectional,!0);this.l=f="memory"=== +a.optimize;this.s=A(a.fastupdate,!0);this.j=a.minlength||1;this.g=f?E(e-g):G();e=h.resolution||e;g=h.threshold||g;g>=e&&(g=e-1);this.h=e;this.o=g;this.i=f?E(e-g):G();this.B=d&&d.B||a.rtl;this.A=(b=a.matcher||c&&c.A)&&da(b,!1);this.C=(b=a.stemmer||c&&c.C)&&da(b,!0);if(a=b=a.filter||c&&c.filter){a=b;d=G();for(let l=0,k=a.length;l=this.j&&(r||!t[p])){var e=Math.min(this.m/k*v|0,v);if(ef;l--)g=p.substring(f,l),g.length>=this.j&&P(this,t,g,h,a,d)}break}case "reverse":if(2< +c){for(f=c-1;0=this.j&&P(this,t,g,e,a,d);g=""}case "forward":if(1=this.j&&P(this,t,g,e,a,d);break;default:if(P(this,t,p,e,a,d),r&&1=this.j&&!e[p]){if(e[p]=1,l=Math.min((this.h-f)/k*v+h|0,v+(h-1)),lg;P(this,m,z?g:p,l,a,d,z?p:g)}}else f=Math.min(f+1,k-v)}}}}this.s||(this.register[a]=1)}}return this}; +function P(a,b,d,c,e,g,f){let h=f?a.i:a.g;if(!b[d]||f&&!b[d][f])a.l&&(h=h[c]),f?(b[d]||(b[d]=G()),b[d][f]=1,h=h[f]||(h[f]=G())):b[d]=1,h=h[d]||(h[d]=[]),a.l||(h=h[c]||(h[c]=[])),g&&-1!==h.indexOf(e)||(h[h.length]=e,a.s&&(a=a.register[e]||(a.register[e]=[]),a[a.length]=h))} +w.search=function(a,b,d){I(a)?(d=a,a=d.query):I(b)&&(d=b);let c=[],e;var g=this.threshold;let f,h=0;if(d){b=d.limit;h=d.offset||0;g=A(d.threshold,g);var l=d.context;f=d.suggest}if(a&&(a=this.encode(a),e=a.length,1=this.j&&!d[m])if(this.l||f||this.g[m])k[t++]=m,d[m]=1;else return c;a=k;e=a.length}if(!e)return c;b||(b=100);d=this.m-g;g=this.h-g;l=this.depth&&1b?c.slice(0,b):c}}return ja(c,b,h,f)};function ma(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 la(a,b,d,c,e,g,f,h){let l=[],k=h?a.i:a.g;a.l||(k=ma(k,f,h,a.u));if(k){let r=0;c=Math.min(k.length,c);for(let q=0,t=0,m;q=e)));q++);if(r){if(g)return l=1===r?l[0]:[].concat.apply([],l),l.length>e?l.slice(0,e):l;b[b.length]=l;return}}return!d&&l}w.contain=function(a){return!!this.register[a]};w.update=function(a,b){return this.remove(a).add(a,b)}; +w.remove=function(a,b){const d=this.register[a];if(d){if(this.s)for(let c=0,e;cb||q)f=f.slice(q,q+b);h&&(f=oa.call(c,f));n= +{tag:g,result:f}}else n=void 0;n&&(e[e.length]=n,t++)}return t?e:[]}m||(m=c.g);r=r&&(1=b)return g.concat(d.slice(0,b-e));g=g.concat(d);e+=k}return g};function G(a,b){if(!(this instanceof G))return new G(a);if(a){var c=a.charset;var d=a.lang;"string"===typeof c&&(-1===c.indexOf(":")&&(c+=":default"),c=E[c]);"string"===typeof d&&(d=D[d])}else a={};var g,k=a.context||{};this.encode=a.encode||c&&c.encode||A;this.register=b||v();var e=a.resolution||9;var f=a.threshold||0;f>=e&&(f=e-1);this.i=e;this.s=f;this.F=b=c&&c.G||a.tokenize||"strict";this.m="strict"===b&&k.depth;this.v=H(k.bidirectional,!0);this.g=g="memory"===a.optimize;this.C=H(a.fastupdate, -!0);this.h=a.minlength||1;this.j=g?r(e-f):v();e=k.resolution||e;f=k.threshold||f;f>=e&&(f=e-1);this.l=e;this.D=f;this.B=g?r(e-f):v();this.u=c&&c.u||a.rtl;this.o=(b=a.matcher||d&&d.o)&&C(b,!1);this.A=(b=a.stemmer||d&&d.A)&&C(b,!0);if(a=b=a.filter||d&&d.filter){a=b;c=v();d=0;for(b=a.length;d=this.h&&(g||!e[l])){var q=Math.min(this.i/d*h|0,h);if(qm;u--)n=l.substring(m,u),n.length>=this.h&&I(this,e,n,t,a,c)}break}case "reverse":if(2< -p){for(m=p-1;0=this.h&&I(this,e,n,q,a,c);n=""}case "forward":if(1=this.h&&I(this,e,n,q,a,c);break;default:if(I(this,e,l,q,a,c),g&&1=this.h&&!q[l]){if(q[l]=1,u=Math.min((this.l-m)/d*h+t|0,h+(t-1)),un;I(this,f,x?n:l,u,a,c,x?l:n)}}else m=Math.min(m+1,d-h)}}}}this.C||(this.register[a]=1)}}return this}; -function I(a,b,c,d,g,k,e){var f=e?a.B:a.j;if(!b[c]||e&&!b[c][e])a.g&&(f=f[d]),e?(b[c]||(b[c]=v()),b[c][e]=1,f=f[e]||(f[e]=v())):b[c]=1,f=f[c]||(f[c]=[]),a.g||(f=f[d]||(f[d]=[])),k&&-1!==f.indexOf(g)||(f[f.length]=g,a.C&&(a=a.register[g]||(a.register[g]=[]),a[a.length]=f))} -G.prototype.search=function(a,b,c){"object"===typeof a?(c=a,a=c.query):"object"===typeof b&&(c=b);var d=[],g=this.s;if(c){b=c.limit;g=H(c.threshold,g);var k=c.context;var e=!1}if(a){a=this.encode(a);var f=a.length;if(1=this.h&&!c[q])if(this.g||e||this.j[q])h[p++]=q,c[q]=1;else return d;a=h;f=a.length}}if(!f)return d;b||(b=100);c=this.i-g;g=this.l-g;k=this.m&&1b?d.slice(0,b):d}}return F(d,b,e)};function K(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 J(a,b,c,d,g,k,e,f){var h=[],l=f?a.B:a.j;a.g||(l=K(l,e,f,a.v));if(l){var p=0;d=Math.min(l.length,d);for(var q=0,n=0,m;q=g)));q++);if(p){if(k)return h=1===p?h[0]:[].concat.apply([],h),h.length>g?h.slice(0,g):h;b[b.length]=h;return}}return!c&&h}G.prototype.contain=function(a){return!!this.register[a]};G.prototype.update=function(a,b){return this.remove(a).add(a,b)}; -G.prototype.remove=function(a,b){var c=this.register[a];if(c){if(this.C)for(var d=0,g;d=c)return g.concat(l.slice(0,c-h));g=g.concat(l);h+=r}return g};function I(a,c){if(!(this instanceof I))return new I(a);let d;if(a){var b=a.charset;d=a.lang;"string"===typeof b&&(-1===b.indexOf(":")&&(b+=":default"),b=F[b]);"string"===typeof d&&(d=E[d])}else a={};let e,g,f,h=a.context||{};this.encode=a.encode||b&&b.encode||v;this.register=c||B();e=a.resolution||9;g=a.threshold||0;g>=e&&(g=e-1);this.i=e;this.s=g;this.F=c=b&&b.G||a.tokenize||"strict";this.m="strict"===c&&h.depth;this.v=z(h.bidirectional,!0);this.g=f="memory"===a.optimize;this.C=z(a.fastupdate,!0); +this.h=a.minlength||1;this.j=f?A(e-g):B();e=h.resolution||e;g=h.threshold||g;g>=e&&(g=e-1);this.l=e;this.D=g;this.B=f?A(e-g):B();this.u=b&&b.u||a.rtl;this.o=(c=a.matcher||d&&d.o)&&D(c,!1);this.A=(c=a.stemmer||d&&d.A)&&D(c,!0);if(a=c=a.filter||d&&d.filter){a=c;b=B();for(let k=0,l=a.length;k=this.h&&(r||!p[q])){var e=Math.min(this.i/l*t|0,t);if(ef;k--)g=q.substring(f,k),g.length>=this.h&&J(this,p,g,h,a,d)}break}case "reverse":if(2< +b){for(f=b-1;0=this.h&&J(this,p,g,e,a,d);g=""}case "forward":if(1=this.h&&J(this,p,g,e,a,d);break;default:if(J(this,p,q,e,a,d),r&&1=this.h&&!e[q]){if(e[q]=1,k=Math.min((this.l-f)/l*t+h|0,t+(h-1)),kg;J(this,m,w?g:q,k,a,d,w?q:g)}}else f=Math.min(f+1,l-t)}}}}this.C||(this.register[a]=1)}}return this}; +function J(a,c,d,b,e,g,f){let h=f?a.B:a.j;if(!c[d]||f&&!c[d][f])a.g&&(h=h[b]),f?(c[d]||(c[d]=B()),c[d][f]=1,h=h[f]||(h[f]=B())):c[d]=1,h=h[d]||(h[d]=[]),a.g||(h=h[b]||(h[b]=[])),g&&-1!==h.indexOf(e)||(h[h.length]=e,a.C&&(a=a.register[e]||(a.register[e]=[]),a[a.length]=h))} +I.prototype.search=function(a,c,d){"object"===typeof a?(d=a,a=d.query):"object"===typeof c&&(d=c);let b=[],e;var g=this.s;let f,h=0;if(d){c=d.limit;h=d.offset||0;g=z(d.threshold,g);var k=d.context;f=!1}if(a&&(a=this.encode(a),e=a.length,1=this.h&&!d[m])if(this.g||f||this.j[m])l[p++]=m,d[m]=1;else return b;a=l;e=a.length}if(!e)return b;c||(c=100);d=this.i-g;g=this.l-g;k=this.m&&1c?b.slice(0,c):b}}return G(b,c,h,f)};function L(a,c,d,b){d?(b=b&&c>d,a=(a=a[b?c:d])&&a[b?d:c]):a=a[c];return a} +function K(a,c,d,b,e,g,f,h){let k=[],l=h?a.B:a.j;a.g||(l=L(l,f,h,a.v));if(l){let r=0;b=Math.min(l.length,b);for(let n=0,p=0,m;n=e)));n++);if(r){if(g)return k=1===r?k[0]:[].concat.apply([],k),k.length>e?k.slice(0,e):k;c[c.length]=k;return}}return!d&&k}I.prototype.contain=function(a){return!!this.register[a]};I.prototype.update=function(a,c){return this.remove(a).add(a,c)}; +I.prototype.remove=function(a,c){const d=this.register[a];if(d){if(this.C)for(let b=0,e;b=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=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=this.o&&(u||!q[r])){var e=Math.min(this.s/l*w|0,w);if(eg;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=this.o&&Q(this,q,f,e,a,d);f=""}case "forward":if(1=this.o&&Q(this,q,f,e,a,d);break;default:if(Q(this,q,r,e,a,d),u&&1=this.o&&!e[r]){if(e[r]=1,k=Math.min((this.j-g)/l*w+h|0,w+(h-1)),kf;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=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&&1b?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=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;cg||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=b)return f.concat(l.slice(0,b-h));f=f.concat(l);h+=r}return f} +;function N(a){this.h=!0!==a&&a;this.cache=A();this.g=[]}function la(a,b,d){G(a)&&(a=a.query);let c=this.cache.get(a);c||(c=this.search(a,b,d),this.cache.set(a,c));return c}N.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=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=y(h.bidirectional,!0);this.l=g="memory"=== +a.optimize;this.i=y(a.fastupdate,!0);this.o=a.minlength||1;this.g=g?da(e-f):A();e=h.resolution||e;f=h.threshold||f;f>=e&&(f=e-1);this.j=e;this.m=f;this.h=g?da(e-f):A();this.D=d&&d.D||a.rtl;this.B=(b=a.matcher||c&&c.B)&&fa(b,!1);this.F=(b=a.stemmer||c&&c.F)&&fa(b,!0);if(d=b=a.filter||c&&c.filter){d=b;c=A();for(let k=0,l=d.length;k=this.o&&(r||!u[p])){var e=Math.min(this.s/l*x|0,x);if(eg;k--)f=p.substring(g,k),f.length>=this.o&&P(this,u,f,h,a,d)}break}case "reverse":if(2< +c){for(g=c-1;0=this.o&&P(this,u,f,e,a,d);f=""}case "forward":if(1=this.o&&P(this,u,f,e,a,d);break;default:if(P(this,u,p,e,a,d),r&&1=this.o&&!e[p]){if(e[p]=1,k=Math.min((this.j-g)/l*x+h|0,x+(h-1)),kf;P(this,m,z?f:p,k,a,d,z?p:f)}}else g=Math.min(g+1,l-x)}}}}this.i||(this.register[a]=1)}}return this}; +function P(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]=A()),b[d][g]=1,h=h[g]||(h[g]=A())):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){G(a)?(d=a,a=d.query):G(b)&&(d=b);let c=[],e;var f=this.threshold;let g,h=0;if(d){b=d.limit;h=d.offset||0;f=y(d.threshold,f);var k=d.context;g=d.suggest}if(a&&(a=this.encode(a),e=a.length,1=this.o&&!d[m])if(this.l||g||this.g[m])l[u++]=m,d[m]=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&&1b?c.slice(0,b):c}}return ka(c,b,h,g)};function pa(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 oa(a,b,d,c,e,f,g,h){let k=[],l=h?a.h:a.g;a.l||(l=pa(l,g,h,a.v));if(l){let r=0;c=Math.min(l.length,c);for(let q=0,u=0,m;q=e)));q++);if(r){if(f)return k=1===r?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;cb||q)g=g.slice(q,q+b);h&&(g=sa.call(c,g)); +n={tag:f,result:g}}else n=void 0;n&&(e[e.length]=n,u++)}return u?e:[]}m||(m=c.g);r=r&&(1 When passing field-specific options you need to provide the full configuration for each field. They get not inherited like the document descriptor. + #### Complex Documents You need to follow 2 rules for your documents: @@ -652,16 +709,22 @@ Search through a given set of fields: index.search(query, { field: ["title", "content"] }); ``` +Same as: + +```js +index.search(query, ["title", "content"]); +``` + Pass custom modifiers to each field: ```js index.search(query, { field: { - title: { + "title": { threshold: 0, limit: 50 }, - content: { + "content": { threshold: 3, limit: 100, suggest: true @@ -675,12 +738,12 @@ Or pass custom query to each field: ```js index.search({ field: { - title: { + "title": { query: "some title", threshold: 0, limit: 50 }, - content: { + "content": { query: "some content", threshold: 3, limit: 100, @@ -690,6 +753,24 @@ index.search({ }); ``` +Or as array notation along a "field" property: + +```js +index.search([{ + field: "content", + query: "some query", + limit: 100, + suggest: true +},{ + field: "content", + query: "some other query", + limit: 100, + suggest: true +}]); +``` + +One advantage here is you can perform a search through the same field with different queries. + ### New Result Set One of the few breaking changes which needs migration of your old implementation is the result set. I was thinking a long time about it and came to the conclusion, that this new structure might look weird on the first time, but also comes with some nice new capabilities. diff --git a/package.json b/package.json index 1e364aa..6aa9baf 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "flexsearch", - "version": "0.7.0-pre-alpha", + "version": "0.7.0-beta", "description": "Next-Generation full text search library with zero dependencies.", "homepage": "https://github.com/nextapps-de/flexsearch/", "author": "Thomas Wilkerling", diff --git a/src/adapter.js b/src/adapter.js index 8bf8f3c..80ed250 100644 --- a/src/adapter.js +++ b/src/adapter.js @@ -1,4 +1,5 @@ import { promise as Promise } from "./polyfill.js"; +import { is_function, is_object } from "./common.js"; let counter = 0; @@ -10,13 +11,13 @@ let counter = 0; function WorkerAdapter(id, options){ - if(typeof id === "object"){ + if(is_object(id)){ - options = id; + options = /** @type {Object} */ (id); id = 0; } - if(typeof options["encode"] === "function"){ + if(is_function(options["encode"])){ options["encode"] = options["encode"].toString(); } @@ -48,7 +49,7 @@ function register(key){ const arg = args[args.length - 1]; let callback; - if(typeof arg === "function"){ + if(is_function(arg)){ callback = arg; args.splice(args.length - 1, 1); diff --git a/src/async.js b/src/async.js index 76d219a..5dac75d 100644 --- a/src/async.js +++ b/src/async.js @@ -1,6 +1,7 @@ import Index from "./index.js"; import Document from "./document.js"; import { promise as Promise } from "./polyfill.js"; +import { is_function, is_object } from "./common.js"; export default function(prototype){ @@ -20,7 +21,7 @@ function register(prototype, key){ const arg = args[args.length - 1]; let callback; - if(typeof arg === "function"){ + if(is_function(arg)){ callback = arg; delete args[args.length - 1]; diff --git a/src/cache.js b/src/cache.js index 93da948..c1f9b06 100644 --- a/src/cache.js +++ b/src/cache.js @@ -1,6 +1,6 @@ import Index from "./index.js"; import Document from "./index.js"; -import { create_object } from "./common.js"; +import { create_object, is_object } from "./common.js"; /** * @param {boolean|number=} limit @@ -24,7 +24,7 @@ function CacheClass(limit){ export default CacheClass; /** - * @param {!string} query + * @param {string|Object} query * @param {number|Object=} limit * @param {Object=} options * @this {Document|Index} @@ -33,7 +33,7 @@ export default CacheClass; export function searchCache(query, limit, options){ - if(typeof query === "object"){ + if(is_object(query)){ query = query["query"]; } diff --git a/src/common.js b/src/common.js index b4dee19..9f93426 100644 --- a/src/common.js +++ b/src/common.js @@ -1,4 +1,7 @@ -//import FlexSearch from "./flexsearch.js"; +export function parse_option(value, default_value){ + + return typeof value !== "undefined" ? value : default_value; +} /** * @param {!Object} obj @@ -52,4 +55,24 @@ export function concat(arrays){ export function sort_by_length_down(a, b){ return b.length - a.length; -} \ No newline at end of file +} + +export function is_array(val){ + + return val.constructor === Array; +} + +export function is_string(val){ + + return typeof val === "string"; +} + +export function is_object(val){ + + return typeof val === "object"; +} + +export function is_function(val){ + + return typeof val === "function"; +} diff --git a/src/document.js b/src/document.js index 54e011e..9659ba1 100644 --- a/src/document.js +++ b/src/document.js @@ -19,7 +19,7 @@ import { import Index from "./index.js"; import Cache, { searchCache } from "./cache.js"; -import { create_object } from "./common.js"; +import { create_object, is_array, is_string, is_object, parse_option } from "./common.js"; import apply_async from "./async.js"; import { intersect, intersect_union } from "./intersect.js"; import { exportDocument, importDocument } from "./serialize.js"; @@ -82,11 +82,6 @@ function Document(options){ export default Document; -function parse_option(value, default_value){ - - return typeof value !== "undefined" ? value : default_value; -} - /** * @this Document */ @@ -97,38 +92,37 @@ function parse_descriptor(options){ let field = options["doc"]; let field_options; - if(typeof field === "string"){ + if(is_string(field)){ field = [field]; } - else if(field.constructor !== Array){ + else if(!is_array(field)){ field_options = field; field = Object.keys(field); } - for(let i = 0, key, item; i < field.length; i++){ + for(let i = 0, key, opt; i < field.length; i++){ key = field[i]; - if(field_options){ + if(!is_string(key)){ - item = field_options[key]; - item = typeof item === "object" ? Object.assign({}, options, item) : options; + opt = key; + key = key["field"]; } - else{ + else if(field_options){ - item = options; + opt = field_options[key]; } - if(this.worker){ + opt = is_object(opt) ? Object.assign({}, options, opt) : options; - index[key] = new WorkerAdapter(item); - } - else{ + index[key] = this.worker ? - index[key] = new Index(item, this.register); - } + new WorkerAdapter(opt) + : + new Index(opt, this.register); this.tree[i] = parse_tree(key, this.marker); this.field[i] = key; @@ -138,7 +132,7 @@ function parse_descriptor(options){ let store = options["store"]; - if(typeof store === "string"){ + if(is_string(store)){ store = [store]; } @@ -187,7 +181,7 @@ function parse_tree(key, marker){ function parse_simple(obj, tree){ - if(typeof tree === "string"){ + if(is_string(tree)){ obj = obj[tree]; } @@ -216,7 +210,7 @@ function store_value(obj, store, tree, pos, key){ } else if(obj){ - if(obj.constructor === Array){ + if(is_array(obj)){ store = store[key] = new Array(obj.length); @@ -246,7 +240,7 @@ function add_index(obj, tree, marker, pos, index, id, key, _append){ // handle target value - if(obj.constructor === Array){ + if(is_array(obj)){ if(marker[pos]){ @@ -265,7 +259,7 @@ function add_index(obj, tree, marker, pos, index, id, key, _append){ } else if(obj){ - if(obj.constructor === Array){ + if(is_array(obj)){ for(let i = 0; i < obj.length; i++){ @@ -292,7 +286,7 @@ function add_index(obj, tree, marker, pos, index, id, key, _append){ Document.prototype.add = function(id, content, _append){ - if(typeof id === "object"){ + if(is_object(id)){ content = id; id = parse_simple(content, this.key); @@ -310,7 +304,7 @@ Document.prototype.add = function(id, content, _append){ field = this.field[i]; tree = this.tree[i]; - if(typeof tree === "string"){ + if(is_string(tree)){ tree = [tree]; } @@ -323,7 +317,7 @@ Document.prototype.add = function(id, content, _append){ let tag = parse_simple(content, this.tag); let dupes = create_object(); - if(typeof tag === "string"){ + if(is_string(tag)){ tag = [tag]; } @@ -365,7 +359,7 @@ Document.prototype.add = function(id, content, _append){ tree = this.storetree[i]; - if(typeof tree === "string"){ + if(is_string(tree)){ store[tree] = content[tree]; } @@ -396,7 +390,7 @@ Document.prototype.update = function(id, content){ Document.prototype.remove = function(id){ - if(typeof id === "object"){ + if(is_object(id)){ id = id[this.key]; } @@ -407,7 +401,9 @@ Document.prototype.remove = function(id){ this.index[this.field[i]].remove(id, true); - if(this.fastupdate){ + // workers does not share the register + + if(this.fastupdate && !this.worker){ // when fastupdate was enabled all ids will // be already cleanup after the first loop @@ -421,7 +417,7 @@ Document.prototype.remove = function(id){ // when fastupdate was enabled the id will // be already cleanup by the index - if(!this.fastupdate){ + if(!this.fastupdate || this.worker){ for(let key in this.tagindex){ @@ -456,14 +452,15 @@ Document.prototype.remove = function(id){ Document.prototype.search = async function(query, limit, options){ - if(typeof query === "object"){ + if(is_object(query)){ options = query; query = options["query"]; } - else if(typeof limit === "object"){ + else if(is_object(limit)){ options = limit; + limit = 0; } let result = [], result_field = []; @@ -472,51 +469,64 @@ Document.prototype.search = async function(query, limit, options){ if(options){ - pluck = options["pluck"]; - field = pluck || options["field"]; - tag = SUPPORT_TAGS && options["tag"]; - enrich = SUPPORT_STORE && this.store && options["enrich"]; - bool = options["bool"] === "and"; - limit = options["limit"]; - offset = options["offset"]; + if(is_array(options)){ - if(field){ - - if(typeof field === "string"){ - - field = [field]; - } - else if(field.constructor !== Array){ - - field_options = field; - field = Object.keys(field); - } + field = options; + options = null; } + // else if(is_string(options)){ + // + // field = [options]; + // options = null; + // } + else{ - if(tag){ + pluck = options["pluck"]; + field = pluck || options["field"] || options["doc"]; + tag = SUPPORT_TAGS && options["tag"]; + enrich = SUPPORT_STORE && this.store && options["enrich"]; + bool = options["bool"] === "and"; + limit = options["limit"] || 100; + offset = options["offset"] || 0; - if(typeof tag === "string"){ + if(field){ - tag = [tag]; + if(is_string(field)){ + + field = [field]; + } + else if(!is_array(field)){ + + field_options = field; + field = Object.keys(field); + } } - // when tags is used and no query was set, - // then just return the tag indexes + if(tag){ - if(!query){ + if(is_string(tag)){ - for(let i = 0, res; i < tag.length; i++){ - - res = get_tag.call(this, tag[i], limit || 100, offset || 0, enrich); - - if(res){ - - result[result.length] = res; - count++; - } + tag = [tag]; } - return count ? result : []; + // when tags is used and no query was set, + // then just return the tag indexes + + if(!query){ + + for(let i = 0, res; i < tag.length; i++){ + + res = get_tag.call(this, tag[i], limit, offset, enrich); + + if(res){ + + result[result.length] = res; + count++; + } + } + + return count ? result : []; + } } } } @@ -532,8 +542,21 @@ Document.prototype.search = async function(query, limit, options){ for(let i = 0, key; i < field.length; i++){ + let opt; + key = field[i]; - async_res[i] = this.index[key][this.async ? "searchAsync" : "search"](query, limit, field_options ? field_options[key] : options); + + if(!is_string(key)){ + + opt = key; + key = key["field"]; + } + else if(field_options){ + + opt = field_options[key]; + } + + async_res[i] = this.index[key].searchAsync(query, limit, opt || options); } async_res = await Promise.all(async_res); @@ -543,8 +566,20 @@ Document.prototype.search = async function(query, limit, options){ for(let i = 0, res, key, len; i < field.length; i++){ + let opt; + key = field[i]; + if(!is_string(key)){ + + opt = key; + key = key["field"]; + } + else if(field_options){ + + opt = field_options[key]; + } + if(this.worker || this.async){ res = async_res[i]; @@ -553,7 +588,7 @@ Document.prototype.search = async function(query, limit, options){ // 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, opt || options); } len = res.length; @@ -725,13 +760,13 @@ if(SUPPORT_CACHE){ Document.prototype.searchCache = searchCache; } -if(SUPPORT_ASYNC){ - - apply_async(Document.prototype); -} - if(SUPPORT_SERIALIZE){ Document.prototype.export = exportDocument; Document.prototype.import = importDocument; } + +if(SUPPORT_ASYNC){ + + apply_async(Document.prototype); +} diff --git a/src/engine.js b/src/engine.js index 933e263..cc70df9 100644 --- a/src/engine.js +++ b/src/engine.js @@ -12,7 +12,8 @@ function Engine(index){ if(DEBUG){ - if(this.constructor === Engine){ + //if(this.constructor === Engine){ + if(this instanceof Engine){ throw new Error("Can't instantiate abstract class!"); } diff --git a/src/index.js b/src/index.js index f397843..1b52f0d 100644 --- a/src/index.js +++ b/src/index.js @@ -17,7 +17,7 @@ import { } 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 { create_object, create_object_array, concat, sort_by_length_down, is_array, is_string, is_object, parse_option } from "./common.js"; import { pipeline, init_stemmer_or_matcher, init_filter } from "./lang.js"; import { global_lang, global_charset } from "./global.js"; import apply_async from "./async.js"; @@ -52,7 +52,7 @@ function Index(options, _register){ charset = options["charset"]; lang = options["lang"]; - if(typeof charset === "string"){ + if(is_string(charset)){ if(charset.indexOf(":") === -1){ @@ -62,7 +62,7 @@ function Index(options, _register){ charset = global_charset[charset]; } - if(typeof lang === "string"){ + if(is_string(lang)){ lang = global_lang[lang]; } @@ -122,11 +122,6 @@ function Index(options, _register){ export default Index; -function parse_option(value, default_value){ - - return typeof value !== "undefined" ? value : default_value; -} - Index.prototype.pipeline = pipeline; /** @@ -355,7 +350,7 @@ Index.prototype.push_index = function(dupes, value, score, id, append, keyword){ } /** - * @param {!string|Object} query + * @param {string|Object} query * @param {number|Object=} limit * @param {Object=} options * @returns {Array} @@ -363,14 +358,14 @@ Index.prototype.push_index = function(dupes, value, score, id, append, keyword){ Index.prototype.search = function(query, limit, options){ - if(typeof query === "object"){ + if(is_object(query)){ - options = query; + options = /** @type {Object} */ (query); query = options["query"]; } - else if(typeof limit === "object"){ + else if(is_object(limit)){ - options = limit; + options = /** @type {Object} */ (limit); } let result = []; @@ -697,7 +692,7 @@ function remove_index(map, id, res, optimize, resolution){ let count = 0; - if(map.constructor === Array){ + if(is_array(map)){ // the first array is the score array in both strategies @@ -763,13 +758,13 @@ if(SUPPORT_CACHE){ Index.prototype.searchCache = searchCache; } -if(SUPPORT_ASYNC){ - - apply_async(Index.prototype); -} - if(SUPPORT_SERIALIZE){ Index.prototype.export = exportIndex; Index.prototype.import = importIndex; -} \ No newline at end of file +} + +if(SUPPORT_ASYNC){ + + apply_async(Index.prototype); +} diff --git a/src/presets.js b/src/presets.js index 345842c..c39f5dc 100644 --- a/src/presets.js +++ b/src/presets.js @@ -1,4 +1,5 @@ import { DEBUG } from "./config.js"; +import { is_string } from "./common.js"; /** * @enum {Object} @@ -67,7 +68,7 @@ const presets = { export default function apply_preset(options){ - if(typeof options === "string"){ + if(is_string(options)){ if(DEBUG && !presets[options]){ diff --git a/src/serialize.js b/src/serialize.js index b300252..33d20db 100644 --- a/src/serialize.js +++ b/src/serialize.js @@ -1,6 +1,6 @@ import Index from "./index.js"; import Document from "./document.js"; -import { create_object } from "./common.js"; +import { create_object, is_string } from "./common.js"; function async(callback, self, key, index_doc, index, data){ @@ -99,7 +99,7 @@ export function importIndex(key, data){ return; } - if(typeof data === "string"){ + if(is_string(data)){ data = JSON.parse(data); } @@ -202,7 +202,7 @@ export function importDocument(key, data){ return; } - if(typeof data === "string"){ + if(is_string(data)){ data = JSON.parse(data); } diff --git a/src/worker.js b/src/worker.js index 11f5254..0d22601 100644 --- a/src/worker.js +++ b/src/worker.js @@ -1,23 +1,26 @@ import Index from "./index.js"; +import { is_string, is_object } from "./common.js"; let index, id; onmessage = function(event) { const data = event.data; + const args = data["args"]; switch(data["task"]){ case "create": const options = data["options"] || {}; + const encode = options["encode"]; options["cache"] = false; id = data["id"]; - if(typeof options["encode"] === "string"){ + if(is_string(encode)){ - options["encode"] = Function(options["encode"]); + options["encode"] = new Function("return " + encode)(); } index = new Index(options); @@ -25,29 +28,29 @@ onmessage = function(event) { case "add": - index.add.apply(index, data["args"]); + index.add.apply(index, args); break; case "append": - index.append.apply(index, data["args"]); + index.append.apply(index, args); break; case "search": - const results = index.search.apply(index, data["args"]); + const results = index.search.apply(index, args); //postMessage({ id: id, results: results }); postMessage(results); break; case "update": - index.update.apply(index, data["args"]); + index.update.apply(index, args); break; case "remove": - index.remove.apply(index, data["args"]); + index.remove.apply(index, args); break; } };