From 8fb63c48d857f367bf3f2d76a575b8ce5d4fc48e Mon Sep 17 00:00:00 2001 From: Thomas Wilkerling Date: Thu, 22 Mar 2018 02:31:39 +0100 Subject: [PATCH] MOD refactoring FIX remove from contextual index --- doc/memory-comparison.svg | 2 +- flexsearch.js | 293 +++++++++++++++++++------------------- flexsearch.light.js | 24 ++-- flexsearch.min.js | 48 +++---- package.json | 18 ++- test/index.html | 2 +- test/matching.html | 170 ++++++++++++++++------ test/test.js | 6 +- 8 files changed, 321 insertions(+), 242 deletions(-) diff --git a/doc/memory-comparison.svg b/doc/memory-comparison.svg index 2553efe..7e95b3f 100644 --- a/doc/memory-comparison.svg +++ b/doc/memory-comparison.svg @@ -1,3 +1,3 @@ -569743 bytes3518156 bytes4341000 bytes117021480 bytes2398442 bytes226943056 bytes2227612 bytes4746825 bytes0500000100000015000002000000250000030000003500000400000045000005000000RAMFlexSearchBulkSearchElasticlunrLunrWadeJsSearchJSiibm25Memory Consumption of Search Engines (lower ist better)https://github.com/nextapps-de/flexsearch/ +569,743 Bytes3,518,156 Bytes4,341,000 Bytes117,021,480 Bytes2,398,442 Bytes226,943,056 Bytes2,227,612 Bytes4,746,825 Bytes0500000100000015000002000000250000030000003500000400000045000005000000RAMFlexSearchBulkSearchElasticlunrLunrWadeJsSearchJSiibm25Memory Consumption of Search Engines (lower ist better)https://github.com/nextapps-de/flexsearch/ diff --git a/flexsearch.js b/flexsearch.js index cb1d50f..42c819a 100644 --- a/flexsearch.js +++ b/flexsearch.js @@ -1,5 +1,5 @@ ;/**! - * @preserve FlexSearch v0.2.32 + * @preserve FlexSearch v0.2.4 * Copyright 2017-2018 Thomas Wilkerling * Released under the Apache 2.0 Licence * https://github.com/nextapps-de/flexsearch @@ -35,20 +35,10 @@ var SUPPORT_ASYNC = true; var defaults = { - // use on of built-in functions - // or pass custom encoding algorithm encode: 'icase', - - // type of information - mode: 'forward', - - // use flexible cache (scales automatically) + mode: 'ngram', cache: false, - - // use flexible cache (scales automatically) async: false, - - // use flexible cache (scales automatically) worker: false, // minimum scoring (0 - 9) @@ -60,6 +50,7 @@ var SUPPORT_ASYNC = true; /** * @private + * @enum {Object} * @const * @final */ @@ -653,7 +644,11 @@ var SUPPORT_ASYNC = true; if(custom = options['matcher']) { - this.addMatcher(/** @type {Object} */ (custom)); + this.addMatcher( + + /** @type {Object} */ + (custom) + ); } if(SUPPORT_BUILTINS && (custom = options['filter'])) { @@ -664,7 +659,8 @@ var SUPPORT_ASYNC = true; filter : - /** @type {Array} */ (custom) + /** @type {Array} */ + (custom) ), this.encoder); } @@ -677,29 +673,23 @@ var SUPPORT_ASYNC = true; stemmer : - /** @type {Object} */ (custom) + /** @type {Object} */ + (custom) ), this.encoder); } } - // initialize index + // initialize primary index this._map = [ - {/* 0 */}, - {/* 1 */}, - {/* 2 */}, - {/* 3 */}, - {/* 4 */}, - {/* 5 */}, - {/* 6 */}, - {/* 7 */}, - {/* 8 */}, - {/* 9 */}, - {/* ctx */} + {/* 0 */}, {/* 1 */}, {/* 2 */}, {/* 3 */}, {/* 4 */}, + {/* 5 */}, {/* 6 */}, {/* 7 */}, {/* 8 */}, {/* 9 */} ]; + this._ctx = {}; + this._ids = {}; /** @@ -757,18 +747,19 @@ var SUPPORT_ASYNC = true; if(value && this.filter){ var words = value.split(' '); - var final = ""; + //var final = ""; for(var i = 0; i < words.length; i++){ var word = words[i]; + var filter = this.filter[word]; - if(this.filter[word]){ + if(filter){ //var length = word.length - 1; // TODO completely filter out words actually breaks the context chain - words[i] = this.filter[word]; + words[i] = filter; //words[i] = word[0] + (length ? word[1] : ''); //words[i] = '~' + word[0]; //words.splice(i, 1); @@ -789,18 +780,20 @@ var SUPPORT_ASYNC = true; }; /** - * @param {Object} matcher + * @param {Object} custom * @export */ - FlexSearch.prototype.addMatcher = function(matcher){ + FlexSearch.prototype.addMatcher = function(custom){ - for(var key in matcher){ + var matcher = this._matcher; - if(matcher.hasOwnProperty(key)){ + for(var key in custom){ - this._matcher[this._matcher.length] = regex(key); - this._matcher[this._matcher.length] = matcher[key]; + if(custom.hasOwnProperty(key)){ + + matcher[matcher.length] = regex(key); + matcher[matcher.length] = custom[key]; } } @@ -810,17 +803,18 @@ var SUPPORT_ASYNC = true; /** * @param {?number|string} id * @param {?string} content + * @param {boolean=} _skip_update * @this {FlexSearch} * @export */ - FlexSearch.prototype.add = function(id, content){ + FlexSearch.prototype.add = function(id, content, _skip_update){ if((typeof content === 'string') && content && (id || (id === 0))){ // check if index ID already exist - if(this._ids[id]){ + if(this._ids[id] && !_skip_update){ this.update(id, content); } @@ -878,9 +872,11 @@ var SUPPORT_ASYNC = true; :( tokenizer === 'ngram' ? - /** @type {!Array} */ (ngram(content)) + /** @type {!Array} */ + (ngram(content)) : - /** @type {string} */ (content).split(regex_split) + /** @type {string} */ + (content).split(regex_split) ) ); @@ -922,7 +918,8 @@ var SUPPORT_ASYNC = true; dupes, tmp, id, - /** @type {string} */ (content), + /** @type {string} */ + (content), threshold ); } @@ -943,7 +940,8 @@ var SUPPORT_ASYNC = true; dupes, tmp, id, - /** @type {string} */ (content), + /** @type {string} */ + (content), threshold ); } @@ -966,7 +964,8 @@ var SUPPORT_ASYNC = true; dupes, tmp, id, - /** @type {string} */ (content), + /** @type {string} */ + (content), threshold ); } @@ -984,37 +983,27 @@ var SUPPORT_ASYNC = true; dupes, value, id, - /** @type {string} */ (content), + /** @type {string} */ + (content), threshold ); if(depth && (word_length > 1) && (score >= threshold)){ - var ctx_map = map[10]; var ctx_dupes = dupes['_ctx'][value] || (dupes['_ctx'][value] = {}); - var ctx_tmp = ctx_map[value] || (ctx_map[value] = [ + var ctx_tmp = this._ctx[value] || (this._ctx[value] = [ - {/* 0 */}, - {/* 1 */}, - {/* 2 */}, - {/* 3 */}, - {/* 4 */}, - {/* 5 */}, - {/* 6 */}, - {/* 7 */}, - {/* 8 */}, - {/* 9 */} - // TODO test concept of deep trees instead of flat ones - //{/* ctx */} + {/* 0 */}, {/* 1 */}, {/* 2 */}, {/* 3 */}, {/* 4 */}, + {/* 5 */}, {/* 6 */}, {/* 7 */}, {/* 8 */}, {/* 9 */} ]); var x = i - depth; - var y = i + depth; + var y = i + depth + 1; if(x < 0) x = 0; - if(y > word_length - 1) y = word_length - 1; + if(y > word_length) y = word_length; - for(; x <= y; x++){ + for(; x < y; x++){ if(x !== i) addIndex( @@ -1022,7 +1011,8 @@ var SUPPORT_ASYNC = true; ctx_dupes, words[x], id, - /** @type {string} */ (content), + /** @type {string} */ + (content), threshold ); } @@ -1051,50 +1041,10 @@ var SUPPORT_ASYNC = true; FlexSearch.prototype.update = function(id, content){ - if((typeof content === 'string') && (id || (id === 0))){ + if(this._ids[id] && content && (typeof content === 'string')){ - if(this._ids[id]){ - - if(SUPPORT_WORKER && this.worker){ - - var int = parseInt(this._ids[id], 10); - - this._worker[int].postMessage(int, { - - 'update': true, - 'id': id, - 'content': content - }); - - return this; - } - - if(SUPPORT_ASYNC && this.async){ - - this._stack[id] || ( - - this._stack_keys[this._stack_keys.length] = id - ); - - this._stack[id] = [ - - enum_task.update, - id, - content - ]; - - register_task(this); - - return this; - } - - this.remove(id); - - if(content){ - - this.add(id, content); - } - } + this.remove(id); + this.add(id, content, /* skip_update: */ true); } return this; @@ -1144,32 +1094,12 @@ var SUPPORT_ASYNC = true; for(var z = 0; z < 10; z++){ - var keys = Object.keys(this._map[z]); + removeIndex(this._map[z], id); + } - for(var i = 0; i < keys.length; i++){ + if(this.depth){ - var key = keys[i]; - var tmp = this._map[z]; - tmp = tmp && tmp[key]; - - if(tmp && tmp.length){ - - for(var a = 0; a < tmp.length; a++){ - - if(tmp[a] === id){ - - tmp.splice(a, 1); - - break; - } - } - } - - if(!tmp.length){ - - delete this._map[z][key]; - } - } + removeIndex(this._ctx, id); } delete this._ids[id]; @@ -1311,9 +1241,11 @@ var SUPPORT_ASYNC = true; :( tokenizer === 'ngram' ? - /** @type {!Array} */ (ngram(_query)) + /** @type {!Array} */ + (ngram(_query)) : - /** @type {string} */ (_query).split(regex_split) + /** @type {string} */ + (_query).split(regex_split) ) ); @@ -1341,7 +1273,7 @@ var SUPPORT_ASYNC = true; var ctx_map; - if(!use_contextual || (ctx_map = this._map[10])[ctx_root]){ + if(!use_contextual || (ctx_map = this._ctx)[ctx_root]){ for(var a = use_contextual ? 1 : 0; a < length; a++){ @@ -1522,9 +1454,14 @@ var SUPPORT_ASYNC = true; // release references + this.filter = + this.stemmer = this._scores = this._map = + this._ctx = this._ids = + this._stack = + this._stack_keys = this._cache = null; return this; @@ -1554,6 +1491,13 @@ var SUPPORT_ASYNC = true; } })(); + /** @const */ + + var global_encoder_icase = function(value){ + + return value.toLowerCase(); + }; + /** * Phonetic Encoders * @dict {Function} @@ -1566,12 +1510,9 @@ var SUPPORT_ASYNC = true; // case insensitive search - 'icase': function(value){ + 'icase': global_encoder_icase, - return value.toLowerCase(); - }, - - // simple phonetic normalization (latin) + // literal normalization 'simple': (function(){ @@ -1618,7 +1559,7 @@ var SUPPORT_ASYNC = true; }; }()), - // advanced phonetic transformation (latin) + // literal transformation 'advanced': (function(){ @@ -1698,6 +1639,8 @@ var SUPPORT_ASYNC = true; })(), + // phonetic transformation + 'extra': (function(){ var soundex_b = regex('p'), @@ -1762,13 +1705,7 @@ var SUPPORT_ASYNC = true; } : { - // case insensitive search - - 'icase': function(value){ - - return value.toLowerCase(); - }, - + 'icase': global_encoder_icase, 'balance': global_encoder_balance }; @@ -1895,15 +1832,59 @@ var SUPPORT_ASYNC = true; if(score >= threshold){ - var arr = map[(score + 0.5) | 0]; + var arr = map[((score + 0.5) | 0)]; arr = arr[tmp] || (arr[tmp] = []); - arr[arr.length] = id; + + arr[arr.length] = id; } } return score || dupes[tmp]; } + /** + * @param {Object} map + * @param {string|number} id + */ + + function removeIndex(map, id){ + + if(map){ + + var keys = Object.keys(map); + + for(var i = 0, length_keys = keys.length; i < length_keys; i++){ + + var key = keys[i]; + var tmp = map[key]; + + if(tmp){ + + for(var a = 0, length_map = tmp.length; a < length_map; a++){ + + if(tmp[a] === id){ + + if(length_map === 1){ + + delete map[key]; + } + else{ + + tmp.splice(a, 1); + } + + break; + } + else if(typeof tmp[a] === 'object'){ + + removeIndex(tmp[a], id); + } + } + } + } + } + } + /** * @param {!string} part * @param {!string} ref @@ -1912,6 +1893,8 @@ var SUPPORT_ASYNC = true; function calcScore(part, ref){ + // TODO: use word distance further than char distance + var context_index = ref.indexOf(part); var partial_index = context_index - ref.lastIndexOf(" ", context_index); @@ -2446,13 +2429,25 @@ var SUPPORT_ASYNC = true; if(data['search']){ + var results = flexsearch['search'](data['content'], + + data['threshold'] ? + + { + 'limit': data['limit'], + 'threshold': data['threshold'] + } + : + data['limit'] + ); + /** @lends {Worker} */ self.postMessage({ - 'result': flexsearch['search'](data['content'], data['threshold'] ? {'limit': data['limit'], 'threshold': data['threshold']} : data['limit']), 'id': id, 'content': data['content'], - 'limit': data['limit'] + 'limit': data['limit'], + 'result':results }); } else if(data['add']){ diff --git a/flexsearch.light.js b/flexsearch.light.js index 39db111..6ee6e03 100644 --- a/flexsearch.light.js +++ b/flexsearch.light.js @@ -1,18 +1,18 @@ /* - FlexSearch v0.2.32 + FlexSearch v0.2.4 Copyright 2017-2018 Thomas Wilkerling 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]);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 l(a){return new RegExp(a,"g")}function q(a,b,c){if("undefined"=== -typeof c){for(c=0;c=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 y={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",mode:"strict",threshold:9,depth:1}},v=[],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)&&(v[v.length]=l(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.c=[];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.h=(b=a.encode||b.encode)&&w[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&&v.length&&(a=q(a,v));a&&this.c.length&&(a=q(a,this.c));a&&this.h&&(a=this.h.call(w,a));if(a&&this.i){a=a.split(" ");for(var b=0;bh;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=g&&(a=a[e+.5|0],a=a[c]||(a[c]=[]),a[a.length]=h)}return e||b[c]}function z(a,b){if(a)for(var c=Object.keys(a),h=0,f=c.length;ha?1:0a?-1:0b&&(c=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=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)&&(v[v.length]=l(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.b=[];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.j=(b=a.encode||b.encode)&&w[b]||("function"===typeof b?b:this.j||!1);(b=a.matcher)&&this.addMatcher(b)}this.g=[{},{},{},{},{},{},{},{},{},{}];this.c={};this.a={};this.f="";this.h=!0;return this};d.prototype.encode=function(a){a&&v.length&&(a=q(a,v));a&&this.b.length&&(a=q(a,this.b));a&&this.j&&(a=this.j.call(w,a));if(a&&this.i){a=a.split(" "); +for(var b=0;bm;l--)u=n.substring(m,l),x(p,f,u,a,b,g);break;default:if(m=x(p,f,n,a,b,g),e&&1=g)for(r=f._ctx[n]||(f._ctx[n]={}),n=this.c[n]||(this.c[n]=[{},{},{},{},{},{},{},{},{},{}]),m=d-e,l=d+e,0>m&&(m=0),l>k-1&&(l=k-1);m<=l;m++)m!== +d&&x(n,r,h[m],a,b,g)}}}this.a[a]="1";this.h=!1}return this};d.prototype.update=function(a,b){this.a[a]&&b&&"string"===typeof b&&(this.remove(a),b&&this.add(a,b,!0));return this};d.prototype.remove=function(a){if(this.a[a]){for(var b=0;10>b;b++)z(this.g[b],a);this.depth&&z(this.c,a);delete this.a[a];this.h=!1}return this};d.prototype.search=function(a,b,c){var h=[];if(a&&"object"===typeof a){c=a.callback||b;b=a.limit;var f=a.threshold;a=a.query}f=(f||this.threshold||0)|0;"function"===typeof b?(c=b, +b=1E3):b||(b=1E3);if(c){var g=this;H(function(){c(g.search(a,b));g=null},1,"search-"+this.id);return null}if(!a||"string"!==typeof a)return h;if(!this.h)this.h=!0;else if(this.f&&0===a.indexOf(this.f))return h;var e=this.encode(a);if(!e.length)return h;var d=this.mode;e="function"===typeof d?d(e):"ngram"===d?B(e):e.split(C);d=e.length;var k=!0,l=[],n={};if(1=f;z--)if(v=(r?m[u]:this.g)[z][t])w[y++]=v,x=!0;if(x)l[l.length]=1=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=k&&(a=a[f+.5|0],a=a[e]||(a[e]=[]),a[a.length]=c)}return f||b[e]}function m(a,b){if(a)for(var e=Object.keys(a),c=0,l=e.length;ca?1:0a?-1:0b&&(e=e.slice(0,b)));return e}function E(a){a.C||(a.C=I(function(){a.C=null;var b=a.async;b&&(a.async=!1);if(a.f.length){for(var e=J(),c;(c=a.f.shift())||0===c;){var d=a.h[c];switch(d[0]){case C.add:a.add(d[1],d[2]);break; +case C.update:a.update(d[1],d[2]);break;case C.remove:a.remove(d[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=[]))})}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=d&&(c.o=c.b),c.B&&c.o===c.b&&(c.g.length?c.c="":c.c||(c.c=b),c.cache&&c.j.set(b,c.g),c.B(c.g),c.g=[]))})}this.mode=a.mode||e.mode||this.mode||w.mode;this.cache=a.cache||this.cache||w.cache;this.async=a.async||this.async||w.async;this.b=a.worker||this.b||w.b;this.threshold=a.threshold||e.threshold||this.threshold||w.threshold;this.depth=a.depth||e.depth||this.depth||w.depth;this.w=(b=a.encode||e.encode)&&z[b]||("function"===typeof b?b:this.w||!1);this.F=a.debug|| +this.F;(b=a.matcher)&&this.addMatcher(b);if(b=a.filter)this.A=N(!0===b?T:b,this.w);if(b=a.stemmer)this.D=O(!0===b?H:b,this.w)}this.l=[{},{},{},{},{},{},{},{},{},{}];this.s={};this.a={};this.h={};this.f=[];this.C=null;this.c="";this.v=!0;this.j=this.cache?new U(3E4,50,!0):!1;return this};g.prototype.encode=function(a){a&&A.length&&(a=x(a,A));a&&this.m.length&&(a=x(a,this.m));a&&this.w&&(a=this.w.call(z,a));if(a&&this.A){a=a.split(" ");for(var b=0;b=this.i.length&&(this.u=0),this.i[this.u].postMessage(this.u,{add:!0,id:a,content:b}),this.a[a]=""+this.u,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;e=this.mode;for(var c="function"===typeof e?e(b):"ngram"===e?G(b):b.split(K),d={_ctx:{}},k=this.threshold,f=this.depth,h=this.l,g=c.length,t=0;tn;m--)v=p.substring(n,m),y(h,d,v,a,b,k);break;default:if(n= +y(h,d,p,a,b,k),f&&1=k)for(u=d._ctx[p]||(d._ctx[p]={}),p=this.s[p]||(this.s[p]=[{},{},{},{},{},{},{},{},{},{}]),n=t-f,m=t+f,0>n&&(n=0),m>g-1&&(m=g-1);n<=m;n++)n!==t&&y(p,u,c[n],a,b,k)}}}this.a[a]="1";this.v=!1}return this};g.prototype.update=function(a,b){this.a[a]&&b&&"string"===typeof b&&(this.remove(a),b&&this.add(a,b,!0));return this};g.prototype.remove=function(a){if(this.a[a]){if(this.b){var b=parseInt(this.a[a],10);this.i[b].postMessage(b,{remove:!0,id:a});delete this.a[a];return this}if(this.async)return this.h[a]|| +(this.f[this.f.length]=a),this.h[a]=[C.remove,a],E(this),this;for(b=0;10>b;b++)m(this.l[b],a);this.depth&&m(this.s,a);delete this.a[a];this.v=!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 e=a.threshold;a=a.query}e=(e||this.threshold||0)|0;"function"===typeof b?(d=b,b=1E3):b||(b=1E3);if(this.b){this.B=d;this.o=0;this.g=[];for(c=0;c=e;A--)if(x=(u?n[v]:this.l)[A][q])w[z++]=x,y=!0;if(y)t[t.length]=1f;f++)for(b=Object.keys(this.l[f]),a=0;a - + diff --git a/test/test.js b/test/test.js index 22a5487..0d4bb2d 100644 --- a/test/test.js +++ b/test/test.js @@ -41,6 +41,7 @@ describe('Initialize', function(){ flexsearch_sync = new FlexSearch({ + mode: 'forward', encode: false, async: false, worker: false @@ -48,6 +49,7 @@ describe('Initialize', function(){ flexsearch_async = FlexSearch.create({ + mode: 'forward', encode: false, async: true, worker: false @@ -169,7 +171,7 @@ describe('Initialize', function(){ it('Should have the correct options', function(){ expect(flexsearch_default.async).to.equal(false); - expect(flexsearch_default.mode).to.equal("forward"); + expect(flexsearch_default.mode).to.equal("ngram"); expect(flexsearch_sync.async).to.equal(false); expect(flexsearch_async.async).to.equal(true); expect(flexsearch_strict.mode).to.equal("strict"); @@ -579,7 +581,7 @@ describe('Add (Worker)', function(){ encode: 'icase', mode: 'strict', - async: true, + async: false, worker: 4 });