1
0
mirror of https://github.com/nextapps-de/flexsearch.git synced 2025-08-30 01:00:11 +02:00

added support for export and import indexes

This commit is contained in:
Thomas Wilkerling
2021-05-18 13:26:59 +02:00
parent 3e89963192
commit 41398573ca
7 changed files with 427 additions and 110 deletions

View File

@@ -5,24 +5,27 @@
* Licence: Apache-2.0
* https://github.com/nextapps-de/flexsearch
*/
(function(){var q;function u(a){for(var b=Array(a),c=0;c<a;c++)b[c]=B();return b}function B(){return Object.create(null)}function aa(a,b){return b.length-a.length};var C=/[\W_]+/;function ba(a){if(a=a.toLowerCase())if(a&&this.B&&(a=D(a,this.B)),this.D&&1<a.length&&(a=D(a,this.D)),a&&(C||""===C)&&(a=a.split(C),this.filter)){for(var b=this.filter,c=a.length,d=[],f=0,h=0;f<c;f++){var e=a[f];e&&!b[e]&&(d[h++]=e)}a=d}return a};function ca(a){for(var b=B(),c=0,d=a.length;c<d;c++)b[a[c]]=1;return b}function E(a,b){for(var c=Object.keys(a),d=c.length,f=[],h="",e=0,g=0,k,l;g<d;g++)k=c[g],(l=a[k])?(f[e++]=new RegExp(b?"(?!\\b)"+k+"(\\b|_)":k,"g"),f[e++]=l):h+=(h?"|":"")+k;h&&(f[e++]=new RegExp(b?"(?!\\b)("+h+")(\\b|_)":"("+h+")","g"),f[e]="");return f}function D(a,b){for(var c=0,d=b.length;c<d&&(a=a.replace(b[c],b[c+1]),a);c+=2);return a};var F={},G={};var H=Promise;Object.assign||(Object.assign=function(){for(var a=arguments,b=a.length,c=a[0],d=1,f,h,e;d<b;d++){f=a[d];h=Object.keys(f);e=h.length;for(var g=0,k;g<e;g++)k=h[g],c[k]=f[k]}return c});if(!H){var I=function(a){this.g=null;var b=this;a(function(c){b.g&&(b.g(c),b=b.g=null)})};I.prototype.then=function(a){this.g=a};H=I};function J(a,b,c){return K.call(this,this.add,arguments)}function L(a,b,c){return K.call(this,this.append,arguments)}function M(a,b,c){return K.call(this,this.search,arguments)}function N(a,b,c){return K.call(this,this.update,arguments)}function O(a,b){return K.call(this,this.remove,arguments)}function K(a,b){for(var c,d=0;d<b.length;d++)if("function"===typeof b[d]){c=b[d];delete b[d];break}var f=this;d=new H(function(h){setTimeout(function(){h(a.apply(f,b))})});return c?(d.then(c),this):d};function P(a,b,c){var d=a.length,f=[],h=B(),e=0;c&&(c=[]);for(var g=0;g<d;g++){for(var k=a[g],l=k.length,r=B(),p=!g,n=0;n<l;n++){var m=k[n],t=m.length;if(t){c&&(c[n]=[]);for(var v=0,x=0,w;v<t;v++)if(w=m[v],!g)r[w]=1;else if(h[w]){if(g===d-1){if(f[e++]=w,e===b)return f}else c&&x<b&&(c[n][x++]=w),r[w]=1;p=!0}}}if(!p&&!c)return[];h=r}if(c)for(a=c.length-1;0<=a;a--)if(h=(d=c[a])&&d.length){if(e+h>=b)return f.concat(d.slice(0,b-e));f=f.concat(d);e+=h}return f};function Q(a){this.h=!0!==a&&a;this.cache=B();this.g=[]}function R(a,b,c){"object"===typeof a&&(a=a.query);var d=this.cache.get(a);d||(d=this.search(a,b,c),this.cache.set(a,d));return d}Q.prototype.set=function(a,b){if(!this.cache[a]){var c=this.g.length;c===this.h?delete this.cache[this.g[c-1]]:c++;for(--c;0<c;c--)this.g[c]=this.g[c-1];this.g[0]=a}this.cache[a]=b};
Q.prototype.get=function(a){var b=this.cache[a];if(this.h&&b&&(a=this.g.indexOf(a))){var c=this.g[a-1];this.g[a-1]=this.g[a];this.g[a]=c}return b};var da={memory:{charset:"latin:extra",o:3,l:3,s:!1,m:"memory"},performance:{threshold:8,l:3,context:{depth:1,v:!0}},match:{charset:"latin:extra",G:"full",o:3},score:{charset:"latin:advanced",threshold:1,context:{depth:3,v:!0}},"default":{o:3,threshold:0,depth:3}};function ea(a){if("string"===typeof a)a=da[a];else{var b=a.preset;b&&(a=Object.assign({},da[b],a))}return a};function S(a,b){if(!(this instanceof S))return new S(a);if(a){a=ea(a);var c=a.charset;var d=a.lang;"string"===typeof c&&(-1===c.indexOf(":")&&(c+=":default"),c=G[c]);"string"===typeof d&&(d=F[d])}else a={};var f,h=a.context||{};this.encode=a.encode||c&&c.encode||ba;this.register=b||B();var e=a.resolution||9;var g=a.threshold||0;g>=e&&(g=e-1);this.o=e;this.threshold=g;this.A=b=c&&c.G||a.tokenize||"strict";this.depth="strict"===b&&h.depth;this.v=T(h.bidirectional,!0);this.m=f="memory"===a.optimize;
this.s=T(a.fastupdate,!0);this.l=a.minlength||1;this.g=f?u(e-g):B();e=h.resolution||e;g=h.threshold||g;g>=e&&(g=e-1);this.h=e;this.j=g;this.i=f?u(e-g):B();this.C=c&&c.C||a.rtl;this.B=(b=a.matcher||d&&d.B)&&E(b,!1);this.D=(b=a.stemmer||d&&d.D)&&E(b,!0);this.filter=(b=a.filter||d&&d.filter)&&ca(b);this.cache=(b=a.cache)&&new Q(b);this.u=(b=a.worker)&&new Worker("worker.js",{type:"module"});b&&(this.u.onmessage=function(k){console.log(k.data)},this.u.postMessage({H:"register",options:a}))}
(function(){'use strict';var q;var t=/[\W_]+/;function aa(a){if(a=a.toLowerCase())if(a&&this.B&&(a=B(a,this.B)),this.D&&1<a.length&&(a=B(a,this.D)),a&&(t||""===t)&&(a=a.split(t),this.filter)){for(var b=this.filter,c=a.length,d=[],e=0,h=0;e<c;e++){var f=a[e];f&&!b[f]&&(d[h++]=f)}a=d}return a};function C(a){for(var b=Array(a),c=0;c<a;c++)b[c]=D();return b}function D(){return Object.create(null)}function ba(a,b){return b.length-a.length};function ca(a){for(var b=D(),c=0,d=a.length;c<d;c++)b[a[c]]=1;return b}function F(a,b){for(var c=Object.keys(a),d=c.length,e=[],h="",f=0,g=0,k,l;g<d;g++)k=c[g],(l=a[k])?(e[f++]=new RegExp(b?"(?!\\b)"+k+"(\\b|_)":k,"g"),e[f++]=l):h+=(h?"|":"")+k;h&&(e[f++]=new RegExp(b?"(?!\\b)("+h+")(\\b|_)":"("+h+")","g"),e[f]="");return e}function B(a,b){for(var c=0,d=b.length;c<d&&(a=a.replace(b[c],b[c+1]),a);c+=2);return a};var G={},H={};var I=Promise;Object.assign||(Object.assign=function(){for(var a=arguments,b=a.length,c=a[0],d=1,e,h,f;d<b;d++){e=a[d];h=Object.keys(e);f=h.length;for(var g=0,k;g<f;g++)k=h[g],c[k]=e[k]}return c});if(!I){var J=function(a){this.g=null;var b=this;a(function(c){b.g&&(b.g(c),b=b.g=null)})};J.prototype.then=function(a){this.g=a};I=J};function K(a,b,c){return L.call(this,this.add,arguments)}function M(a,b,c){return L.call(this,this.append,arguments)}function N(a,b,c){return L.call(this,this.search,arguments)}function P(a,b,c){return L.call(this,this.update,arguments)}function Q(a,b){return L.call(this,this.remove,arguments)}function L(a,b){for(var c,d=0;d<b.length;d++)if("function"===typeof b[d]){c=b[d];delete b[d];break}var e=this;d=new I(function(h){setTimeout(function(){h(a.apply(e,b))})});return c?(d.then(c),this):d};function da(a,b,c,d){var e=a.length,h=[],f=D(),g=0;d&&(d=[]);for(var k=0;k<e;k++){for(var l=a[k],n=l.length,r=D(),p=!k,m=0;m<n;m++){var u=l[m],w=u.length;if(w){d&&(d[m]=[]);for(var v=0,x=0,y;v<w;v++)if(y=u[v],!k)r[y]=1;else if(f[y]){if(k===e-1)if(c)c--;else{if(h[g++]=y,g===b)return h}else d&&x<b&&(d[m][x++]=y),r[y]=1;p=!0}}}if(!p&&!d)return[];f=r}if(d)for(a=d.length-1;0<=a;a--)if(e=(c=d[a])&&c.length){if(g+e>=b)return h.concat(c.slice(0,b-g));h=h.concat(c);g+=e}return h};function R(a){this.h=!0!==a&&a;this.cache=D();this.g=[]}function ea(a,b,c){"object"===typeof a&&(a=a.query);var d=this.cache.get(a);d||(d=this.search(a,b,c),this.cache.set(a,d));return d}R.prototype.set=function(a,b){if(!this.cache[a]){var c=this.g.length;c===this.h?delete this.cache[this.g[c-1]]:c++;for(--c;0<c;c--)this.g[c]=this.g[c-1];this.g[0]=a}this.cache[a]=b};
R.prototype.get=function(a){var b=this.cache[a];if(this.h&&b&&(a=this.g.indexOf(a))){var c=this.g[a-1];this.g[a-1]=this.g[a];this.g[a]=c}return b};var fa={memory:{charset:"latin:extra",u:3,s:3,i:!1,l:"memory"},performance:{threshold:8,s:3,context:{depth:1,v:!0}},match:{charset:"latin:extra",G:"full",u:3},score:{charset:"latin:advanced",threshold:1,context:{depth:3,v:!0}},"default":{u:3,threshold:0,depth:3}};function ha(a){if("string"===typeof a)a=fa[a];else{var b=a.preset;b&&(a=Object.assign({},fa[b],a))}return a};function ia(a,b,c,d,e,h){setTimeout(function(){var f=a(c,JSON.stringify(h));f&&f.then?f.then(function(){b.export(a,b,c,d,e+1)}):b.export(a,b,c,d,e+1)})};function S(a,b){if(!(this instanceof S))return new S(a);if(a){a=ha(a);var c=a.charset;var d=a.lang;"string"===typeof c&&(-1===c.indexOf(":")&&(c+=":default"),c=H[c]);"string"===typeof d&&(d=G[d])}else a={};var e,h=a.context||{};this.encode=a.encode||c&&c.encode||aa;this.register=b||D();var f=a.resolution||9;var g=a.threshold||0;g>=f&&(g=f-1);this.u=f;this.threshold=g;this.A=b=c&&c.G||a.tokenize||"strict";this.depth="strict"===b&&h.depth;this.v=T(h.bidirectional,!0);this.l=e="memory"===a.optimize;
this.i=T(a.fastupdate,!0);this.s=a.minlength||1;this.g=e?C(f-g):D();f=h.resolution||f;g=h.threshold||g;g>=f&&(g=f-1);this.j=f;this.m=g;this.h=e?C(f-g):D();this.C=c&&c.C||a.rtl;this.B=(b=a.matcher||d&&d.B)&&F(b,!1);this.D=(b=a.stemmer||d&&d.D)&&F(b,!0);this.filter=(b=a.filter||d&&d.filter)&&ca(b);this.cache=(b=a.cache)&&new R(b);this.o=(b=a.worker)&&new Worker("worker.js",{type:"module"});b&&(this.o.onmessage=function(k){console.log(k.data)},this.o.postMessage({H:"register",options:a}))}
function T(a,b){return"undefined"!==typeof a?a:b}q=S.prototype;q.append=function(a,b){return this.add(a,b,!0)};
q.add=function(a,b,c){if(this.register[a]&&!c)return this.update(a,b);if(b&&(a||0===a)){b=this.encode(b);var d=b.length;if(d){for(var f=this.depth,h=this.o-this.threshold,e=B(),g=B(),k=0;k<d;k++){var l=b[this.C?d-1-k:k],r=l.length;if(l&&r>=this.l&&(f||!e[l])){var p=Math.min(this.o/d*k|0,k);if(p<h){var n="";switch(this.A){case "full":if(3<r){for(var m=0;m<r;m++){var t=m?Math.min(p/2+this.o/r*m/2|0,p+m):p;if(t<h)for(var v=r;v>m;v--)n=l.substring(m,v),n.length>=this.l&&U(this,e,n,t,a,c)}break}case "reverse":if(2<
r){for(m=r-1;0<m;m--)n=l[m]+n,n.length>=this.l&&U(this,e,n,p,a,c);n=""}case "forward":if(1<r)for(m=0;m<r;m++)n+=l[m],n.length>=this.l&&U(this,e,n,p,a,c);break;default:if(U(this,e,l,p,a,c),f&&1<d&&k<d-1)for(r=this.h-this.j,p=B(),n=l,m=Math.min(f+1,d-k),t=p[n]=1;t<m;t++)if((l=b[this.C?d-1-k-t:k+t])&&l.length>=this.l&&!p[l]){if(p[l]=1,v=Math.min((this.h-m)/d*k+t|0,k+(t-1)),v<r){var x=this.v&&l>n;U(this,g,x?n:l,v,a,c,x?l:n)}}else m=Math.min(m+1,d-k)}}}}this.s||(this.register[a]=1)}}return this};
function U(a,b,c,d,f,h,e){var g=e?a.i:a.g;if(!b[c]||e&&!b[c][e])a.m&&(g=g[d]),e?(b[c]||(b[c]=B()),b[c][e]=1,g=g[e]||(g[e]=B())):b[c]=1,g=g[c]||(g[c]=[]),a.m||(g=g[d]||(g[d]=[])),h&&-1!==g.indexOf(f)||(g[g.length]=f,a.s&&(a=a.register[f]||(a.register[f]=[]),a[a.length]=g))}
q.search=function(a,b,c){"object"===typeof a?(c=a,a=c.query):"object"===typeof b&&(c=b);var d=[],f=this.threshold;if(c){b=c.limit;f=T(c.threshold,f);var h=c.context;var e=c.suggest}if(a){a=this.encode(a);var g=a.length;if(1<g){c=B();for(var k=[],l=0,r=0,p;l<g;l++)if((p=a[l])&&p.length>=this.l&&!c[p])if(this.m||e||this.g[p])k[r++]=p,c[p]=1;else return d;a=k;g=a.length}}if(!g)return d;b||(b=100);c=this.o-f;f=this.h-f;h=this.depth&&1<g&&!1!==h;k=0;if(h){var n=a[0];k=1}else 1<g&&a.sort(aa);for(;k<g;k++){r=
a[k];h?(l=fa(this,d,e,f,b,2===g,r,n),e&&!1===l&&d.length||(n=r)):l=fa(this,d,e,c,b,1===g,r);if(l)return l;if(e&&k===g-1){l=d.length;if(!l){if(h){h=0;k=-1;continue}return d}if(1===l)return d=d[0],d=1===d.length?d[0]:[].concat.apply([],d),d.length>b?d.slice(0,b):d}}return P(d,b,e)};function ha(a,b,c,d){c?(d=d&&b>c,a=(a=a[d?b:c])&&a[d?c:b]):a=a[b];return a}
function fa(a,b,c,d,f,h,e,g){var k=[],l=g?a.i:a.g;a.m||(l=ha(l,e,g,a.v));if(l){var r=0;d=Math.min(l.length,d);for(var p=0,n=0,m;p<d&&!(m=l[p],a.m&&(m=ha(m,e,g,a.v)),m&&(k[r++]=m,h&&(n+=m.length,n>=f)));p++);if(r){if(h)return k=1===r?k[0]:[].concat.apply([],k),k.length>f?k.slice(0,f):k;b[b.length]=k;return}}return!c&&k}q.contain=function(a){return!!this.register[a]};q.update=function(a,b){return this.remove(a).add(a,b)};
q.remove=function(a,b){var c=this.register[a];if(c){if(this.s)for(var d=0,f;d<c.length;d++)f=c[d],f.splice(f.indexOf(a),1);else V(this.g,a,this.o-this.threshold,this.m),this.depth&&V(this.i,a,this.h-this.j,this.m);b||delete this.register[a];if(this.cache)for(b=this.cache,c=0;c<b.g.length;c++)f=b.g[c],d=b.cache[f],-1!==d.indexOf(a)&&(b.g.splice(c--,1),delete b.cache[f])}return this};
function V(a,b,c,d,f){var h=0;if(a.constructor===Array)if(f)b=a.indexOf(b),-1!==b?1<a.length&&(a.splice(b,1),h++):h++;else{f=Math.min(a.length,c);for(var e=0,g;e<f;e++)if(g=a[e])h=V(g,b,c,d,f),d||h||delete a[e]}else for(e in a)(h=V(a[e],b,c,d,f))||delete a[e];return h}q.searchCache=R;q.addAsync=J;q.appendAsync=L;q.searchAsync=M;q.updateAsync=N;q.removeAsync=O;function W(a){if(!(this instanceof W))return new W(a);var b;a||(a={});this.F=[];this.h=[];this.i=[];this.register=B();this.key=(b=a.key)&&X(b,this.i)||"id";b=a.fastupdate;this.s="undefined"!==typeof b?b:!0;this.store=(b=a.store)&&B();this.j=b&&!0!==b&&[];this.u=(b=a.tag)&&X(b,this.i);this.g=b&&B();this.cache=(b=a.cache)&&new Q(b);a.cache=!1;b=a;a=B();var c=b.doc;if("string"===typeof c)c=[c];else if(c.constructor!==Array){var d=c;c=Object.keys(c)}for(var f=0,h,e;f<c.length;f++)h=c[f],d?(e=d[h],e="object"===
typeof e?Object.assign({},b,e):b):e=b,a[h]=new S(e,this.register),this.F[f]=X(h,this.i),this.h[f]=h;if(this.j)for(d=b.store,"string"===typeof d&&(d=[d]),b=0;b<d.length;b++)this.j[b]=X(d[b],this.i);this.A=a}function X(a,b){for(var c=a.split(":"),d=0,f=0;f<c.length;f++)a=c[f],0<=a.indexOf("[]")&&(a=a.substring(0,a.length-2))&&(b[d]=!0),a&&(c[d++]=a);d<c.length&&(c.length=d);return 1<d?c:c[0]}function ia(a,b){if("string"===typeof b)a=a[b];else for(var c=0;a&&c<b.length;c++)a=a[b[c]];return a}
function Y(a,b,c,d,f){a=a[f];if(d===c.length-1)b[f]=a;else if(a)if(a.constructor===Array)for(b=b[f]=Array(a.length),f=0;f<a.length;f++)Y(a,b,c,d,f);else b=b[f]||(b[f]=B()),f=c[++d],Y(a,b,c,d,f)}function Z(a,b,c,d,f,h,e,g){a=a[e];if(d===b.length-1){if(a.constructor===Array){if(c[d]){for(b=0;b<a.length;b++)f.add(h,a[b],!0);return}a=a.join(" ")}f.add(h,a,g)}else if(a)if(a.constructor===Array)for(e=0;e<a.length;e++)Z(a,b,c,d,f,h,e,g);else e=b[++d],Z(a,b,c,d,f,h,e,g)}q=W.prototype;
q.add=function(a,b,c){"object"===typeof a&&(b=a,a=ia(b,this.key));if(b&&(a||0===a)){if(this.register[a])return this.update(a,b);for(var d=0,f,h;d<this.h.length;d++)h=this.h[d],f=this.F[d],"string"===typeof f&&(f=[f]),Z(b,f,this.i,0,this.A[h],a,f[0],c);if(this.u){d=ia(b,this.u);f=B();"string"===typeof d&&(d=[d]);h=0;for(var e;h<d.length;h++)if(e=d[h],!f[e]&&(f[e]=1,e=this.g[e]||(this.g[e]=[]),!c||-1===e.indexOf(a)))if(e[e.length]=a,this.s){var g=this.register[a]||(this.register[a]=[]);g[g.length]=
e}}if(this.store){if(this.j){var k=B();for(c=0;c<this.j.length;c++)d=this.j[c],"string"===typeof d?k[d]=b[d]:Y(b,k,d,0,d[0])}this.store[a]=k||b}}return this};q.append=function(a,b){return this.add(a,b,!0)};q.update=function(a,b){return this.remove(a).add(a,b)};
q.remove=function(a){"object"===typeof a&&(a=a[this.key]);if(this.register[a]){for(var b=0;b<this.h.length;b++)this.A[this.h[b]].remove(a,!0);if(this.u&&!this.s)for(var c in this.g){b=this.g[c];var d=b.indexOf(a);-1!==d&&(1<b.length?b.splice(d,1):delete this.g[c])}this.store&&delete this.store[a];delete this.register[a]}return this};
q.search=function(a,b,c){"object"===typeof a?(c=a,a=c.query):"object"===typeof b&&(c=b);var d=[],f,h=0;if(c){var e=(f=c.pluck)||c.field;var g=c.tag;var k=c.enrich;var l="and"===c.bool;b=c.limit;if(e)if("string"===typeof e)e=[e];else if(e.constructor!==Array){var r=e;e=Object.keys(e)}if(g&&("string"===typeof g&&(g=[g]),!a)){for(f=0;f<g.length;f++){e=g[f];var p=b||100;(c=(a=this.g[e])&&a.length)?(c>p&&(a=a.slice(0,p)),k&&this.store&&(a=ja.call(this,a)),e={tag:e,result:a}):e=void 0;e&&(d[d.length]=e,
h++)}return h?d:[]}}e||(e=this.h);l=l&&(1<e.length||g&&1<g.length);p=[];for(var n=0,m,t,v;n<e.length;n++){t=e[n];m=this.A[t].search(a,b,r?r[t]:c);v=m.length;if(g&&v){var x=p[n]=[];t=[];var w=0;l&&(t[0]=[m]);for(var y=0,A,z;y<g.length;y++)if(A=g[y],v=(z=this.g[A])&&z.length)t[t.length]=l?[z]:z,x[w++]=A;if(w){if(l)t=P(t,b||100);else{w=m;m=B();v=B();x=[];for(y=0;y<w.length;y++)m[w[y]]=1;for(w=0;w<t.length;w++)for(y=t[w],A=0;A<y.length;A++)z=y[A],m[z]&&!v[z]&&(v[z]=1,x[x.length]=z);t=x}m=t;v=m.length}}if(v)d[h++]=
m;else if(l)return[]}if(!h)return[];if(f&&(!k||!this.store))return d[0];for(b=0;b<e.length;b++){h=e[b];g=d[b];g.length&&k&&this.store&&(g=ja.call(this,g));if(f)return g;d[b]=g={field:h,result:g};this.u&&(g.tag=p[b])}return d};function ja(a){for(var b=Array(a.length),c=0,d;c<a.length;c++)d=a[c],b[c]={key:d,doc:this.store[d]};return b}q.contain=function(a){return!!this.register[a]};q.get=function(a){return this.store[a]};q.set=function(a,b){this.store[a]=b;return this};q.searchCache=R;q.addAsync=J;
q.appendAsync=L;q.searchAsync=M;q.updateAsync=N;q.removeAsync=O;window.FlexSearch={Index:S,Document:W,registerCharset:function(a,b){G[a]=b},registerLanguage:function(a,b){F[a]=b}};}).call(this);
q.add=function(a,b,c){if(this.register[a]&&!c)return this.update(a,b);if(b&&(a||0===a)){b=this.encode(b);var d=b.length;if(d){for(var e=this.depth,h=this.u-this.threshold,f=D(),g=D(),k=0;k<d;k++){var l=b[this.C?d-1-k:k],n=l.length;if(l&&n>=this.s&&(e||!f[l])){var r=Math.min(this.u/d*k|0,k);if(r<h){var p="";switch(this.A){case "full":if(3<n){for(var m=0;m<n;m++){var u=m?Math.min(r/2+this.u/n*m/2|0,r+m):r;if(u<h)for(var w=n;w>m;w--)p=l.substring(m,w),p.length>=this.s&&U(this,f,p,u,a,c)}break}case "reverse":if(2<
n){for(m=n-1;0<m;m--)p=l[m]+p,p.length>=this.s&&U(this,f,p,r,a,c);p=""}case "forward":if(1<n)for(m=0;m<n;m++)p+=l[m],p.length>=this.s&&U(this,f,p,r,a,c);break;default:if(U(this,f,l,r,a,c),e&&1<d&&k<d-1)for(n=this.j-this.m,r=D(),p=l,m=Math.min(e+1,d-k),u=r[p]=1;u<m;u++)if((l=b[this.C?d-1-k-u:k+u])&&l.length>=this.s&&!r[l]){if(r[l]=1,w=Math.min((this.j-m)/d*k+u|0,k+(u-1)),w<n){var v=this.v&&l>p;U(this,g,v?p:l,w,a,c,v?l:p)}}else m=Math.min(m+1,d-k)}}}}this.i||(this.register[a]=1)}}return this};
function U(a,b,c,d,e,h,f){var g=f?a.h:a.g;if(!b[c]||f&&!b[c][f])a.l&&(g=g[d]),f?(b[c]||(b[c]=D()),b[c][f]=1,g=g[f]||(g[f]=D())):b[c]=1,g=g[c]||(g[c]=[]),a.l||(g=g[d]||(g[d]=[])),h&&-1!==g.indexOf(e)||(g[g.length]=e,a.i&&(a=a.register[e]||(a.register[e]=[]),a[a.length]=g))}
q.search=function(a,b,c){"object"===typeof a?(c=a,a=c.query):"object"===typeof b&&(c=b);var d=[],e=this.threshold,h=0;if(c){b=c.limit;h=c.offset||0;e=T(c.threshold,e);var f=c.context;var g=c.suggest}if(a){a=this.encode(a);var k=a.length;if(1<k){c=D();for(var l=[],n=0,r=0,p;n<k;n++)if((p=a[n])&&p.length>=this.s&&!c[p])if(this.l||g||this.g[p])l[r++]=p,c[p]=1;else return d;a=l;k=a.length}}if(!k)return d;b||(b=100);c=this.u-e;e=this.j-e;f=this.depth&&1<k&&!1!==f;l=0;if(f){var m=a[0];l=1}else 1<k&&a.sort(ba);
for(;l<k;l++){r=a[l];f?(n=ja(this,d,g,e,b,2===k,r,m),g&&!1===n&&d.length||(m=r)):n=ja(this,d,g,c,b,1===k,r);if(n)return n;if(g&&l===k-1){n=d.length;if(!n){if(f){f=0;l=-1;continue}return d}if(1===n)return d=d[0],d=1===d.length?d[0]:[].concat.apply([],d),d.length>b?d.slice(0,b):d}}return da(d,b,h,g)};function ka(a,b,c,d){c?(d=d&&b>c,a=(a=a[d?b:c])&&a[d?c:b]):a=a[b];return a}
function ja(a,b,c,d,e,h,f,g){var k=[],l=g?a.h:a.g;a.l||(l=ka(l,f,g,a.v));if(l){var n=0;d=Math.min(l.length,d);for(var r=0,p=0,m;r<d&&!(m=l[r],a.l&&(m=ka(m,f,g,a.v)),m&&(k[n++]=m,h&&(p+=m.length,p>=e)));r++);if(n){if(h)return k=1===n?k[0]:[].concat.apply([],k),k.length>e?k.slice(0,e):k;b[b.length]=k;return}}return!c&&k}q.contain=function(a){return!!this.register[a]};q.update=function(a,b){return this.remove(a).add(a,b)};
q.remove=function(a,b){var c=this.register[a];if(c){if(this.i)for(var d=0,e;d<c.length;d++)e=c[d],e.splice(e.indexOf(a),1);else V(this.g,a,this.u-this.threshold,this.l),this.depth&&V(this.h,a,this.j-this.m,this.l);b||delete this.register[a];if(this.cache)for(b=this.cache,c=0;c<b.g.length;c++)e=b.g[c],d=b.cache[e],-1!==d.indexOf(a)&&(b.g.splice(c--,1),delete b.cache[e])}return this};
function V(a,b,c,d,e){var h=0;if(a.constructor===Array)if(e)b=a.indexOf(b),-1!==b?1<a.length&&(a.splice(b,1),h++):h++;else{e=Math.min(a.length,c);for(var f=0,g;f<e;f++)if(g=a[f])h=V(g,b,c,d,e),d||h||delete a[f]}else for(f in a)(h=V(a[f],b,c,d,e))||delete a[f];return h}q.searchCache=ea;q.addAsync=K;q.appendAsync=M;q.searchAsync=N;q.updateAsync=P;q.removeAsync=Q;
q.export=function(a,b,c,d,e){switch(e||(e=0)){case 0:var h="reg";if(this.i){var f=D();for(var g in this.register)f[g]=1}else f=this.register;break;case 1:h="cfg";f={doc:0,opt:this.l?1:0};break;case 2:h="map";f=this.g;break;case 3:h="ctx";f=this.h;break;default:return}ia(a,b||this,c?c+"."+h:h,d,e,f);return!0};
q.import=function(a,b){if(b)switch("string"===typeof b&&(b=JSON.parse(b)),a){case "cfg":this.l=!!b.opt;break;case "reg":this.i=!1;this.register=b;break;case "map":this.g=b;break;case "ctx":this.h=b}};function W(a){if(!(this instanceof W))return new W(a);var b;a||(a={});this.F=[];this.g=[];this.m=[];this.register=D();this.key=(b=a.key)&&X(b,this.m)||"id";b=a.fastupdate;this.i="undefined"!==typeof b?b:!0;this.store=(b=a.store)&&D();this.o=b&&!0!==b&&[];this.A=(b=a.tag)&&X(b,this.m);this.h=b&&D();this.cache=(b=a.cache)&&new R(b);a.cache=!1;b=a;a=D();var c=b.doc;if("string"===typeof c)c=[c];else if(c.constructor!==Array){var d=c;c=Object.keys(c)}for(var e=0,h,f;e<c.length;e++)h=c[e],d?(f=d[h],f="object"===
typeof f?Object.assign({},b,f):b):f=b,a[h]=new S(f,this.register),this.F[e]=X(h,this.m),this.g[e]=h;if(this.o)for(d=b.store,"string"===typeof d&&(d=[d]),b=0;b<d.length;b++)this.o[b]=X(d[b],this.m);this.j=a}function X(a,b){for(var c=a.split(":"),d=0,e=0;e<c.length;e++)a=c[e],0<=a.indexOf("[]")&&(a=a.substring(0,a.length-2))&&(b[d]=!0),a&&(c[d++]=a);d<c.length&&(c.length=d);return 1<d?c:c[0]}function la(a,b){if("string"===typeof b)a=a[b];else for(var c=0;a&&c<b.length;c++)a=a[b[c]];return a}
function Y(a,b,c,d,e){a=a[e];if(d===c.length-1)b[e]=a;else if(a)if(a.constructor===Array)for(b=b[e]=Array(a.length),e=0;e<a.length;e++)Y(a,b,c,d,e);else b=b[e]||(b[e]=D()),e=c[++d],Y(a,b,c,d,e)}function Z(a,b,c,d,e,h,f,g){a=a[f];if(d===b.length-1){if(a.constructor===Array){if(c[d]){for(b=0;b<a.length;b++)e.add(h,a[b],!0);return}a=a.join(" ")}e.add(h,a,g)}else if(a)if(a.constructor===Array)for(f=0;f<a.length;f++)Z(a,b,c,d,e,h,f,g);else f=b[++d],Z(a,b,c,d,e,h,f,g)}q=W.prototype;
q.add=function(a,b,c){"object"===typeof a&&(b=a,a=la(b,this.key));if(b&&(a||0===a)){if(this.register[a])return this.update(a,b);for(var d=0,e,h;d<this.g.length;d++)h=this.g[d],e=this.F[d],"string"===typeof e&&(e=[e]),Z(b,e,this.m,0,this.j[h],a,e[0],c);if(this.A){d=la(b,this.A);e=D();"string"===typeof d&&(d=[d]);h=0;for(var f;h<d.length;h++)if(f=d[h],!e[f]&&(e[f]=1,f=this.h[f]||(this.h[f]=[]),!c||-1===f.indexOf(a)))if(f[f.length]=a,this.i){var g=this.register[a]||(this.register[a]=[]);g[g.length]=
f}}if(this.store){if(this.o){var k=D();for(c=0;c<this.o.length;c++)d=this.o[c],"string"===typeof d?k[d]=b[d]:Y(b,k,d,0,d[0])}this.store[a]=k||b}}return this};q.append=function(a,b){return this.add(a,b,!0)};q.update=function(a,b){return this.remove(a).add(a,b)};
q.remove=function(a){"object"===typeof a&&(a=a[this.key]);if(this.register[a]){for(var b=0;b<this.g.length&&(this.j[this.g[b]].remove(a,!0),!this.i);b++);if(this.A&&!this.i)for(var c in this.h){b=this.h[c];var d=b.indexOf(a);-1!==d&&(1<b.length?b.splice(d,1):delete this.h[c])}this.store&&delete this.store[a];delete this.register[a]}return this};
q.search=function(a,b,c){"object"===typeof a?(c=a,a=c.query):"object"===typeof b&&(c=b);var d=[],e=[],h,f=0;if(c){var g=(h=c.pluck)||c.field;var k=c.tag;var l=this.store&&c.enrich;var n="and"===c.bool;b=c.limit;var r=c.offset;if(g)if("string"===typeof g)g=[g];else if(g.constructor!==Array){var p=g;g=Object.keys(g)}if(k&&("string"===typeof k&&(k=[k]),!a)){for(e=0;e<k.length;e++){h=k[e];a=b||100;c=r||0;if((p=(g=this.h[h])&&g.length-c)&&0<p){if(p>a||c)g=g.slice(c,c+a);l&&(g=ma.call(this,g));h={tag:h,
result:g}}else h=void 0;h&&(d[d.length]=h,f++)}return f?d:[]}}g||(g=this.g);n=n&&(1<g.length||k&&1<k.length);for(var m=0,u,w,v;m<g.length;m++){w=g[m];u=this.j[w].search(a,b,p?p[w]:c);v=u.length;if(k&&v){var x=[],y=0;n&&(x[0]=[u]);for(var A=0,z;A<k.length;A++)if(v=k[A],v=(z=this.h[v])&&z.length)y++,x[x.length]=n?[z]:z;if(y){if(n)x=da(x,b||100,r||0);else{v=u;u=D();y=D();A=[];for(z=0;z<v.length;z++)u[v[z]]=1;for(v=0;v<x.length;v++){z=x[v];for(var O=0,E;O<z.length;O++)E=z[O],u[E]&&!y[E]&&(y[E]=1,A[A.length]=
E)}x=A}u=x;v=u.length}}if(v)e[f]=w,d[f++]=u;else if(n)return[]}if(!f)return[];if(h&&(!l||!this.store))return d[0];for(b=0;b<e.length;b++){k=d[b];k.length&&l&&(k=ma.call(this,k));if(h)return k;d[b]={field:e[b],result:k}}return d};function ma(a){for(var b=Array(a.length),c=0,d;c<a.length;c++)d=a[c],b[c]={key:d,doc:this.store[d]};return b}q.contain=function(a){return!!this.register[a]};q.get=function(a){return this.store[a]};q.set=function(a,b){this.store[a]=b;return this};q.searchCache=ea;
q.addAsync=K;q.appendAsync=M;q.searchAsync=N;q.updateAsync=P;q.removeAsync=Q;q.export=function(a,b,c,d,e){e||(e=0);d||(d=0);if(d<this.g.length){var h=this.g[d],f=this.j[h];b=this;setTimeout(function(){f.export(a,b,e?h.replace(":","-"):"",d,e++)||(d++,e=1,b.export(a,b,h,d,e))})}else{switch(e){case 1:c="tag";var g=this.h;break;case 2:c="store";g=this.store;break;default:return}ia(a,this,c,d,e,g)}};
q.import=function(a,b){if(b)switch("string"===typeof b&&(b=JSON.parse(b)),a){case "tag":this.h=b;break;case "reg":this.i=!1;this.register=b;a=0;for(var c;a<this.g.length;a++)c=this.j[this.g[a]],c.register=b,c.i=!1;break;case "store":this.store=b;break;default:a=a.split("."),c=a[0],a=a[1],c&&a&&this.j[c].import(a,b)}};window.FlexSearch={Index:S,Document:W,registerCharset:function(a,b){H[a]=b},registerLanguage:function(a,b){G[a]=b}};}).call(this);

