@@ -44,10 +44,9 @@ SELECT
4444 EntityTypeFeedJoin .entities ,
4545 -- locations
4646 FeedLocationJoin .locations ,
47- -- translations
48- FeedCountryTranslationJoin .translations AS country_translations,
49- FeedSubdivisionNameTranslationJoin .translations AS subdivision_name_translations,
50- FeedMunicipalityTranslationJoin .translations AS municipality_translations,
47+ -- osm locations grouped
48+ OsmLocationJoin .osm_locations ,
49+
5150 -- full-text searchable document
5251 setweight(to_tsvector(' english' , coalesce(unaccent(Feed .feed_name ), ' ' )), ' C' ) ||
5352 setweight(to_tsvector(' english' , coalesce(unaccent(Feed .provider ), ' ' )), ' C' ) ||
@@ -61,27 +60,8 @@ SELECT
6160 )
6261 FROM json_array_elements(FeedLocationJoin .locations ) AS location
6362 )), ' ' )), ' A' ) ||
64- setweight(to_tsvector(' english' , coalesce(unaccent((
65- SELECT string_agg(
66- coalesce(translation- >> ' value' , ' ' ),
67- ' '
68- )
69- FROM json_array_elements(FeedCountryTranslationJoin .translations ) AS translation
70- )), ' ' )), ' A' ) ||
71- setweight(to_tsvector(' english' , coalesce(unaccent((
72- SELECT string_agg(
73- coalesce(translation- >> ' value' , ' ' ),
74- ' '
75- )
76- FROM json_array_elements(FeedSubdivisionNameTranslationJoin .translations ) AS translation
77- )), ' ' )), ' A' ) ||
78- setweight(to_tsvector(' english' , coalesce(unaccent((
79- SELECT string_agg(
80- coalesce(translation- >> ' value' , ' ' ),
81- ' '
82- )
83- FROM json_array_elements(FeedMunicipalityTranslationJoin .translations ) AS translation
84- )), ' ' )), ' A' ) AS document
63+ setweight(to_tsvector(' english' , coalesce(unaccent(OsmLocationNamesJoin .osm_location_names ), ' ' )), ' A' )
64+ AS document
8565FROM Feed
8666LEFT JOIN (
8767 SELECT *
@@ -124,50 +104,52 @@ LEFT JOIN (
124104 FROM officialstatushistory
125105 ORDER BY feed_id, timestamp DESC
126106) AS Latest_official_status ON Latest_official_status .feed_id = Feed .id
127- LEFT JOIN (
128- SELECT
129- LocationFeed .feed_id ,
130- json_agg(json_build_object(' value' , Translation .value , ' key' , Translation .key )) AS translations
131- FROM Location
132- LEFT JOIN Translation ON Location .country = Translation .key
133- LEFT JOIN LocationFeed ON LocationFeed .location_id = Location .id
134- WHERE Translation .language_code = ' en'
135- AND Translation .type = ' country'
136- AND Location .country IS NOT NULL
137- GROUP BY LocationFeed .feed_id
138- ) AS FeedCountryTranslationJoin ON FeedCountryTranslationJoin .feed_id = Feed .id
139- LEFT JOIN (
140- SELECT
141- LocationFeed .feed_id ,
142- json_agg(json_build_object(' value' , Translation .value , ' key' , Translation .key )) AS translations
143- FROM Location
144- LEFT JOIN Translation ON Location .subdivision_name = Translation .key
145- LEFT JOIN LocationFeed ON LocationFeed .location_id = Location .id
146- WHERE Translation .language_code = ' en'
147- AND Translation .type = ' subdivision_name'
148- AND Location .subdivision_name IS NOT NULL
149- GROUP BY LocationFeed .feed_id
150- ) AS FeedSubdivisionNameTranslationJoin ON FeedSubdivisionNameTranslationJoin .feed_id = Feed .id
151- LEFT JOIN (
152- SELECT
153- LocationFeed .feed_id ,
154- json_agg(json_build_object(' value' , Translation .value , ' key' , Translation .key )) AS translations
155- FROM Location
156- LEFT JOIN Translation ON Location .municipality = Translation .key
157- LEFT JOIN LocationFeed ON LocationFeed .location_id = Location .id
158- WHERE Translation .language_code = ' en'
159- AND Translation .type = ' municipality'
160- AND Location .municipality IS NOT NULL
161- GROUP BY LocationFeed .feed_id
162- ) AS FeedMunicipalityTranslationJoin ON FeedMunicipalityTranslationJoin .feed_id = Feed .id
163107LEFT JOIN (
164108 SELECT
165109 feed_id,
166110 array_agg(entity_name) AS entities
167111 FROM EntityTypeFeed
168112 GROUP BY feed_id
169113) AS EntityTypeFeedJoin ON EntityTypeFeedJoin .feed_id = Feed .id AND Feed .data_type = ' gtfs_rt'
170- ;
114+ LEFT JOIN (
115+ WITH locations_per_group AS (
116+ SELECT
117+ fog .feed_id ,
118+ olg .group_name ,
119+ jsonb_agg(
120+ DISTINCT jsonb_build_object(
121+ ' admin_level' , gp .admin_level ,
122+ ' name' , gp .name
123+ )
124+ ) AS locations
125+ FROM FeedOsmLocationGroup fog
126+ JOIN OsmLocationGroup olg ON olg .group_id = fog .group_id
127+ JOIN OsmLocationGroupGeopolygon olgg ON olgg .group_id = olg .group_id
128+ JOIN Geopolygon gp ON gp .osm_id = olgg .osm_id
129+ GROUP BY fog .feed_id , olg .group_name
130+ )
131+ SELECT
132+ feed_id,
133+ jsonb_agg(
134+ jsonb_build_object(
135+ ' group_name' , group_name,
136+ ' locations' , locations
137+ )
138+ )::json AS osm_locations
139+ FROM locations_per_group
140+ GROUP BY feed_id
141+ ) AS OsmLocationJoin ON OsmLocationJoin .feed_id = Feed .id
142+ LEFT JOIN (
143+ SELECT
144+ fog .feed_id ,
145+ string_agg(DISTINCT gp .name , ' ' ) AS osm_location_names
146+ FROM FeedOsmLocationGroup fog
147+ JOIN OsmLocationGroup olg ON olg .group_id = fog .group_id
148+ JOIN OsmLocationGroupGeopolygon olgg ON olgg .group_id = olg .group_id
149+ JOIN Geopolygon gp ON gp .osm_id = olgg .osm_id
150+ WHERE gp .name IS NOT NULL
151+ GROUP BY fog .feed_id
152+ ) AS OsmLocationNamesJoin ON OsmLocationNamesJoin .feed_id = Feed .id ;
171153
172154
173155-- This index allows concurrent refresh on the materialized view avoiding table locks
@@ -178,3 +160,6 @@ CREATE INDEX feedsearch_document_idx ON FeedSearch USING GIN(document);
178160CREATE INDEX feedsearch_feed_stable_id ON FeedSearch(feed_stable_id);
179161CREATE INDEX feedsearch_data_type ON FeedSearch(data_type);
180162CREATE INDEX feedsearch_status ON FeedSearch(status);
163+
164+ DROP VIEW IF EXISTS location_with_translations_en;
165+ DROP TABLE IF EXISTS translation;
0 commit comments