Skip to content

Commit

Permalink
Merge branch 'main' of github.com:jcuenod/parabible-deno-server
Browse files Browse the repository at this point in the history
  • Loading branch information
jcuenod committed Aug 3, 2024
2 parents 3e97949 + 62e007e commit 15fa7d7
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 31 deletions.
4 changes: 2 additions & 2 deletions src/helpers/termSearchQueryBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }) =>
Expand Down Expand Up @@ -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
Expand Down
78 changes: 57 additions & 21 deletions src/routes/termSearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -80,8 +116,6 @@ const get = ({
return mainResolve({
count,
matchingText: [],
matchingWords: [],
warmWords: [],
});
}

Expand Down Expand Up @@ -143,13 +177,15 @@ const get = ({
]) => {
mainResolve({
count,
matchingText: mapMatchingTextSearchResults(
matchingText: denormalizeParallelTextsIntoSearchResults(
orderedResults,
matchingText,
integrateWordTemperatureIntoParallelTexts(
matchingText,
matchingWords,
warmWords,
),
moduleIds,
),
matchingWords,
warmWords,
});
}).catch(mainReject);
},
Expand Down
9 changes: 1 addition & 8 deletions types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -63,6 +55,7 @@ type WordArray = {
leader?: string
text: string
trailer?: string
temp?: "warm" | "hot" | ""
}[]

type ClickhouseResponse<T> = {
Expand Down

0 comments on commit 15fa7d7

Please sign in to comment.