View File

@@ -67,11 +67,11 @@ Source Code v0.7.0-pre-alpha available here:<br>
What is not included yet but comes soon?
- Worker (almost done)
- Pagination (not implemented yet)
- Export/Import (not implemented yet)
- Offset-Pagination (not implemented yet)
- ~~Export/Import (not implemented yet)~~
- Engines (almost done)
- Tags (almost done)
- Bundles: Light, Compact, Full (almost done)
- ~~Tags (almost done)~~
- ~~Bundles: Light, Compact, Full (almost done)~~
- Test Automation (needs to be migrated)
- Benchmark Suite (almost done)
@@ -79,6 +79,7 @@ What will be dropped?
- Where-Clause
- Index Information `index.info()`
- Paging Cursor (gets replaced by offset)
## Builtin Profiles
@@ -1029,6 +1030,70 @@ When you perform a field search through all fields then this task is perfectly b
The main thread has to solve a last intersection calculation as before. On this step it needs to apply "bool" and "paging" logic, also "suggestions". I'm thinking about to move the workload from the main tread to another worker, so all computations will perform in background completely, the index from the main thread just holds the configuration and its document store (when using store).
## Export / Import
### Export
The export has slightly changed. The export now consist of several smaller parts, instead of just one large bulk. You need to pass a callback function which has 2 arguments "key" and "data". This callback function is called by each part, e.g.:
```js
index.export(function(key, data){
// you need to store both the key and the data!
// e.g. use the key for the filename and save your data
localStorage.setItem(key, data);
});
```
Exporting data to the localStorage isn't really a good practice, but if size is not a concern than use it if you like. The export primarily exists for the usage in Node.js or to store indexes you want to delegate from a server to the client.
> The size of the export corresponds to the memory consumption of the library. To reduce export size you have to use a configuration which has less memory footprint (use the table at the bottom to get information about configs and its memory allocation).
When your save routine runs asynchronously you have to return a promise:
```js
index.export(function(key, data){
return new Promise(function(resolve){
// do the saving as async
resolve();
});
});
```
> You cannot export the additional table for the "fastupdate" feature. These table exists of references and when stored they fully get serialized and becomes too large. The lib will handle these automatically for you. When importing data, the index automatically disables "fastupdate".
### Import
Before you can import data, you need to create your index first. For document indexes provide the same document descriptor you used when export the data. This configuration isn't stored in the export.
```js
var index = new Index({ ... });
```
To import the data just pass a key and data:
```js
index.import(key, localStorage.getItem(key));
```
You need to import every key! Otherwise, your index does not work. You need to store the keys from the export and use this keys for the import (the order of the keys can differ).
This is just for demonstration and is not recommended, because you might have other keys in your localStorage which aren't supported as an import:
```js
var keys = Object.keys(localStorage);
for(let i = 0, key; i < keys.length; i++){
key = keys[i];
index.import(key, localStorage.getItem(key));
}
```
## Engines (Custom Indexes)
FlexSearch supports custom engines for the index. The default engine is `Index` which extends the abstract class `Engine` under the hood. These abstract class provides some basic methods like cache and async handler. It needs to just implement the 5 standard functions `add`, `append`, `update`, `remove` and `search`. Then you can use this new engine fully integrated into FlexSearch workflow (Document Handler, Worker, Async Handler, Cache, Document Storage, Paging). That it also some demonstration of the powerful flexibility of extending FlexSearch.

