Skip to content

Commit

Permalink
fix: more robust check for whether to do exact match search for city
Browse files Browse the repository at this point in the history
Signed-off-by: Evan Prodromou <evan@openearth.org>
  • Loading branch information
evanp committed Jan 7, 2025
1 parent 746efaf commit 6300711
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions api/routes/search.routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,19 +254,28 @@ router.get(
})
);

const caseMatch = (q: string) =>
q
.trim()
.split(/\s+/)
.every((word) => {
return (
word[0] === word[0].toUpperCase() &&
word.slice(1) === word.slice(1).toLowerCase()
);
});

async function searchCity(res: any, q: string) {
const sequelize = connect();

const isLowercase = q[0] === q[0].toLowerCase();

const result = await sequelize.query(
`SELECT DISTINCT a.actor_id, lp.population
FROM
"Actor" a JOIN "ActorName" an ON a.actor_id = an.actor_id
LEFT JOIN "LatestPopulation" lp ON a.actor_id = lp.actor_id
WHERE
a.type = 'city'
AND an.name ${isLowercase ? "ILIKE" : "LIKE"} :search
AND an.name ${caseMatch(q) ? "LIKE" : "ILIKE"} :search
ORDER BY lp.population DESC NULLS LAST
LIMIT 100;`,
{
Expand Down

0 comments on commit 6300711

Please sign in to comment.