Skip to content

Commit 1015ac4

Browse files
authored
Merge pull request #3332 from lonvia/improve-cutting-of-result-list
Prefilter bad results before adding details and reranking
2 parents 2833362 + 4ce13f5 commit 1015ac4

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)