Skip to content

Commit 4ce13f5

Browse files
committed
prefilter bad results before adding details and reranking
Move the first cutting of the result list before reranking by result match. This means that results with significantly less importance are removed early and independently of the fact how well they match the original query. Fixes #3266.
1 parent 2833362 commit 4ce13f5

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

nominatim/api/search/geocoder.py

+15-5
Original file line numberDiff line numberDiff line change
@@ -104,19 +104,27 @@ async def execute_searches(self, query: QueryStruct,
104104
return SearchResults(results.values())
105105

106106

107+
def pre_filter_results(self, results: SearchResults) -> SearchResults:
108+
""" Remove results that are significantly worse than the
109+
best match.
110+
"""
111+
if results:
112+
max_ranking = min(r.ranking for r in results) + 0.5
113+
results = SearchResults(r for r in results if r.ranking < max_ranking)
114+
115+
return results
116+
117+
107118
def sort_and_cut_results(self, results: SearchResults) -> SearchResults:
108119
""" Remove badly matching results, sort by ranking and
109120
limit to the configured number of results.
110121
"""
111122
if results:
112-
min_ranking = min(r.ranking for r in results)
113-
results = SearchResults(r for r in results if r.ranking < min_ranking + 0.5)
114123
results.sort(key=lambda r: r.ranking)
115-
116-
if results:
117124
min_rank = results[0].rank_search
125+
min_ranking = results[0].ranking
118126
results = SearchResults(r for r in results
119-
if r.ranking + 0.05 * (r.rank_search - min_rank)
127+
if r.ranking + 0.03 * (r.rank_search - min_rank)
120128
< min_ranking + 0.5)
121129

122130
results = SearchResults(results[:self.limit])
@@ -174,6 +182,7 @@ async def lookup_pois(self, categories: List[Tuple[str, str]],
174182
if query:
175183
searches = [wrap_near_search(categories, s) for s in searches[:50]]
176184
results = await self.execute_searches(query, searches)
185+
results = self.pre_filter_results(results)
177186
await add_result_details(self.conn, results, self.params)
178187
log().result_dump('Preliminary Results', ((r.accuracy, r) for r in results))
179188
results = self.sort_and_cut_results(results)
@@ -203,6 +212,7 @@ async def lookup(self, phrases: List[Phrase]) -> SearchResults:
203212
if searches:
204213
# Execute SQL until an appropriate result is found.
205214
results = await self.execute_searches(query, searches[:50])
215+
results = self.pre_filter_results(results)
206216
await add_result_details(self.conn, results, self.params)
207217
log().result_dump('Preliminary Results', ((r.accuracy, r) for r in results))
208218
self.rerank_by_query(query, results)

0 commit comments

Comments
 (0)