diff --git a/README.md b/README.md index 4575c71..ade75bb 100644 --- a/README.md +++ b/README.md @@ -828,11 +828,11 @@ The required memory for the index depends on several options:
h;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 k&&(a=a[f],a=a[c]||(a[c]=[]),a[a.length]=h)}return f||b[c]}function x(a){for(var b="",c="",h="",n=0;n a?1:0a?-1:0b&&(c=c.slice(0,b)));return c}function A(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(),h;(h=a.c.shift())||0===h;){var d=a.h[h];switch(d[0]){case z.add:a.add(d[1],d[2]);break;case z.update:a.update(d[1],d[2]);break;case z.remove:a.remove(d[1])}a.h[h]=null;delete a.h[h];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};e.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};e.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};e.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]=[z.add,a,b],A(this),this;b=this.encode(b);if(!b.length)return this;if("ngram"===this.mode){var c=b;var h=[];if(c)for(var d=0,k=0,f=0,e="",r=c.length,m=0;m g;l--)t=m.substring(g,l),q(f,h,t,a,b,d);break;default:if(g=q(f,h,m,a,b,d),k&&1 d)for(g=f[10],p=h._ctx[m]||(h._ctx[m]={}),m=g[m]||(g[m]=[{},{},{},{},{},{},{},{},{},{}]),g=r-k,l=r+k,0>g&&(g=0), +l>e-1&&(l=e-1);g<=l;g++)g!==r&&q(m,p,c[g],a,b,d)}}this.a[a]="1";this.u=!1}return this};e.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]=[z.update,a,b],A(this),this;this.remove(a);b&&this.add(a,b)}return this};e.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]=[z.remove,a],A(this),this;for(b=0;10>b;b++)for(var c=Object.keys(this.g[b]),d=0;d =e;A--)if(w=(p?u[t]:this.g)[A][q])x[B++]=w,z=!0;if(z)m[m.length]=1f;f++)for(b=Object.keys(this.g[f]),a=0;a + + + + Matching Test + + + + + + + + + + + + + +Relevance Scoring Comparison
+Indexed Text: "Gulliver's Travels" (Swift Jonathan 1726)
+
+ ++++
++ +Query +flexsearch +bulksearch +elasticlunr +lunr +wade +fuse +jssearch +jsii +bm25 ++ +"without breach of modesty" +wait ... +wait ... +wait ... +wait ... +wait ... +wait ... +wait ... +wait ... +wait ... ++ +"went softly stream" +wait ... +wait ... +wait ... +wait ... +wait ... +wait ... +wait ... +wait ... +wait ... ++ +"i already observed" +wait ... +wait ... +wait ... +wait ... +wait ... +wait ... +wait ... +wait ... +wait ... ++ +"let a of his" +wait ... +wait ... +wait ... +wait ... +wait ... +wait ... +wait ... +wait ... +wait ... ++ +"take that to the rocks" +wait ... +wait ... +wait ... +wait ... +wait ... +wait ... +wait ... +wait ... +wait ... ++ +"bignes of splaknuk" +wait ... +wait ... +wait ... +wait ... +wait ... +wait ... +wait ... +wait ... +wait ... ++ +"matematikal musikal instruments" +wait ... +wait ... +wait ... +wait ... +wait ... +wait ... +wait ... +wait ... +wait ... ++ +"lalkon the camberlayhn" +wait ... +wait ... +wait ... +wait ... +wait ... +wait ... +wait ... +wait ... +wait ... +
+Note: Open console and type e.g. data[493] + + + diff --git a/test/test.js b/test/test.js index 979a33e..ac45656 100644 --- a/test/test.js +++ b/test/test.js @@ -209,14 +209,6 @@ describe('Add (Sync)', function(){ flexsearch_extra.add(3, " - "); expect(flexsearch_extra.length).to.equal(0); - - flexsearch_extra.add(4, "Thomas"); - flexsearch_extra.add(5, "Arithmetic"); - flexsearch_extra.add(6, "Mahagoni"); - - expect(flexsearch_extra.search("tomass")).to.include(4); - expect(flexsearch_extra.search("arytmetik")).to.include(5); - expect(flexsearch_extra.search("mahagony")).to.include(6); }); }); @@ -230,6 +222,14 @@ describe('Search (Sync)', function(){ expect(flexsearch_sync.search("foo foo")).to.have.members([0, 1]); expect(flexsearch_sync.search("foo foo")).to.have.members([0, 1]); + + flexsearch_extra.add(4, "Thomas"); + flexsearch_extra.add(5, "Arithmetic"); + flexsearch_extra.add(6, "Mahagoni"); + + expect(flexsearch_extra.search("tomass")).to.include(4); + expect(flexsearch_extra.search("arytmetik")).to.include(5); + expect(flexsearch_extra.search("mahagony")).to.include(6); }); it('Should have been limited', function(){ @@ -914,6 +914,63 @@ describe('Options', function(){ }); }); +// ------------------------------------------------------------------------ +// Relevance Tests +// ------------------------------------------------------------------------ + +describe('Relevance', function(){ + + it('Should have been sorted by relevance properly', function(){ + + var index = new FlexSearch({ + encode: 'advanced', + mode: 'strict' + }); + + index.add(0, "1 2 3 2 4 1 5 3"); + index.add(1, "zero one two three four five six seven eight nine ten"); + index.add(2, "four two zero one three ten five seven eight six nine"); + + expect(index.search("1")).to.have.members([0]); + expect(index.search("one")).to.have.members([1, 2]); + expect(index.search("one two")).to.have.members([1, 2]); + expect(index.search("four one")).to.have.members([1, 2]); + + var index = new FlexSearch({ + encode: 'advanced', + mode: 'strict', + threshold: 5, + depth: 3 + }); + + index.add(0, "1 2 3 2 4 1 5 3"); + index.add(1, "zero one two three four five six seven eight nine ten"); + index.add(2, "four two zero one three ten five seven eight six nine"); + + expect(index.search("1")).to.have.members([0]); + expect(index.search("one")).to.have.members([1, 2]); + expect(index.search("one two")).to.have.members([1, 2]); + expect(index.search("four one")).to.have.members([1, 2]); + + var index = new FlexSearch({ + encode: 'extra', + mode: 'ngram', + threshold: 5, + depth: 3 + }); + + index.add(0, "1 2 3 2 4 1 5 3"); + index.add(1, "zero one two three four five six seven eight nine ten"); + index.add(2, "four two zero one three ten five seven eight six nine"); + + expect(index.search("1 3 4")).to.have.members([0]); + expect(index.search("1 5 3 4")).to.have.members([0]); + expect(index.search("one")).to.have.members([1, 2]); + expect(index.search("one two")).to.have.members([1, 2]); + expect(index.search("four one")).to.have.members([1, 2]); + }); +}); + // ------------------------------------------------------------------------ // Feature Tests // ------------------------------------------------------------------------