diff --git a/README.md b/README.md index 659d6ec..3779733 100644 --- a/README.md +++ b/README.md @@ -29,33 +29,36 @@ Supported Platforms: - Browser - Node.js + All Features: These features are not available in the 50% smaller light version: - WebWorker -- Async handler -- Cache handler -- Built-in encoders except 'balanced' (you can still pass in customs) -- Built-in stemmers and stopword filter (you can still pass in customs) +- Asynchronous +- Cache +- Built-in encoders except 'balance' and 'icase' (you can still pass in customs) +- Built-in stemmer and filter (you can still pass in customs) - Debug logging - _index.info()_ method @@ -518,7 +521,7 @@ FlexSearch ist highly customizable. Make use of the the righ "fastest" - The configuration profile.
+ The configuration profile. Choose your preferation.
@@ -545,7 +548,7 @@ FlexSearch ist highly customizable. Make use of the the righ "simple"
"advanced"
"extra"
- "balanced"
+ "balance"
function() The encoding type.

Choose one of the
built-ins or pass a custom encoding function. @@ -572,12 +575,13 @@ FlexSearch ist highly customizable. Make use of the the righ --> - cache

+ cache


+ false
true
- false + {number} - Enable/Disable caching. + Enable/Disable and/or set capacity of cached entries.

When passing a number as a limit the cache automatically balance stored entries related to their popularity.

Note: When just using "true" the cache has no limits and is actually 5 times faster (the balancer should not run). @@ -586,7 +590,7 @@ FlexSearch ist highly customizable. Make use of the the
righ true
false - Enable/Disable asynchronous processing. + Enable/Disable asynchronous processing.

Each job will be queued for non-blocking processing. Recommended when using WebWorkers. @@ -606,6 +610,33 @@ FlexSearch ist highly customizable. Make use of the the
righ Enable/Disable contextual indexing and also sets relevance depth (experimental). + + + threshold

+ + false
+ {number} + + Enable/Disable the threshold of minimum relevance results should have.

Note: You can take a lower threshold for indexing and pass a higher value when calling .search(), but not other turn around. + + + + stemmer

+ + false
+ {function} + + Disable or pass in custom object/array. + + + + filter

