From 9b2fa1472941d807b58911dc6b9f46a4794848be Mon Sep 17 00:00:00 2001 From: Tommaso Allevi Date: Thu, 19 Dec 2024 10:22:41 +0100 Subject: [PATCH] Optimize search results --- _config.ts | 12 +++++++++--- orama.ts | 29 ++++++++++++++++++++++++++--- search.client.ts | 12 ++++++++++++ 3 files changed, 47 insertions(+), 6 deletions(-) diff --git a/_config.ts b/_config.ts index 6ae98cb66..96de11ad4 100644 --- a/_config.ts +++ b/_config.ts @@ -211,12 +211,11 @@ site.process([".html"], (pages) => { const ORAMA_API_KEY = Deno.env.get("ORAMA_CLOUD_API_KEY"); const ORAMA_INDEX_ID = Deno.env.get("ORAMA_CLOUD_INDEX_ID"); + if (ORAMA_API_KEY && ORAMA_INDEX_ID) { site.process([".html"], async (pages) => { let pageEntries: OramaDocument[] = []; - await oramaClear(ORAMA_API_KEY, ORAMA_INDEX_ID); - for (const page of pages) { if ( page.document && @@ -228,7 +227,14 @@ if (ORAMA_API_KEY && ORAMA_INDEX_ID) { } } - await oramaNotify(ORAMA_API_KEY, ORAMA_INDEX_ID, pageEntries, "pages"); + await oramaClear(ORAMA_API_KEY, ORAMA_INDEX_ID); + + console.log('Pushing', pageEntries.length, 'pages to Orama'); + const chunkSize = 100; + for (let i = 0; i < pageEntries.length; i += chunkSize) { + const chunk = pageEntries.slice(i, i + chunkSize); + await oramaNotify(ORAMA_API_KEY, ORAMA_INDEX_ID, chunk, "pages"); + } await oramaNotify( ORAMA_API_KEY, diff --git a/orama.ts b/orama.ts index 30c7135ca..0a24aaef0 100644 --- a/orama.ts +++ b/orama.ts @@ -8,6 +8,7 @@ export interface OramaDocument { content: string; section: string; category: string; + headerType: string; } export function generateDocumentsForPage(page: Page): OramaDocument[] { @@ -45,9 +46,32 @@ export function generateDocumentsForPage(page: Page): OramaDocument[] { category: page.data.url.startsWith("/runtime/") ? "Runtime" : (page.data.url.startsWith("/deploy/") ? "Deploy" : "Subhosting"), + headerType: header.tagName, }); } + const allData: OramaDocument[] = [] + for (let i = 0; i < documents.length; i++) { + const parents: OramaDocument[] = [] + for (let j = 0; j < i; j++) { + const possibleParent = documents[j] + if (possibleParent.headerType < documents[i].headerType) { + parents.push(possibleParent) + } + } + + const doc = documents[i] + + for (const parent of parents) { + doc[`parent_${parent.headerType}`] = { + section: parent.section, + content: parent.content, + } + } + + allData.push(doc) + } + return documents; } @@ -84,9 +108,8 @@ export async function generateDocumentsForSymbols(): Promise { .replace(/\.html$/, ""), title: node.name, content: node.doc, - section: `API > ${kind}${node.file !== "." ? ` > ${node.file}` : ""}${ - node.category ? ` > ${node.category}` : "" - }`, + section: `API > ${kind}${node.file !== "." ? ` > ${node.file}` : ""}${node.category ? ` > ${node.category}` : "" + }`, category: "Reference", }); } diff --git a/search.client.ts b/search.client.ts index b28f7c14b..13127d115 100644 --- a/search.client.ts +++ b/search.client.ts @@ -110,6 +110,18 @@ document.addEventListener("DOMContentLoaded", () => { "How to configure?", "How to deploy?", ]; + + oramaSearchBox.searchParams = { + boost: { + "content": 100, + "section": 900, + "parent_H2.section": 700, + "title": 1_000, + "parent_H2.content": 1, + "parent_H1.content": 1, + }, + properties: ["title", "section", "content", "parent_H2.section", "parent_H2.content"], + } } const oramaSearchButton: OramaJSX.OramaSearchButton | null = document