From 7494141e92084b70fb0e8cf2ca8606fa83fde84c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?James=20Cu=C3=A9nod?= <4253884+jcuenod@users.noreply.github.com> Date: Mon, 8 Jul 2024 22:25:53 -0500 Subject: [PATCH 1/4] Integrate temp into search results --- src/routes/termSearch.ts | 78 +++++++++++++++++++++++++++++----------- types.d.ts | 9 +---- 2 files changed, 58 insertions(+), 29 deletions(-) diff --git a/src/routes/termSearch.ts b/src/routes/termSearch.ts index 96c1d2a..09a7232 100644 --- a/src/routes/termSearch.ts +++ b/src/routes/termSearch.ts @@ -7,24 +7,60 @@ import { mapTextResult } from "../helpers/mapTextResult.ts"; type MapToTermSearchResponseFunction = ( orderedResults: number[][], - matchingText: ParallelTextQueryResult, + matchingText: DisambiguatedTextResult[], moduleIds: number[], ) => TermSearchTextResponse; -const mapMatchingTextSearchResults: MapToTermSearchResponseFunction = ( - orderedResults, +const denormalizeParallelTextsIntoSearchResults: + MapToTermSearchResponseFunction = ( + orderedResults, + matchingText, + moduleIds, + ) => + orderedResults.map((parallelIds) => + moduleIds.map((moduleId) => + parallelIds.map((parallelId) => { + const row = matchingText.find((row) => + row.parallelId === parallelId && row.moduleId === moduleId + ); + return row || null; + }).filter((parallelText) => !!parallelText) + ) + ); + +const integrateWordTemperatureIntoParallelTexts: ( + matchingText: ParallelTextQueryResult, + matchingWords: WordQueryResult, + warmWords: ModuleWarmWords[], +) => DisambiguatedTextResult[] = ( matchingText, - moduleIds, -) => - orderedResults.map((parallelIds) => - moduleIds.map((moduleId) => - parallelIds.map((parallelId) => { - const row = matchingText.find((row) => - row.parallelId === parallelId && row.moduleId === moduleId - ); - return row ? mapTextResult(row) : null; - }).filter(parallelText => !!parallelText) - ) - ); + matchingWords, + warmWords, +) => { + const warmWordsLookup = warmWords.map(({ wids, moduleId }) => + wids.map((wid) => ({ wid, moduleId })) + ).flat(); + const temperatureMap: { [key: string]: "warm" | "hot" } = { + ...Object.fromEntries(warmWordsLookup.map(({ wid, moduleId }) => [ + `${wid}-${moduleId}`, + "warm", + ])), + ...Object.fromEntries(matchingWords.map(({ wid, moduleId }) => [ + `${wid}-${moduleId}`, + "hot", + ])), + }; + + return matchingText.map((row) => { + const mappedRow = mapTextResult(row); + if (mappedRow.type === "wordArray") { + mappedRow.wordArray = mappedRow.wordArray.map((word) => ({ + ...word, + temp: temperatureMap[`${word.wid}-${row.moduleId}`] || "", + })); + } + return mappedRow; + }); +}; type ModuleWarmWords = { moduleId: number; @@ -80,8 +116,6 @@ const get = ({ return mainResolve({ count, matchingText: [], - matchingWords: [], - warmWords: [], }); } @@ -143,13 +177,15 @@ const get = ({ ]) => { mainResolve({ count, - matchingText: mapMatchingTextSearchResults( + matchingText: denormalizeParallelTextsIntoSearchResults( orderedResults, - matchingText, + integrateWordTemperatureIntoParallelTexts( + matchingText, + matchingWords, + warmWords, + ), moduleIds, ), - matchingWords, - warmWords, }); }).catch(mainReject); }, diff --git a/types.d.ts b/types.d.ts index c65a2ce..f2c798b 100644 --- a/types.d.ts +++ b/types.d.ts @@ -31,14 +31,6 @@ type WordResponse = { type TermSearchResponse = { count: number matchingText: TermSearchTextResponse - matchingWords: { - wid: number - moduleId: number - }[] - warmWords: { - wids: number[] - moduleId: number - }[] } type HighlightResponse = { data: { @@ -63,6 +55,7 @@ type WordArray = { leader?: string text: string trailer?: string + temp?: string }[] type ClickhouseResponse = { From db7067e0d0d29dd87cf89c87eca179cf1865a273 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?James=20Cu=C3=A9nod?= <4253884+jcuenod@users.noreply.github.com> Date: Mon, 8 Jul 2024 22:28:24 -0500 Subject: [PATCH 2/4] Fix type from previous commit --- types.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types.d.ts b/types.d.ts index f2c798b..677ed6e 100644 --- a/types.d.ts +++ b/types.d.ts @@ -55,7 +55,7 @@ type WordArray = { leader?: string text: string trailer?: string - temp?: string + temp?: "warm" | "hot" | "" }[] type ClickhouseResponse = { From ec57fcf8724cd1e19777ba7f4d7b42aec0d63ff5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?James=20Cu=C3=A9nod?= <4253884+jcuenod@users.noreply.github.com> Date: Mon, 8 Jul 2024 22:29:42 -0500 Subject: [PATCH 3/4] Correctly annotate type for helper function --- src/helpers/termSearchQueryBuilder.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/helpers/termSearchQueryBuilder.ts b/src/helpers/termSearchQueryBuilder.ts index 4d60e8b..909c496 100644 --- a/src/helpers/termSearchQueryBuilder.ts +++ b/src/helpers/termSearchQueryBuilder.ts @@ -11,7 +11,7 @@ const treeNode = (tnt: keyof typeof mapTreeNodeTypes) => ? mapTreeNodeTypes[tnt] : "parallel_id" -const toNormalizedKVPairs = (obj: any) => +const toNormalizedKVPairs = (obj: {[key: string]: string}) => Object.keys(obj).map(k => ({ key: k, value: obj[k].normalize("NFC") })) const featureToWhere = ({ key, value }: { key: string; value: string }) => From 9bf129f3b144021df828bc468e77329f6d98bce3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?James=20Cu=C3=A9nod?= <4253884+jcuenod@users.noreply.github.com> Date: Mon, 8 Jul 2024 22:30:20 -0500 Subject: [PATCH 4/4] Fix failure to search when corpusFilter present --- src/helpers/termSearchQueryBuilder.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/helpers/termSearchQueryBuilder.ts b/src/helpers/termSearchQueryBuilder.ts index 909c496..9ec0957 100644 --- a/src/helpers/termSearchQueryBuilder.ts +++ b/src/helpers/termSearchQueryBuilder.ts @@ -77,7 +77,7 @@ const getTermSearchQuery = ({ HAVING ${treeNode(treeNodeType)} > 0 AND ${searchTerms.map(searchTermToHavingLength).join("\n\t\t\tAND ")} - ${parallelIdQuery ? `AND parallel_id IN (${parallelIdQuery})` : ""} + ${parallelIdQuery ? `AND arrayJoin(parallelIdSet) IN (${parallelIdQuery})` : ""} ) t LEFT JOIN ordering_index ON ordering_index.parallel_id = t.lowestParallelId