Skip to content
This repository has been archived by the owner on Nov 5, 2023. It is now read-only.

Commit

Permalink
Merge pull request #220 from helium/madninja/fix_city_search_order
Browse files Browse the repository at this point in the history
Fix city search word order
  • Loading branch information
madninja authored Aug 5, 2021
2 parents d186895 + 52f582e commit 99ee3df
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions migrations/1628163172-location_city_search_word_order.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
-- migrations/1628163172-location_city_search_word_order.sql
-- :up

create or replace function location_city_words(l locations) returns text as $$
begin
return (select string_agg(word, ' ' order by rn)
from (select word, min(rn) as rn
from regexp_split_to_table(
lower(
coalesce(l.long_city, '') || ' ' || coalesce(l.short_city, '') || ' ' ||
coalesce(l.long_state, '') || ' ' || coalesce(l.short_state, '') || ' ' ||
coalesce(l.long_country, '') || ' ' || coalesce(l.short_country, '') || ' '
) , '\s'
) with ordinality x(word, rn) where length(word) >= 3
group by word) x);
end;
$$ language plpgsql;

update locations set search_city = location_city_words(locations::locations);

-- :down

create or replace function location_words(l locations) returns text as $$
begin
return (select string_agg(distinct word, ' ')
from regexp_split_to_table(
lower(
coalesce(l.long_city, '') || ' ' || coalesce(l.short_city, '') || ' ' ||
coalesce(l.long_state, '') || ' ' || coalesce(l.short_state, '') || ' ' ||
coalesce(l.long_country, '') || ' ' || coalesce(l.short_country, '') || ' ' ||
coalesce(l.long_street, '') || ' ' || coalesce(l.short_street, '')
) , '\s'
) as word where length(word) >= 3);
end;
$$ language plpgsql;

update locations set search_city = location_city_words(locations::locations);

0 comments on commit 99ee3df

Please sign in to comment.