]> git.openstreetmap.org Git - nominatim.git/blob - sql/tables.sql
fix rank of postcode results
[nominatim.git] / sql / tables.sql
1 drop table if exists import_status;
2 CREATE TABLE import_status (
3   lastimportdate timestamp NOT NULL,
4   sequence_id integer,
5   indexed boolean
6   );
7 GRANT SELECT ON import_status TO "{www-user}" ;
8
9 drop table if exists import_osmosis_log;
10 CREATE TABLE import_osmosis_log (
11   batchend timestamp,
12   batchseq integer,
13   batchsize bigint,
14   starttime timestamp,
15   endtime timestamp,
16   event text
17   );
18
19 CREATE TABLE new_query_log (
20   type text,
21   starttime timestamp,
22   ipaddress text,
23   useragent text,
24   language text,
25   query text,
26   searchterm text,
27   endtime timestamp,
28   results integer,
29   format text,
30   secret text
31   );
32 CREATE INDEX idx_new_query_log_starttime ON new_query_log USING BTREE (starttime);
33 GRANT INSERT ON new_query_log TO "{www-user}" ;
34 GRANT UPDATE ON new_query_log TO "{www-user}" ;
35 GRANT SELECT ON new_query_log TO "{www-user}" ;
36
37 GRANT SELECT ON TABLE country_name TO "{www-user}";
38 GRANT SELECT ON TABLE gb_postcode TO "{www-user}";
39 GRANT SELECT ON TABLE us_postcode TO "{www-user}";
40
41 drop table IF EXISTS word;
42 CREATE TABLE word (
43   word_id INTEGER,
44   word_token text,
45   word text,
46   class text,
47   type text,
48   country_code varchar(2),
49   search_name_count INTEGER,
50   operator TEXT
51   ) {ts:search-data};
52 CREATE INDEX idx_word_word_token on word USING BTREE (word_token) {ts:search-index};
53 GRANT SELECT ON word TO "{www-user}" ;
54 DROP SEQUENCE IF EXISTS seq_word;
55 CREATE SEQUENCE seq_word start 1;
56
57 drop table IF EXISTS location_area CASCADE;
58 CREATE TABLE location_area (
59   place_id BIGINT,
60   keywords INTEGER[],
61   partition SMALLINT,
62   rank_search SMALLINT NOT NULL,
63   rank_address SMALLINT NOT NULL,
64   country_code VARCHAR(2),
65   isguess BOOL,
66   postcode TEXT,
67   centroid GEOMETRY(Point, 4326),
68   geometry GEOMETRY(Geometry, 4326)
69   );
70
71 CREATE TABLE location_area_large () INHERITS (location_area);
72
73 DROP TABLE IF EXISTS location_area_country;
74 CREATE TABLE location_area_country (
75   place_id BIGINT,
76   country_code varchar(2),
77   geometry GEOMETRY(Geometry, 4326)
78   ) {ts:address-data};
79 CREATE INDEX idx_location_area_country_geometry ON location_area_country USING GIST (geometry) {ts:address-index};
80
81
82 drop table IF EXISTS location_property CASCADE;
83 CREATE TABLE location_property (
84   place_id BIGINT,
85   parent_place_id BIGINT,
86   partition SMALLINT,
87   housenumber TEXT,
88   postcode TEXT,
89   centroid GEOMETRY(Point, 4326)
90   );
91
92 CREATE TABLE location_property_aux () INHERITS (location_property);
93 CREATE INDEX idx_location_property_aux_place_id ON location_property_aux USING BTREE (place_id);
94 CREATE INDEX idx_location_property_aux_parent_place_id ON location_property_aux USING BTREE (parent_place_id);
95 CREATE INDEX idx_location_property_aux_housenumber_parent_place_id ON location_property_aux USING BTREE (parent_place_id, housenumber);
96 GRANT SELECT ON location_property_aux TO "{www-user}";
97
98 CREATE TABLE location_property_tiger (
99   place_id BIGINT,
100   parent_place_id BIGINT,
101   startnumber INTEGER,
102   endnumber INTEGER,
103   partition SMALLINT,
104   linegeo GEOMETRY,
105   interpolationtype TEXT,
106   postcode TEXT);
107 GRANT SELECT ON location_property_tiger TO "{www-user}";
108
109 drop table if exists location_property_osmline;
110 CREATE TABLE location_property_osmline (
111     place_id BIGINT NOT NULL,
112     osm_id BIGINT,
113     parent_place_id BIGINT,
114     geometry_sector INTEGER,
115     indexed_date TIMESTAMP,
116     startnumber INTEGER,
117     endnumber INTEGER,
118     partition SMALLINT,
119     indexed_status SMALLINT,
120     linegeo GEOMETRY,
121     interpolationtype TEXT,
122     address HSTORE,
123     postcode TEXT,
124     country_code VARCHAR(2)
125   ){ts:search-data};
126 CREATE UNIQUE INDEX idx_osmline_place_id ON location_property_osmline USING BTREE (place_id) {ts:search-index};
127 CREATE INDEX idx_osmline_geometry_sector ON location_property_osmline USING BTREE (geometry_sector) {ts:address-index};
128 CREATE INDEX idx_osmline_linegeo ON location_property_osmline USING GIST (linegeo) {ts:search-index};
129 GRANT SELECT ON location_property_osmline TO "{www-user}";
130
131 drop table IF EXISTS search_name;
132 CREATE TABLE search_name (
133   place_id BIGINT,
134   importance FLOAT,
135   search_rank SMALLINT,
136   address_rank SMALLINT,
137   name_vector integer[],
138   nameaddress_vector integer[],
139   country_code varchar(2),
140   centroid GEOMETRY(Geometry, 4326)
141   ) {ts:search-data};
142 CREATE INDEX idx_search_name_place_id ON search_name USING BTREE (place_id) {ts:search-index};
143
144 drop table IF EXISTS place_addressline;
145 CREATE TABLE place_addressline (
146   place_id BIGINT,
147   address_place_id BIGINT,
148   distance FLOAT,
149   cached_rank_address SMALLINT,
150   fromarea boolean,
151   isaddress boolean
152   ) {ts:search-data};
153 CREATE INDEX idx_place_addressline_place_id on place_addressline USING BTREE (place_id) {ts:search-index};
154
155 drop table if exists placex;
156 CREATE TABLE placex (
157   place_id BIGINT NOT NULL,
158   parent_place_id BIGINT,
159   linked_place_id BIGINT,
160   importance FLOAT,
161   indexed_date TIMESTAMP,
162   geometry_sector INTEGER,
163   rank_address SMALLINT,
164   rank_search SMALLINT,
165   partition SMALLINT,
166   indexed_status SMALLINT,
167   LIKE place INCLUDING CONSTRAINTS,
168   wikipedia TEXT, -- calculated wikipedia article name (language:title)
169   country_code varchar(2),
170   housenumber TEXT,
171   postcode TEXT,
172   centroid GEOMETRY(Geometry, 4326)
173   ) {ts:search-data};
174 CREATE UNIQUE INDEX idx_place_id ON placex USING BTREE (place_id) {ts:search-index};
175 CREATE INDEX idx_placex_osmid ON placex USING BTREE (osm_type, osm_id) {ts:search-index};
176 CREATE INDEX idx_placex_linked_place_id ON placex USING BTREE (linked_place_id) {ts:address-index} WHERE linked_place_id IS NOT NULL;
177 CREATE INDEX idx_placex_rank_search ON placex USING BTREE (rank_search, geometry_sector) {ts:address-index};
178 CREATE INDEX idx_placex_geometry ON placex USING GIST (geometry) {ts:search-index};
179 CREATE INDEX idx_placex_adminname on placex USING BTREE (make_standard_name(name->'name'),rank_search) {ts:address-index} WHERE osm_type='N' and rank_search < 26;
180
181 DROP SEQUENCE IF EXISTS seq_place;
182 CREATE SEQUENCE seq_place start 1;
183 GRANT SELECT on placex to "{www-user}" ;
184 GRANT SELECT ON search_name to "{www-user}" ;
185 GRANT SELECT on place_addressline to "{www-user}" ;
186 GRANT SELECT ON seq_word to "{www-user}" ;
187 GRANT SELECT ON planet_osm_ways to "{www-user}" ;
188 GRANT SELECT ON planet_osm_rels to "{www-user}" ;
189 GRANT SELECT on location_area to "{www-user}" ;
190
191 -- insert creates the location tables, creates location indexes if indexed == true
192 CREATE TRIGGER placex_before_insert BEFORE INSERT ON placex
193     FOR EACH ROW EXECUTE PROCEDURE placex_insert();
194 CREATE TRIGGER osmline_before_insert BEFORE INSERT ON location_property_osmline
195     FOR EACH ROW EXECUTE PROCEDURE osmline_insert();
196
197 -- update insert creates the location tables
198 CREATE TRIGGER placex_before_update BEFORE UPDATE ON placex
199     FOR EACH ROW EXECUTE PROCEDURE placex_update();
200 CREATE TRIGGER osmline_before_update BEFORE UPDATE ON location_property_osmline
201     FOR EACH ROW EXECUTE PROCEDURE osmline_update();
202
203 -- diff update triggers
204 CREATE TRIGGER placex_before_delete AFTER DELETE ON placex
205     FOR EACH ROW EXECUTE PROCEDURE placex_delete();
206 CREATE TRIGGER place_before_delete BEFORE DELETE ON place
207     FOR EACH ROW EXECUTE PROCEDURE place_delete();
208 CREATE TRIGGER place_before_insert BEFORE INSERT ON place
209     FOR EACH ROW EXECUTE PROCEDURE place_insert();
210
211 -- Table for synthetic postcodes.
212 DROP TABLE IF EXISTS location_postcode;
213 CREATE TABLE location_postcode (
214   place_id BIGINT,
215   parent_place_id BIGINT,
216   rank_search SMALLINT,
217   rank_address SMALLINT,
218   indexed_status SMALLINT,
219   indexed_date TIMESTAMP,
220   country_code varchar(2),
221   postcode TEXT,
222   geometry GEOMETRY(Geometry, 4326)
223   );
224 CREATE INDEX idx_postcode_geometry ON location_postcode USING GIST (geometry) {ts:address-index};
225 GRANT SELECT ON location_postcode TO "{www-user}" ;
226
227 CREATE TRIGGER location_postcode_before_update BEFORE UPDATE ON location_postcode
228     FOR EACH ROW EXECUTE PROCEDURE postcode_update();
229
230 DROP TABLE IF EXISTS import_polygon_error;
231 CREATE TABLE import_polygon_error (
232   osm_id BIGINT,
233   osm_type CHAR(1),
234   class TEXT NOT NULL,
235   type TEXT NOT NULL,
236   name HSTORE,
237   country_code varchar(2),
238   updated timestamp,
239   errormessage text,
240   prevgeometry GEOMETRY(Geometry, 4326),
241   newgeometry GEOMETRY(Geometry, 4326)
242   );
243 CREATE INDEX idx_import_polygon_error_osmid ON import_polygon_error USING BTREE (osm_type, osm_id);
244 GRANT SELECT ON import_polygon_error TO "{www-user}";
245
246 DROP TABLE IF EXISTS import_polygon_delete;
247 CREATE TABLE import_polygon_delete (
248   osm_id BIGINT,
249   osm_type CHAR(1),
250   class TEXT NOT NULL,
251   type TEXT NOT NULL
252   );
253 CREATE INDEX idx_import_polygon_delete_osmid ON import_polygon_delete USING BTREE (osm_type, osm_id);
254 GRANT SELECT ON import_polygon_delete TO "{www-user}";
255
256 DROP SEQUENCE IF EXISTS file;
257 CREATE SEQUENCE file start 1;
258
259 -- null table so it won't error
260 -- deliberately no drop - importing the table is expensive and static, if it is already there better to avoid removing it
261 CREATE TABLE wikipedia_article (
262     language text NOT NULL,
263     title text NOT NULL,
264     langcount integer,
265     othercount integer,
266     totalcount integer,
267     lat double precision,
268     lon double precision,
269     importance double precision,
270     osm_type character(1),
271     osm_id bigint
272 );
273 ALTER TABLE ONLY wikipedia_article ADD CONSTRAINT wikipedia_article_pkey PRIMARY KEY (language, title);
274 CREATE INDEX idx_wikipedia_article_osm_id ON wikipedia_article USING btree (osm_type, osm_id);
275
276 CREATE TABLE wikipedia_redirect (
277     language text,
278     from_title text,
279     to_title text
280 );
281 ALTER TABLE ONLY wikipedia_redirect ADD CONSTRAINT wikipedia_redirect_pkey PRIMARY KEY (language, from_title);
282