Fix search sort

This commit is contained in:
Zoe Roux 2025-01-24 21:16:35 +01:00
parent 1e2fd919ff
commit 1634624701
3 changed files with 10 additions and 10 deletions

View File

@ -1,5 +1,7 @@
create extension if not exists pg_trgm;
CREATE INDEX "name_trgm" ON "kyoo"."show_translations" USING gin ("name" gin_trgm_ops);--> statement-breakpoint
CREATE INDEX "tags" ON "kyoo"."show_translations" USING btree ("tags");--> statement-breakpoint
CREATE INDEX "kind" ON "kyoo"."shows" USING hash ("kind");--> statement-breakpoint
CREATE INDEX "rating" ON "kyoo"."shows" USING btree ("rating");--> statement-breakpoint
CREATE INDEX "startAir" ON "kyoo"."shows" USING btree ("start_air");
CREATE INDEX "startAir" ON "kyoo"."shows" USING btree ("start_air");

View File

@ -6,7 +6,7 @@ import {
showTranslations,
shows,
} from "~/db/schema";
import { getColumns, sqlarr } from "~/db/schema/utils";
import { getColumns, sqlarr } from "~/db/utils";
import { KError } from "~/models/error";
import { bubble } from "~/models/examples";
import {
@ -26,7 +26,6 @@ import {
isUuid,
keysetPaginate,
processLanguages,
createPage,
sortToSql,
} from "~/models/utils";
import { comment } from "~/utils";
@ -323,7 +322,7 @@ export const movies = new Elysia({ prefix: "/movies", tags: ["movies"] })
),
)
.orderBy(
...(query // && sort.isDefault
...(query
? [sql`word_similarity(${query}::text, ${showTranslations.name})`]
: sortToSql(sort, shows)),
shows.pk,

View File

@ -12,7 +12,6 @@ export type Sort<
desc: boolean;
}[];
random?: { seed: number };
isDefault?: boolean;
};
export type NonEmptyArray<T> = [T, ...T[]];
@ -29,19 +28,19 @@ export const Sort = <
}: {
default?: T[number][];
description?: string;
remap: Remap;
remap?: Remap;
},
) =>
t
.Transform(
t.Array(
t.Union([
t.Literal("random"),
t.TemplateLiteral("random:${number}"),
t.UnionEnum([
...values,
...values.map((x: T[number]) => `-${x}` as const),
"random",
]),
t.TemplateLiteral("random:${number}"),
]),
{
// TODO: support explode: true (allow sort=slug,-createdAt). needs a pr to elysia
@ -63,10 +62,10 @@ export const Sort = <
sort: sort.map((x) => {
const desc = x[0] === "-";
const key = (desc ? x.substring(1) : x) as T[number];
if (key in remap) return { key: remap[key]!, remmapedKey: key, desc };
if (remap && key in remap)
return { key: remap[key]!, remmapedKey: key, desc };
return { key: key as Exclude<typeof key, keyof Remap>, desc };
}),
isDefault: "isDefault" in sort,
};
})
.Encode(() => {