From 4305160c91ffb2aebb54353e768821df46a49756 Mon Sep 17 00:00:00 2001 From: Sarah Hoffmann Date: Sun, 7 Jan 2024 11:11:11 +0100 Subject: [PATCH 1/3] prioritize country searches when penaly is equal --- nominatim/api/search/db_searches.py | 5 +++++ nominatim/api/search/geocoder.py | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/nominatim/api/search/db_searches.py b/nominatim/api/search/db_searches.py index 254d2ca67..68447f6a7 100644 --- a/nominatim/api/search/db_searches.py +++ b/nominatim/api/search/db_searches.py @@ -245,6 +245,7 @@ async def _get_tiger(conn: SearchConnection, place_ids: List[int], class AbstractSearch(abc.ABC): """ Encapuslation of a single lookup in the database. """ + SEARCH_PRIO: int = 2 def __init__(self, penalty: float) -> None: self.penalty = penalty @@ -448,6 +449,8 @@ def _base_query() -> SaSelect: class CountrySearch(AbstractSearch): """ Search for a country name or country code. """ + SEARCH_PRIO = 0 + def __init__(self, sdata: SearchData) -> None: super().__init__(sdata.penalty) self.countries = sdata.countries @@ -604,6 +607,8 @@ async def lookup(self, conn: SearchConnection, class PlaceSearch(AbstractSearch): """ Generic search for an address or named place. """ + SEARCH_PRIO = 1 + def __init__(self, extra_penalty: float, sdata: SearchData, expected_count: int) -> None: super().__init__(sdata.penalty + extra_penalty) self.countries = sdata.countries diff --git a/nominatim/api/search/geocoder.py b/nominatim/api/search/geocoder.py index bb3c6a1c8..6db7c9cef 100644 --- a/nominatim/api/search/geocoder.py +++ b/nominatim/api/search/geocoder.py @@ -64,7 +64,7 @@ async def build_searches(self, log().table_dump('Searches for assignment', _dump_searches(searches, query, num_searches)) num_searches = len(searches) - searches.sort(key=lambda s: s.penalty) + searches.sort(key=lambda s: (s.penalty, s.SEARCH_PRIO)) return query, searches From 7337898b84963791ad59e8a02808b04ecdf04b93 Mon Sep 17 00:00:00 2001 From: Sarah Hoffmann Date: Sun, 7 Jan 2024 15:37:53 +0100 Subject: [PATCH 2/3] dump params in log view --- nominatim/api/search/geocoder.py | 1 + 1 file changed, 1 insertion(+) diff --git a/nominatim/api/search/geocoder.py b/nominatim/api/search/geocoder.py index 6db7c9cef..6b58c9457 100644 --- a/nominatim/api/search/geocoder.py +++ b/nominatim/api/search/geocoder.py @@ -85,6 +85,7 @@ async def execute_searches(self, query: QueryStruct, if search.penalty > prev_penalty and (search.penalty > min_ranking or i > 20): break log().table_dump(f"{i + 1}. Search", _dump_searches([search], query)) + log().var_dump('Params', self.params) lookup_results = await search.lookup(self.conn, self.params) for result in lookup_results: rhash = (result.source_table, result.place_id, From b2afe3ce3ec7df3691a85462802b547b3d34ce4a Mon Sep 17 00:00:00 2001 From: Sarah Hoffmann Date: Sun, 7 Jan 2024 17:29:12 +0100 Subject: [PATCH 3/3] when a country is in the results, restrict further searches to places A country search result usually comes with a very high importance. As a result only other very well known places will show up together with country results and that means only places with lower address ranks. Name searches for country names tend to yield a lot of POI results because the country name is part of the name (think "embassy of Sweden"). By excluding POIs from further searches, the search is sped up quite a bit. --- nominatim/api/search/db_searches.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/nominatim/api/search/db_searches.py b/nominatim/api/search/db_searches.py index 68447f6a7..555819e74 100644 --- a/nominatim/api/search/db_searches.py +++ b/nominatim/api/search/db_searches.py @@ -484,7 +484,14 @@ async def lookup(self, conn: SearchConnection, result.bbox = Bbox.from_wkb(row.bbox) results.append(result) - return results or await self.lookup_in_country_table(conn, details) + if not results: + results = await self.lookup_in_country_table(conn, details) + + if results: + details.min_rank = min(5, details.max_rank) + details.max_rank = min(25, details.max_rank) + + return results async def lookup_in_country_table(self, conn: SearchConnection,