diff --git a/src/helpers/termSearchQueryBuilder.ts b/src/helpers/termSearchQueryBuilder.ts index 4d60e8b..9ec0957 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 }) => @@ -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 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..677ed6e 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?: "warm" | "hot" | "" }[] type ClickhouseResponse = {