/** * elasticlunr - http://weixsong.github.io * Lightweight full-text search engine in Javascript for browser search and offline search. - 0.8.8 * * Copyright (C) 2016 Oliver Nightingale * Copyright (C) 2016 Wei Song * MIT Licensed * @license */ !function(){var e=function(t){var n=new e.Index;return n.pipeline.add(e.trimmer,e.stopWordFilter,e.stemmer),t&&t.call(n,n),n};e.version="0.8.8",e.utils={},e.utils.warn=function(e){return function(t){e.console&&console.warn&&console.warn(t)}}(this),e.utils.toString=function(e){return void 0===e||null===e?"":e.toString()},e.EventEmitter=function(){this.events={}},e.EventEmitter.prototype.addListener=function(){var e=Array.prototype.slice.call(arguments),t=e.pop(),n=e;if("function"!=typeof t)throw new TypeError("last argument must be a function");n.forEach(function(e){this.hasHandler(e)||(this.events[e]=[]),this.events[e].push(t)},this)},e.EventEmitter.prototype.removeListener=function(e,t){if(this.hasHandler(e)){var n=this.events[e].indexOf(t);-1!=n&&(this.events[e].splice(n,1),0==this.events[e].length&&delete this.events[e])}},e.EventEmitter.prototype.emit=function(e){if(this.hasHandler(e)){var t=Array.prototype.slice.call(arguments,1);this.events[e].forEach(function(e){e.apply(void 0,t)})}},e.EventEmitter.prototype.hasHandler=function(e){return e in this.events},e.tokenizer=function(t){if(!arguments.length||null===t||void 0===t)return[];if(Array.isArray(t)){var n=t.filter(function(e){return null===e||void 0===e?!1:!0});n=n.map(function(t){return e.utils.toString(t).toLowerCase()});var i=[];return n.forEach(function(t){var n=t.split(e.tokenizer.seperator);i=i.concat(n)},this),i}return t.toString().trim().toLowerCase().split(e.tokenizer.seperator)},e.tokenizer.defaultSeperator=/[\s\-]+/,e.tokenizer.seperator=e.tokenizer.defaultSeperator,e.tokenizer.setSeperator=function(t){null!==t&&void 0!==t&&"object"==typeof t&&(e.tokenizer.seperator=t)},e.tokenizer.resetSeperator=function(){e.tokenizer.seperator=e.tokenizer.defaultSeperator},e.tokenizer.getSeperator=function(){return e.tokenizer.seperator},e.Pipeline=function(){this._queue=[]},e.Pipeline.registeredFunctions={},e.Pipeline.registerFunction=function(t,n){n in e.Pipeline.registeredFunctions&&e.utils.warn("Overwriting existing registered function: "+n),t.label=n,e.Pipeline.registeredFunctions[n]=t},e.Pipeline.getRegisteredFunction=function(t){return t in e.Pipeline.registeredFunctions!=!0?null:e.Pipeline.registeredFunctions[t]},e.Pipeline.warnIfFunctionNotRegistered=function(t){var n=t.label&&t.label in this.registeredFunctions;n||e.utils.warn("Function is not registered with pipeline. This may cause problems when serialising the index.\n",t)},e.Pipeline.load=function(t){var n=new e.Pipeline;return t.forEach(function(t){var i=e.Pipeline.getRegisteredFunction(t);if(!i)throw new Error("Cannot load un-registered function: "+t);n.add(i)}),n},e.Pipeline.prototype.add=function(){var t=Array.prototype.slice.call(arguments);t.forEach(function(t){e.Pipeline.warnIfFunctionNotRegistered(t),this._queue.push(t)},this)},e.Pipeline.prototype.after=function(t,n){e.Pipeline.warnIfFunctionNotRegistered(n);var i=this._queue.indexOf(t);if(-1===i)throw new Error("Cannot find existingFn");this._queue.splice(i+1,0,n)},e.Pipeline.prototype.before=function(t,n){e.Pipeline.warnIfFunctionNotRegistered(n);var i=this._queue.indexOf(t);if(-1===i)throw new Error("Cannot find existingFn");this._queue.splice(i,0,n)},e.Pipeline.prototype.remove=function(e){var t=this._queue.indexOf(e);-1!==t&&this._queue.splice(t,1)},e.Pipeline.prototype.run=function(e){for(var t=[],n=e.length,i=this._queue.length,o=0;n>o;o++){for(var r=e[o],s=0;i>s&&(r=this._queue[s](r,o,e),void 0!==r&&null!==r);s++);void 0!==r&&null!==r&&t.push(r)}return t},e.Pipeline.prototype.reset=function(){this._queue=[]},e.Pipeline.prototype.get=function(){return this._queue},e.Pipeline.prototype.toJSON=function(){return this._queue.map(function(t){return e.Pipeline.warnIfFunctionNotRegistered(t),t.label})},e.Index=function(){this._fields=[],this._ref="id",this.pipeline=new e.Pipeline,this.documentStore=new e.DocumentStore,this.index={},this.eventEmitter=new e.EventEmitter,this._idfCache={},this.on("add","remove","update",function(){this._idfCache={}}.bind(this))},e.Index.prototype.on=function(){var e=Array.prototype.slice.call(arguments);return this.eventEmitter.addListener.apply(this.eventEmitter,e)},e.Index.prototype.off=function(e,t){return this.eventEmitter.removeListener(e,t)},e.Index.load=function(t){t.version!==e.version&&e.utils.warn("version mismatch: current "+e.version+" importing "+t.version);var n=new this;n._fields=t.fields,n._ref=t.ref,n.documentStore=e.DocumentStore.load(t.documentStore),n.pipeline=e.Pipeline.load(t.pipeline),n.index={};for(var i in t.index)n.index[i]=e.InvertedIndex.load(t.index[i]);return n},e.Index.prototype.addField=function(t){return this._fields.push(t),this.index[t]=new e.InvertedIndex,this},e.Index.prototype.setRef=function(e){return this._ref=e,this},e.Index.prototype.saveDocument=function(t){return this.documentStore=new e.DocumentStore(t),this},e.Index.prototype.addDoc=function(t,n){if(t){var n=void 0===n?!0:n,i=t[this._ref];this.documentStore.addDoc(i,t),this._fields.forEach(function(n){var o=this.pipeline.run(e.tokenizer(t[n]));this.documentStore.addFieldLength(i,n,o.length);var r={};o.forEach(function(e){e in r?r[e]+=1:r[e]=1},this);for(var s in r){var u=r[s];u=Math.sqrt(u),this.index[n].addToken(s,{ref:i,tf:u})}},this),n&&this.eventEmitter.emit("add",t,this)}},e.Index.prototype.removeDocByRef=function(t){if(t){if(0==this.documentStore.isDocStored())return void e.utils.warn("remove doc by ref is not allowed, because currectly not storing documents in DocumentStore");if(this.documentStore.hasDoc(t)){var n=this.documentStore.getDoc(t);this.removeDoc(n)}}},e.Index.prototype.removeDoc=function(t,n){if(t){var n=void 0===n?!0:n,i=t[this._ref];this.documentStore.hasDoc(i)&&(this.documentStore.removeDoc(i),this._fields.forEach(function(n){var o=this.pipeline.run(e.tokenizer(t[n]));o.forEach(function(e){this.index[n].removeToken(e,i)},this)},this),n&&this.eventEmitter.emit("remove",t,this))}},e.Index.prototype.update=function(e,t){var t=void 0===t?!0:t;this.removeDoc(e,!1),this.addDoc(e,!1),t&&this.eventEmitter.emit("update",e,this)},e.Index.prototype.idf=function(e,t){var n="@"+t+"/"+e;if(Object.prototype.hasOwnProperty.call(this._idfCache,n))return this._idfCache[n];var i=this.index[t].getDocFreq(e),o=1+Math.log(this.documentStore.length/(i+1));return this._idfCache[n]=o,o},e.Index.prototype.getFields=function(){return this._fields.slice()},e.Index.prototype.search=function(t,n){if(!t)return[];var i=null;null!=n&&(i=JSON.stringify(n));var o=new e.Configuration(i,this.getFields()).get(),r=this.pipeline.run(e.tokenizer(t)),s={};for(var u in o){var a=this.fieldSearch(r,u,o),l=o[u].boost;for(var d in a)a[d]=a[d]*l;for(var d in a)d in s?s[d]+=a[d]:s[d]=a[d]}var c=[];for(var d in s)c.push({ref:d,score:s[d]});return c.sort(function(e,t){return t.score-e.score}),c},e.Index.prototype.fieldSearch=function(e,t,n){var i=n[t].bool,o=n[t].expand,r={},s={};return e.forEach(function(e){var n=[e];1==o&&(n=this.index[t].expandToken(e)),n.forEach(function(n){var i=this.index[t].getDocs(n),o=this.idf(n,t);for(var u in i){var a=this.index[t].getTermFrequency(n,u),l=this.documentStore.getFieldLength(u,t),d=1;0!=l&&(d=1/Math.sqrt(l));var c=1;n!=e?c=.15*(1-(n.length-e.length)/n.length):this.fieldSearchStats(s,n,i);var f=a*o*d*c;u in r?r[u]+=f:r[u]=f}},this)},this),"AND"==i&&(r=this.intersect(r,s,e.length)),r=this.coordNorm(r,s,e.length)},e.Index.prototype.fieldSearchStats=function(e,t,n){for(var i in n)i in e?e[i].push(t):e[i]=[t]},e.Index.prototype.intersect=function(e,t,n){var i={};for(var o in e)o in t&&t[o].length==n&&(i[o]=e[o]);return i},e.Index.prototype.coordNorm=function(e,t,n){for(var i in e)if(i in t){var o=t[i].length;e[i]=e[i]*o/n}return e},e.Index.prototype.toJSON=function(){var t={};return this._fields.forEach(function(e){t[e]=this.index[e].toJSON()},this),{version:e.version,fields:this._fields,ref:this._ref,documentStore:this.documentStore.toJSON(),index:t,pipeline:this.pipeline.toJSON()}},e.Index.prototype.use=function(e){var t=Array.prototype.slice.call(arguments,1);t.unshift(this),e.apply(this,t)},e.DocumentStore=function(e){this._save=null===e||void 0===e?!0:e,this.docs={},this.docInfo={},this.length=0},e.DocumentStore.load=function(e){var t=new this;return t.length=e.length,t.docs=e.docs,t.docInfo=e.docInfo,t._save=e.save,t},e.DocumentStore.prototype.isDocStored=function(){return this._save},e.DocumentStore.prototype.addDoc=function(e,t){this.hasDoc(e)||this.length++,this.docs[e]=this._save===!0?t:null},e.DocumentStore.prototype.getDoc=function(e){return this.hasDoc(e)===!1?null:this.docs[e]},e.DocumentStore.prototype.hasDoc=function(e){return e in this.docs},e.DocumentStore.prototype.removeDoc=function(e){this.hasDoc(e)&&(delete this.docs[e],delete this.docInfo[e],this.length--)},e.DocumentStore.prototype.addFieldLength=function(e,t,n){null!==e&&void 0!==e&&0!=this.hasDoc(e)&&(this.docInfo[e]||(this.docInfo[e]={}),this.docInfo[e][t]=n)},e.DocumentStore.prototype.updateFieldLength=function(e,t,n){null!==e&&void 0!==e&&0!=this.hasDoc(e)&&this.addFieldLength(e,t,n)},e.DocumentStore.prototype.getFieldLength=function(e,t){return null===e||void 0===e?0:e in this.docs&&t in this.docInfo[e]?this.docInfo[e][t]:0},e.DocumentStore.prototype.toJSON=function(){return{docs:this.docs,docInfo:this.docInfo,length:this.length,save:this._save}},e.stemmer=function(){var e={ational:"ate",tional:"tion",enci:"ence",anci:"ance",izer:"ize",bli:"ble",alli:"al",entli:"ent",eli:"e",ousli:"ous",ization:"ize",ation:"ate",ator:"ate",alism:"al",iveness:"ive",fulness:"ful",ousness:"ous",aliti:"al",iviti:"ive",biliti:"ble",logi:"log"},t={icate:"ic",ative:"",alize:"al",iciti:"ic",ical:"ic",ful:"",ness:""},n="[^aeiou]",i="[aeiouy]",o=n+"[^aeiouy]*",r=i+"[aeiou]*",s="^("+o+")?"+r+o,u="^("+o+")?"+r+o+"("+r+")?$",a="^("+o+")?"+r+o+r+o,l="^("+o+")?"+i,d=new RegExp(s),c=new RegExp(a),f=new RegExp(u),h=new RegExp(l),p=/^(.+?)(ss|i)es$/,v=/^(.+?)([^s])s$/,g=/^(.+?)eed$/,m=/^(.+?)(ed|ing)$/,y=/.$/,x=/(at|bl|iz)$/,w=new RegExp("([^aeiouylsz])\\1$"),I=new RegExp("^"+o+i+"[^aeiouwxy]$"),S=/^(.+?[^aeiou])y$/,b=/^(.+?)(ational|tional|enci|anci|izer|bli|alli|entli|eli|ousli|ization|ation|ator|alism|iveness|fulness|ousness|aliti|iviti|biliti|logi)$/,E=/^(.+?)(icate|ative|alize|iciti|ical|ful|ness)$/,D=/^(.+?)(al|ance|ence|er|ic|able|ible|ant|ement|ment|ent|ou|ism|ate|iti|ous|ive|ize)$/,F=/^(.+?)(s|t)(ion)$/,_=/^(.+?)e$/,P=/ll$/,k=new RegExp("^"+o+i+"[^aeiouwxy]$"),z=function(n){var i,o,r,s,u,a,l;if(n.length<3)return n;if(r=n.substr(0,1),"y"==r&&(n=r.toUpperCase()+n.substr(1)),s=p,u=v,s.test(n)?n=n.replace(s,"$1$2"):u.test(n)&&(n=n.replace(u,"$1$2")),s=g,u=m,s.test(n)){var z=s.exec(n);s=d,s.test(z[1])&&(s=y,n=n.replace(s,""))}else if(u.test(n)){var z=u.exec(n);i=z[1],u=h,u.test(i)&&(n=i,u=x,a=w,l=I,u.test(n)?n+="e":a.test(n)?(s=y,n=n.replace(s,"")):l.test(n)&&(n+="e"))}if(s=S,s.test(n)){var z=s.exec(n);i=z[1],n=i+"i"}if(s=b,s.test(n)){var z=s.exec(n);i=z[1],o=z[2],s=d,s.test(i)&&(n=i+e[o])}if(s=E,s.test(n)){var z=s.exec(n);i=z[1],o=z[2],s=d,s.test(i)&&(n=i+t[o])}if(s=D,u=F,s.test(n)){var z=s.exec(n);i=z[1],s=c,s.test(i)&&(n=i)}else if(u.test(n)){var z=u.exec(n);i=z[1]+z[2],u=c,u.test(i)&&(n=i)}if(s=_,s.test(n)){var z=s.exec(n);i=z[1],s=c,u=f,a=k,(s.test(i)||u.test(i)&&!a.test(i))&&(n=i)}return s=P,u=c,s.test(n)&&u.test(n)&&(s=y,n=n.replace(s,"")),"y"==r&&(n=r.toLowerCase()+n.substr(1)),n};return z}(),e.Pipeline.registerFunction(e.stemmer,"stemmer"),e.stopWordFilter=function(t){return t&&e.stopWordFilter.stopWords[t]!==!0?t:void 0},e.clearStopWords=function(){e.stopWordFilter.stopWords={}},e.addStopWords=function(t){null!=t&&Array.isArray(t)!==!1&&t.forEach(function(t){e.stopWordFilter.stopWords[t]=!0},this)},e.resetStopWords=function(){e.stopWordFilter.stopWords=e.defaultStopWords},e.defaultStopWords={"":!0,a:!0,able:!0,about:!0,across:!0,after:!0,all:!0,almost:!0,also:!0,am:!0,among:!0,an:!0,and:!0,any:!0,are:!0,as:!0,at:!0,be:!0,because:!0,been:!0,but:!0,by:!0,can:!0,cannot:!0,could:!0,dear:!0,did:!0,"do":!0,does:!0,either:!0,"else":!0,ever:!0,every:!0,"for":!0,from:!0,get:!0,got:!0,had:!0,has:!0,have:!0,he:!0,her:!0,hers:!0,him:!0,his:!0,how:!0,however:!0,i:!0,"if":!0,"in":!0,into:!0,is:!0,it:!0,its:!0,just:!0,least:!0,let:!0,like:!0,likely:!0,may:!0,me:!0,might:!0,most:!0,must:!0,my:!0,neither:!0,no:!0,nor:!0,not:!0,of:!0,off:!0,often:!0,on:!0,only:!0,or:!0,other:!0,our:!0,own:!0,rather:!0,said:!0,say:!0,says:!0,she:!0,should:!0,since:!0,so:!0,some:!0,than:!0,that:!0,the:!0,their:!0,them:!0,then:!0,there:!0,these:!0,they:!0,"this":!0,tis:!0,to:!0,too:!0,twas:!0,us:!0,wants:!0,was:!0,we:!0,were:!0,what:!0,when:!0,where:!0,which:!0,"while":!0,who:!0,whom:!0,why:!0,will:!0,"with":!0,would:!0,yet:!0,you:!0,your:!0},e.stopWordFilter.stopWords=e.defaultStopWords,e.Pipeline.registerFunction(e.stopWordFilter,"stopWordFilter"),e.trimmer=function(e){if(null===e||void 0===e)throw new Error("token should not be undefined");return e.replace(/^\W+/,"").replace(/\W+$/,"")},e.Pipeline.registerFunction(e.trimmer,"trimmer"),e.InvertedIndex=function(){this.root={docs:{},df:0},this.length=0},e.InvertedIndex.load=function(e){var t=new this;return t.root=e.root,t.length=e.length,t},e.InvertedIndex.prototype.addToken=function(e,t,n){for(var n=n||this.root,i=0;i<=e.length-1;){var o=e[i];o in n||(n[o]={docs:{},df:0}),i+=1,n=n[o]}var r=t.ref;n.docs[r]?n.docs[r]={tf:t.tf}:(n.docs[r]={tf:t.tf},n.df+=1,this.length+=1)},e.InvertedIndex.prototype.hasToken=function(e){if(!e)return!1;for(var t=this.root,n=0;n0&&t.push(e);for(var i in n)"docs"!==i&&"df"!==i&&this.expandToken(e+i,t,n[i]);return t},e.InvertedIndex.prototype.toJSON=function(){return{root:this.root,length:this.length}},e.Configuration=function(t,n){var t=t||"";if(void 0==n||null==n)throw new Error("fields should not be null");this.config={};var i;try{i=JSON.parse(t),this.buildUserConfig(i,n)}catch(o){e.utils.warn("user configuration parse failed, will use default configuration"),this.buildDefaultConfig(n)}},e.Configuration.prototype.buildDefaultConfig=function(e){this.reset(),e.forEach(function(e){this.config[e]={boost:1,bool:"OR",expand:!1}},this)},e.Configuration.prototype.buildUserConfig=function(t,n){var i="OR",o=!1;if(this.reset(),"bool"in t&&(i=t.bool||i),"expand"in t&&(o=t.expand||o),"fields"in t)for(var r in t.fields)if(n.indexOf(r)>-1){var s=t.fields[r],u=o;void 0!=s.expand&&(u=s.expand),this.config[r]={boost:s.boost||1,bool:s.bool||i,expand:u}}else e.utils.warn("field name in user configuration not found in index instance fields");else this.addAllFields2UserConfig(i,o,n)},e.Configuration.prototype.addAllFields2UserConfig=function(e,t,n){n.forEach(function(n){this.config[n]={boost:1,bool:e,expand:t}},this)},e.Configuration.prototype.get=function(){return this.config},e.Configuration.prototype.reset=function(){this.config={}},function(e,t){"function"==typeof define&&define.amd?define(t):"object"==typeof exports?module.exports=t():e.elasticlunr=t()}(this,function(){return e})}();