View File

@@ -6,12 +6,13 @@
* https://github.com/nextapps-de/flexsearch
*/
import { SUPPORT_ASYNC, SUPPORT_CACHE, SUPPORT_STORE, SUPPORT_TAGS } from "./config.js";
import { SUPPORT_ASYNC, SUPPORT_CACHE, SUPPORT_SERIALIZE, SUPPORT_STORE, SUPPORT_TAGS } from "./config.js";
import Index from "./index.js";
import Cache, { searchCache } from "./cache.js";
import { create_object } from "./common.js";
import { addAsync, appendAsync, removeAsync, searchAsync, updateAsync } from "./async.js";
import { intersect, intersect_union } from "./intersect.js";
import { exportDocument, importDocument } from "./serialize.js";
/**
* @param {Object=} options
@@ -55,7 +56,6 @@ function Document(options){
options["cache"] = false;
}
/** @private */
this.index = parse_descriptor.call(this, options);
}
@@ -73,7 +73,7 @@ function parse_option(value, default_value){
function parse_descriptor(options){
const index = create_object();
let field = options["doc"]; // options["document"]
let field = options["doc"];
let field_options;
if(typeof field === "string"){
@@ -377,11 +377,20 @@ Document.prototype.remove = function(id){
for(let i = 0; i < this.field.length; i++){
this.index[this.field[i]].remove(id, true);
if(this.fastupdate){
// when fastupdate was enabled all ids will
// be already cleanup after the first loop
break;
}
}
if(SUPPORT_TAGS && this.tag){
// when fastupdate was enabled the id will be already cleanup by the index
// when fastupdate was enabled the id will
// be already cleanup by the index
if(!this.fastupdate){
@@ -428,9 +437,9 @@ Document.prototype.search = function(query, limit, options){
options = limit;
}
let result = [];
let result = [], result_field = [];
let pluck, enrich;
let field, field_options, tag, bool, count = 0;
let field, field_options, tag, bool, offset, count = 0;
if(options){
@@ -440,6 +449,7 @@ Document.prototype.search = function(query, limit, options){
enrich = SUPPORT_STORE && this.store && options["enrich"];
bool = options["bool"] === "and";
limit = options["limit"];
offset = options["offset"];
if(field){
@@ -468,7 +478,7 @@ Document.prototype.search = function(query, limit, options){
for(let i = 0, res; i < tag.length; i++){
res = get_tag.call(this, tag[i], limit || 100, enrich);
res = get_tag.call(this, tag[i], limit || 100, offset || 0, enrich);
if(res){
@@ -485,32 +495,19 @@ Document.prototype.search = function(query, limit, options){
field || (field = this.field);
bool = bool && ((field.length > 1) || (tag && (tag.length > 1)));
let found_tag = [];
// TODO solve this in one loop below
for(let i = 0, res, key, item, len; i < field.length; i++){
for(let i = 0, res, key, len; i < field.length; i++){
key = field[i];
// if(field_options){
//
// item = field_options[key];
//
// // inherit options also when search? it is just for laziness, Object.assign() has a cost
// //item = typeof item === "object" ? Object.assign({}, options, item) : options;
// }
// else{
//
// item = options;
// }
// inherit options also when search? it is just for laziness, Object.assign() has a cost
res = this.index[key].search(query, limit, field_options ? field_options[key] : options);
len = res.length;
if(tag && len){
const field_tag = found_tag[i] = [];
const arr = [];
let count = 0;
@@ -529,8 +526,8 @@ Document.prototype.search = function(query, limit, options){
if(len){
count++;
arr[arr.length] = bool ? [res] : res;
field_tag[count++] = key;
}
}
@@ -538,7 +535,7 @@ Document.prototype.search = function(query, limit, options){
if(bool){
res = intersect(arr, limit || 100);
res = intersect(arr, limit || 100, offset || 0);
}
else{
@@ -551,18 +548,12 @@ Document.prototype.search = function(query, limit, options){
if(len){
result_field[count] = key;
result[count++] = res;
}
else if(bool){
//if(!len){
return [];
//}
// add a pseudo relevance index for the intersection
// used when squash the results on boolean "and"
//res = [res];
}
}
@@ -573,20 +564,6 @@ Document.prototype.search = function(query, limit, options){
return [];
}
// squash the results on boolean "and"?
// if(bool){
//
// limit || (limit = 100);
//
// if(enrich && this.store){
//
// return apply_enrich.call(this, intersect(result, limit));
// }
//
// return intersect(result, limit);
// }
if(pluck && (!enrich || !this.store)){
// fast path optimization
@@ -594,9 +571,8 @@ Document.prototype.search = function(query, limit, options){
return result[0];
}
for(let i = 0, res, key; i < field.length; i++){
for(let i = 0, res; i < result_field.length; i++){
key = field[i];
res = result[i];
if(res.length){
@@ -612,16 +588,11 @@ Document.prototype.search = function(query, limit, options){
return res;
}
result[i] = res = {
result[i] = {
"field": key,
"field": result_field[i],
"result": res
};
if(tag){
res["tag"] = found_tag[i];
}
}
return result;
@@ -631,16 +602,16 @@ Document.prototype.search = function(query, limit, options){
* @this Document
*/
function get_tag(key, limit, enrich){
function get_tag(key, limit, offset, enrich){
let res = this.tagindex[key];
let len = res && res.length;
let len = res && (res.length - offset);
if(len){
if(len && (len > 0)){
if(len > limit){
if((len > limit) || offset){
res = res.slice(0, limit);
res = res.slice(offset, offset + limit);
}
if(enrich){
@@ -710,3 +681,9 @@ if(SUPPORT_ASYNC){
Document.prototype.updateAsync = updateAsync;
Document.prototype.removeAsync = removeAsync;
}
if(SUPPORT_SERIALIZE){
Document.prototype.export = exportDocument;
Document.prototype.import = importDocument;
}

View File

@@ -6,7 +6,14 @@
* https://github.com/nextapps-de/flexsearch
*/
import { SUPPORT_PRESET, SUPPORT_CACHE, SUPPORT_ASYNC, SUPPORT_WORKER, SUPPORT_SUGGESTION } from "./config.js";
import {
SUPPORT_PRESET,
SUPPORT_CACHE,
SUPPORT_ASYNC,
SUPPORT_WORKER,
SUPPORT_SUGGESTION,
SUPPORT_SERIALIZE
} from "./config.js";
import { encode as default_encoder } from "./lang/latin/default.js";
import { create_object, create_object_array, concat, sort_by_length_down } from "./common.js";
import { pipeline, init_stemmer_or_matcher, init_filter } from "./lang.js";
@@ -15,6 +22,7 @@ import { addAsync, appendAsync, removeAsync, searchAsync, updateAsync } from "./
import { intersect } from "./intersect.js";
import Cache, { searchCache } from "./cache.js";
import apply_preset from "./presets.js";
import { exportIndex, importIndex } from "./serialize.js";
/**
* @param {Object=} options
@@ -377,11 +385,12 @@ Index.prototype.search = function(query, limit, options){
let result = [];
let length;
let threshold = this.threshold, context, suggest;
let threshold = this.threshold, context, suggest, offset = 0;
if(options){
limit = options["limit"];
offset = options["offset"] || 0;
threshold = parse_option(options["threshold"], threshold);
context = options["context"];
suggest = SUPPORT_SUGGESTION && options["suggest"];
@@ -512,12 +521,14 @@ Index.prototype.search = function(query, limit, options){
result = concat(result);
}
// TODO apply offset
return result.length > limit ? result.slice(0, limit) : result;
}
}
}
return intersect(result, limit, suggest);
return intersect(result, limit, offset, suggest);
};
function get_array(arr, term, keyword, bidirectional){
@@ -579,6 +590,8 @@ Index.prototype.add_result = function(result, suggest, resolution, limit, just_o
if(tmp){
// TODO apply offset
// keep score (sparse array):
//word_arr[x] = arr;
@@ -603,6 +616,8 @@ Index.prototype.add_result = function(result, suggest, resolution, limit, just_o
if(just_one_loop){
// TODO apply offset
// fast path optimization
if(count === 1){
@@ -766,3 +781,9 @@ if(SUPPORT_ASYNC){
Index.prototype.updateAsync = updateAsync;
Index.prototype.removeAsync = removeAsync;
}
if(SUPPORT_SERIALIZE){
Index.prototype.export = exportIndex;
Index.prototype.import = importIndex;
}

View File

@@ -3,32 +3,16 @@ import { create_object, concat } from "./common.js";
/**
* @param arrays
* @param limit
* @param offset
* @param {boolean|Array=} suggest
* @returns {Array}
*/
export function intersect(arrays, limit, suggest) {
export function intersect(arrays, limit, offset, suggest) {
const length = arrays.length;
let result = [];
// if(!length){
//
// return result;
// }
//
// if(length === 1){
//
// const tmp = arrays[0];
//
// if(tmp.length === 1){
//
// return tmp[0];
// }
//
// return concat(tmp);
// }
// arrays.sort(function(a, b){
//
// return a.length - b.length;
@@ -78,6 +62,12 @@ export function intersect(arrays, limit, suggest) {
if(x === (length - 1)){
if(offset){
offset--;
}
else{
result[count++] = id;
if(count === limit){
@@ -87,6 +77,7 @@ export function intersect(arrays, limit, suggest) {
return result;
}
}
}
else{
if(suggest && (count_suggest < limit)){
@@ -118,6 +109,8 @@ export function intersect(arrays, limit, suggest) {
res = suggest[i];
len = res && res.length;
// TODO apply offset
if(len){
if(count + len >= limit){

249
src/serialize.js Normal file
View File

@@ -0,0 +1,249 @@
import Index from "./index.js";
import Document from "./document.js";
import { create_object } from "./common.js";
function async(callback, self, key, index_doc, index, data){
setTimeout(function(){
const res = callback(key, JSON.stringify(data));
// await isn't supported by ES5
if(res && res["then"]){
res["then"](function(){
self.export(callback, self, key, index_doc, index + 1);
})
}
else{
self.export(callback, self, key, index_doc, index + 1);
}
});
}
/**
* @this Index
*/
export function exportIndex(callback, self, field, index_doc, index){
let key, data;
switch(index || (index = 0)){
case 0:
key = "reg";
// fastupdate isn't supported by export
if(this.fastupdate){
data = create_object();
for(let key in this.register){
data[key] = 1;
}
}
else{
data = this.register;
}
break;
case 1:
key = "cfg";
data = {
"doc": 0,
"opt": this.optimize ? 1 : 0
};
break;
case 2:
key = "map";
data = this.map;
break;
case 3:
key = "ctx";
data = this.ctx;
break;
default:
return;
}
async(callback, self || this, field ? field + "." + key : key, index_doc, index, data);
return true;
}
/**
* @this Index
*/
export function importIndex(key, data){
if(!data){
return;
}
if(typeof data === "string"){
data = JSON.parse(data);
}
switch(key){
case "cfg":
this.optimize = !!data["opt"];
break;
case "reg":
// fastupdate isn't supported by import
this.fastupdate = false;
this.register = data;
break;
case "map":
this.map = data;
break;
case "ctx":
this.ctx = data;
break;
}
}
/**
* @this Document
*/
export function exportDocument(callback, self, field, index_doc, index){
index || (index = 0);
index_doc || (index_doc = 0);
if(index_doc < this.field.length){
const field = this.field[index_doc];
const idx = this.index[field];
self = this;
setTimeout(function(){
if(!idx.export(callback, self, index ? field.replace(":", "-") : "", index_doc, index++)){
index_doc++;
index = 1;
self.export(callback, self, field, index_doc, index);
}
});
}
else{
let key, data;
switch(index){
case 1:
key = "tag";
data = this.tagindex;
break;
case 2:
key = "store";
data = this.store;
break;
// case 3:
//
// key = "reg";
// data = this.register;
// break;
default:
return;
}
async(callback, this, key, index_doc, index, data);
}
}
/**
* @this Document
*/
export function importDocument(key, data){
if(!data){
return;
}
if(typeof data === "string"){
data = JSON.parse(data);
}
switch(key){
case "tag":
this.tagindex = data;
break;
case "reg":
// fastupdate isn't supported by import
this.fastupdate = false;
this.register = data;
for(let i = 0, index; i < this.field.length; i++){
index = this.index[this.field[i]];
index.register = data;
index.fastupdate = false;
}
break;
case "store":
this.store = data;
break;
default:
key = key.split(".");
const field = key[0];
key = key[1];
if(field && key){
this.index[field].import(key, data);
}
}
}

View File

@@ -1,4 +1,4 @@
import { SUPPORT_ASYNC, SUPPORT_DOCUMENT, SUPPORT_CACHE } from "./config.js";
import { SUPPORT_ASYNC, SUPPORT_DOCUMENT, SUPPORT_CACHE, SUPPORT_SERIALIZE } from "./config.js";
import Document from "./document.js";
import Index from "./index.js";
import { registerCharset, registerLanguage } from "./global.js";
@@ -40,6 +40,15 @@ if(SUPPORT_ASYNC){
/** @export */ Index.prototype.removeAsync;
}
if(SUPPORT_SERIALIZE){
/** @export */ Index.prototype.export;
/** @export */ Index.prototype.import;
/** @export */ Document.prototype.export;
/** @export */ Document.prototype.import;
}
window["FlexSearch"] = {
"Index": Index,