@dogsbay/search 0.2.0-beta.152 → 0.2.0-beta.153
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/dist/dogsbay-search.iife.js +1 -1
- package/dist/entry.d.ts +1 -1
- package/dist/query.d.ts +13 -3
- package/dist/query.js +58 -22
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -40,7 +40,7 @@ Every `.html` page is indexed, except redirects and `noindex` pages. The indexer
|
|
|
40
40
|
</script>
|
|
41
41
|
```
|
|
42
42
|
|
|
43
|
-
Several indexes in one page (a product's versions, or several products): give each its own `scriptData`, and search them together with `searchSources(q, [{ data, boost, collapse }, …])`, which merges results by score and shows a page once across versions. An index that does not load (a version not built yet)
|
|
43
|
+
Several indexes in one page (a product's versions, or several products): give each its own `scriptData`, and search them together with `searchSources(q, [{ data, boost, collapse }, …])`, which merges results by score and shows a page once across versions. An index that does not load (a version not built yet), is another format, or whose term file fails is left out and listed in `missing`; an excerpt file that fails leaves its results without an excerpt. Pages can carry facets (`SearchDoc.facets`, one per taxonomy): `search` and `searchSources` return their counts over every result as `facets`. Over several indexes, a facet filter leaves out every index that does not declare that facet. See `docs-dev/design.md` ("Several indexes").
|
|
44
44
|
|
|
45
45
|
`dogsbay-search.iife.js` is in `dist/` (about 15 KB). URLs in results are relative to the site root the index was built from. The package has no search box of its own: dogsbay-ssg's dialog (`src/client/search.ts` there) is an example.
|
|
46
46
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var DogsbaySearch=(()=>{var T=Object.defineProperty;var Mt=Object.getOwnPropertyDescriptor;var Rt=Object.getOwnPropertyNames;var Pt=Object.prototype.hasOwnProperty;var kt=(e,t)=>{for(var o in t)T(e,o,{get:t[o],enumerable:!0})},Ft=(e,t,o,n)=>{if(t&&typeof t=="object"||typeof t=="function")for(let r of Rt(t))!Pt.call(e,r)&&r!==o&&T(e,r,{get:()=>t[r],enumerable:!(n=Mt(t,r))||n.enumerable});return e};var Dt=e=>Ft(T({},"__esModule",{value:!0}),e);var ee={};kt(ee,{facetCounts:()=>j,facetNames:()=>O,loadDetails:()=>pt,loadScript:()=>te,parseQuery:()=>_,scriptData:()=>wt,search:()=>mt,searchSources:()=>bt});var U=/^[a-z][a-z0-9_-]{0,63}$/,$={docs:"docs.js",shard:e=>`t-${e}.js`,fragment:e=>`f-${e}.js`},E={docs:"docs",shard:e=>e,fragment:e=>`frag:${e}`};var _t={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"},Ot={icate:"ic",ative:"",alize:"al",iciti:"ic",ical:"ic",ful:"",ness:""},vt="[^aeiou]",Q="[aeiouy]",P="("+vt+"[^aeiouy]*)",z="("+Q+"[aeiou]*)",L=new RegExp("^"+P+"?"+z+P),$t=new RegExp("^"+P+"?"+z+P+z+"?$"),A=new RegExp("^"+P+"?("+z+P+"){2,}"),J=new RegExp("^"+P+"?"+Q),V=new RegExp("^"+P+Q+"[^aeiouwxy]$"),Et=/ll$/,At=/^(.+?)e$/,zt=/^(.+?)y$/,Ct=/^(.+?(s|t))(ion)$/,jt=/^(.+?)(ed|ing)$/,Gt=/(at|bl|iz)$/,Nt=/^(.+?)eed$/,Tt=/^.+?[^s]s$/,Lt=/^.+?(ss|i)es$/,Qt=/([^aeiouylsz])\1$/,Wt=/^(.+?)(ational|tional|enci|anci|izer|bli|alli|entli|eli|ousli|ization|ation|ator|alism|iveness|fulness|ousness|aliti|iviti|biliti|logi)$/,It=/^(.+?)(icate|ative|alize|iciti|ical|ful|ness)$/,Ht=/^(.+?)(al|ance|ence|er|ic|able|ible|ant|ement|ment|ent|ou|ism|ate|iti|ous|ive|ize)$/;function X(e){let t=String(e).toLowerCase();if(t.length<3)return t;let o=!1;t.codePointAt(0)===121&&(o=!0,t="Y"+t.slice(1)),Lt.test(t)?t=t.slice(0,-2):Tt.test(t)&&(t=t.slice(0,-1));let n;return(n=Nt.exec(t))?L.test(n[1])&&(t=t.slice(0,-1)):(n=jt.exec(t))&&J.test(n[1])&&(t=n[1],Gt.test(t)?t+="e":Qt.test(t)?t=t.slice(0,-1):V.test(t)&&(t+="e")),(n=zt.exec(t))&&J.test(n[1])&&(t=n[1]+"i"),(n=Wt.exec(t))&&L.test(n[1])&&(t=n[1]+_t[n[2]]),(n=It.exec(t))&&L.test(n[1])&&(t=n[1]+Ot[n[2]]),(n=Ht.exec(t))?A.test(n[1])&&(t=n[1]):(n=Ct.exec(t))&&A.test(n[1])&&(t=n[1]),(n=At.exec(t))&&(A.test(n[1])||$t.test(n[1])&&!V.test(n[1]))&&(t=n[1]),Et.test(t)&&A.test(t)&&(t=t.slice(0,-1)),o&&(t="y"+t.slice(1)),t}var Z=new Set("a an and are as at be by for from has have in is it its of on or that the this to was were will with you your can not".split(" ")),Yt=/[\p{Script=Han}\p{Script=Hiragana}\p{Script=Katakana}\p{Script=Hangul}]/u,F;function Bt(e){if(F===void 0)try{F=typeof Intl<"u"&&"Segmenter"in Intl?new Intl.Segmenter(void 0,{granularity:"word"}):null}catch{F=null}if(F)return[...F.segment(e)].filter(n=>n.isWordLike).map(n=>n.segment);let t=[...e];if(t.length<2)return t;let o=[];for(let n=0;n<t.length-1;n++)o.push(t[n]+t[n+1]);return o}function k(e){let t=[];for(let o of e.toLowerCase().normalize("NFKD").replace(/[̀-ͯ]/g,"").normalize("NFC").matchAll(/[\p{L}\p{N}][\p{L}\p{N}_.-]*[\p{L}\p{N}]|[\p{L}\p{N}]/gu)){let n=o[0],r=n.split(/[._-]+/).filter(Boolean);if(r.length>1&&t.push(n),r.length>1&&!/[._]/.test(n)){let s=r.join("");s.length>=3&&!Z.has(s)&&t.push(s)}for(let s of r)Yt.test(s)?t.push(...Bt(s)):s.length>=2&&!Z.has(s)&&t.push(s)}return t}function W(e,t){let o="";for(let n of e.slice(0,t))o+=/[a-z0-9]/.test(n)?n:`_${n.codePointAt(0).toString(16)}_`;return o}function D(e){if(!(!e||!/^en(-|$)/i.test(e)))return t=>/^[a-z]{3,}$/.test(t)?X(t):t}function _(e,t={}){var x,h;let o=[],n=[],r=[],s=!1,i={tag:[],section:[]},a={tag:[],section:[]},u=Object.create(null),l=Object.create(null),f=["tag","section",...new Set((t.facets??[]).filter(g=>U.test(g)&&g!=="tag"&&g!=="section"))],p=new RegExp(`(?<!\\S)(-?)(${f.join("|")}):(?:"([^"]*)"?|(?!\\/\\/)(\\S*))|(-?)"([^"]*)"?|(\\S+)`,"gi"),S=[...e.matchAll(p)].map(g=>({facetNeg:g[1]==="-",facet:g[2]?.toLowerCase(),value:g[3]??g[4],neg:g[5],phrase:g[6],word:g[7]})),c=!1,d=!1;for(let[g,m]of S.entries()){if(m.facet){let M=B(m.value??"");if(!M)continue;m.facet==="tag"||m.facet==="section"?(m.facetNeg?a:i)[m.facet].push(M):((x=m.facetNeg?l:u)[h=m.facet]??(x[h]=[])).push(M);continue}let w=m.word;if(w==="OR"){d=o.length>0,s=!0;continue}if(w==="NOT"){c=!0,s=!0;continue}if(w==="AND"){s=!0;continue}let G=m.phrase!==void 0,K=c||m.neg==="-"||w?.startsWith("-")&&w.length>1;c=!1;let q=G?m.phrase:K&&w?.startsWith("-")?w.slice(1):w,y=[...new Set(k(q))];if(!y.length){d=!1;continue}if(K){n.push(G?y:[y[0]]),s=!0,d=!1;continue}if(G){d=!1,s=!0,y.length>1&&r.push(k(q).join(" "));for(let M of y)o.push({terms:[M],prefix:!1,exact:!0});continue}let St=g===S.length-1&&!/\s$/.test(e);if(d){o[o.length-1].terms.push(y[0]);for(let M of y.slice(1))o.push({terms:[M],prefix:!1});d=!1;continue}let N=y[0],xt=/[._-]/.test(N),yt=N.replace(/-/g,"");for(let[M,v]of y.entries())o.push({terms:[v],prefix:St&&v.length>=2&&(M===y.length-1||xt&&(v===N||v===yt))})}let b=[...new Set(o.flatMap(g=>g.terms))];return{groups:o,not:n,phrases:r,strict:s,highlight:b,filters:i,notFilters:a,...Object.keys(u).length?{facets:u}:{},...Object.keys(l).length?{notFacets:l}:{}}}async function Kt(e,t){let o=await t.meta(),n=typeof e=="string"?_(e,{facets:O([o])}):e,r=await ot(n,o,t),s=ct(n,o);if(!n.groups.length)return it(o,r,s);let i=[];for(let u of n.groups)i.push(await Jt(u,o,t));let a=H(i,o,r,s,"and").sort(C);return a.length||n.strict?a:H(i,o,r,s,"or").sort(C)}var C=(e,t)=>t.score-e.score;async function ot(e,t,o){let n=new Set;for(let r of e.not){let s;for(let i of r){let[a]=at(i,t),u=a?await o.shard(a):void 0,l=u&&Object.hasOwn(u,i)?u[i]:[],f=new Set;for(let p=0;p<l.length;p+=2)(!s||s.has(l[p]))&&f.add(l[p]);s=f}for(let i of s??[])n.add(i)}return n}function it(e,t,o){if(!o)return[];let n=[];return e.docs.forEach(([r,s,i],a)=>o(a)&&!t.has(a)&&n.push({id:a,title:r,url:s,section:e.sections[i]??"",score:1})),n}function H(e,t,o,n,r){let s=new Map,i=new Map;for(let u of e)for(let[l,f]of u)s.set(l,(s.get(l)??0)+f),i.set(l,(i.get(l)??0)+1);let a=[];for(let[u,l]of s){if(o.has(u)||n&&!n(u)||r==="and"&&i.get(u)!==e.length)continue;let[f,p,S]=t.docs[u];a.push({id:u,title:f,url:p,section:t.sections[S]??"",score:l*(i.get(u)/e.length)})}return a}var R={prefix:.6,stem:.4,titleAll:1.5,titleExact:2,headingAll:1.4,headingExact:1.8,phrase:1.5,typo:.3};function qt(e,t){if(e===t)return!1;if(e.length===t.length){let s=[];for(let i=0;i<e.length&&s.length<=2;i++)e[i]!==t[i]&&s.push(i);return s.length===1?!0:s.length===2&&s[1]===s[0]+1&&e[s[0]]===t[s[1]]&&e[s[1]]===t[s[0]]}let[o,n]=e.length<t.length?[e,t]:[t,e];if(n.length-o.length!==1)return!1;let r=0;for(;r<o.length&&o[r]===n[r];)r++;return o.slice(r)===n.slice(r+1)}function at(e,t,o=!1){let n=Ut(t);for(let s=Math.min(e.length,tt);s>=1;s--){let i=W(e,s);if(n.has(i))return!o||s<e.length?[i]:[i,...t.shards.filter(a=>a.length>i.length&&a.startsWith(i)).slice(0,10)]}let r=W(e,Math.min(e.length,tt));return o?t.shards.filter(s=>s.startsWith(r)).slice(0,10):[]}var tt=3,et=new WeakMap;function Ut(e){let t=et.get(e);return t||et.set(e,t=new Set(e.shards)),t}function B(e){return e.replace(/"/g,"").trim().toLowerCase()}function ct(e,t){let{tag:o,section:n}=e.filters,r=e.notFilters??{tag:[],section:[]},s=Object.entries(e.facets??{}).filter(([,h])=>h.length),i=Object.entries(e.notFacets??{}).filter(([,h])=>h.length);if(!o.length&&!n.length&&!r.tag.length&&!r.section.length&&!s.length&&!i.length)return;let a=(h,g)=>new Set(h.flatMap((m,w)=>g.includes(B(m))?[w]:[])),u=a(t.sections,n),l=a(t.tags??[],o),f=a(t.sections,r.section),p=a(t.tags??[],r.tag),S=h=>(t.facets??[]).findIndex(g=>g.name===h),c=h=>h.map(([g,m])=>{let w=S(g);return[w,w<0?new Set:a(t.facets[w].values,m)]}),d=c(s),b=c(i).filter(([h])=>h>=0),x=(h,[g,m])=>g>=0&&(h[4]?.[g]??[]).some(w=>m.has(w));return h=>{let g=t.docs[h],m=g[3]??[];if(n.length&&!u.has(g[2])||o.length&&!m.some(w=>l.has(w))||f.has(g[2])||m.some(w=>p.has(w)))return!1;for(let w of d)if(!x(g,w))return!1;for(let w of b)if(x(g,w))return!1;return!0}}function O(e){return[...new Set(e.flatMap(t=>(t?.facets??[]).map(o=>o.name)))]}function j(e,t){let o=Object.create(null),n=new Map,r=(s,i)=>{let a=`${s}\0${B(i)}`,u=n.get(a);u===void 0&&n.set(a,u=i);let l=o[s]??(o[s]=Object.create(null));l[u]=(l[u]??0)+1};for(let s of e){let i=typeof t=="function"?t(s):t,a=i?.docs[s.id];if(a){i.sections[a[2]]&&r("section",i.sections[a[2]]);for(let u of new Set(a[3]??[]))i.tags[u]!==void 0&&r("tag",i.tags[u]);(a[4]??[]).forEach((u,l)=>{let f=i.facets?.[l];if(f)for(let p of new Set(u))f.values[p]!==void 0&&r(f.name,f.values[p])})}}return o}async function Jt(e,t,o){let n=await lt(e,t,o,!0);return e.corrected=n.corrected.length?n.corrected:void 0,ut(n,Y(t.count,n.docs.size))}function Y(e,t){return Math.log(1+(e-t+.5)/(t+.5))}function ut(e,t){let o=new Map;for(let[n,r]of e.lists)for(let s=0;s<n.length;s+=2)o.set(n[s],Math.max(o.get(n[s])??0,n[s+1]*r*t));return o}async function lt(e,t,o,n){let r=D(t.lang),s=new Map,i=new Map;for(let f of e.terms)for(let p of at(f,t,e.prefix)){let S=await o.shard(p);if(!S)continue;let c=(h,g)=>{g>(s.get(h)??0)&&(s.set(h,g),i.set(h,S))};Object.hasOwn(S,f)&&c(f,1);let d=r?.(f),b=d?d.slice(0,Math.max(3,d.length-1)):void 0,x=0;for(let h in S)h!==f&&(e.prefix&&h.startsWith(f)&&x++<200&&c(h,R.prefix),d&&b&&h.startsWith(b)&&r(h)===d&&c(h,R.stem));if(n&&!s.size&&!e.exact&&f.length>=4&&!/[._-]/.test(f))for(let h in S)Math.abs(h.length-f.length)<=1&&qt(f,h)&&c(h,R.typo)}let a=[...s].filter(([,f])=>f===R.typo).map(([f])=>f),u=[...s].map(([f,p])=>[i.get(f)[f],p]),l=new Set;for(let[f]of u)for(let p=0;p<f.length;p+=2)l.add(f[p]);return{lists:u,docs:l,corrected:a}}var ft=e=>typeof e=="function"?e:()=>e;async function gt(e,t){let o=ft(t),n=new Map;for(let r of e){let s=o(r),i=n.get(s);i||n.set(s,i=[]),i.push(r)}await Promise.all([...n].map(async([r,s])=>{let i=await r.meta(),a=[...new Set(s.map(l=>Math.floor(l.id/i.block)))],u=new Map;await Promise.all(a.map(async l=>u.set(l,await r.fragment(l))));for(let l of s)l.fragment??(l.fragment=u.get(Math.floor(l.id/i.block))?.[l.id%i.block])}))}async function dt(e){let t=ft(e),o=new Map;return async n=>{let r=t(n),s=o.get(r);if(!s){let i=D((await r.meta()).lang);s=(a,u)=>u.terms.some(l=>a===l||u.prefix&&a.startsWith(l)||i!==void 0&&i(a)===i(l))||(u.corrected??[]).includes(a),o.set(r,s)}return s}}async function ht(e,t,o,n=10){let r=t.slice(0,n);await gt(r,o);let s=e.groups,i=await dt(o);for(let a of r){let u=await i(a),[l,f]=a.fragment??["",[]],p=k(a.title),S=1;s.length&&s.every(d=>p.some(b=>u(b,d)))&&(S=nt(p,s,u)?R.titleExact:R.titleAll);let c=[];for(let[d,b]of f){let x=k(d);!s.length||!s.every(h=>x.some(g=>u(g,h)))||(c.push([d,b]),S=Math.max(S,nt(x,s,u)?R.headingExact:R.headingAll))}if(a.sections=c.slice(0,3),e.phrases.length){let d=[a.title,l,...f.map(([b])=>b)].map(b=>k(b).join(" ")).join(" \0 ");e.phrases.every(b=>d.includes(b))&&(S*=R.phrase)}a.score*=S}return r.sort((a,u)=>u.score-a.score),[...r,...t.slice(n)]}async function pt(e,t,o){await gt(t,o);let n=await dt(o);for(let r of t){if(!r.fragment||r.sections)continue;let s=await n(r);r.sections=r.fragment[1].filter(([i])=>{let a=k(i);return e.groups.length>0&&e.groups.every(u=>a.some(l=>s(l,u)))}).slice(0,3)}}function nt(e,t,o){return e.every(n=>t.some(r=>o(n,r)))&&t.every(n=>e.some(r=>o(r,n)))}async function mt(e,t,o=10){let n=await t.meta(),r=_(e,{facets:O([n])}),s=D(n.lang);s&&(r.highlight=[...new Set([...r.highlight,...r.highlight.map(s).filter(a=>a.length>=3)])]);let i=await Kt(r,t);return r.highlight=[...new Set([...r.highlight,...r.groups.flatMap(a=>a.corrected??[])])],{parsed:r,results:await ht(r,i,t,o),facets:j(i,n)}}async function Vt(e){return Promise.all(e.map(t=>t.data.meta().then(o=>o&&o.format===5?o:void 0,()=>{})))}async function Xt(e,t,o,n){let r=t.flatMap((c,d)=>o[d]?[d]:[]),s=o,i=[],a=[];await Promise.all(r.map(async c=>i[c]=await ot(e,s[c],t[c].data)));for(let c of r)a[c]=ct(e,s[c]);let u=(c,d)=>c.map(b=>({...b,source:d,score:b.score*Zt(t[d])}));if(!e.groups.length)return st(r.flatMap(c=>u(it(s[c],i[c],a[c]),c)).sort(C),t);let l=t.map(()=>[]),f=r.reduce((c,d)=>c+s[d].count,0);for(let c of e.groups){let d=g=>Promise.all(r.map(m=>lt(c,s[m],t[m].data,g))),b=await d(!1);b.every(g=>!g.docs.size)&&(b=await d(!0));let x=[...new Set(b.flatMap(g=>g.corrected))];c.corrected=x.length?x:void 0;let h=b.reduce((g,m)=>g+m.docs.size,0);b.forEach((g,m)=>{let w=r[m];l[w].push(ut(g,n.stats==="own"?Y(s[w].count,g.docs.size):Y(f,h)))})}let p=c=>r.flatMap(d=>u(H(l[d],s[d],i[d],a[d],c),d)).sort(C),S=p("and");return st(S.length||e.strict?S:p("or"),t)}function Zt(e){return typeof e.boost=="number"&&Number.isFinite(e.boost)&&e.boost>=0?e.boost:1}function st(e,t){let o=new Map,n=[];for(let r of e){let s=t[r.source].collapse;if(s===void 0){n.push(r);continue}let i=`${s}\0${r.url}`,a=o.get(i);a?(a.others??(a.others=[])).push({source:r.source,id:r.id,url:r.url,score:r.score}):(o.set(i,r),n.push(r))}return n}async function bt(e,t,o=10,n={}){let r=await Vt(t),s=_(e,{facets:O(r)}),i=t.flatMap((l,f)=>r[f]?[]:[f]);for(let l of new Set(r.flatMap(f=>f?[f.lang]:[]))){let f=D(l);f&&(s.highlight=[...new Set([...s.highlight,...s.highlight.map(f).filter(p=>p.length>=3)])])}let a=await Xt(s,t,r,n);s.highlight=[...new Set([...s.highlight,...s.groups.flatMap(l=>l.corrected??[])])];let u=j(a,l=>r[l.source]);return{parsed:s,results:await ht(s,a,l=>t[l.source].data,o),missing:i,facets:u}}function wt(e,t,o=globalThis){let n=o.document?.baseURI??o.location?.href,r=rt(e,n),s=o.__DOGSBAY_SEARCH__??{q:[],load(){}},i=s.stores??(s.stores=new Map),a=i.get(r)??new Map;i.set(r,a);let u=(c,d,b)=>{let x;if(b){let h="",g=rt(b,n);for(let m of i.keys())g.startsWith(m)&&m.length>h.length&&(h=m);x=h?i.get(h):void 0}!x&&i.size===1&&(x=a),x?x.set(c,d):s.q.push([c,d,b])},l=s.q;s.q=[],s.load=u;for(let[c,d,b]of l)u(c,d,b);o.__DOGSBAY_SEARCH__=s;let f=async(c,d)=>(a.has(c)||await t(e+d),a.get(c)),p,S={meta:()=>p??(p=f(E.docs,$.docs).then(c=>{if(!c||c.format!==5)throw new Error(`search index format ${c?.format??"missing"}, expected ${5}: rebuild the site`);return c}).catch(c=>{throw p=void 0,c})),shard:async c=>(await S.meta()).shards.includes(c)?f(E.shard(c),$.shard(c)):void 0,fragment:c=>f(E.fragment(c),$.fragment(c))};return S}function rt(e,t){try{return new URL(e,t).href}catch{return e}}function te(e){return new Promise((t,o)=>{let n=document.createElement("script");n.src=e,n.onload=()=>t(),n.onerror=()=>o(new Error(`failed to load ${e}`)),document.head.append(n)})}return Dt(ee);})();
|
|
1
|
+
"use strict";var DogsbaySearch=(()=>{var H=Object.defineProperty;var yt=Object.getOwnPropertyDescriptor;var Mt=Object.getOwnPropertyNames;var Rt=Object.prototype.hasOwnProperty;var Pt=(e,t)=>{for(var o in t)H(e,o,{get:t[o],enumerable:!0})},kt=(e,t,o,n)=>{if(t&&typeof t=="object"||typeof t=="function")for(let r of Mt(t))!Rt.call(e,r)&&r!==o&&H(e,r,{get:()=>t[r],enumerable:!(n=yt(t,r))||n.enumerable});return e};var Ft=e=>kt(H({},"__esModule",{value:!0}),e);var ee={};Pt(ee,{facetCounts:()=>W,facetNames:()=>G,loadDetails:()=>bt,loadScript:()=>te,parseQuery:()=>z,scriptData:()=>xt,search:()=>wt,searchSources:()=>St});var X=/^[a-z][a-z0-9_-]{0,63}$/,j={docs:"docs.js",shard:e=>`t-${e}.js`,fragment:e=>`f-${e}.js`},T={docs:"docs",shard:e=>e,fragment:e=>`frag:${e}`};var Dt={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:""},Ot="[^aeiou]",B="[aeiouy]",D="("+Ot+"[^aeiouy]*)",L="("+B+"[aeiou]*)",Y=new RegExp("^"+D+"?"+L+D),vt=new RegExp("^"+D+"?"+L+D+L+"?$"),N=new RegExp("^"+D+"?("+L+D+"){2,}"),Z=new RegExp("^"+D+"?"+B),tt=new RegExp("^"+D+B+"[^aeiouwxy]$"),Et=/ll$/,$t=/^(.+?)e$/,At=/^(.+?)y$/,zt=/^(.+?(s|t))(ion)$/,Gt=/^(.+?)(ed|ing)$/,Ct=/(at|bl|iz)$/,jt=/^(.+?)eed$/,Tt=/^.+?[^s]s$/,Nt=/^.+?(ss|i)es$/,Lt=/([^aeiouylsz])\1$/,Qt=/^(.+?)(ational|tional|enci|anci|izer|bli|alli|entli|eli|ousli|ization|ation|ator|alism|iveness|fulness|ousness|aliti|iviti|biliti|logi)$/,Wt=/^(.+?)(icate|ative|alize|iciti|ical|ful|ness)$/,It=/^(.+?)(al|ance|ence|er|ic|able|ible|ant|ement|ment|ent|ou|ism|ate|iti|ous|ive|ize)$/;function et(e){let t=String(e).toLowerCase();if(t.length<3)return t;let o=!1;t.codePointAt(0)===121&&(o=!0,t="Y"+t.slice(1)),Nt.test(t)?t=t.slice(0,-2):Tt.test(t)&&(t=t.slice(0,-1));let n;return(n=jt.exec(t))?Y.test(n[1])&&(t=t.slice(0,-1)):(n=Gt.exec(t))&&Z.test(n[1])&&(t=n[1],Ct.test(t)?t+="e":Lt.test(t)?t=t.slice(0,-1):tt.test(t)&&(t+="e")),(n=At.exec(t))&&Z.test(n[1])&&(t=n[1]+"i"),(n=Qt.exec(t))&&Y.test(n[1])&&(t=n[1]+Dt[n[2]]),(n=Wt.exec(t))&&Y.test(n[1])&&(t=n[1]+_t[n[2]]),(n=It.exec(t))?N.test(n[1])&&(t=n[1]):(n=zt.exec(t))&&N.test(n[1])&&(t=n[1]),(n=$t.exec(t))&&(N.test(n[1])||vt.test(n[1])&&!tt.test(n[1]))&&(t=n[1]),Et.test(t)&&N.test(t)&&(t=t.slice(0,-1)),o&&(t="y"+t.slice(1)),t}var nt=new Set("a an and are as at be by for from has have in is it its of on or that the this to was were will with you your can not".split(" ")),Ht=/[\p{Script=Han}\p{Script=Hiragana}\p{Script=Katakana}\p{Script=Hangul}]/u,$;function Yt(e){if($===void 0)try{$=typeof Intl<"u"&&"Segmenter"in Intl?new Intl.Segmenter(void 0,{granularity:"word"}):null}catch{$=null}if($)return[...$.segment(e)].filter(n=>n.isWordLike).map(n=>n.segment);let t=[...e];if(t.length<2)return t;let o=[];for(let n=0;n<t.length-1;n++)o.push(t[n]+t[n+1]);return o}function _(e){let t=[];for(let o of e.toLowerCase().normalize("NFKD").replace(/[̀-ͯ]/g,"").normalize("NFC").matchAll(/[\p{L}\p{N}][\p{L}\p{N}_.-]*[\p{L}\p{N}]|[\p{L}\p{N}]/gu)){let n=o[0],r=n.split(/[._-]+/).filter(Boolean);if(r.length>1&&t.push(n),r.length>1&&!/[._]/.test(n)){let s=r.join("");s.length>=3&&!nt.has(s)&&t.push(s)}for(let s of r)Ht.test(s)?t.push(...Yt(s)):s.length>=2&&!nt.has(s)&&t.push(s)}return t}function K(e,t){let o="";for(let n of e.slice(0,t))o+=/[a-z0-9]/.test(n)?n:`_${n.codePointAt(0).toString(16)}_`;return o}function A(e){if(!(!e||!/^en(-|$)/i.test(e)))return t=>/^[a-z]{3,}$/.test(t)?et(t):t}function z(e,t={}){var R,d;let o=[],n=[],r=[],s=!1,i={tag:[],section:[]},a={tag:[],section:[]},c=Object.create(null),u=Object.create(null),l=["tag","section",...new Set((t.facets??[]).filter(p=>X.test(p)&&p!=="tag"&&p!=="section"))],f=new RegExp(`(?<!\\S)(-?)(${l.join("|")}):(?:"([^"]*)"?|(?!\\/\\/)(\\S*))|(-?)"([^"]*)"?|(\\S+)`,"gi"),m=[...e.matchAll(f)].map(p=>({facetNeg:p[1]==="-",facet:p[2]?.toLowerCase(),value:p[3]??p[4],neg:p[5],phrase:p[6],word:p[7]})),h=!1,b=!1;for(let[p,S]of m.entries()){if(S.facet){let P=V(S.value??"");if(!P)continue;S.facet==="tag"||S.facet==="section"?(S.facetNeg?a:i)[S.facet].push(P):((R=S.facetNeg?u:c)[d=S.facet]??(R[d]=[])).push(P);continue}let x=S.word;if(x==="OR"){b=o.length>0,s=!0;continue}if(x==="NOT"){h=!0,s=!0;continue}if(x==="AND"){s=!0;continue}let O=S.phrase!==void 0,g=h||S.neg==="-"||x?.startsWith("-")&&x.length>1;h=!1;let w=O?S.phrase:g&&x?.startsWith("-")?x.slice(1):x,M=[...new Set(_(w))];if(!M.length){b=!1;continue}if(g){n.push(O?M:[M[0]]),s=!0,b=!1;continue}if(O){b=!1,s=!0,M.length>1&&r.push(_(w).join(" "));for(let P of M)o.push({terms:[P],prefix:!1,exact:!0});continue}let E=p===m.length-1&&!/\s$/.test(e);if(b){o[o.length-1].terms.push(M[0]);for(let P of M.slice(1))o.push({terms:[P],prefix:!1});b=!1;continue}let v=M[0],I=/[._-]/.test(v),k=v.replace(/-/g,"");for(let[P,C]of M.entries())o.push({terms:[C],prefix:E&&C.length>=2&&(P===M.length-1||I&&(C===v||C===k))})}let y=[...new Set(o.flatMap(p=>p.terms))];return{groups:o,not:n,phrases:r,strict:s,highlight:y,filters:i,notFilters:a,...Object.keys(c).length?{facets:c}:{},...Object.keys(u).length?{notFacets:u}:{}}}async function Bt(e,t){let o=await t.meta(),n=typeof e=="string"?z(e,{facets:G([o])}):e,r=await at(n,o,t),s=lt(n,o);if(!n.groups.length)return ct(o,r,s);let i=[];for(let c of n.groups)i.push(await Ut(c,o,t));let a=U(i,o,r,s,"and").sort(Q);return a.length||n.strict?a:U(i,o,r,s,"or").sort(Q)}var Q=(e,t)=>t.score-e.score;async function at(e,t,o){let n=new Set;for(let r of e.not){let s;for(let i of r){let[a]=ut(i,t),c=a?await o.shard(a):void 0,u=c&&Object.hasOwn(c,i)?c[i]:[],l=new Set;for(let f=0;f<u.length;f+=2)(!s||s.has(u[f]))&&l.add(u[f]);s=l}for(let i of s??[])n.add(i)}return n}function ct(e,t,o){if(!o)return[];let n=[];return e.docs.forEach(([r,s,i],a)=>o(a)&&!t.has(a)&&n.push({id:a,title:r,url:s,section:e.sections[i]??"",score:1})),n}function U(e,t,o,n,r){let s=new Map,i=new Map;for(let c of e)for(let[u,l]of c)s.set(u,(s.get(u)??0)+l),i.set(u,(i.get(u)??0)+1);let a=[];for(let[c,u]of s){if(o.has(c)||n&&!n(c)||r==="and"&&i.get(c)!==e.length)continue;let[l,f,m]=t.docs[c];a.push({id:c,title:l,url:f,section:t.sections[m]??"",score:u*(i.get(c)/e.length)})}return a}var F={prefix:.6,stem:.4,titleAll:1.5,titleExact:2,headingAll:1.4,headingExact:1.8,phrase:1.5,typo:.3};function Kt(e,t){if(e===t)return!1;if(e.length===t.length){let s=[];for(let i=0;i<e.length&&s.length<=2;i++)e[i]!==t[i]&&s.push(i);return s.length===1?!0:s.length===2&&s[1]===s[0]+1&&e[s[0]]===t[s[1]]&&e[s[1]]===t[s[0]]}let[o,n]=e.length<t.length?[e,t]:[t,e];if(n.length-o.length!==1)return!1;let r=0;for(;r<o.length&&o[r]===n[r];)r++;return o.slice(r)===n.slice(r+1)}function ut(e,t,o=!1){let n=qt(t);for(let s=Math.min(e.length,st);s>=1;s--){let i=K(e,s);if(n.has(i))return!o||s<e.length?[i]:[i,...t.shards.filter(a=>a.length>i.length&&a.startsWith(i)).slice(0,10)]}let r=K(e,Math.min(e.length,st));return o?t.shards.filter(s=>s.startsWith(r)).slice(0,10):[]}var st=3,rt=new WeakMap;function qt(e){let t=rt.get(e);return t||rt.set(e,t=new Set(e.shards)),t}function V(e){return e.replace(/"/g,"").trim().toLowerCase()}function lt(e,t){let{tag:o,section:n}=e.filters,r=e.notFilters??{tag:[],section:[]},s=Object.entries(e.facets??{}).filter(([,d])=>d.length),i=Object.entries(e.notFacets??{}).filter(([,d])=>d.length);if(!o.length&&!n.length&&!r.tag.length&&!r.section.length&&!s.length&&!i.length)return;let a=(d,p)=>new Set(d.flatMap((S,x)=>p.includes(V(S))?[x]:[])),c=a(t.sections,n),u=a(t.tags??[],o),l=a(t.sections,r.section),f=a(t.tags??[],r.tag),m=d=>(t.facets??[]).findIndex(p=>p.name===d),h=d=>d.map(([p,S])=>{let x=m(p);return[x,x<0?new Set:a(t.facets[x].values,S)]}),b=h(s),y=h(i).filter(([d])=>d>=0),R=(d,[p,S])=>p>=0&&(d[4]?.[p]??[]).some(x=>S.has(x));return d=>{let p=t.docs[d],S=p[3]??[];if(n.length&&!c.has(p[2])||o.length&&!S.some(x=>u.has(x))||l.has(p[2])||S.some(x=>f.has(x)))return!1;for(let x of b)if(!R(p,x))return!1;for(let x of y)if(R(p,x))return!1;return!0}}function G(e){return[...new Set(e.flatMap(t=>(t?.facets??[]).map(o=>o.name)))]}function W(e,t){let o=Object.create(null),n=new Map,r=(s,i)=>{let a=`${s}\0${V(i)}`,c=n.get(a);c===void 0&&n.set(a,c=i);let u=o[s]??(o[s]=Object.create(null));u[c]=(u[c]??0)+1};for(let s of e){let i=typeof t=="function"?t(s):t,a=i?.docs[s.id];if(a){i.sections[a[2]]&&r("section",i.sections[a[2]]);for(let c of new Set(a[3]??[]))i.tags[c]!==void 0&&r("tag",i.tags[c]);(a[4]??[]).forEach((c,u)=>{let l=i.facets?.[u];if(l)for(let f of new Set(c))l.values[f]!==void 0&&r(l.name,l.values[f])})}}return o}async function Ut(e,t,o){let n=await gt(e,t,o,!0);return e.corrected=n.corrected.length?n.corrected:void 0,ft(n,J(t.count,n.docs.size))}function J(e,t){return Math.log(1+(e-t+.5)/(t+.5))}function ft(e,t){let o=new Map;for(let[n,r]of e.lists)for(let s=0;s<n.length;s+=2)o.set(n[s],Math.max(o.get(n[s])??0,n[s+1]*r*t));return o}async function gt(e,t,o,n){let r=A(t.lang),s=new Map,i=new Map;for(let l of e.terms)for(let f of ut(l,t,e.prefix)){let m=await o.shard(f);if(!m)continue;let h=(d,p)=>{p>(s.get(d)??0)&&(s.set(d,p),i.set(d,m))};Object.hasOwn(m,l)&&h(l,1);let b=r?.(l),y=b?b.slice(0,Math.max(3,b.length-1)):void 0,R=0;for(let d in m)d!==l&&(e.prefix&&d.startsWith(l)&&R++<200&&h(d,F.prefix),b&&y&&d.startsWith(y)&&r(d)===b&&h(d,F.stem));if(n&&!s.size&&!e.exact&&l.length>=4&&!/[._-]/.test(l))for(let d in m)Math.abs(d.length-l.length)<=1&&Kt(l,d)&&h(d,F.typo)}let a=[...s].filter(([,l])=>l===F.typo).map(([l])=>l),c=[...s].map(([l,f])=>[i.get(l)[l],f]),u=new Set;for(let[l]of c)for(let f=0;f<l.length;f+=2)u.add(l[f]);return{lists:c,docs:u,corrected:a}}var dt=e=>typeof e=="function"?e:()=>e;async function ht(e,t){let o=dt(t),n=new Map;for(let r of e){let s=o(r),i=n.get(s);i||n.set(s,i=[]),i.push(r)}await Promise.all([...n].map(async([r,s])=>{let i=await r.meta(),a=[...new Set(s.map(u=>Math.floor(u.id/i.block)))],c=new Map;await Promise.all(a.map(async u=>{try{c.set(u,await r.fragment(u))}catch{c.set(u,void 0)}}));for(let u of s)u.fragment??(u.fragment=c.get(Math.floor(u.id/i.block))?.[u.id%i.block])}))}async function pt(e){let t=dt(e),o=new Map;return async n=>{let r=t(n),s=o.get(r);if(!s){let i=A((await r.meta()).lang);s=(a,c)=>c.terms.some(u=>a===u||c.prefix&&a.startsWith(u)||i!==void 0&&i(a)===i(u))||(c.corrected??[]).includes(a),o.set(r,s)}return s}}async function mt(e,t,o,n=10){let r=t.slice(0,n);await ht(r,o);let s=e.groups,i=await pt(o);for(let a of r){let c=await i(a),[u,l]=a.fragment??["",[]],f=_(a.title),m=1;s.length&&s.every(b=>f.some(y=>c(y,b)))&&(m=ot(f,s,c)?F.titleExact:F.titleAll);let h=[];for(let[b,y]of l){let R=_(b);!s.length||!s.every(d=>R.some(p=>c(p,d)))||(h.push([b,y]),m=Math.max(m,ot(R,s,c)?F.headingExact:F.headingAll))}if(a.sections=h.slice(0,3),e.phrases.length){let b=[a.title,u,...l.map(([y])=>y)].map(y=>_(y).join(" ")).join(" \0 ");e.phrases.every(y=>b.includes(y))&&(m*=F.phrase)}a.score*=m}return r.sort((a,c)=>c.score-a.score),[...r,...t.slice(n)]}async function bt(e,t,o){await ht(t,o);let n=await pt(o);for(let r of t){if(!r.fragment||r.sections)continue;let s=await n(r);r.sections=r.fragment[1].filter(([i])=>{let a=_(i);return e.groups.length>0&&e.groups.every(c=>a.some(u=>s(u,c)))}).slice(0,3)}}function ot(e,t,o){return e.every(n=>t.some(r=>o(n,r)))&&t.every(n=>e.some(r=>o(r,n)))}async function wt(e,t,o=10){let n=await t.meta(),r=z(e,{facets:G([n])}),s=A(n.lang);s&&(r.highlight=[...new Set([...r.highlight,...r.highlight.map(s).filter(a=>a.length>=3)])]);let i=await Bt(r,t);return r.highlight=[...new Set([...r.highlight,...r.groups.flatMap(a=>a.corrected??[])])],{parsed:r,results:await mt(r,i,t,o),facets:W(i,n)}}async function Jt(e){return Promise.all(e.map(t=>t.data.meta().then(o=>o&&o.format===5?o:void 0,()=>{})))}async function Vt(e,t,o,n){let r=o,s=new Set,i=[],a=async(g,w)=>{if(!s.has(g))try{return await w()}catch(M){s.add(g),i.push({source:g,error:M});return}},c=t.flatMap((g,w)=>o[w]?[w]:[]),u=[];await Promise.all(c.map(async g=>u[g]=await a(g,()=>at(e,r[g],t[g].data))??new Set));let l=[],f=[],m=(g,w)=>Promise.all(c.map(M=>a(M,()=>gt(g,r[M],t[M].data,w)))),h=g=>g.every((w,M)=>!w?.docs.size||s.has(c[M]));for(let g of e.groups){let w=await m(g,!1);f.push(h(w)),f[f.length-1]&&(w=await m(g,!0)),l.push(w)}for(let[g,w]of e.groups.entries())f[g]||!h(l[g])||(w.corrected=void 0,l[g]=await m(w,!0));if(c.length&&c.every(g=>s.has(g)))throw i[0].error;let b=c.filter(g=>!s.has(g)),y=[];for(let g of b)y[g]=lt(e,r[g]);let R=(g,w)=>g.map(M=>({...M,source:w,score:M.score*Xt(t[w])})),d=g=>({results:Zt(g,t),failed:[...s].sort((w,M)=>w-M),errors:i});if(!e.groups.length)return d(b.flatMap(g=>R(ct(r[g],u[g],y[g]),g)).sort(Q));let p=t.map(()=>[]),S=b.reduce((g,w)=>g+r[w].count,0);e.groups.forEach((g,w)=>{let M=l[w],E=c.flatMap((k,P)=>s.has(k)?[]:[[k,M[P]]]),v=[...new Set(E.flatMap(([,k])=>k.corrected))];g.corrected=v.length?v:void 0;let I=E.reduce((k,[,P])=>k+P.docs.size,0);for(let[k,P]of E)p[k].push(ft(P,n.stats==="own"?J(r[k].count,P.docs.size):J(S,I)))});let x=g=>b.flatMap(w=>R(U(p[w],r[w],u[w],y[w],g),w)).sort(Q),O=x("and");return d(O.length||e.strict?O:x("or"))}function Xt(e){return typeof e.boost=="number"&&Number.isFinite(e.boost)&&e.boost>=0?e.boost:1}function Zt(e,t){let o=new Map,n=[];for(let r of e){let s=t[r.source].collapse;if(s===void 0){n.push(r);continue}let i=`${s}\0${r.url}`,a=o.get(i);a?(a.others??(a.others=[])).push({source:r.source,id:r.id,url:r.url,score:r.score}):(o.set(i,r),n.push(r))}return n}async function St(e,t,o=10,n={}){let r=await Jt(t),s=z(e,{facets:G(r)}),i=t.flatMap((f,m)=>r[m]?[]:[m]);for(let f of new Set(r.flatMap(m=>m?[m.lang]:[]))){let m=A(f);m&&(s.highlight=[...new Set([...s.highlight,...s.highlight.map(m).filter(h=>h.length>=3)])])}let{results:a,failed:c,errors:u}=await Vt(s,t,r,n);for(let f of c)i.includes(f)||i.push(f);i.sort((f,m)=>f-m),s.highlight=[...new Set([...s.highlight,...s.groups.flatMap(f=>f.corrected??[])])];let l=W(a,f=>r[f.source]);return{parsed:s,results:await mt(s,a,f=>t[f.source].data,o),missing:i,facets:l,errors:u}}function xt(e,t,o=globalThis){let n=o.document?.baseURI??o.location?.href,r=it(e,n),s=o.__DOGSBAY_SEARCH__??{q:[],load(){}},i=s.stores??(s.stores=new Map),a=i.get(r)??new Map;i.set(r,a);let c=(h,b,y)=>{let R;if(y){let d="",p=it(y,n);for(let S of i.keys())p.startsWith(S)&&S.length>d.length&&(d=S);R=d?i.get(d):void 0}!R&&i.size===1&&(R=a),R?R.set(h,b):s.q.push([h,b,y])},u=s.q;s.q=[],s.load=c;for(let[h,b,y]of u)c(h,b,y);o.__DOGSBAY_SEARCH__=s;let l=async(h,b)=>(a.has(h)||await t(e+b),a.get(h)),f,m={meta:()=>f??(f=l(T.docs,j.docs).then(h=>{if(!h||h.format!==5)throw new Error(`search index format ${h?.format??"missing"}, expected ${5}: rebuild the site`);return h}).catch(h=>{throw f=void 0,h})),shard:async h=>(await m.meta()).shards.includes(h)?l(T.shard(h),j.shard(h)):void 0,fragment:h=>l(T.fragment(h),j.fragment(h))};return m}function it(e,t){try{return new URL(e,t).href}catch{return e}}function te(e){return new Promise((t,o)=>{let n=document.createElement("script");n.src=e,n.onload=()=>t(),n.onerror=()=>o(new Error(`failed to load ${e}`)),document.head.append(n)})}return Ft(ee);})();
|
package/dist/entry.d.ts
CHANGED
|
@@ -2,5 +2,5 @@ export { BLOCK, Doc, FACET_NAME, FORMAT, FacetTable, Fragment, Meta, Postings, f
|
|
|
2
2
|
export { shardKey, stemFor, tokenize } from './text.js';
|
|
3
3
|
export { RANK, SearchDoc, SearchIndexBuilder, extractText } from './index.js';
|
|
4
4
|
export { HtmlPage, extractHtml } from './html.js';
|
|
5
|
-
export { DataFor, FacetCounts, ParseOptions, ParsedQuery, QUERY_RANK, Result, SearchData, Source, SourceResult, SourcesOptions, facetCounts, facetNames, idfOf, loadDetails, loadFragments, memoryData, oneEdit, parseQuery, query, querySources, refine, scriptData, search, searchSources, shardsFor } from './query.js';
|
|
5
|
+
export { DataFor, FacetCounts, ParseOptions, ParsedQuery, QUERY_RANK, Result, SearchData, Source, SourceError, SourceResult, SourcesOptions, facetCounts, facetNames, idfOf, loadDetails, loadFragments, memoryData, oneEdit, parseQuery, query, querySources, refine, scriptData, search, searchSources, shardsFor } from './query.js';
|
|
6
6
|
import '@dogsbay/types';
|
package/dist/query.d.ts
CHANGED
|
@@ -146,7 +146,9 @@ interface Source {
|
|
|
146
146
|
/**
|
|
147
147
|
* Indexes with the same `collapse` value show a page once: results with the same URL (a path inside
|
|
148
148
|
* the index, as the indexer stores it) are one result, the best scored, the others in `others`.
|
|
149
|
-
* Give a product's versions its name; leave it out to show every copy.
|
|
149
|
+
* Give a product's versions its name; leave it out to show every copy. Among equal scores the
|
|
150
|
+
* source listed first is kept (the sort is stable), so list the reader's own version first and
|
|
151
|
+
* prereleases last.
|
|
150
152
|
*/
|
|
151
153
|
collapse?: string;
|
|
152
154
|
}
|
|
@@ -171,9 +173,16 @@ interface SourcesOptions {
|
|
|
171
173
|
}
|
|
172
174
|
/**
|
|
173
175
|
* Rank documents across several indexes from the postings alone: one list, merged by score. An
|
|
174
|
-
* index that does not load
|
|
176
|
+
* index that does not load, is in another format, or whose term file fails is left out
|
|
177
|
+
* (`searchSources` lists it, with the errors; this returns the results alone). When every index's
|
|
178
|
+
* term files fail, it rejects with the first error.
|
|
175
179
|
*/
|
|
176
180
|
declare function querySources(q: string | ParsedQuery, sources: Source[], opts?: SourcesOptions): Promise<SourceResult[]>;
|
|
181
|
+
/** A source left out of a search because one of its files failed, and what its load rejected with. */
|
|
182
|
+
interface SourceError {
|
|
183
|
+
source: number;
|
|
184
|
+
error: unknown;
|
|
185
|
+
}
|
|
177
186
|
/**
|
|
178
187
|
* Query several indexes, then refine the top results: a search box over products and versions.
|
|
179
188
|
* `missing` lists the sources left out (not loaded, or another format), by their place in the list.
|
|
@@ -183,6 +192,7 @@ declare function searchSources(q: string, sources: Source[], n?: number, opts?:
|
|
|
183
192
|
results: SourceResult[];
|
|
184
193
|
missing: number[];
|
|
185
194
|
facets: FacetCounts;
|
|
195
|
+
errors: SourceError[];
|
|
186
196
|
}>;
|
|
187
197
|
interface SearchGlobal {
|
|
188
198
|
/** Files that loaded before any index claimed them: key, data, the script's URL. */
|
|
@@ -211,4 +221,4 @@ declare function scriptData(base: string, loadScript: (src: string) => Promise<v
|
|
|
211
221
|
/** SearchData over already-loaded data, keyed as load() keys it (tests, tools). */
|
|
212
222
|
declare function memoryData(store: Map<string, unknown>): SearchData;
|
|
213
223
|
|
|
214
|
-
export { type DataFor, type FacetCounts, type ParseOptions, type ParsedQuery, QUERY_RANK, type Result, type SearchData, type Source, type SourceResult, type SourcesOptions, facetCounts, facetNames, facetValue, idfOf, loadDetails, loadFragments, memoryData, oneEdit, parseQuery, query, querySources, refine, scriptData, search, searchSources, shardsFor };
|
|
224
|
+
export { type DataFor, type FacetCounts, type ParseOptions, type ParsedQuery, QUERY_RANK, type Result, type SearchData, type Source, type SourceError, type SourceResult, type SourcesOptions, facetCounts, facetNames, facetValue, idfOf, loadDetails, loadFragments, memoryData, oneEdit, parseQuery, query, querySources, refine, scriptData, search, searchSources, shardsFor };
|
package/dist/query.js
CHANGED
|
@@ -305,7 +305,15 @@ async function loadFragments(results, data) {
|
|
|
305
305
|
const meta = await d.meta();
|
|
306
306
|
const blocks = [...new Set(rs.map((r) => Math.floor(r.id / meta.block)))];
|
|
307
307
|
const loaded = /* @__PURE__ */ new Map();
|
|
308
|
-
await Promise.all(
|
|
308
|
+
await Promise.all(
|
|
309
|
+
blocks.map(async (b) => {
|
|
310
|
+
try {
|
|
311
|
+
loaded.set(b, await d.fragment(b));
|
|
312
|
+
} catch {
|
|
313
|
+
loaded.set(b, void 0);
|
|
314
|
+
}
|
|
315
|
+
})
|
|
316
|
+
);
|
|
309
317
|
for (const r of rs) r.fragment ??= loaded.get(Math.floor(r.id / meta.block))?.[r.id % meta.block];
|
|
310
318
|
})
|
|
311
319
|
);
|
|
@@ -388,34 +396,60 @@ async function sourceMetas(sources) {
|
|
|
388
396
|
}
|
|
389
397
|
async function querySources(q, sources, opts = {}) {
|
|
390
398
|
const metas = await sourceMetas(sources);
|
|
391
|
-
return rankSources(typeof q === "string" ? parseQuery(q, { facets: facetNames(metas) }) : q, sources, metas, opts);
|
|
399
|
+
return (await rankSources(typeof q === "string" ? parseQuery(q, { facets: facetNames(metas) }) : q, sources, metas, opts)).results;
|
|
392
400
|
}
|
|
393
401
|
async function rankSources(p, sources, all, opts) {
|
|
394
|
-
const live = sources.flatMap((_, i) => all[i] ? [i] : []);
|
|
395
402
|
const metas = all;
|
|
403
|
+
const failed = /* @__PURE__ */ new Set();
|
|
404
|
+
const errors = [];
|
|
405
|
+
const guard = async (i, f) => {
|
|
406
|
+
if (failed.has(i)) return void 0;
|
|
407
|
+
try {
|
|
408
|
+
return await f();
|
|
409
|
+
} catch (error) {
|
|
410
|
+
failed.add(i);
|
|
411
|
+
errors.push({ source: i, error });
|
|
412
|
+
return void 0;
|
|
413
|
+
}
|
|
414
|
+
};
|
|
415
|
+
const live = sources.flatMap((_, i) => all[i] ? [i] : []);
|
|
396
416
|
const excluded = [];
|
|
417
|
+
await Promise.all(live.map(async (i) => excluded[i] = await guard(i, () => excludedDocs(p, metas[i], sources[i].data)) ?? /* @__PURE__ */ new Set()));
|
|
418
|
+
const matched = [];
|
|
419
|
+
const typoTried = [];
|
|
420
|
+
const match = (g, typo) => Promise.all(live.map((i) => guard(i, () => matchGroup(g, metas[i], sources[i].data, typo))));
|
|
421
|
+
const noneHas = (matches) => matches.every((m, j) => !m?.docs.size || failed.has(live[j]));
|
|
422
|
+
for (const g of p.groups) {
|
|
423
|
+
let matches = await match(g, false);
|
|
424
|
+
typoTried.push(noneHas(matches));
|
|
425
|
+
if (typoTried[typoTried.length - 1]) matches = await match(g, true);
|
|
426
|
+
matched.push(matches);
|
|
427
|
+
}
|
|
428
|
+
for (const [gi, g] of p.groups.entries()) {
|
|
429
|
+
if (typoTried[gi] || !noneHas(matched[gi])) continue;
|
|
430
|
+
g.corrected = void 0;
|
|
431
|
+
matched[gi] = await match(g, true);
|
|
432
|
+
}
|
|
433
|
+
if (live.length && live.every((i) => failed.has(i))) throw errors[0].error;
|
|
434
|
+
const ok = live.filter((i) => !failed.has(i));
|
|
397
435
|
const keeps = [];
|
|
398
|
-
|
|
399
|
-
for (const i of live) keeps[i] = facetFilter(p, metas[i]);
|
|
436
|
+
for (const i of ok) keeps[i] = facetFilter(p, metas[i]);
|
|
400
437
|
const tag = (rs, i) => rs.map((r) => ({ ...r, source: i, score: r.score * boostOf(sources[i]) }));
|
|
401
|
-
|
|
438
|
+
const done = (results) => ({ results: collapse(results, sources), failed: [...failed].sort((a, b) => a - b), errors });
|
|
439
|
+
if (!p.groups.length) return done(ok.flatMap((i) => tag(filteredDocs(metas[i], excluded[i], keeps[i]), i)).sort(byScore));
|
|
402
440
|
const perGroup = sources.map(() => []);
|
|
403
|
-
const n =
|
|
404
|
-
|
|
405
|
-
const
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
const corrected = [...new Set(matches.flatMap((m) => m.corrected))];
|
|
441
|
+
const n = ok.reduce((t, i) => t + metas[i].count, 0);
|
|
442
|
+
p.groups.forEach((g, gi) => {
|
|
443
|
+
const matches = matched[gi];
|
|
444
|
+
const kept = live.flatMap((i, j) => failed.has(i) ? [] : [[i, matches[j]]]);
|
|
445
|
+
const corrected = [...new Set(kept.flatMap(([, m]) => m.corrected))];
|
|
409
446
|
g.corrected = corrected.length ? corrected : void 0;
|
|
410
|
-
const df =
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
});
|
|
415
|
-
}
|
|
416
|
-
const ranked = (mode) => live.flatMap((i) => tag(combineGroups(perGroup[i], metas[i], excluded[i], keeps[i], mode), i)).sort(byScore);
|
|
447
|
+
const df = kept.reduce((t, [, m]) => t + m.docs.size, 0);
|
|
448
|
+
for (const [i, m] of kept) perGroup[i].push(groupScores(m, opts.stats === "own" ? idfOf(metas[i].count, m.docs.size) : idfOf(n, df)));
|
|
449
|
+
});
|
|
450
|
+
const ranked = (mode) => ok.flatMap((i) => tag(combineGroups(perGroup[i], metas[i], excluded[i], keeps[i], mode), i)).sort(byScore);
|
|
417
451
|
const and = ranked("and");
|
|
418
|
-
return
|
|
452
|
+
return done(and.length || p.strict ? and : ranked("or"));
|
|
419
453
|
}
|
|
420
454
|
function boostOf(s) {
|
|
421
455
|
return typeof s.boost === "number" && Number.isFinite(s.boost) && s.boost >= 0 ? s.boost : 1;
|
|
@@ -447,10 +481,12 @@ async function searchSources(q, sources, n = 10, opts = {}) {
|
|
|
447
481
|
const stem = stemFor(lang);
|
|
448
482
|
if (stem) parsed.highlight = [.../* @__PURE__ */ new Set([...parsed.highlight, ...parsed.highlight.map(stem).filter((s) => s.length >= 3)])];
|
|
449
483
|
}
|
|
450
|
-
const ranked = await rankSources(parsed, sources, metas, opts);
|
|
484
|
+
const { results: ranked, failed, errors } = await rankSources(parsed, sources, metas, opts);
|
|
485
|
+
for (const i of failed) if (!missing.includes(i)) missing.push(i);
|
|
486
|
+
missing.sort((a, b) => a - b);
|
|
451
487
|
parsed.highlight = [.../* @__PURE__ */ new Set([...parsed.highlight, ...parsed.groups.flatMap((g) => g.corrected ?? [])])];
|
|
452
488
|
const facets = facetCounts(ranked, (r) => metas[r.source]);
|
|
453
|
-
return { parsed, results: await refine(parsed, ranked, (r) => sources[r.source].data, n), missing, facets };
|
|
489
|
+
return { parsed, results: await refine(parsed, ranked, (r) => sources[r.source].data, n), missing, facets, errors };
|
|
454
490
|
}
|
|
455
491
|
function scriptData(base, loadScript, g = globalThis) {
|
|
456
492
|
const page = g.document?.baseURI ?? g.location?.href;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dogsbay/search",
|
|
3
|
-
"version": "0.2.0-beta.
|
|
3
|
+
"version": "0.2.0-beta.153",
|
|
4
4
|
"description": "Static search for docs sites: builds in a Worker, queries from file://",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/entry.js",
|
|
@@ -46,7 +46,7 @@
|
|
|
46
46
|
],
|
|
47
47
|
"dependencies": {
|
|
48
48
|
"stemmer": "^2.0.1",
|
|
49
|
-
"@dogsbay/types": "0.2.0-beta.
|
|
49
|
+
"@dogsbay/types": "0.2.0-beta.153"
|
|
50
50
|
},
|
|
51
51
|
"devDependencies": {
|
|
52
52
|
"@playwright/test": "1.61.1",
|
|
@@ -56,7 +56,7 @@
|
|
|
56
56
|
"tsup": "^8.5.1",
|
|
57
57
|
"typescript": "^5.9.3",
|
|
58
58
|
"vitest": "^4.1.10",
|
|
59
|
-
"@dogsbay/format-openapi": "0.2.0-beta.
|
|
59
|
+
"@dogsbay/format-openapi": "0.2.0-beta.153"
|
|
60
60
|
},
|
|
61
61
|
"license": "MIT",
|
|
62
62
|
"repository": {
|