Skip to content

Commit 1915258

Browse files
committed
better search
1 parent cec00cb commit 1915258

1 file changed

Lines changed: 10 additions & 1 deletion

File tree

src/schema/user/userFetcher.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { SQL, and, asc, desc, ilike, inArray, or } from "drizzle-orm";
1+
import { SQL, and, asc, desc, ilike, inArray, or, sql } from "drizzle-orm";
22

33
import { ORM_TYPE } from "~/datasources/db";
44
import {
@@ -98,6 +98,15 @@ const getSearchUsersQuery = (
9898

9999
const orderBy: SQL<unknown>[] = [];
100100

101+
if (text) {
102+
orderBy.push(sql`GREATEST(
103+
similarity(LOWER(${usersSchema.name}), LOWER(${text})),
104+
similarity(LOWER(${usersSchema.lastName}), LOWER(${text})),
105+
similarity(LOWER(${usersSchema.username}), LOWER(${text})),
106+
similarity(LOWER(${usersSchema.email}), LOWER(${text}))
107+
) DESC`);
108+
}
109+
101110
if (sort) {
102111
const sorts = sort.map(([field, direction]) => {
103112
const sortDirection = direction === "asc" ? asc : desc;

0 commit comments

Comments
 (0)