X-Git-Url: https://git.openstreetmap.org./nominatim.git/blobdiff_plain/8fcc2bb7f58db967c17a3575f1869c134c60486e..0806e5dff439ef5bdb91a378abb0fd61983d6a24:/nominatim/api/search/db_searches.py?ds=sidebyside diff --git a/nominatim/api/search/db_searches.py b/nominatim/api/search/db_searches.py index ee638bc4..232f816e 100644 --- a/nominatim/api/search/db_searches.py +++ b/nominatim/api/search/db_searches.py @@ -66,7 +66,7 @@ def _select_placex(t: SaFromClause) -> SaSelect: t.c.class_, t.c.type, t.c.address, t.c.extratags, t.c.housenumber, t.c.postcode, t.c.country_code, - t.c.importance, t.c.wikipedia, + t.c.wikipedia, t.c.parent_place_id, t.c.rank_address, t.c.rank_search, t.c.linked_place_id, t.c.admin_level, t.c.centroid, @@ -158,7 +158,8 @@ async def _get_placex_housenumbers(conn: SearchConnection, place_ids: List[int], details: SearchDetails) -> AsyncIterator[nres.SearchResult]: t = conn.t.placex - sql = _select_placex(t).where(t.c.place_id.in_(place_ids)) + sql = _select_placex(t).add_columns(t.c.importance)\ + .where(t.c.place_id.in_(place_ids)) if details.geometry_output: sql = _add_geometry_columns(sql, t.c.geometry, details) @@ -255,9 +256,20 @@ class NearSearch(AbstractSearch): base.sort(key=lambda r: (r.accuracy, r.rank_search)) max_accuracy = base[0].accuracy + 0.5 + if base[0].rank_address == 0: + min_rank = 0 + max_rank = 0 + elif base[0].rank_address < 26: + min_rank = 1 + max_rank = min(25, base[0].rank_address + 4) + else: + min_rank = 26 + max_rank = 30 base = nres.SearchResults(r for r in base if r.source_table == nres.SourceTable.PLACEX and r.accuracy <= max_accuracy - and r.bbox and r.bbox.area < 20) + and r.bbox and r.bbox.area < 20 + and r.rank_address >= min_rank + and r.rank_address <= max_rank) if base: baseids = [b.place_id for b in base[:5] if b.place_id] @@ -307,7 +319,8 @@ class NearSearch(AbstractSearch): .group_by(table.c.place_id).subquery() t = conn.t.placex - sql = _select_placex(t).join(inner, inner.c.place_id == t.c.place_id)\ + sql = _select_placex(t).add_columns((-inner.c.dist).label('importance'))\ + .join(inner, inner.c.place_id == t.c.place_id)\ .order_by(inner.c.dist) sql = sql.where(no_index(t.c.rank_address).between(MIN_RANK_PARAM, MAX_RANK_PARAM)) @@ -350,6 +363,8 @@ class PoiSearch(AbstractSearch): # simply search in placex table def _base_query() -> SaSelect: return _select_placex(t) \ + .add_columns((-t.c.centroid.ST_Distance(NEAR_PARAM)) + .label('importance'))\ .where(t.c.linked_place_id == None) \ .where(t.c.geometry.ST_DWithin(NEAR_PARAM, NEAR_RADIUS_PARAM)) \ .order_by(t.c.centroid.ST_Distance(NEAR_PARAM)) \ @@ -378,6 +393,7 @@ class PoiSearch(AbstractSearch): table = await conn.get_class_table(*category) if table is not None: sql = _select_placex(t)\ + .add_columns(t.c.importance)\ .join(table, t.c.place_id == table.c.place_id)\ .where(t.c.class_ == category[0])\ .where(t.c.type == category[1]) @@ -423,6 +439,7 @@ class CountrySearch(AbstractSearch): ccodes = self.countries.values sql = _select_placex(t)\ + .add_columns(t.c.importance)\ .where(t.c.country_code.in_(ccodes))\ .where(t.c.rank_address == 4) @@ -599,15 +616,7 @@ class PlaceSearch(AbstractSearch): tsearch = conn.t.search_name sql: SaLambdaSelect = sa.lambda_stmt(lambda: - sa.select(t.c.place_id, t.c.osm_type, t.c.osm_id, t.c.name, - t.c.class_, t.c.type, - t.c.address, t.c.extratags, t.c.admin_level, - t.c.housenumber, t.c.postcode, t.c.country_code, - t.c.wikipedia, - t.c.parent_place_id, t.c.rank_address, t.c.rank_search, - t.c.centroid, - t.c.geometry.ST_Expand(0).label('bbox')) - .where(t.c.place_id == tsearch.c.place_id)) + _select_placex(t).where(t.c.place_id == tsearch.c.place_id)) if details.geometry_output: @@ -757,9 +766,6 @@ class PlaceSearch(AbstractSearch): assert result result.bbox = Bbox.from_wkb(row.bbox) result.accuracy = row.accuracy - if not details.excluded or not result.place_id in details.excluded: - results.append(result) - if self.housenumbers and row.rank_address < 30: if row.placex_hnr: subs = _get_placex_housenumbers(conn, row.placex_hnr, details) @@ -779,6 +785,14 @@ class PlaceSearch(AbstractSearch): sub.accuracy += 0.6 results.append(sub) - result.accuracy += 1.0 # penalty for missing housenumber + # Only add the street as a result, if it meets all other + # filter conditions. + if (not details.excluded or result.place_id not in details.excluded)\ + and (not self.qualifiers or result.category in self.qualifiers.values)\ + and result.rank_address >= details.min_rank: + result.accuracy += 1.0 # penalty for missing housenumber + results.append(result) + else: + results.append(result) return results