diff --git a/README.md b/README.md index b0256b7..4575c71 100644 --- a/README.md +++ b/README.md @@ -826,8 +826,59 @@ The required memory for the index depends on several options: "full" * n * (n - 1) + + Contextual Index + Multiplied with: + + + + * 2(depth + 1) + +## Example Options + +Memory-optimized: + +```js +{ + encode: "extra", + mode: "strict", + threshold: 5 +} +``` + +Speed-optimized: + +```js +{ + encode: "icase", + mode: "strict", + threshold: 5, + depth: 2 +} +``` + +Matching-tolerant: + +```js +{ + encode: "extra", + mode: "full" +} +``` + +Balanced: + +```js +{ + encode: "simple", + mode: "ngram", + threshold: 3, + depth: 3 +} +``` + --- Author FlexSearch: Thomas Wilkerling
diff --git a/flexsearch.js b/flexsearch.js index c6ec0d6..2e6de68 100644 --- a/flexsearch.js +++ b/flexsearch.js @@ -498,6 +498,7 @@ }; var threshold = this.threshold; + var depth = this.depth; var map = this._map; var word_length = words.length; @@ -595,7 +596,7 @@ threshold ); - if((word_length > 1) && this.depth && (score > threshold)){ + if(depth && (word_length > 1) && (score > threshold)){ var ctx_map = map[10]; var ctx_dupes = dupes['_ctx'][value] || (dupes['_ctx'][value] = {}); @@ -615,8 +616,8 @@ //{/* ctx */} ]); - var x = i - this.depth; - var y = i + this.depth; + var x = i - depth; + var y = i + depth; if(x < 0) x = 0; if(y > word_length - 1) y = word_length - 1; diff --git a/flexsearch.min.js b/flexsearch.min.js index 8c8c9ba..f4507b1 100644 --- a/flexsearch.min.js +++ b/flexsearch.min.js @@ -3,23 +3,23 @@ @version: 0.2.11 @license: Apache 2.0 Licence */ -'use strict';(function(p,C,f){var d;(d=f.define)&&d.amd?d([],function(){return C}):(d=f.modules)?d[p.toLowerCase()]=C:"undefined"!==typeof module?module.exports=C:f[p]=C})("FlexSearch",function H(p){function f(a){a||(a=u);this.id=a.id||I++;this.init(a);Object.defineProperty(this,"index",{get:function(){return this.a}});Object.defineProperty(this,"length",{get:function(){return Object.keys(this.a).length}})}function d(a){return new RegExp(a,"g")}function v(a,b,c){if("undefined"===typeof c){for(c=0;c< -b.length;c+=2)a=a.replace(b[c],b[c+1]);return a}return a.replace(b,c)}function q(a,b,c,e,k,l){if("undefined"===typeof b[c]){var g=k.indexOf(c);g=3/k.length*(k.length-g)+6/(g-k.lastIndexOf(" ",g))+.5|0;b[c]=g;g>l&&(a=a[g],a=a[c]||(a[c]=[]),a[a.length]=e)}return g||b[c]}function x(a){for(var b="",c="",e="",k=0;ka?1:0a?-1:0b&&(c=c.slice(0,b)));return c}function z(a){a.w||(a.w=E(function(){a.w= -null;var b=a.async;b&&(a.async=!1);if(a.c.length){for(var c=F(),e;(e=a.c.shift())||0===e;){var k=a.h[e];switch(k[0]){case A.add:a.add(k[1],k[2]);break;case A.update:a.update(k[1],k[2]);break;case A.remove:a.remove(k[1])}a.h[e]=null;delete a.h[e];if(100n&&(a=a[g],a=a[c]||(a[c]=[]),a[a.length]=e)}return g||b[c]}function x(a){for(var b="",c="",e="",l=0;la?1:0a?-1:0b&&(c=c.slice(0,b)));return c}function z(a){a.w||(a.w=E(function(){a.w= +null;var b=a.async;b&&(a.async=!1);if(a.c.length){for(var c=F(),e;(e=a.c.shift())||0===e;){var l=a.h[e];switch(l[0]){case A.add:a.add(l[1],l[2]);break;case A.update:a.update(l[1],l[2]);break;case A.remove:a.remove(l[1])}a.h[e]=null;delete a.h[e];if(100=d&&(b.o=b.b),b.v&&b.o===b.b&&(b.i.length?b.f="":b.f||(b.f=c),b.cache&&b.l.set(c,b.i),b.v(b.i),b.i=[]))})}this.mode= -a.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||this.threshold||u.threshold;this.depth=a.depth||this.depth||u.depth;this.encoder=a.encode&&B[a.encode]||("function"===typeof a.encode?a.encode:this.encoder||!1);this.A=a.debug||this.A;a.matcher&&this.addMatcher(a.matcher)}this.g=[{},{},{},{},{},{},{},{},{},{},{}];this.a={};this.h={};this.c=[];this.w=null;this.f="";this.u=!0;this.l=this.cache? -new M(3E4,50,!0):!1;return this};f.prototype.encode=function(a){a&&w.length&&(a=v(a,w));a&&this.m.length&&(a=v(a,this.m));a&&this.encoder&&(a=this.encoder.call(B,a));return a};f.prototype.addMatcher=function(a){for(var b in a)a.hasOwnProperty(b)&&(this.m[this.m.length]=d(b),this.m[this.m.length]=a[b]);return this};f.prototype.add=function(a,b){if("string"===typeof b&&b&&(a||0===a))if(this.a[a])this.update(a,b);else{if(this.b)return++this.s>=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]=[A.add,a,b],z(this),this;b=this.encode(b);if(!b.length)return this;if("ngram"===this.mode){var c=b;var e=[];if(c)for(var d=0,l=0,g=-1,f="",n=c.length,h=0;hh;m--)t=n.substring(h,m),q(l,e,t,a,b,d);break;default:if(h=q(l,e,n,a,b,d),1d)for(h=l[10],r=e._ctx[n]||(e._ctx[n]={}),n=h[n]||(h[n]=[{},{},{},{},{},{},{},{},{},{}]),h=f-this.depth,m=f+this.depth,0>h&&(h=0),m>g-1&&(m=g-1);h<= -m;h++)h!==f&&q(n,r,c[h],a,b,d)}}this.a[a]="1";this.u=!1}return this};f.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]=[A.update,a,b],z(this),this;this.remove(a);b&&this.add(a,b)}return this};f.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]=[A.remove,a],z(this),this;for(b=0;10>b;b++)for(var c=Object.keys(this.g[b]),e=0;e=d;z--)if(w=r?u[t][z][q]:this.g[z][q])x[B++]=w,A=!0;if(A)h[h.length]=1g;g++)for(b=Object.keys(this.g[g]),a=0;a=d&&(b.o=b.b),b.v&&b.o===b.b&&(b.i.length?b.f="":b.f||(b.f=c),b.cache&&b.l.set(c,b.i),b.v(b.i),b.i=[]))})}this.mode= +a.mode||this.mode||t.mode;this.cache=a.cache||this.cache||t.cache;this.async=a.async||this.async||t.async;this.b=a.worker||this.b||t.b;this.threshold=a.threshold||this.threshold||t.threshold;this.depth=a.depth||this.depth||t.depth;this.encoder=a.encode&&B[a.encode]||("function"===typeof a.encode?a.encode:this.encoder||!1);this.A=a.debug||this.A;a.matcher&&this.addMatcher(a.matcher)}this.g=[{},{},{},{},{},{},{},{},{},{},{}];this.a={};this.h={};this.c=[];this.w=null;this.f="";this.u=!0;this.l=this.cache? +new M(3E4,50,!0):!1;return this};f.prototype.encode=function(a){a&&v.length&&(a=u(a,v));a&&this.m.length&&(a=u(a,this.m));a&&this.encoder&&(a=this.encoder.call(B,a));return a};f.prototype.addMatcher=function(a){for(var b in a)a.hasOwnProperty(b)&&(this.m[this.m.length]=d(b),this.m[this.m.length]=a[b]);return this};f.prototype.add=function(a,b){if("string"===typeof b&&b&&(a||0===a))if(this.a[a])this.update(a,b);else{if(this.b)return++this.s>=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]=[A.add,a,b],z(this),this;b=this.encode(b);if(!b.length)return this;if("ngram"===this.mode){var c=b;var e=[];if(c)for(var d=0,n=0,g=-1,f="",q=c.length,k=0;kh;w--)r=k.substring(h,w),p(g,e,r,a,b,d);break;default:if(h=p(g,e,k,a,b,d),n&&1d)for(h=g[10],m=e._ctx[k]||(e._ctx[k]={}),k=h[k]||(h[k]=[{},{},{},{},{},{},{},{},{},{}]),h=q-n,w=q+n,0>h&&(h=0),w>f-1&&(w=f-1);h<=w;h++)h!== +q&&p(k,m,c[h],a,b,d)}}this.a[a]="1";this.u=!1}return this};f.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]=[A.update,a,b],z(this),this;this.remove(a);b&&this.add(a,b)}return this};f.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]=[A.remove,a],z(this),this;for(b=0;10>b;b++)for(var c=Object.keys(this.g[b]),e=0;e=d;z--)if(v=t?w[r][z][p]:this.g[z][p])x[B++]=v,A=!0;if(A)k[k.length]=1g;g++)for(b=Object.keys(this.g[g]),a=0;a