- SELECT place_id, keywords, rank_address, rank_search, min(ST_Distance(feature, centroid)) as distance, isguess, centroid FROM (
- SELECT * FROM location_area_large_-partition- WHERE ST_Intersects(geometry, feature) and rank_search < maxrank
- UNION ALL
- SELECT * FROM location_area_country WHERE ST_Intersects(geometry, feature) and rank_search < maxrank
- ) as location_area
- GROUP BY place_id, keywords, rank_address, rank_search, isguess, centroid
- ORDER BY rank_address, isin_tokens && keywords desc, isguess asc,
- ST_Distance(feature, centroid) *
- CASE
- WHEN rank_address = 16 AND rank_search = 15 THEN 0.2 -- capital city
- WHEN rank_address = 16 AND rank_search = 16 THEN 0.25 -- city
- WHEN rank_address = 16 AND rank_search = 17 THEN 0.5 -- town
- ELSE 1 END ASC -- everything else
+ SELECT place_id, keywords, rank_address, rank_search, min(ST_Distance(feature, centroid)) as distance, isguess, postcode, centroid
+ FROM location_area_large_-partition-
+ WHERE geometry && feature
+ AND is_relevant_geometry(ST_Relate(geometry, feature), ST_GeometryType(feature))
+ AND rank_address < maxrank
+ GROUP BY place_id, keywords, rank_address, rank_search, isguess, postcode, centroid