Skip to content

Commit 4fa3493

Browse files
authored
Merge pull request #3362 from lonvia/find-postcode-areas
Lookup postcode areas for postcode results
2 parents e5a5f02 + 50beac8 commit 4fa3493

File tree

6 files changed

+51
-12
lines changed

6 files changed

+51
-12
lines changed

lib-sql/indices.sql

+5
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ CREATE INDEX IF NOT EXISTS idx_placex_parent_place_id
2121
ON placex USING BTREE (parent_place_id) {{db.tablespace.search_index}}
2222
WHERE parent_place_id IS NOT NULL;
2323
---
24+
-- Used to find postcode areas after a search in location_postcode.
25+
CREATE INDEX IF NOT EXISTS idx_placex_postcode_areas
26+
ON placex USING BTREE (country_code, postcode) {{db.tablespace.search_index}}
27+
WHERE osm_type = 'R' AND class = 'boundary' AND type = 'postal_code';
28+
---
2429
CREATE INDEX IF NOT EXISTS idx_placex_geometry ON placex
2530
USING GIST (geometry) {{db.tablespace.search_index}};
2631
-- Index is needed during import but can be dropped as soon as a full

nominatim/api/search/db_searches.py

+17-3
Original file line numberDiff line numberDiff line change
@@ -602,10 +602,24 @@ async def lookup(self, conn: SearchConnection,
602602

603603
results = nres.SearchResults()
604604
for row in await conn.execute(sql, _details_to_bind_params(details)):
605-
result = nres.create_from_postcode_row(row, nres.SearchResult)
605+
p = conn.t.placex
606+
placex_sql = _select_placex(p).add_columns(p.c.importance)\
607+
.where(sa.text("""class = 'boundary'
608+
AND type = 'postal_code'
609+
AND osm_type = 'R'"""))\
610+
.where(p.c.country_code == row.country_code)\
611+
.where(p.c.postcode == row.postcode)\
612+
.limit(1)
613+
for prow in await conn.execute(placex_sql, _details_to_bind_params(details)):
614+
result = nres.create_from_placex_row(prow, nres.SearchResult)
615+
break
616+
else:
617+
result = nres.create_from_postcode_row(row, nres.SearchResult)
618+
606619
assert result
607-
result.accuracy = row.accuracy
608-
results.append(result)
620+
if result.place_id not in details.excluded:
621+
result.accuracy = row.accuracy
622+
results.append(result)
609623

610624
return results
611625

nominatim/tools/migration.py

+10
Original file line numberDiff line numberDiff line change
@@ -382,3 +382,13 @@ def add_improved_geometry_reverse_placenode_index(conn: Connection, **_: Any) ->
382382
WHERE rank_address between 4 and 25 AND type != 'postcode'
383383
AND name is not null AND linked_place_id is null AND osm_type = 'N'
384384
""")
385+
386+
@_migration(4, 4, 99, 0)
387+
def create_postcode_ara_lookup_index(conn: Connection, **_: Any) -> None:
388+
""" Create index needed for looking up postcode areas from postocde points.
389+
"""
390+
with conn.cursor() as cur:
391+
cur.execute("""CREATE INDEX IF NOT EXISTS idx_placex_postcode_areas
392+
ON placex USING BTREE (country_code, postcode)
393+
WHERE osm_type = 'R' AND class = 'boundary' AND type = 'postal_code'
394+
""")

nominatim/version.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def __str__(self) -> str:
3434
return f"{self.major}.{self.minor}.{self.patch_level}-{self.db_patch_level}"
3535

3636

37-
NOMINATIM_VERSION = NominatimVersion(4, 4, 0, 0)
37+
NOMINATIM_VERSION = NominatimVersion(4, 4, 99, 0)
3838

3939
POSTGRESQL_REQUIRED_VERSION = (9, 6)
4040
POSTGIS_REQUIRED_VERSION = (2, 2)

test/bdd/api/details/simple.feature

-8
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,6 @@
33
Feature: Object details
44
Check details page for correctness
55

6-
Scenario: Details by place ID
7-
When sending details query for 107077
8-
Then the result is valid json
9-
And results contain
10-
| place_id |
11-
| 107077 |
12-
13-
146
Scenario Outline: Details via OSM id
157
When sending details query for <type><id>
168
Then the result is valid json

test/bdd/db/query/postcodes.feature

+18
Original file line numberDiff line numberDiff line change
@@ -95,3 +95,21 @@ Feature: Querying fo postcode variants
9595
| type | display_name |
9696
| postcode | E4 7EA, United Kingdom |
9797

98+
99+
@fail-legacy
100+
@v1-api-python-only
101+
Scenario: Postcode areas are preferred over postcode points
102+
Given the grid with origin DE
103+
| 1 | 2 |
104+
| 4 | 3 |
105+
Given the places
106+
| osm | class | type | postcode | geometry |
107+
| R23 | boundary | postal_code | 12345 | (1,2,3,4,1) |
108+
When importing
109+
Then location_postcode contains exactly
110+
| country | postcode |
111+
| de | 12345 |
112+
When sending search query "12345, de"
113+
Then results contain
114+
| osm |
115+
| R23 |

0 commit comments

Comments
 (0)