+ + false
+ {function} + + Disable or pass in custom object/array. + diff --git a/flexsearch.js b/flexsearch.js index 8e0c7f2..2b1489d 100644 --- a/flexsearch.js +++ b/flexsearch.js @@ -1,5 +1,5 @@ ;/**! - * @preserve FlexSearch v0.2.42 + * @preserve FlexSearch v0.2.44 * Copyright 2018 Thomas Wilkerling * Released under the Apache 2.0 Licence * https://github.com/nextapps-de/flexsearch @@ -504,7 +504,9 @@ var SUPPORT_ASYNC = true; /** @type {Array} */ this._matcher = []; - if(options){ + //if(options){ + + options || (options = defaults); var custom = options['profile']; var profile = custom ? profiles[custom] : {}; @@ -588,13 +590,6 @@ var SUPPORT_ASYNC = true; defaults.mode ); - if(SUPPORT_CACHE) this.cache = ( - - options['cache'] || - this.cache || - defaults.cache - ); - if(SUPPORT_ASYNC) this.async = ( options['async'] || @@ -678,7 +673,7 @@ var SUPPORT_ASYNC = true; ), this.encoder); } - } + //} // initialize primary index @@ -689,19 +684,8 @@ var SUPPORT_ASYNC = true; ]; this._ctx = {}; - this._ids = {}; - - /** - * @type {Object} - */ - this._stack = {}; - - /** - * @type {Array} - */ - this._stack_keys = []; /** @@ -712,11 +696,21 @@ var SUPPORT_ASYNC = true; this._last_empty_query = ""; this._status = true; - if(SUPPORT_CACHE) this._cache = this.cache ? + if(SUPPORT_CACHE) { - (new cache(30 * 1000, 50, true)) - : - false; + this.cache = custom = ( + + options['cache'] || + this.cache || + defaults.cache + ); + + this._cache = custom ? + + (new cache(custom)) + : + false; + } return this; }; @@ -900,6 +894,7 @@ var SUPPORT_ASYNC = true; if(value){ var length = value.length; + var context_score = (word_length - i) / word_length; switch(tokenizer){ @@ -918,9 +913,8 @@ var SUPPORT_ASYNC = true; dupes, tmp, id, - /** @type {string} */ - (content), - 0, + (length - a) / length, + context_score, threshold ); } @@ -941,9 +935,8 @@ var SUPPORT_ASYNC = true; dupes, tmp, id, - /** @type {string} */ - (content), - 0, + 1, + context_score, threshold ); } @@ -956,6 +949,8 @@ var SUPPORT_ASYNC = true; for(var x = 0; x < length; x++){ + var partial_score = (length - x) / length; + for(var y = length; y > x; y--){ tmp = value.substring(x, y); @@ -966,9 +961,8 @@ var SUPPORT_ASYNC = true; dupes, tmp, id, - /** @type {string} */ - (content), - 0, + partial_score, + context_score, threshold ); } @@ -986,9 +980,10 @@ var SUPPORT_ASYNC = true; dupes, value, id, - /** @type {string} */ - (content), - depth ? 1 : 0, + // Note: ngrams has partial scoring (sequence->word) and contextual scoring (word->context) + // TODO compute and pass distance of ngram sequences as the initial score for each word + 1, + context_score, threshold ); @@ -1015,9 +1010,8 @@ var SUPPORT_ASYNC = true; ctx_dupes, words[x], id, - /** @type {string} */ - (content), - (x < i ? i - x : x - i), + 0, + 10 - (x < i ? i - x : x - i), threshold ); } @@ -1465,6 +1459,7 @@ var SUPPORT_ASYNC = true; if(SUPPORT_CACHE && this.cache){ this._cache.reset(); + this._cache = null; } // release references @@ -1476,8 +1471,7 @@ var SUPPORT_ASYNC = true; this._ctx = this._ids = this._stack = - this._stack_keys = - this._cache = null; + this._stack_keys = null; return this; }; @@ -1496,8 +1490,8 @@ var SUPPORT_ASYNC = true; regex_space, ' ', regex_strip, '', - regex_whitespace, ' ', - regex_vowel, '' + regex_whitespace, ' ' + //regex_vowel, '' ]; return function(value){ @@ -1747,7 +1741,7 @@ var SUPPORT_ASYNC = true; })() : null; - // Xone Flexi-Cache Handler Fallback + // Flexi-Cache var cache = SUPPORT_CACHE ? (function(){ @@ -1755,7 +1749,8 @@ var SUPPORT_ASYNC = true; function Cache(limit){ this.reset(); - this.limit = limit || 1000; + + this.limit = (limit !== true) && limit; } /** @this {Cache} */ @@ -1770,7 +1765,7 @@ var SUPPORT_ASYNC = true; /** @this {Cache} */ Cache.prototype.set = function(id, value){ - if(!this.count[id]){ + if(this.limit && (typeof this.cache[id] === 'undefined')){ var length = this.keys.length; @@ -1787,25 +1782,29 @@ var SUPPORT_ASYNC = true; this.index[id] = length; this.keys[length] = id; - this.count[id] = 1; + this.count[id] = -1; + this.cache[id] = value; + + // shift up counter +1 + + this.get(id); } + else{ - this.cache[id] = value; - - // shift up counter +1 - - this.get(id); + this.cache[id] = value; + } }; /** - * Note: It is a lot better to have the complexity when fetching the cache: + * Note: It is better to have the complexity when fetching the cache: * @this {Cache} */ + Cache.prototype.get = function(id){ var cache = this.cache[id]; - if(cache){ + if(this.limit && cache){ var count = ++this.count[id]; var index = this.index; @@ -1816,18 +1815,32 @@ var SUPPORT_ASYNC = true; var keys = this.keys; var old_index = current_index; - while(current_index && this.count[keys[current_index--]] <= count){} + // forward pointer + while(this.count[keys[--current_index]] <= count){ - if(current_index >= 0 && ((current_index + 1) !== old_index)){ + if(current_index === -1){ - var tmp = keys[current_index]; + break; + } + } - // TODO splice is too slow + // move pointer back + current_index++; + + if(current_index !== old_index){ + + // copy values from predecessors + for(var i = old_index; i > current_index; i--) { + + var key = keys[i - 1]; + + keys[i] = key; + index[key] = i; + } + + // push new value on top + keys[current_index] = id; index[id] = current_index; - keys.splice(current_index, 1, id); - - index[tmp] = old_index; - keys.splice(old_index, 1, tmp); } } } @@ -1893,16 +1906,24 @@ var SUPPORT_ASYNC = true; * @param {Object} dupes * @param {string} tmp * @param {string|number} id - * @param {string} content - * @param {number} context + * @param {number} partial_score + * @param {number} context_score * @param {number} threshold */ - function addIndex(map, dupes, tmp, id, content, context, threshold){ + function addIndex(map, dupes, tmp, id, partial_score, context_score, threshold){ if(typeof dupes[tmp] === 'undefined'){ - var score = context ? (10 - context) : calcScore(tmp, content); + var score = ( + + partial_score ? + + ((9 - (threshold || 6)) * context_score) + ((threshold || 6) * partial_score) + // calcScore(tmp, content) + : + context_score + ); dupes[tmp] = score; @@ -1918,6 +1939,23 @@ var SUPPORT_ASYNC = true; return score || dupes[tmp]; } + /** + * @param {!string} part + * @param {!string} ref + * @returns {number} + */ + + function calcScore(part, ref){ + + var context_index = ref.indexOf(part); + var partial_index = context_index - ref.lastIndexOf(" ", context_index); + + return ( + + (3 / ref.length * (ref.length - context_index)) + (6 / partial_index) + ); + } + /** * @param {Object} map * @param {string|number} id @@ -1961,23 +1999,6 @@ var SUPPORT_ASYNC = true; } } - /** - * @param {!string} part - * @param {!string} ref - * @returns {number} - */ - - function calcScore(part, ref){ - - var context_index = ref.indexOf(part); - var partial_index = context_index - ref.lastIndexOf(" ", context_index); - - return ( - - (3 / ref.length * (ref.length - context_index)) + (6 / partial_index) - ); - } - /** * @param {!string} value * @this {global_encoder} @@ -2027,9 +2048,21 @@ var SUPPORT_ASYNC = true; tmp += char; } + //console.log(tmp); + // dynamic n-gram sequences - if((char === ' ') || ((count_vowels > 1) && (count_literal > 1)) || (count_vowels > 2) || (count_literal > 2) || (i === length - 1)){ + if((char === ' ') || ( + + (count_vowels >= (length > 8 ? 2 : 1)) && + (count_literal >= 2) + + ) || ( + + (count_vowels >= 2) && + (count_literal >= (length > 8 ? 2 : 1)) + + ) || (i === length - 1)){ if(tmp){ diff --git a/flexsearch.light.js b/flexsearch.light.js index d47b75b..080a256 100644 --- a/flexsearch.light.js +++ b/flexsearch.light.js @@ -1,18 +1,18 @@ /* - FlexSearch v0.2.42 + FlexSearch v0.2.44 Copyright 2018 Thomas Wilkerling Released under the Apache 2.0 Licence https://github.com/nextapps-de/flexsearch */ -'use strict';(function(d,t,m){var q;(q=m.define)&&q.amd?q([],function(){return t}):(q=m.modules)?q[d.toLowerCase()]=t:"undefined"!==typeof module?module.exports=t:m[d]=t})("FlexSearch",function(){function d(a){"string"===typeof a&&(a=A[a]);a||(a=y);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 m(a){return new RegExp(a,"g")}function q(a,b,c){if("undefined"=== -typeof c){for(c=0;c=g&&(a=a[f+.5|0],a=a[c]||(a[c]=[]),a[a.length]=l)}return f||b[c]}function z(a,b){if(a)for(var c=Object.keys(a),l=0,e=c.length;la?1:0a?-1:0b))return c.slice(0,b);return c}var y={encode:"icase",mode:"ngram",cache:!1,async:!1,m:!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",mode:"strict",threshold:9,depth:1}},v=[],D=0,C=m("[ -/]");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)&&(v[v.length]=m(b),v[v.length]=a[b]);return this};d.register=function(a,b){w[a]=b;return this};d.encode=function(a,b){return w[a].call(w,b)};d.prototype.init=function(a){this.h=[];if(a){var b= -a.profile;b=b?A[b]:{};this.mode=a.mode||b.mode||this.mode||y.mode;this.threshold=a.threshold||b.threshold||this.threshold||y.threshold;this.depth=a.depth||b.depth||this.depth||y.depth;this.i=(b=a.encode||b.encode)&&w[b]||("function"===typeof b?b:this.i||!1);(b=a.matcher)&&this.addMatcher(b)}this.f=[{},{},{},{},{},{},{},{},{},{}];this.b={};this.a={};this.c="";this.g=!0;return this};d.prototype.encode=function(a){a&&v.length&&(a=q(a,v));a&&this.h.length&&(a=q(a,this.h));a&&this.i&&(a=this.i.call(w, -a));if(a&&this.j){a=a.split(" ");for(var b=0;bk;m--)u=n.substring(k,m),x(f,e,u,a,b,0,h);break;default:if(k=x(f,e,n,a,b,g?1:0,h),g&&1=h)for(r=e._ctx[n]||(e._ctx[n]={}),n=this.b[n]||(this.b[n]=[{},{},{},{},{},{},{},{},{},{}]),k=d-g,m=d+ -g+1,0>k&&(k=0),m>p&&(m=p);kb;b++)z(this.f[b],a);this.depth&&z(this.b,a);delete this.a[a];this.g=!1}return this};d.prototype.search=function(a,b,c){var l=[];if(a&&"object"===typeof a){c=a.callback||b;b=a.limit;var e=a.threshold;a=a.query}e=(e|| -this.threshold||0)|0;"function"===typeof b?(c=b,b=1E3):b||(b=1E3);if(c){var h=this;H(function(){c(h.search(a,b));h=null},1,"search-"+this.id);return null}if(!a||"string"!==typeof a)return l;if(!this.g)this.g=!0;else if(this.c&&0===a.indexOf(this.c))return l;var g=this.encode(a);if(!g.length)return l;var f=this.mode;g="function"===typeof f?f(g):"ngram"===f?B(g):g.split(C);f=g.length;var d=!0,m=[],n={};if(1=e;z--)if(v=(r?k[u]:this.f)[z][t])w[y++]=v,x=!0;if(x)m[m.length]=1=e&&(a=a[f+.5|0],a=a[c]||(a[c]=[]),a[a.length]=l)}return f||b[c]}function z(a,b){if(a)for(var c=Object.keys(a),l=0,g=c.length;l=(8=(8a?1:0a?-1:0b))return c.slice(0,b);return c}var x={encode:"icase",mode:"ngram",cache:!1,async:!1,m:!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",mode:"strict",threshold:9,depth:1}},w=[],E=0,D=p("[ -/]");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]=p(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.h=[];a||(a=x);var b=a.profile;b=b?A[b]:{};this.mode=a.mode||b.mode||this.mode|| +x.mode;this.threshold=a.threshold||b.threshold||this.threshold||x.threshold;this.depth=a.depth||b.depth||this.depth||x.depth;this.i=(b=a.encode||b.encode)&&y[b]||("function"===typeof b?b:this.i||!1);(b=a.matcher)&&this.addMatcher(b);this.f=[{},{},{},{},{},{},{},{},{},{}];this.b={};this.a={};this.c="";this.g=!0;return this};d.prototype.encode=function(a){a&&w.length&&(a=q(a,w));a&&this.h.length&&(a=q(a,this.h));a&&this.i&&(a=this.i.call(y,a));if(a&&this.j){a=a.split(" ");for(var b=0;bm;t--)r=d.substring(m,t),v(e,l,r,a,q,p,g);break;default:if(m=v(e,l,d,a,1,p,g),h&&1=g)for(n=l._ctx[d]||(l._ctx[d]={}),d=this.b[d]||(this.b[d]=[{},{},{},{},{},{},{},{},{},{}]),m=k-h,t=k+h+1,0>m&&(m=0),t>f&&(t=f);mb;b++)z(this.f[b],a);this.depth&&z(this.b,a);delete this.a[a];this.g=!1}return this};d.prototype.search=function(a,b,c){var l=[];if(a&&"object"===typeof a){c=a.callback||b;b=a.limit;var d=a.threshold;a=a.query}d=(d||this.threshold||0)|0;"function"===typeof b? +(c=b,b=1E3):b||(b=1E3);if(c){var h=this;I(function(){c(h.search(a,b));h=null},1,"search-"+this.id);return null}if(!a||"string"!==typeof a)return l;if(!this.g)this.g=!0;else if(this.c&&0===a.indexOf(this.c))return l;var e=this.encode(a);if(!e.length)return l;var f=this.mode;e="function"===typeof f?f(e):"ngram"===f?B(e):e.split(D);f=e.length;var k=!0,p=[],n={};if(1=d;z--)if(w=(q?m[r]:this.f)[z][t])v[y++]=w,x=!0;if(x)p[p.length]=1=k&&(a=a[h+.5|0],a=a[d]||(a[d]=[]),a[a.length]=c)}return h||b[d]}function n(a,b){if(a)for(var d=Object.keys(a),c=0,f=d.length;ca?1:0 -a?-1:0b))return d.slice(0,b);return d}function E(a){a.C||(a.C=H(function(){a.C=null;var b=a.async;b&&(a.async=!1);if(a.f.length){for(var d=I(),c;(c=a.f.shift())||0===c;){var f=a.h[c];switch(f[0]){case C.add:a.add(f[1], -f[2]);break;case C.update:a.update(f[1],f[2]);break;case C.remove:a.remove(f[1])}a.h[c]=null;delete a.h[c];if(100=k&&(a=a[h+.5|0],a=a[d]||(a[d]=[]),a[a.length]=c)}return h||b[d]}function n(a,b){if(a)for(var d=Object.keys(a),c=0,f=d.length;c=(8=(8a?1:0a?-1:0b))return d.slice(0,b);return d}function E(a){a.D||(a.D=H(function(){a.D=null;var b=a.async;b&&(a.async=!1);if(a.f.length){for(var d=I(),c;(c=a.f.shift())||0===c;){var f=a.h[c];switch(f[0]){case C.add:a.add(f[1],f[2]);break;case C.update:a.update(f[1], +f[2]);break;case C.remove:a.remove(f[1])}a.h[c]=null;delete a.h[c];if(100=f&&(c.m=c.b),c.w&&c.m===c.b&&(c.g.length?c.c="":c.c||(c.c=b),c.cache&&c.j.set(b,c.g),c.w(c.g),c.g=[]))})}this.mode=a.mode||d.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||d.threshold||this.threshold||v.threshold;this.depth=a.depth||d.depth||this.depth||v.depth;this.v=(b=a.encode||d.encode)&&z[b]||("function"===typeof b?b:this.v||!1);this.F=a.debug|| -this.F;(b=a.matcher)&&this.addMatcher(b);if(b=a.filter)this.A=M(!0===b?S:b,this.v);if(b=a.stemmer)this.D=N(!0===b?T:b,this.v)}this.l=[{},{},{},{},{},{},{},{},{},{}];this.o={};this.a={};this.h={};this.f=[];this.C=null;this.c="";this.u=!0;this.j=this.cache?new U(3E4,50,!0):!1;return this};g.prototype.encode=function(a){a&&A.length&&(a=w(a,A));a&&this.B.length&&(a=w(a,this.B));a&&this.v&&(a=this.v.call(z,a));if(a&&this.A){a=a.split(" ");for(var b=0;b=this.i.length&&(this.s=0),this.i[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.f[this.f.length]=a),this.h[a]=[C.add,a,b],E(this),this; -b=this.encode(b);if(!b.length)return this;d=this.mode;for(var c="function"===typeof d?d(b):"ngram"===d?G(b):b.split(J),f={_ctx:{}},e=this.threshold,k=this.depth,h=this.l,g=c.length,y=0;ym;n--)u=p.substring(m,n),x(h,f,u,a,b,0,e);break;default:if(m=x(h,f,p,a,b,k? -1:0,e),k&&1=e)for(t=f._ctx[p]||(f._ctx[p]={}),p=this.o[p]||(this.o[p]=[{},{},{},{},{},{},{},{},{},{}]),m=y-k,n=y+k+1,0>m&&(m=0),n>g&&(n=g);m=f&&(c.m=c.b),c.w&&c.m===c.b&&(c.g.length?c.c="":c.c||(c.c=b),c.cache&&c.j.set(b,c.g),c.w(c.g),c.g=[]))})}this.mode=a.mode||d.mode||this.mode||x.mode;this.async=a.async||this.async||x.async;this.b=a.worker||this.b||x.b;this.threshold=a.threshold||d.threshold||this.threshold||x.threshold;this.depth=a.depth||d.depth||this.depth||x.depth;this.v=(b=a.encode||d.encode)&&z[b]||("function"===typeof b?b:this.v||!1);this.G=a.debug||this.G;(b=a.matcher)&&this.addMatcher(b); +if(b=a.filter)this.B=M(!0===b?S:b,this.v);if(b=a.stemmer)this.F=N(!0===b?T:b,this.v);this.l=[{},{},{},{},{},{},{},{},{},{}];this.o={};this.a={};this.h={};this.f=[];this.D=null;this.c="";this.u=!0;this.j=(this.cache=b=a.cache||this.cache||x.cache)?new U(b):!1;return this};g.prototype.encode=function(a){a&&A.length&&(a=y(a,A));a&&this.C.length&&(a=y(a,this.C));a&&this.v&&(a=this.v.call(z,a));if(a&&this.B){a=a.split(" ");for(var b=0;b=this.i.length&&(this.s=0),this.i[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.f[this.f.length]=a),this.h[a]=[C.add,a,b],E(this),this;b=this.encode(b); +if(!b.length)return this;d=this.mode;b="function"===typeof d?d(b):"ngram"===d?G(b):b.split(J);for(var c={_ctx:{}},f=this.threshold,e=this.depth,k=this.l,h=b.length,g=0;gl;w--)v=r.substring(l,w),u(k,c,v,a,q,n,f);break;default:if(l=u(k,c, +r,a,1,n,f),e&&1=f)for(p=c._ctx[r]||(c._ctx[r]={}),r=this.o[r]||(this.o[r]=[{},{},{},{},{},{},{},{},{},{}]),l=g-e,w=g+e+1,0>l&&(l=0),w>h&&(w=h);lb;b++)n(this.l[b],a);this.depth&&n(this.o,a);delete this.a[a];this.u=!1}return this};g.prototype.search=function(a,b,d){var c=[];if(a&&"object"===typeof a){d=a.callback||b;b=a.limit;var f=a.threshold;a=a.query}f=(f||this.threshold||0)|0;"function"===typeof b?(d=b,b=1E3):b||(b=1E3);if(this.b){this.w=d;this.m=0;this.g=[];for(c=0;c=f;A--)if(w=(t?m[u]:this.l)[A][q])v[z++]=w,x=!0;if(x)n[n.length]=1g;g++)for(b=Object.keys(this.l[g]),a=0;a=f;A--)if(x=(q?l[v]:this.l)[A][w])u[z++]=x,y=!0;if(y)n[n.length]=1g;g++)for(b=Object.keys(this.l[g]),a=0;ag;c--)h=k[c-1],k[c]=h,e[h]=c;k[g]=a;e[a]=g}}}return b};return a}(); +return g}(function(){var q={},B=!("undefined"===typeof Blob||"undefined"===typeof URL||!URL.createObjectURL);return function(g,t,e,y,u){var n=g;g=B?URL.createObjectURL(new Blob(["var SUPPORT_WORKER = true;var SUPPORT_BUILTINS = true;var SUPPORT_DEBUG = true;var SUPPORT_CACHE = true;var SUPPORT_ASYNC = true;("+e.toString()+")()"],{type:"text/javascript"})):"../"+n+".js";n+="-"+t;q[n]||(q[n]=[]);q[n][u]=new Worker(g);q[n][u].onmessage=y;console.log("Register Worker: "+n+"@"+u);return{postMessage:function(e, +g){q[n][e].postMessage(g)}}}}()),this); diff --git a/package.json b/package.json index 1498a06..c002e3e 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { - "name": "FlexSearch", - "version": "0.2.42", - "description": "World's fastest and most memory efficient full text search library.", + "name": "flexsearch", + "version": "0.2.44", + "description": "Next-Generation full text search library with zero dependencies.", "homepage": "https://nextapps-de.github.io/xone/", "author": "Thomas Wilkerling", "copyright": "Nextapps GmbH", diff --git a/test/matching-flexsearch.html b/test/matching-flexsearch.html index 1d50272..bfffcc6 100644 --- a/test/matching-flexsearch.html +++ b/test/matching-flexsearch.html @@ -238,17 +238,19 @@ var text_data = "LIBRARY OF THE FUTURE (R) First Edition Ver. 4.02 Gulliver's Tr // --------------------------------------- - do_test('test-1', 'without breach of modesty', '493'); - do_test('test-2', 'went softly stream', '446'); - do_test('test-3', 'princes of the ambition', '72'); - do_test('test-4', 'five-thousand leagues', '2'); - do_test('test-5', 'i already observed', '458'); - do_test('test-6', 'let a of his', '50'); - do_test('test-7', 'take that to the rocks', '175'); - do_test('test-8', 'bignes of splaknuk', '146'); - do_test('test-9', 'matematikal musikal instruments', '267'); - do_test('test-10', 'matical sical strument', '267'); - do_test('test-11', 'lalkon the camberlayhn', '99'); + do_test('test-1', 'without breach of modesty', [493]); + do_test('test-2', 'went softly stream', [446]); + do_test('test-3', 'princes of the ambition', [72, 408]); + do_test('test-4', 'five-thousand leagues', [2]); + do_test('test-5', 'i already observed', [458, 346]); + do_test('test-6', 'let a of his', [50]); + do_test('test-7', 'take that to the rocks', [175]); + do_test('test-8', 'bignes of splaknuk', [146]); + do_test('test-9', 'matematikal musikal instruments', [267]); + do_test('test-10', 'matical sical strument', [267]); + do_test('test-11', 'lalkon the camberlayhn', [99]); + + // --------------------------------------- // --------------------------------------- @@ -256,30 +258,65 @@ var text_data = "LIBRARY OF THE FUTURE (R) First Edition Ver. 4.02 Gulliver's Tr var nodes = document.getElementById(id).getElementsByTagName('td'); - nodes[1].innerHTML = flexsearch_default.search(query).slice(0, 1).join('
') || '-'; - nodes[2].innerHTML = flexsearch_memory.search(query).slice(0, 1).join('
') || '-'; - nodes[3].innerHTML = flexsearch_speed.search(query).slice(0, 1).join('
') || '-'; - nodes[4].innerHTML = flexsearch_match.search(query).slice(0, 1).join('
') || '-'; - nodes[5].innerHTML = flexsearch_score.search(query).slice(0, 1).join('
') || '-'; - nodes[6].innerHTML = flexsearch_balance.search(query).slice(0, 1).join('
') || '-'; - nodes[7].innerHTML = flexsearch_fastest.search(query).slice(0, 1).join('
') || '-'; - for(var i = 1; i < nodes.length; i++){ - if(nodes[i].innerHTML === '-'){ + var results; - nodes[i].style.backgroundColor = '#f00'; - } - else if(nodes[i].innerHTML === ref){ + switch(i){ - nodes[i].style.backgroundColor = '#0a0'; - } - else{ + case 1: + results = flexsearch_default.search(query); + break; - nodes[i].style.backgroundColor = 'orange'; + case 2: + results = flexsearch_memory.search(query); + break; + + case 3: + results = flexsearch_speed.search(query); + break; + + case 4: + results = flexsearch_match.search(query); + break; + + case 5: + results = flexsearch_score.search(query); + break; + + case 6: + results = flexsearch_balance.search(query); + break; + + case 7: + results = flexsearch_fastest.search(query); + break; } - nodes[i].style.color = '#fff'; + for(var a = 0; a < ref.length; a++){ + + var current = ref[a]; + + nodes[i].innerHTML = results[0] || '-'; + nodes[i].style.color = '#fff'; + + if((results[0] === current) || (results[0] === String(current))){ + + nodes[i].style.backgroundColor = '#0a0'; + break; + } + else if(!results.length || ((results.indexOf(current) === -1) && (results.indexOf(String(current)) === -1))){ + + if(nodes[i].style.backgroundColor !== 'orange'){ + + nodes[i].style.backgroundColor = '#f00'; + } + } + else{ + + nodes[i].style.backgroundColor = 'orange'; + } + } } } diff --git a/test/matching.html b/test/matching.html index 5b18e23..5b842a6 100644 --- a/test/matching.html +++ b/test/matching.html @@ -459,7 +459,7 @@ var text_data = "LIBRARY OF THE FUTURE (R) First Edition Ver. 4.02 Gulliver's Tr } } - }, 100); + }, 50); diff --git a/test/test.js b/test/test.js index 0d4bb2d..2c657e3 100644 --- a/test/test.js +++ b/test/test.js @@ -356,11 +356,19 @@ describe('Apply Sort by Scoring', function(){ expect(flexsearch_reverse.search("xxx").length).to.equal(1); expect(flexsearch_reverse.search("yyy").length).to.equal(1); expect(flexsearch_reverse.search("zzz").length).to.equal(1); + expect(flexsearch_reverse.search({query: "xxx", threshold: 2}).length).to.equal(1); - expect(flexsearch_reverse.search({query: "xxx", threshold: 5}).length).to.equal(0); - expect(flexsearch_reverse.search({query: "yyy", threshold: 2}).length).to.equal(0); - expect(flexsearch_reverse.search({query: "zzz", threshold: 1}).length).to.equal(0); + expect(flexsearch_reverse.search({query: "xxx", threshold: 3}).length).to.equal(1); + expect(flexsearch_reverse.search({query: "xxx", threshold: 5}).length).to.equal(1); + expect(flexsearch_reverse.search({query: "xxx", threshold: 7}).length).to.equal(0); // <-- stop + + expect(flexsearch_reverse.search({query: "yyy", threshold: 0}).length).to.equal(1); + expect(flexsearch_reverse.search({query: "yyy", threshold: 2}).length).to.equal(1); + expect(flexsearch_reverse.search({query: "yyy", threshold: 5}).length).to.equal(0); // <-- stop + expect(flexsearch_reverse.search({query: "zzz", threshold: 0}).length).to.equal(1); + expect(flexsearch_reverse.search({query: "zzz", threshold: 1}).length).to.equal(1); + expect(flexsearch_reverse.search({query: "zzz", threshold: 3}).length).to.equal(0); // <-- stop }); });