Skip to content

Commit

Permalink
feat: add strict inclusion to search results
Browse files Browse the repository at this point in the history
Signed-off-by: Maud Royer <hello@maudroyer.fr>
  • Loading branch information
jillro committed Oct 10, 2024
1 parent a7750c4 commit 63ef022
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/db/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,15 @@ export const getResults = unstable_cache(async function (
.select(({ fn, val }) => [
fn("word_similarity", [fn("unaccent", [val(query)]), "token"]).as("sml"),
])
.where(sql<boolean>`token %> unaccent(${query})`)
.where(
query.length > 2 // if the query is too short, we don't do ilike search to avoid too many results
? ({ eb }) =>
eb.or([
sql<boolean>`token %> unaccent(${query})`,
eb("token", "ilike", `%${query}%`),
])
: sql<boolean>`token %> unaccent(${query})`,
)
.orderBy("sml", "desc")
.orderBy(({ fn }) => fn("length", ["token"]));

Expand Down

0 comments on commit 63ef022

Please sign in to comment.