Skip to content

Commit

Permalink
feat: add promises search index and include promise in search result
Browse files Browse the repository at this point in the history
  • Loading branch information
Th1nkK1D committed Nov 28, 2024
1 parent 96c506b commit 7627d58
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 6 deletions.
3 changes: 2 additions & 1 deletion src/components/NavigationBar/SearchContainer.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@
categories={[
SearchIndexCategory.Politicians,
SearchIndexCategory.Votings,
SearchIndexCategory.Bills
SearchIndexCategory.Bills,
SearchIndexCategory.Promises
]}
name="navSearch"
type="text"
Expand Down
14 changes: 12 additions & 2 deletions src/lib/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,16 @@ export function search(
url: '/bills/explore?proposername=' + proposer.item.name
})
)
: undefined,
promises: searchIndexes.promises
? getScoredAndHighlightedResultItems(queries, searchIndexes.promises, keepTopN).map(
(promise) => ({
heading: promise.item.name.slice(0, 16),
headingHighlight: highlight ? promise.highlightedName : undefined,
promiseStatus: promise.item.status,
url: '/promises/' + promise.item.id
})
)
: undefined
};
}
Expand All @@ -103,7 +113,7 @@ function getScoredAndHighlightedResultItems<T extends { name: string }>(
searchIndexes: T[],
keepTopN: number
) {
return postCalcuateScore(calculateScore(queries, searchIndexes), keepTopN);
return postCalculateScore(calculateScore(queries, searchIndexes), keepTopN);
}

/**
Expand Down Expand Up @@ -179,7 +189,7 @@ export function calculateScore<T extends { name: string }>(
* @param keepTopN - The number of top candidates to return.
* @returns - The list of top candidates with highlighted matched indices.
*/
function postCalcuateScore<T extends { name: string }>(
function postCalculateScore<T extends { name: string }>(
candidates: ScoreResultItem<T>[],
keepTopN = 5
): ScoreAndHighlightResultItem<T>[] {
Expand Down
9 changes: 7 additions & 2 deletions src/models/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ export enum SearchIndexCategory {
Politicians = 'politicians',
Bills = 'bills',
Votings = 'votings',
BillProposers = 'billProposers'
BillProposers = 'billProposers',
Promises = 'promises'
}

// TODO - เช็คว่าตัวอื่นต้องใช้อะไรในการ Link ไปหน้านั้นๆ
export interface SearchIndexes {
[SearchIndexCategory.Politicians]?: {
id: string;
Expand All @@ -30,6 +30,11 @@ export interface SearchIndexes {
description: string;
proposedBillsCount: number;
}[];
[SearchIndexCategory.Promises]?: {
id: string;
name: string;
status: PromiseStatus;
}[];
}

interface BaseSearchResultItem {
Expand Down
1 change: 1 addition & 0 deletions src/routes/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@
title="คำสัญญาทางการเมือง"
icon={PromiseIcon}
searchPlaceholder="ค้นหาด้วยคำสัญญา เช่น กระเป๋าเงินดิจิทัล"
searchCategories={[SearchIndexCategory.Promises]}
class="bg-ui-white"
>
<span slot="description"
Expand Down
19 changes: 18 additions & 1 deletion src/routes/files/search-indexes/[category].json/+server.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { fetchPoliticians, fetchVotings, fetchBills } from '$lib/datasheets/index.js';
import {
fetchPoliticians,
fetchVotings,
fetchBills,
fetchPromises
} from '$lib/datasheets/index.js';
import { BillProposerType } from '$models/bill';
import type { AssemblyRoleHistory, PartyRoleHistory } from '$models/politician';
import { SearchIndexCategory, type SearchIndexes } from '$models/search';
Expand Down Expand Up @@ -98,6 +103,18 @@ export async function GET({ params }) {
return createJSONFileResponse(indexes);
}

case SearchIndexCategory.Promises: {
const indexes: SearchIndexes['promises'] = (await fetchPromises()).map(
({ id, statements, status }) => ({
id,
name: statements[0],
status
})
);

return createJSONFileResponse(indexes);
}

default:
error(404);
}
Expand Down

0 comments on commit 7627d58

Please sign in to comment.