-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
big refactor, use federated search instead
- Loading branch information
1 parent
41659e2
commit f66e8c3
Showing
6 changed files
with
243 additions
and
14 deletions.
There are no files selected for viewing
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { availableSearchIndexes } from "./searchTypes"; | ||
|
||
export type SearchIndex = | ||
(typeof availableSearchIndexes)[keyof typeof availableSearchIndexes]; | ||
|
||
export function getFederatedWeight(index: string): number { | ||
switch (index) { | ||
case availableSearchIndexes.members: | ||
return 5; | ||
case availableSearchIndexes.events: | ||
return 1; | ||
case availableSearchIndexes.articles: | ||
return 1; | ||
case availableSearchIndexes.positions: | ||
return 5; | ||
case availableSearchIndexes.songs: | ||
return 1; | ||
default: | ||
return 1; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import type { | ||
Article, | ||
Member, | ||
Event, | ||
Song, | ||
Committee, | ||
Position, | ||
} from "@prisma/client"; | ||
|
||
export const availableSearchIndexes = { | ||
members: "members", | ||
events: "events", | ||
articles: "articles", | ||
positions: "positions", | ||
songs: "songs", | ||
} as const; | ||
|
||
export type SearchMember = Pick< | ||
Member, | ||
| "studentId" | ||
| "firstName" | ||
| "lastName" | ||
| "nickname" | ||
| "picturePath" | ||
| "classYear" | ||
> & { | ||
name: string; | ||
id: string; | ||
}; | ||
export type SearchSong = Pick< | ||
Song, | ||
"title" | "category" | "lyrics" | "melody" | "slug" | ||
> & { | ||
id: string; | ||
}; | ||
export type SearchArticle = Pick< | ||
Article, | ||
"body" | "bodyEn" | "header" | "headerEn" | "slug" | ||
> & { | ||
id: string; | ||
}; | ||
export type SearchEvent = Pick< | ||
Event, | ||
"title" | "titleEn" | "description" | "descriptionEn" | "slug" | ||
> & { | ||
id: string; | ||
}; | ||
export type SearchPosition = Pick< | ||
Position, | ||
"committeeId" | "description" | "descriptionEn" | "name" | "nameEn" | ||
> & { | ||
committee: Committee | null; | ||
id: string; | ||
dsekId: Position["id"]; | ||
}; | ||
|
||
export type SearchDataWithType = | ||
| { | ||
type: "members"; | ||
data: SearchMember; | ||
} | ||
| { | ||
type: "events"; | ||
data: SearchEvent; | ||
} | ||
| { | ||
type: "articles"; | ||
data: SearchArticle; | ||
} | ||
| { | ||
type: "songs"; | ||
data: SearchSong; | ||
} | ||
| { | ||
type: "positions"; | ||
data: SearchPosition; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,42 @@ | ||
import type { SearchDataWithType } from "$lib/search/searchTypes"; | ||
import type { Actions } from "./$types"; | ||
import type { Hits } from "meilisearch"; | ||
|
||
export const actions = { | ||
default: async (event) => { | ||
const data = await event.request.formData(); | ||
const search = data.get("input"); | ||
if (typeof search !== "string") return; | ||
const response = await event.fetch( | ||
"/api/members?" + new URLSearchParams({ search }), | ||
); | ||
const users = await response.json(); | ||
return { users }; | ||
|
||
const query = data.get("input"); | ||
if (typeof query !== "string") return; | ||
|
||
const includeMembers = data.get("members") === "on"; | ||
const includeEvents = data.get("events") === "on"; | ||
const includeArticles = data.get("articles") === "on"; | ||
const includePositions = data.get("positions") === "on"; | ||
const includeSongs = data.get("songs") === "on"; | ||
const indexes = []; | ||
if (includeMembers) indexes.push("members"); | ||
if (includeEvents) indexes.push("events"); | ||
if (includeArticles) indexes.push("articles"); | ||
if (includePositions) indexes.push("positions"); | ||
if (includeSongs) indexes.push("songs"); | ||
|
||
const url = new URL("/api/search", event.request.url); | ||
url.searchParams.set("query", query); | ||
url.searchParams.set("indexes", JSON.stringify(indexes)); | ||
const response = await event.fetch(url); | ||
if (!response.ok) { | ||
// silently fail | ||
return; | ||
} | ||
const json: Hits = await response.json(); | ||
return { | ||
results: json.map((hit) => { | ||
return { | ||
data: hit, | ||
type: hit._federation?.indexUid, | ||
} as SearchDataWithType; | ||
}), | ||
}; | ||
}, | ||
} satisfies Actions; |