diff --git a/search/worker/src/query.ts b/search/worker/src/query.ts index c438030..c338619 100644 --- a/search/worker/src/query.ts +++ b/search/worker/src/query.ts @@ -14,17 +14,18 @@ const searchQuery = ` ON e.addr ILIKE '%' || st.term || '%' OR e.description ILIKE '%' || st.term || '%' GROUP BY id, last_updated, type, addr, version, title, description, link_variables, document, popularity, warnings), + max_popularity AS (SELECT max(popularity) AS max_popularity FROM term_matches tm), ranked_entities AS (SELECT *, /* The provider rank fudge ranks providers higher than their resources */ CASE WHEN type = 'provider' THEN 1 ELSE 0 END AS provider_rank_fudge, /* When warnings are present, rank the provider lower because it's likely deprecated. */ CASE WHEN warnings > 0 THEN -1 ELSE 0 END AS warnings_rank_fudge, /* Give a slight boost to providers with a higher star rating. */ - tm.popularity / (SELECT max(popularity) FROM tm) AS popularity_rank, + tm.popularity / (SELECT CASE WHEN max_popularity > 0 THEN max_popularity ELSE 1 END FROM max_popularity) AS popularity_rank, /* Text similarity rankings, each taking a value from 0 to 1. */ similarity(tm.addr, $1) AS title_sim, similarity(tm.description, $1) AS description_sim, - similarity(link_variables ->> 'name', $1) AS name_sim, + similarity(link_variables ->> 'name', $1) AS name_sim FROM term_matches tm), providers AS (SELECT * FROM ranked_entities