From 80b9bc321ca1aba3c09d93e117ccb1fad0398b1c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?James=20Cu=C3=A9nod?= <4253884+jcuenod@users.noreply.github.com> Date: Sat, 8 Jun 2024 09:38:50 -0500 Subject: [PATCH 1/2] Denormalize matchingText in text and termSearch endpoints (Simplifies the API) --- src/helpers/mapTextResult.ts | 25 +++++++++++++++++++++++++ src/routes/termSearch.ts | 23 ++++++++++++++++++++--- src/routes/text.ts | 27 +++++++++++++++++++++++---- types.d.ts | 31 +++++++++++++++++++------------ 4 files changed, 87 insertions(+), 19 deletions(-) create mode 100644 src/helpers/mapTextResult.ts diff --git a/src/helpers/mapTextResult.ts b/src/helpers/mapTextResult.ts new file mode 100644 index 0000000..1ed4bd5 --- /dev/null +++ b/src/helpers/mapTextResult.ts @@ -0,0 +1,25 @@ +export const mapTextResult = ( + parallelTextQueryResultRow: ParallelTextQueryResultRow, +): DisambiguatedTextResult => { + const { parallelId, moduleId, rid, text } = parallelTextQueryResultRow; + try { + const maybeWordArray = JSON.parse(text); + if (Array.isArray(maybeWordArray)) { + return { + parallelId, + moduleId, + rid, + type: "wordArray", + wordArray: maybeWordArray, + html: "", + }; + } + } catch (e) { + // Ignore this path. + + // We can't return here because we need to handle the text being + // HTML but JSON.parse also succeeding (so the output is not the + // expected array), e.g. if the text is surrounded in quotes. + } + return { parallelId, moduleId, rid, type: "html", wordArray: [], html: text }; +}; \ No newline at end of file diff --git a/src/routes/termSearch.ts b/src/routes/termSearch.ts index 6a0733f..0704d0b 100644 --- a/src/routes/termSearch.ts +++ b/src/routes/termSearch.ts @@ -4,6 +4,21 @@ import { getTermSearchQuery } from "../helpers/termSearchQueryBuilder.ts"; import { getTextQuery } from "../helpers/parallelTextQueryBuilder.ts"; import { getWordQuery } from "../helpers/wordMapQueryBuilder.ts"; +const mapMatchingTextSearchResults = ( + orderedResults, + matchingText, + moduleIds, +) => + orderedResults.map((parallelIds) => + moduleIds.map((moduleId) => + parallelIds.map((parallelId) => + matchingText.find((row) => + row.parallelId === parallelId && row.moduleId === moduleId + ) + ) + ) + ); + type ModuleWarmWords = { moduleId: number; wids: number[]; @@ -57,7 +72,6 @@ const get = ({ if (data.length === 0) { return mainResolve({ count, - orderedResults: [[]], matchingText: [], matchingWords: [], warmWords: [], @@ -122,8 +136,11 @@ const get = ({ ]) => { mainResolve({ count, - orderedResults, - matchingText, + matchingText: mapMatchingTextSearchResults( + orderedResults, + matchingText, + moduleIds, + ), matchingWords, warmWords, }); diff --git a/src/routes/text.ts b/src/routes/text.ts index c0cb1b9..54d1ed6 100644 --- a/src/routes/text.ts +++ b/src/routes/text.ts @@ -6,6 +6,18 @@ import { getModuleIdsFromModules, getVersificationSchemaIdFromModuleId, } from "../helpers/moduleInfo.ts"; +import { mapTextResult } from "../helpers/mapTextResult.ts"; + +const getMatchingTextForModuleAndRow = ( + moduleId: number, + parallelId: number, + matchingTextResult: ParallelTextQueryResult, +) => { + const matchingText = matchingTextResult.find( + (row) => row.moduleId === moduleId && row.parallelId === parallelId, + ); + return matchingText ? mapTextResult(matchingText) : null; +}; type Params = { modules: string; @@ -46,10 +58,17 @@ const get = ({ reference, modules }: Params) => matchingText: ParallelTextQueryResult, order: ParallelOrderingResult, ]) => { - mainResolve({ - matchingText, - order: order.map((row) => row.parallelId), - }); + mainResolve( + order.map((row) => + moduleIds.map((moduleId) => + getMatchingTextForModuleAndRow( + moduleId, + row.parallelId, + matchingText, + ) + ) + ), + ); }).catch((error) => { console.error("Error while gathering words and paralel text"); console.error(error); diff --git a/types.d.ts b/types.d.ts index 7caf3c8..f603f13 100644 --- a/types.d.ts +++ b/types.d.ts @@ -29,13 +29,7 @@ type WordResponse = { } type TermSearchResponse = { count: number - orderedResults: number[][] - matchingText: { - parallelId: number - moduleId: number - rid: number - text: string - }[], + matchingText: DisambiguatedTextResult[][] matchingWords: { wid: number moduleId: number @@ -52,11 +46,23 @@ type HighlightResponse = { wid: number }[] } -type TextResponse = { - matchingText: ParallelTextQueryResult, - order: number[] +type TextResponse = (DisambiguatedTextResult | null)[][] +type DisambiguatedTextResult = { + parallelId: number + moduleId: number + rid: number + type: "wordArray" | "html" + wordArray: WordArray + html: string } +type WordArray = { + wid: number + leader?: string + text: string + trailer?: string +}[] + type ClickhouseResponse = { data: T meta: [{ name: string, type: string }] @@ -79,12 +85,13 @@ type WordQueryResult = { type ParallelOrderingResult = { parallelId: number }[] -type ParallelTextQueryResult = { +type ParallelTextQueryResultRow = { parallelId: number moduleId: number rid: number text: string -}[] +} +type ParallelTextQueryResult = ParallelTextQueryResultRow[] type TermSearchQueryResult = { moduleId?: number lowestParallelId: number From e54b97d7053c3f64e153845bb65fce2a054c4368 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?James=20Cu=C3=A9nod?= <4253884+jcuenod@users.noreply.github.com> Date: Sat, 8 Jun 2024 13:07:26 -0500 Subject: [PATCH 2/2] Fix termSearch return type annotations --- src/routes/termSearch.ts | 17 ++++++++++++----- types.d.ts | 6 ++++-- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/src/routes/termSearch.ts b/src/routes/termSearch.ts index 0704d0b..40048b4 100644 --- a/src/routes/termSearch.ts +++ b/src/routes/termSearch.ts @@ -3,19 +3,26 @@ import { getVersificationSchemaIdFromModuleId } from "../helpers/moduleInfo.ts"; import { getTermSearchQuery } from "../helpers/termSearchQueryBuilder.ts"; import { getTextQuery } from "../helpers/parallelTextQueryBuilder.ts"; import { getWordQuery } from "../helpers/wordMapQueryBuilder.ts"; +import { mapTextResult } from "../helpers/mapTextResult.ts"; -const mapMatchingTextSearchResults = ( +type MapToTermSearchResponseFunction = ( + orderedResults: number[][], + matchingText: ParallelTextQueryResult, + moduleIds: number[], +) => TermSearchTextResponse; +const mapMatchingTextSearchResults: MapToTermSearchResponseFunction = ( orderedResults, matchingText, moduleIds, ) => orderedResults.map((parallelIds) => moduleIds.map((moduleId) => - parallelIds.map((parallelId) => - matchingText.find((row) => + parallelIds.map((parallelId) => { + const row = matchingText.find((row) => row.parallelId === parallelId && row.moduleId === moduleId - ) - ) + ); + return row ? mapTextResult(row) : null; + }) ) ); diff --git a/types.d.ts b/types.d.ts index f603f13..c65a2ce 100644 --- a/types.d.ts +++ b/types.d.ts @@ -27,9 +27,10 @@ type WordResponse = { value: string }[] } + type TermSearchResponse = { count: number - matchingText: DisambiguatedTextResult[][] + matchingText: TermSearchTextResponse matchingWords: { wid: number moduleId: number @@ -46,7 +47,6 @@ type HighlightResponse = { wid: number }[] } -type TextResponse = (DisambiguatedTextResult | null)[][] type DisambiguatedTextResult = { parallelId: number moduleId: number @@ -55,6 +55,8 @@ type DisambiguatedTextResult = { wordArray: WordArray html: string } +type TextResponse = (DisambiguatedTextResult | null)[][] +type TermSearchTextResponse = (DisambiguatedTextResult | null)[][][] type WordArray = { wid: number