Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce data sent to client during search #660

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/lib/search/searchTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,3 +180,23 @@ export type SearchDataWithType =
type: "committees";
data: CommitteeSearchReturnAttributes;
};

export const attributesUsedAsLink: {
members: keyof MemberSearchReturnAttributes;
events: keyof EventSearchReturnAttributes;
articles: keyof ArticleSearchReturnAttributes;
songs: keyof SongSearchReturnAttributes;
positions: keyof PositionSearchReturnAttributes;
committees: keyof CommitteeSearchReturnAttributes;
} = {
members: "studentId",
events: "slug",
articles: "slug",
songs: "slug",
positions: "dsekId",
committees: "shortName",
};

export const listOfattributesUsedAsLink: string[] = Object.values(
attributesUsedAsLink,
) satisfies string[];
18 changes: 17 additions & 1 deletion src/routes/(app)/api/search/+server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import {
getFederatedWeight,
getSearchableAttributes,
} from "$lib/search/searchHelpers";
import type { SearchDataWithType } from "$lib/search/searchTypes";
import {
listOfattributesUsedAsLink,
type SearchDataWithType,
} from "$lib/search/searchTypes";

/**
* This endpoint is used to search multiple indexes at once.
Expand Down Expand Up @@ -73,6 +76,19 @@ export const GET: RequestHandler = async ({ url, locals }) => {
}
hit = hitCopy;
}

// Reduce the amount of data sent to the client
// all string values are capped at 60 characters
for (const key of Object.keys(hit)) {
if (
// We musn't cap the attributes that are used as links
!listOfattributesUsedAsLink.includes(key) &&
typeof hit[key] === "string"
) {
hit[key] = hit[key].slice(0, 60);
}
}

return {
data: hit,
type: hit._federation?.indexUid,
Expand Down
Loading