Skip to content

Commit

Permalink
Reduce data sent to client during search (#660)
Browse files Browse the repository at this point in the history
* reduce data sent to client during search
  • Loading branch information
alfredgrip authored Dec 30, 2024
1 parent 3f2120f commit d0e72c6
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
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 truncated to 60 characters
for (const key of Object.keys(hit)) {
if (
// We must not truncate 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

0 comments on commit d0e72c6

Please sign in to comment.