1 create type nearplace as (
5 create type nearfeature as (
13 CREATE TABLE location_area_country () INHERITS (location_area_large);
14 CREATE INDEX idx_location_area_country_place_id ON location_area_country USING BTREE (place_id);
15 CREATE INDEX idx_location_area_country_geometry ON location_area_country USING GIST (geometry);
17 CREATE TABLE search_name_country () INHERITS (search_name_blank);
18 CREATE INDEX idx_search_name_country_place_id ON search_name_country USING BTREE (place_id);
19 CREATE INDEX idx_search_name_country_centroid ON search_name_country USING GIST (centroid);
20 CREATE INDEX idx_search_name_country_name_vector ON search_name_country USING GIN (name_vector gin__int_ops);
21 CREATE INDEX idx_search_name_country_nameaddress_vector ON search_name_country USING GIN (nameaddress_vector gin__int_ops);
24 CREATE TABLE location_area_large_-partition- () INHERITS (location_area_large);
25 CREATE INDEX idx_location_area_large_-partition-_place_id ON location_area_large_-partition- USING BTREE (place_id);
26 CREATE INDEX idx_location_area_large_-partition-_geometry ON location_area_large_-partition- USING GIST (geometry);
28 CREATE TABLE search_name_-partition- () INHERITS (search_name_blank);
29 CREATE INDEX idx_search_name_-partition-_place_id ON search_name_-partition- USING BTREE (place_id);
30 CREATE INDEX idx_search_name_-partition-_centroid ON search_name_-partition- USING GIST (centroid);
31 CREATE INDEX idx_search_name_-partition-_name_vector ON search_name_-partition- USING GIN (name_vector gin__int_ops);
32 CREATE INDEX idx_search_name_-partition-_nameaddress_vector ON search_name_-partition- USING GIN (nameaddress_vector gin__int_ops);
36 create or replace function getNearFeatures(in_partition INTEGER, point GEOMETRY, maxrank INTEGER, isin_tokens INT[]) RETURNS setof nearfeature AS $$
38 r nearfeature%rowtype;
42 IF in_partition = -partition- THEN
44 SELECT place_id, keywords, rank_address, rank_search, ST_Distance(point, centroid) as distance FROM (
45 SELECT * FROM location_area_large_-partition- WHERE ST_Contains(geometry, point) and rank_search < maxrank
47 SELECT * FROM location_area_country WHERE ST_Contains(geometry, point) and rank_search < maxrank
49 ORDER BY rank_search desc, isin_tokens && keywords desc, isguess asc, rank_address asc, ST_Distance(point, centroid) ASC
57 RAISE EXCEPTION 'Unknown partition %', in_partition;
62 create or replace function deleteLocationArea(in_partition INTEGER, in_place_id integer) RETURNS BOOLEAN AS $$
67 IF in_partition = -partition- THEN
68 DELETE from location_area_large_-partition- WHERE place_id = in_place_id;
73 RAISE EXCEPTION 'Unknown partition %', in_partition;
80 create or replace function insertLocationAreaLarge(
81 in_partition INTEGER, in_place_id integer, in_country_code VARCHAR(2), in_keywords INTEGER[],
82 in_rank_search INTEGER, in_rank_address INTEGER, in_estimate BOOLEAN,
83 in_centroid GEOMETRY, in_geometry GEOMETRY) RETURNS BOOLEAN AS $$
87 IF in_rank_search <= 4 THEN
88 INSERT INTO location_area_country values (in_partition, in_place_id, in_country_code, in_keywords, in_rank_search, in_rank_address, in_estimate, in_centroid, in_geometry);
93 IF in_partition = -partition- THEN
94 INSERT INTO location_area_large_-partition- values (in_partition, in_place_id, in_country_code, in_keywords, in_rank_search, in_rank_address, in_estimate, in_centroid, in_geometry);
99 RAISE EXCEPTION 'Unknown partition %', in_partition;
105 create or replace function getNearestNamedFeature(in_partition INTEGER, point GEOMETRY, maxrank INTEGER, isin_token INTEGER) RETURNS setof nearfeature AS $$
107 r nearfeature%rowtype;
111 IF in_partition = -partition- THEN
113 SELECT place_id, name_vector, address_rank, search_rank,
114 ST_Distance(centroid, point) as distance
115 FROM search_name_-partition-
116 WHERE name_vector @> ARRAY[isin_token]
117 AND search_rank < maxrank
119 SELECT place_id, name_vector, address_rank, search_rank,
120 ST_Distance(centroid, point) as distance
121 FROM search_name_country
122 WHERE name_vector @> ARRAY[isin_token]
123 AND search_rank < maxrank
124 ORDER BY distance ASC limit 1
132 RAISE EXCEPTION 'Unknown partition %', in_partition;
137 create or replace function getNearestNamedRoadFeature(in_partition INTEGER, point GEOMETRY, isin_token INTEGER) RETURNS setof nearfeature AS $$
139 r nearfeature%rowtype;
143 IF in_partition = -partition- THEN
145 SELECT place_id, name_vector, address_rank, search_rank,
146 ST_Distance(centroid, point) as distance
147 FROM search_name_-partition-
148 WHERE name_vector @> ARRAY[isin_token]
149 AND ST_DWithin(centroid, point, 0.01)
150 AND search_rank between 22 and 27
151 ORDER BY distance ASC limit 1
159 RAISE EXCEPTION 'Unknown partition %', in_partition;
164 create or replace function insertSearchName(
165 in_partition INTEGER, in_place_id integer, in_country_code VARCHAR(2),
166 in_name_vector INTEGER[], in_nameaddress_vector INTEGER[],
167 in_rank_search INTEGER, in_rank_address INTEGER,
168 in_centroid GEOMETRY) RETURNS BOOLEAN AS $$
172 INSERT INTO search_name values (in_place_id, in_rank_search, in_rank_address, 0, in_country_code,
173 in_name_vector, in_nameaddress_vector, in_centroid);
175 IF in_rank_search <= 4 THEN
176 INSERT INTO search_name_country values (in_place_id, in_rank_search, in_rank_address, 0, in_country_code,
177 in_name_vector, in_nameaddress_vector, in_centroid);
182 IF in_partition = -partition- THEN
183 INSERT INTO search_name_-partition- values (in_place_id, in_rank_search, in_rank_address, 0, in_country_code,
184 in_name_vector, in_nameaddress_vector, in_centroid);
189 RAISE EXCEPTION 'Unknown partition %', in_partition;
195 create or replace function deleteSearchName(in_partition INTEGER, in_place_id integer) RETURNS BOOLEAN AS $$
199 DELETE from search_name WHERE place_id = in_place_id;
200 DELETE from search_name_country WHERE place_id = in_place_id;
203 IF in_partition = -partition- THEN
204 DELETE from search_name_-partition- WHERE place_id = in_place_id;
209 RAISE EXCEPTION 'Unknown partition %', in_partition;