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 "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 "tags" ON "kyoo"."show_translations" USING btree ("tags");--> statement-breakpoint
CREATE INDEX "kind" ON "kyoo"."shows" USING hash ("kind");--> 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 "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, showTranslations,
shows, shows,
} from "~/db/schema"; } from "~/db/schema";
import { getColumns, sqlarr } from "~/db/schema/utils"; import { getColumns, sqlarr } from "~/db/utils";
import { KError } from "~/models/error"; import { KError } from "~/models/error";
import { bubble } from "~/models/examples"; import { bubble } from "~/models/examples";
import { import {
@ -26,7 +26,6 @@ import {
isUuid, isUuid,
keysetPaginate, keysetPaginate,
processLanguages, processLanguages,
createPage,
sortToSql, sortToSql,
} from "~/models/utils"; } from "~/models/utils";
import { comment } from "~/utils"; import { comment } from "~/utils";
@ -323,7 +322,7 @@ export const movies = new Elysia({ prefix: "/movies", tags: ["movies"] })
), ),
) )
.orderBy( .orderBy(
...(query // && sort.isDefault ...(query
? [sql`word_similarity(${query}::text, ${showTranslations.name})`] ? [sql`word_similarity(${query}::text, ${showTranslations.name})`]
: sortToSql(sort, shows)), : sortToSql(sort, shows)),
shows.pk, shows.pk,

View File

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