My current one : const jobs = await ctx.db.$queryRaw< Array<{ id: string; title: string; description: string; location: string; open: boolean; publishedAt: Date; atsLink: string; enterpriseId: string | null; remote: boolean; enterpriseName: string | null; website: string | null; }> >` WITH question_counts AS ( SELECT "JobForm"."id" AS "jobFormId", COUNT("JobQuestion"."id") AS "questionCount" FROM "JobForm" LEFT JOIN "_JobFormToJobQuestion" AS "JFTJQ" ON "JFTJQ"."A" = "JobForm"."id" LEFT JOIN "JobQuestion" ON "JobQuestion"."id" = "JFTJQ"."B" GROUP BY "JobForm"."id" ), filtered_jobs AS ( SELECT jf.id, jf.title, jf.description, jf.location, jf.open, jf."publishedAt", jf."atsLink", jf."enterpriseId", jf."remote", c.name AS "enterpriseName", c.website, 1 - (jf."locationEmbedded" <=> ${locationEmebeded}::vector) AS "locationSimilarity", 1 - (jf."titleEmbedded" <=> ${queryEmebeded}::vector) AS "titleSimilarity", (3 * (1 - (jf."titleEmbedded" <=> ${queryEmebeded}::vector))) + (0.5 * (1 - (jf."locationEmbedded" <=> ${locationEmebeded}::vector))) AS "weightedSimilarity" FROM "JobForm" jf LEFT JOIN "Company" c ON c.id = jf."enterpriseId" LEFT JOIN question_counts jq ON jq."jobFormId" = jf.id LEFT JOIN "Application" a ON a."jobFormId" = jf.id AND a."userId" = ${ctx.session.user.id} WHERE (${input.remoteOnly} AND jf."remote" = true) OR (NOT ${input.remoteOnly}) AND (jq."questionCount" > 0) AND a.id IS NULL AND 1 - (jf."locationEmbedded" <=> ${locationEmebeded}::vector) > 0.5 AND 1 - (jf."titleEmbedded" <=> ${queryEmebeded}::vector) > 0.35 ) SELECT * FROM filtered_jobs ORDER BY "weightedSimilarity" DESC LIMIT ${input.pageSize} OFFSET ${offset}; `;