diff --git a/doc/memory-comparison.svg b/doc/memory-comparison.svg index e17ede1..2553efe 100644 --- a/doc/memory-comparison.svg +++ b/doc/memory-comparison.svg @@ -1,3 +1,3 @@ -661035 bytes3518156 bytes4341000 bytes117021480 bytes2398442 bytes2227612 bytes4746825 bytes0500000100000015000002000000250000030000003500000400000045000005000000RAMFlexSearchBulkSearchElasticlunrLunrWadeJSiiBM25Memory Consumption of Search Engines (lower ist better)https://github.com/nextapps-de/flexsearch/ +569743 bytes3518156 bytes4341000 bytes117021480 bytes2398442 bytes226943056 bytes2227612 bytes4746825 bytes0500000100000015000002000000250000030000003500000400000045000005000000RAMFlexSearchBulkSearchElasticlunrLunrWadeJsSearchJSiibm25Memory Consumption of Search Engines (lower ist better)https://github.com/nextapps-de/flexsearch/ diff --git a/flexsearch.js b/flexsearch.js index e875e1d..cb1d50f 100644 --- a/flexsearch.js +++ b/flexsearch.js @@ -59,7 +59,6 @@ var SUPPORT_ASYNC = true; }; /** - * @struct * @private * @const * @final @@ -101,6 +100,7 @@ var SUPPORT_ASYNC = true; "fastest": { encode: "icase", + mode: "strict", threshold: 9, depth: 1 } @@ -412,12 +412,10 @@ var SUPPORT_ASYNC = true; if(typeof options === 'string'){ - options = profiles[options] || defaults; + options = profiles[options]; } - else{ - options || (options = defaults); - } + options || (options = defaults); // generate UID @@ -517,7 +515,8 @@ var SUPPORT_ASYNC = true; if(options){ - var custom; + var custom = options['profile']; + var profile = custom ? profiles[custom] : {}; // initialize worker @@ -588,34 +587,12 @@ var SUPPORT_ASYNC = true; } } - // apply profile options - - if((custom = options['profile'])) { - - this.profile = custom || 'custom'; - - custom = profiles[custom]; - - if(custom) { - - for(var option in custom){ - - if(custom.hasOwnProperty(option)){ - - if(typeof options[option] === 'undefined'){ - - options[option] = custom[option]; - } - } - } - } - } - // apply custom options this.mode = ( options['mode'] || + profile.mode || this.mode || defaults.mode ); @@ -644,6 +621,7 @@ var SUPPORT_ASYNC = true; this.threshold = ( options['threshold'] || + profile.threshold || this.threshold || defaults.threshold ); @@ -651,11 +629,12 @@ var SUPPORT_ASYNC = true; this.depth = ( options['depth'] || + profile.depth || this.depth || defaults.depth ); - custom = options['encode']; + custom = options['encode'] || profile.encode; this.encoder = ( @@ -1224,7 +1203,7 @@ var SUPPORT_ASYNC = true; query = query['query']; } - threshold || (threshold = 0); + threshold = (threshold || this.threshold || 0) | 0; if(typeof limit === 'function'){ @@ -1916,7 +1895,7 @@ var SUPPORT_ASYNC = true; if(score >= threshold){ - var arr = map[score]; + var arr = map[(score + 0.5) | 0]; arr = arr[tmp] || (arr[tmp] = []); arr[arr.length] = id; } @@ -1938,7 +1917,7 @@ var SUPPORT_ASYNC = true; return ( - ((3 / ref.length * (ref.length - context_index)) + (6 / partial_index) + 0.5) | 0 + (3 / ref.length * (ref.length - context_index)) + (6 / partial_index) ); } diff --git a/flexsearch.light.js b/flexsearch.light.js index 702b23d..39db111 100644 --- a/flexsearch.light.js +++ b/flexsearch.light.js @@ -4,15 +4,15 @@ Released under the Apache 2.0 Licence https://github.com/nextapps-de/flexsearch */ -'use strict';(function(d,t,l){var q;(q=l.define)&&q.amd?q([],function(){return t}):(q=l.modules)?q[d.toLowerCase()]=t:"undefined"!==typeof module?module.exports=t:l[d]=t})("FlexSearch",function(){function d(a){"string"===typeof a?a=A[a]||x:a||(a=x);this.id=a.id||D++;this.init(a);t(this,"index",function(){return this.a});t(this,"length",function(){return Object.keys(this.a).length})}function t(a,b,c){Object.defineProperty(a,b,{get:c})}function l(a){return new RegExp(a,"g")}function q(a,b,c){if("undefined"=== -typeof c){for(c=0;c=e&&(a=a[f],a=a[c]||(a[c]=[]),a[a.length]=k)}return f||b[c]}function B(a){var b=[];if(!a)return b;for(var c=0,k=0,g=0,e="",f=a.length,p=0;p=e&&(a=a[f+.5|0],a=a[c]||(a[c]=[]),a[a.length]=k)}return f||b[c]}function B(a){var b=[];if(!a)return b;for(var c=0,k=0,g=0,e="",f=a.length,p=0;pa?1:0a?-1:0b&&(c= -c.slice(0,b)));return c}var x={encode:"icase",mode:"forward",cache:!1,async:!1,l:!1,threshold:0,depth:0},A={memory:{encode:"extra",mode:"strict",threshold:7},speed:{encode:"icase",mode:"strict",threshold:7,depth:2},match:{encode:"extra",mode:"full"},score:{encode:"extra",mode:"strict",threshold:5,depth:4},balance:{encode:"balance",mode:"ngram",threshold:6,depth:3},fastest:{encode:"icase",threshold:9,depth:1}},w=[],D=0,C=l("[ -/]");d.new=function(a){return new this(a)};d.create=function(a){return d.new(a)}; -d.addMatcher=function(a){for(var b in a)a.hasOwnProperty(b)&&(w[w.length]=l(b),w[w.length]=a[b]);return this};d.register=function(a,b){y[a]=b;return this};d.encode=function(a,b){return y[a].call(y,b)};d.prototype.init=function(a){this.c=[];if(a){var b;if(b=a.profile)if(b=A[b])for(var c in b)b.hasOwnProperty(c)&&"undefined"===typeof a[c]&&(a[c]=b[c]);this.mode=a.mode||this.mode||x.mode;this.threshold=a.threshold||this.threshold||x.threshold;this.depth=a.depth||this.depth||x.depth;this.h=(b=a.encode)&& -y[b]||("function"===typeof b?b:this.h||!1);(b=a.matcher)&&this.addMatcher(b)}this.b=[{},{},{},{},{},{},{},{},{},{},{}];this.a={};this.f="";this.g=!0;return this};d.prototype.encode=function(a){a&&w.length&&(a=q(a,w));a&&this.c.length&&(a=q(a,this.c));a&&this.h&&(a=this.h.call(y,a));if(a&&this.i){a=a.split(" ");for(var b=0;bh;l--)u=n.substring(h,l),v(p,g,u,a,b,e);break;default:if(h=v(p,g,n,a,b,e),f&&1=e)for(h=p[10],r=g._ctx[n]||(g._ctx[n]={}),n=h[n]||(h[n]=[{},{},{},{},{},{},{},{},{},{}]),h=d-f,l=d+f,0>h&&(h=0),l>m-1&&(l=m-1);h<=l;h++)h!==d&&v(n,r,k[h],a,b,e)}}}this.a[a]="1";this.g=!1}return this};d.prototype.update=function(a,b){"string"===typeof b&&(a||0===a)&&this.a[a]&&(this.remove(a),b&&this.add(a,b));return this};d.prototype.remove= -function(a){if(this.a[a]){for(var b=0;10>b;b++)for(var c=Object.keys(this.b[b]),k=0;k=g;z--)if(w=(r?h[u]:this.b)[z][t])v[y++]= -w,x=!0;if(x)l[l.length]=1h;l--)u=n.substring(h,l),x(p,g,u,a,b,e);break;default:if(h=x(p,g,n,a,b,e),f&&1=e)for(h=p[10],r=g._ctx[n]||(g._ctx[n]={}),n=h[n]||(h[n]=[{},{},{},{},{},{},{},{},{},{}]),h=d-f,l=d+f,0>h&&(h=0),l>m-1&&(l=m-1);h<=l;h++)h!==d&&x(n,r,k[h],a,b,e)}}}this.a[a]="1";this.g=!1}return this};d.prototype.update=function(a,b){"string"===typeof b&&(a||0===a)&&this.a[a]&&(this.remove(a),b&&this.add(a,b));return this};d.prototype.remove=function(a){if(this.a[a]){for(var b= +0;10>b;b++)for(var c=Object.keys(this.b[b]),k=0;k=g;z--)if(v=(r?h[u]:this.b)[z][t])w[y++]=v,x=!0;if(x)l[l.length]= +1=k&&(a=a[h],a=a[c]||(a[c]=[]),a[a.length]=e)}return h||b[c]}function x(a){var b=[];if(!a)return b;for(var c=0,e=0,f=0,k="",h=a.length,B=0;Ba?1:0a?-1:0b&&(c=c.slice(0,b)));return c}function E(a){a.B||(a.B=I(function(){a.B=null;var b=a.async;b&&(a.async=!1);if(a.c.length){for(var c=J(),e;(e=a.c.shift())||0===e;){var f=a.h[e];switch(f[0]){case D.add:a.add(f[1],f[2]);break;case D.update:a.update(f[1],f[2]);break;case D.remove:a.remove(f[1])}a.h[e]=null;delete a.h[e];if(100=k&&(a=a[h+.5|0],a=a[d]||(a[d]=[]),a[a.length]=c)}return h||b[d]}function x(a){var b=[];if(!a)return b;for(var d=0,c=0,f=0,k="",h=a.length,B=0;Ba?1:0a?-1:0b&&(d=d.slice(0,b)));return d}function E(a){a.B||(a.B=I(function(){a.B=null;var b=a.async;b&&(a.async=!1);if(a.c.length){for(var d=J(),c;(c=a.c.shift())||0===c;){var f=a.h[c];switch(f[0]){case D.add:a.add(f[1],f[2]);break;case D.update:a.update(f[1],f[2]);break;case D.remove:a.remove(f[1])}a.h[c]=null;delete a.h[c];if(100=f&&(c.o=c.b),c.w&&c.o===c.b&&(c.i.length?c.f="":c.f||(c.f=b),c.cache&&c.l.set(b,c.i),c.w(c.i),c.i=[]))})}if(b=a.profile)if(b=G[b])for(var f in b)b.hasOwnProperty(f)&&"undefined"===typeof a[f]&&(a[f]=b[f]);this.mode=a.mode||this.mode||v.mode;this.cache=a.cache||this.cache||v.cache;this.async=a.async||this.async||v.async;this.b=a.worker||this.b||v.b;this.threshold=a.threshold||this.threshold||v.threshold;this.depth=a.depth||this.depth||v.depth;this.v=(b=a.encode)&&y[b]||("function"=== -typeof b?b:this.v||!1);this.C=a.debug||this.C;(b=a.matcher)&&this.addMatcher(b);if(b=a.filter)this.A=N(!0===b?T:b,this.v);if(b=a.stemmer)this.D=O(!0===b?H:b,this.v)}this.g=[{},{},{},{},{},{},{},{},{},{},{}];this.a={};this.h={};this.c=[];this.B=null;this.f="";this.u=!0;this.l=this.cache?new U(3E4,50,!0):!1;return this};g.prototype.encode=function(a){a&&z.length&&(a=w(a,z));a&&this.m.length&&(a=w(a,this.m));a&&this.v&&(a=this.v.call(y,a));if(a&&this.A){a=a.split(" ");for(var b=0;b=this.j.length&&(this.s=0),this.j[this.s].postMessage(this.s,{add:!0,id:a,content:b}),this.a[a]=""+this.s,this;if(this.async)return this.h[a]||(this.c[this.c.length]= -a),this.h[a]=[D.add,a,b],E(this),this;b=this.encode(b);if(!b.length)return this;for(var c=this.mode,e="function"===typeof c?c(b):"ngram"===c?x(b):b.split(K),f={_ctx:{}},d=this.threshold,h=this.depth,g=this.g,n=e.length,q=0;ql;A--)u=m.substring(l,A),t(g,f,u,a,b,d);break; -default:if(l=t(g,f,m,a,b,d),h&&1=d)for(l=g[10],r=f._ctx[m]||(f._ctx[m]={}),m=l[m]||(l[m]=[{},{},{},{},{},{},{},{},{},{}]),l=q-h,A=q+h,0>l&&(l=0),A>n-1&&(A=n-1);l<=A;l++)l!==q&&t(m,r,e[l],a,b,d)}}}this.a[a]="1";this.u=!1}return this};g.prototype.update=function(a,b){if("string"===typeof b&&(a||0===a)&&this.a[a]){if(this.b){var c=parseInt(this.a[a],10);this.j[c].postMessage(c,{update:!0,id:a,content:b});return this}if(this.async)return this.h[a]||(this.c[this.c.length]=a),this.h[a]=[D.update, -a,b],E(this),this;this.remove(a);b&&this.add(a,b)}return this};g.prototype.remove=function(a){if(this.a[a]){if(this.b){var b=parseInt(this.a[a],10);this.j[b].postMessage(b,{remove:!0,id:a});delete this.a[a];return this}if(this.async)return this.h[a]||(this.c[this.c.length]=a),this.h[a]=[D.remove,a],E(this),this;for(b=0;10>b;b++)for(var c=Object.keys(this.g[b]),e=0;e=d;z--)if(v=(r?l[u]:this.g)[z][p])t[y++]= -v,w=!0;if(w)q[q.length]=1h;h++)for(b=Object.keys(this.g[h]),a=0;a=f&&(c.o=c.b),c.w&&c.o===c.b&&(c.i.length?c.f="":c.f||(c.f=b),c.cache&&c.l.set(b,c.i),c.w(c.i),c.i=[]))})}this.mode=a.mode||d.mode||this.mode||u.mode;this.cache=a.cache||this.cache||u.cache;this.async=a.async||this.async||u.async;this.b=a.worker||this.b||u.b;this.threshold=a.threshold||d.threshold||this.threshold||u.threshold;this.depth=a.depth||d.depth||this.depth||u.depth;this.v=(b=a.encode||d.encode)&&y[b]||("function"===typeof b?b:this.v||!1);this.C=a.debug|| +this.C;(b=a.matcher)&&this.addMatcher(b);if(b=a.filter)this.A=N(!0===b?T:b,this.v);if(b=a.stemmer)this.D=O(!0===b?H:b,this.v)}this.g=[{},{},{},{},{},{},{},{},{},{},{}];this.a={};this.h={};this.c=[];this.B=null;this.f="";this.u=!0;this.l=this.cache?new U(3E4,50,!0):!1;return this};g.prototype.encode=function(a){a&&z.length&&(a=v(a,z));a&&this.m.length&&(a=v(a,this.m));a&&this.v&&(a=this.v.call(y,a));if(a&&this.A){a=a.split(" ");for(var b=0;b=this.j.length&&(this.s=0),this.j[this.s].postMessage(this.s,{add:!0,id:a,content:b}),this.a[a]=""+this.s,this;if(this.async)return this.h[a]||(this.c[this.c.length]=a),this.h[a]=[D.add,a,b],E(this), +this;b=this.encode(b);if(!b.length)return this;for(var d=this.mode,c="function"===typeof d?d(b):"ngram"===d?x(b):b.split(K),f={_ctx:{}},e=this.threshold,h=this.depth,g=this.g,n=c.length,q=0;ql;A--)t=m.substring(l,A),w(g,f,t,a,b,e);break;default:if(l=w(g,f,m,a,b,e), +h&&1=e)for(l=g[10],r=f._ctx[m]||(f._ctx[m]={}),m=l[m]||(l[m]=[{},{},{},{},{},{},{},{},{},{}]),l=q-h,A=q+h,0>l&&(l=0),A>n-1&&(A=n-1);l<=A;l++)l!==q&&w(m,r,c[l],a,b,e)}}}this.a[a]="1";this.u=!1}return this};g.prototype.update=function(a,b){if("string"===typeof b&&(a||0===a)&&this.a[a]){if(this.b){var d=parseInt(this.a[a],10);this.j[d].postMessage(d,{update:!0,id:a,content:b});return this}if(this.async)return this.h[a]||(this.c[this.c.length]=a),this.h[a]=[D.update,a,b],E(this),this;this.remove(a); +b&&this.add(a,b)}return this};g.prototype.remove=function(a){if(this.a[a]){if(this.b){var b=parseInt(this.a[a],10);this.j[b].postMessage(b,{remove:!0,id:a});delete this.a[a];return this}if(this.async)return this.h[a]||(this.c[this.c.length]=a),this.h[a]=[D.remove,a],E(this),this;for(b=0;10>b;b++)for(var d=Object.keys(this.g[b]),c=0;c=e;z--)if(v=(r?l[t]:this.g)[z][p])u[y++]=v,w=!0;if(w)q[q.length]= +1h;h++)for(b=Object.keys(this.g[h]),a=0;a