Files

8 lines
5.4 KiB
JavaScript

/**
* Bundled by jsDelivr using Rollup v2.79.2 and Terser v5.37.0.
* Original file: /npm/@orama/plugin-qps@3.0.8/dist/index.js
*
* Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files
*/
import{index as e,isVectorType as t,getVectorSize as r}from"./components.js";import{radix as n,vector as o,bkd as s,flat as a,avl as i,bool as l}from"./trees.js";import{setIntersection as u}from"./internals.js";function c(e,u,f){for(let d of Object.entries(u)){let u=d[0],m=d[1],p=`${f}${f?".":""}${u}`;if("object"!=typeof m||Array.isArray(m))if(t(m))e.searchableProperties.push(p),e.searchablePropertiesWithTypes[p]=m,e.vectorIndexes[p]={type:"Vector",node:new o.VectorIndex(r(m)),isArray:!1};else{let t=/\[/.test(m);switch(m){case"boolean":case"boolean[]":e.indexes[p]={type:"Bool",node:new l.BoolNode,isArray:t};break;case"number":case"number[]":e.indexes[p]={type:"AVL",node:new i.AVLTree(0,[]),isArray:t};break;case"string":case"string[]":e.indexes[p]={type:"Radix",node:new n.RadixTree,isArray:t};break;case"enum":case"enum[]":e.indexes[p]={type:"Flat",node:new a.FlatTree,isArray:t};break;case"geopoint":e.indexes[p]={type:"BKD",node:new s.BKDTree,isArray:t};break;default:throw new Error("INVALID_SCHEMA_TYPE: "+p)}e.searchableProperties.push(p),e.searchablePropertiesWithTypes[p]=m}else c(e,m,p)}}function f(e,t){return h(e)+1<<20|(p(e)|1<<t)}function d(e,t,r,n,o,s,a){let i=e.split(/\.|\?|!/),l=0,u=0;for(let e of i){let i=a.tokenize(e,s,n);for(let e of i){u++,r[e]||(r[e]=0);let n=Math.min(l,20);r.tokenQuantums[o][e]=f(r.tokenQuantums[o][e],n),t.insert(e,o)}i.length>1&&l++}r.tokensLength.set(o,u)}function m(e){let t=e.tokens,r=e.radixNode,n=e.exact,o=e.tolerance,s=e.stats,a=e.boostPerProp,i=e.resultMap,l=e.whereFiltersIDs,u=s.tokensLength,c=s.tokenQuantums,f={term:"",exact:n,tolerance:o},d={},m=t.length;for(let e=0;e<m;e++){let n=t[e];f.term=n;let o=r.find(f);d={...d,...o}}let y=Object.keys(d),k=y.length;for(let e=0;e<k;e++){let r=y[e],n=d[r],o=n.length,s=t.includes(r);for(let e=0;e<o;e++){let t=n[e];if(l&&!l.has(t))continue;let o=u.get(t),f=c[t][r],d=h(f),m=p(f),y=(d*d/o+(s?1:0))*a;if(!i.has(t)){i.set(t,[y,m]);continue}let k=i.get(t),b=k[0]+2*x(k[1]&m)+y;k[0]=b,k[1]=k[1]|m}}}function p(e){return 1048575&e}function h(e){return e>>20}function x(e){let t=0;do{1&e&&++t}while(e>>=1);return t}function y(e,t,r,n,o,s,a){let i=a.tokensLength,l=a.tokenQuantums,u=o.tokenize(e,s,r);for(let e of u)t.removeDocumentByWord(e,n,!0);i.delete(n),delete l[n]}var k=new n.RadixNode("","",!1),b={tokenQuantums:{},tokensLength:new Map};function g(e,t,r,n,o,s,a,i,l,u,c){let f=new Map,d={tokens:r.tokenize(t,n),radixNode:k,exact:s,tolerance:a,stats:b,boostPerProp:0,all:f,resultMap:f,whereFiltersIDs:c},p=o.length;for(let t=0;t<p;t++){let r=o[t],n=e.stats[r],s=i[r]??1;d.radixNode=e.indexes[r].node,d.stats=n,d.boostPerProp=s,m(d)}let h=Array.from(f),x=h.length,y=[];for(let e=0;e<x;e++){let t=h[e],r=t[0],n=t[1][0];y.push([r,n])}return y}function w(){return{name:"qps",getComponents:t=>function(t){return{index:{create:function(){let e={indexes:{},vectorIndexes:{},searchableProperties:[],searchablePropertiesWithTypes:{},stats:{}};return c(e,t,""),e},insert:function(t,r,n,o,s,a,i,l,u,c){if("string"!==i&&"string[]"!==i)return e.insert(t,r,n,o,s,a,i,l,u,c);r.stats[n]||(r.stats[n]={tokenQuantums:{},tokensLength:new Map});let f=r.stats[n],m=r.indexes[n].node;if(f.tokenQuantums[s]={},Array.isArray(a))for(let e of a)d(e,m,f,n,s,l,u);else d(a,m,f,n,s,l,u)},remove:function(t,r,n,o,s,a,i,l,u,c){if("string"!==i&&"string[]"!==i)return e.remove(t,r,n,o,s,a,i,l,u,c);let f=r.stats[n],d=r.indexes[n].node;if(Array.isArray(a))for(let e of a)y(e,d,n,s,u,l,f);else y(a,d,n,s,u,l,f)},insertDocumentScoreParameters:()=>{throw new Error},insertTokenScoreParameters:()=>{throw new Error},removeDocumentScoreParameters:()=>{throw new Error},removeTokenScoreParameters:()=>{throw new Error},calculateResultScores:()=>{throw new Error},search:g,searchByWhereClause:function(t,r,n,o){let s,a=Object.entries(n).filter((([e])=>"Radix"===t.indexes[e].type));if(0===a.length)return e.searchByWhereClause(t,r,n,o);for(let[e,n]of a){let a=r.tokenize(n,o),i=t.indexes[e].node,l=new Set;for(let e of a){let t=i.find({term:e,exact:!0})[e];if(t)for(let e of t)l.add(e)}s=s?u(s,l):l}if(0===Object.entries(n).filter((([e])=>"Radix"!==t.indexes[e].type)).length)return s;let i=e.searchByWhereClause(t,r,n,o);return u(s,i)},getSearchableProperties:function(e){return e.searchableProperties},getSearchablePropertiesWithTypes:function(e){return e.searchablePropertiesWithTypes},load:function(t,r){let o=e.load(t,r[0]),s=r[1],a={...o.indexes,...Object.fromEntries(s.radixTrees.map((([e,t,r,o])=>[e,{node:n.RadixNode.fromJSON(o),isArray:t,type:r}])))};return{...o,indexes:a,stats:Object.fromEntries(s.stats.map((([e,{tokenQuantums:t,tokensLength:r}])=>[e,{tokenQuantums:t,tokensLength:new Map(r)}])))}},save:function(t){let r=t,n=Object.entries(r.indexes).filter((([,{type:e}])=>"Radix"!==e));return[e.save({...r,indexes:Object.fromEntries(n)}),{radixTrees:Object.entries(r.indexes).filter((([,{type:e}])=>"Radix"===e)).map((([e,{node:t,isArray:r,type:n}])=>[e,r,n,t.toJSON()])),stats:Object.entries(t.stats).map((([e,{tokenQuantums:t,tokensLength:r}])=>[e,{tokenQuantums:t,tokensLength:Array.from(r.entries())}]))}]}}}}(t)}}export{w as pluginQPS};export default null;
//# sourceMappingURL=/sm/beeb1c0d85a9a4435436ddb2461bc0657f0728cfbdabaeac05e47a77db65282a.map