]> git.openstreetmap.org Git - nominatim.git/blob - test/python/api/search/test_search_places.py
enable flake for Python tests
[nominatim.git] / test / python / api / search / test_search_places.py
1 # SPDX-License-Identifier: GPL-3.0-or-later
2 #
3 # This file is part of Nominatim. (https://nominatim.org)
4 #
5 # Copyright (C) 2025 by the Nominatim developer community.
6 # For a full list of authors see the git log.
7 """
8 Tests for running the generic place searcher.
9 """
10 import json
11
12 import pytest
13
14 import nominatim_api as napi
15 from nominatim_api.types import SearchDetails
16 from nominatim_api.search.db_searches import PlaceSearch
17 from nominatim_api.search.db_search_fields import WeightedStrings, WeightedCategories, \
18                                                   FieldLookup, FieldRanking, RankedTokens
19 from nominatim_api.search.db_search_lookups import LookupAll, LookupAny, Restrict
20
21 APIOPTIONS = ['search']
22
23
24 def run_search(apiobj, frontend, global_penalty, lookup, ranking, count=2,
25                hnrs=[], pcs=[], ccodes=[], quals=[],
26                details=SearchDetails()):
27     class MySearchData:
28         penalty = global_penalty
29         postcodes = WeightedStrings(pcs, [0.0] * len(pcs))
30         countries = WeightedStrings(ccodes, [0.0] * len(ccodes))
31         housenumbers = WeightedStrings(hnrs, [0.0] * len(hnrs))
32         qualifiers = WeightedCategories(quals, [0.0] * len(quals))
33         lookups = lookup
34         rankings = ranking
35
36     search = PlaceSearch(0.0, MySearchData(), count)
37
38     if frontend is None:
39         api = apiobj
40     else:
41         api = frontend(apiobj, options=APIOPTIONS)
42
43     async def run():
44         async with api._async_api.begin() as conn:
45             return await search.lookup(conn, details)
46
47     results = api._loop.run_until_complete(run())
48     results.sort(key=lambda r: r.accuracy)
49
50     return results
51
52
53 class TestNameOnlySearches:
54
55     @pytest.fixture(autouse=True)
56     def fill_database(self, apiobj):
57         apiobj.add_placex(place_id=100, country_code='us',
58                           centroid=(5.6, 4.3))
59         apiobj.add_search_name(100, names=[1, 2, 10, 11], country_code='us',
60                                centroid=(5.6, 4.3))
61         apiobj.add_placex(place_id=101, country_code='mx',
62                           centroid=(-10.3, 56.9))
63         apiobj.add_search_name(101, names=[1, 2, 20, 21], country_code='mx',
64                                centroid=(-10.3, 56.9))
65
66     @pytest.mark.parametrize('lookup_type', [LookupAll, Restrict])
67     @pytest.mark.parametrize('rank,res', [([10], [100, 101]),
68                                           ([20], [101, 100])])
69     def test_lookup_all_match(self, apiobj, frontend, lookup_type, rank, res):
70         lookup = FieldLookup('name_vector', [1, 2], lookup_type)
71         ranking = FieldRanking('name_vector', 0.4, [RankedTokens(0.0, rank)])
72
73         results = run_search(apiobj, frontend, 0.1, [lookup], [ranking])
74
75         assert [r.place_id for r in results] == res
76
77     @pytest.mark.parametrize('lookup_type', [LookupAll, Restrict])
78     def test_lookup_all_partial_match(self, apiobj, frontend, lookup_type):
79         lookup = FieldLookup('name_vector', [1, 20], lookup_type)
80         ranking = FieldRanking('name_vector', 0.4, [RankedTokens(0.0, [21])])
81
82         results = run_search(apiobj, frontend, 0.1, [lookup], [ranking])
83
84         assert len(results) == 1
85         assert results[0].place_id == 101
86
87     @pytest.mark.parametrize('rank,res', [([10], [100, 101]),
88                                           ([20], [101, 100])])
89     def test_lookup_any_match(self, apiobj, frontend, rank, res):
90         lookup = FieldLookup('name_vector', [11, 21], LookupAny)
91         ranking = FieldRanking('name_vector', 0.4, [RankedTokens(0.0, rank)])
92
93         results = run_search(apiobj, frontend, 0.1, [lookup], [ranking])
94
95         assert [r.place_id for r in results] == res
96
97     def test_lookup_any_partial_match(self, apiobj, frontend):
98         lookup = FieldLookup('name_vector', [20], LookupAll)
99         ranking = FieldRanking('name_vector', 0.4, [RankedTokens(0.0, [21])])
100
101         results = run_search(apiobj, frontend, 0.1, [lookup], [ranking])
102
103         assert len(results) == 1
104         assert results[0].place_id == 101
105
106     @pytest.mark.parametrize('cc,res', [('us', 100), ('mx', 101)])
107     def test_lookup_restrict_country(self, apiobj, frontend, cc, res):
108         lookup = FieldLookup('name_vector', [1, 2], LookupAll)
109         ranking = FieldRanking('name_vector', 0.4, [RankedTokens(0.0, [10])])
110
111         results = run_search(apiobj, frontend, 0.1, [lookup], [ranking], ccodes=[cc])
112
113         assert [r.place_id for r in results] == [res]
114
115     def test_lookup_restrict_placeid(self, apiobj, frontend):
116         lookup = FieldLookup('name_vector', [1, 2], LookupAll)
117         ranking = FieldRanking('name_vector', 0.4, [RankedTokens(0.0, [10])])
118
119         results = run_search(apiobj, frontend, 0.1, [lookup], [ranking],
120                              details=SearchDetails(excluded=[101]))
121
122         assert [r.place_id for r in results] == [100]
123
124     @pytest.mark.parametrize('geom', [napi.GeometryFormat.GEOJSON,
125                                       napi.GeometryFormat.KML,
126                                       napi.GeometryFormat.SVG,
127                                       napi.GeometryFormat.TEXT])
128     def test_return_geometries(self, apiobj, frontend, geom):
129         lookup = FieldLookup('name_vector', [20], LookupAll)
130         ranking = FieldRanking('name_vector', 0.4, [RankedTokens(0.0, [21])])
131
132         results = run_search(apiobj, frontend, 0.1, [lookup], [ranking],
133                              details=SearchDetails(geometry_output=geom))
134
135         assert geom.name.lower() in results[0].geometry
136
137     @pytest.mark.parametrize('factor,npoints', [(0.0, 3), (1.0, 2)])
138     def test_return_simplified_geometry(self, apiobj, frontend, factor, npoints):
139         apiobj.add_placex(place_id=333, country_code='us',
140                           centroid=(9.0, 9.0),
141                           geometry='LINESTRING(8.9 9.0, 9.0 9.0, 9.1 9.0)')
142         apiobj.add_search_name(333, names=[55], country_code='us',
143                                centroid=(5.6, 4.3))
144
145         lookup = FieldLookup('name_vector', [55], LookupAll)
146         ranking = FieldRanking('name_vector', 0.4, [RankedTokens(0.0, [21])])
147
148         results = run_search(apiobj, frontend, 0.1, [lookup], [ranking],
149                              details=SearchDetails(geometry_output=napi.GeometryFormat.GEOJSON,
150                                                    geometry_simplification=factor))
151
152         assert len(results) == 1
153         result = results[0]
154         geom = json.loads(result.geometry['geojson'])
155
156         assert result.place_id == 333
157         assert len(geom['coordinates']) == npoints
158
159     @pytest.mark.parametrize('viewbox', ['5.0,4.0,6.0,5.0', '5.7,4.0,6.0,5.0'])
160     @pytest.mark.parametrize('wcount,rids', [(2, [100, 101]), (20000, [100])])
161     def test_prefer_viewbox(self, apiobj, frontend, viewbox, wcount, rids):
162         lookup = FieldLookup('name_vector', [1, 2], LookupAll)
163         ranking = FieldRanking('name_vector', 0.2, [RankedTokens(0.0, [21])])
164
165         api = frontend(apiobj, options=APIOPTIONS)
166         results = run_search(api, None, 0.1, [lookup], [ranking])
167         assert [r.place_id for r in results] == [101, 100]
168
169         results = run_search(api, None, 0.1, [lookup], [ranking], count=wcount,
170                              details=SearchDetails.from_kwargs({'viewbox': viewbox}))
171         assert [r.place_id for r in results] == rids
172
173     @pytest.mark.parametrize('viewbox', ['5.0,4.0,6.0,5.0', '5.55,4.27,5.62,4.31'])
174     def test_force_viewbox(self, apiobj, frontend, viewbox):
175         lookup = FieldLookup('name_vector', [1, 2], LookupAll)
176
177         details = SearchDetails.from_kwargs({'viewbox': viewbox,
178                                              'bounded_viewbox': True})
179
180         results = run_search(apiobj, frontend, 0.1, [lookup], [], details=details)
181         assert [r.place_id for r in results] == [100]
182
183     def test_prefer_near(self, apiobj, frontend):
184         lookup = FieldLookup('name_vector', [1, 2], LookupAll)
185         ranking = FieldRanking('name_vector', 0.4, [RankedTokens(0.0, [21])])
186
187         api = frontend(apiobj, options=APIOPTIONS)
188         results = run_search(api, None, 0.1, [lookup], [ranking])
189         assert [r.place_id for r in results] == [101, 100]
190
191         results = run_search(api, None, 0.1, [lookup], [ranking],
192                              details=SearchDetails.from_kwargs({'near': '5.6,4.3'}))
193         results.sort(key=lambda r: -r.importance)
194         assert [r.place_id for r in results] == [100, 101]
195
196     @pytest.mark.parametrize('radius', [0.09, 0.11])
197     def test_force_near(self, apiobj, frontend, radius):
198         lookup = FieldLookup('name_vector', [1, 2], LookupAll)
199
200         details = SearchDetails.from_kwargs({'near': '5.6,4.3',
201                                              'near_radius': radius})
202
203         results = run_search(apiobj, frontend, 0.1, [lookup], [], details=details)
204
205         assert [r.place_id for r in results] == [100]
206
207
208 class TestStreetWithHousenumber:
209
210     @pytest.fixture(autouse=True)
211     def fill_database(self, apiobj):
212         apiobj.add_placex(place_id=1, class_='place', type='house',
213                           parent_place_id=1000,
214                           housenumber='20 a', country_code='es')
215         apiobj.add_placex(place_id=2, class_='place', type='house',
216                           parent_place_id=1000,
217                           housenumber='21;22', country_code='es')
218         apiobj.add_placex(place_id=1000, class_='highway', type='residential',
219                           rank_search=26, rank_address=26,
220                           country_code='es')
221         apiobj.add_search_name(1000, names=[1, 2, 10, 11],
222                                search_rank=26, address_rank=26,
223                                country_code='es')
224         apiobj.add_placex(place_id=91, class_='place', type='house',
225                           parent_place_id=2000,
226                           housenumber='20', country_code='pt')
227         apiobj.add_placex(place_id=92, class_='place', type='house',
228                           parent_place_id=2000,
229                           housenumber='22', country_code='pt')
230         apiobj.add_placex(place_id=93, class_='place', type='house',
231                           parent_place_id=2000,
232                           housenumber='24', country_code='pt')
233         apiobj.add_placex(place_id=2000, class_='highway', type='residential',
234                           rank_search=26, rank_address=26,
235                           country_code='pt')
236         apiobj.add_search_name(2000, names=[1, 2, 20, 21],
237                                search_rank=26, address_rank=26,
238                                country_code='pt')
239
240     @pytest.mark.parametrize('hnr,res', [('20', [91, 1]), ('20 a', [1]),
241                                          ('21', [2]), ('22', [2, 92]),
242                                          ('24', [93]), ('25', [])])
243     def test_lookup_by_single_housenumber(self, apiobj, frontend, hnr, res):
244         lookup = FieldLookup('name_vector', [1, 2], LookupAll)
245         ranking = FieldRanking('name_vector', 0.3, [RankedTokens(0.0, [10])])
246
247         results = run_search(apiobj, frontend, 0.1, [lookup], [ranking], hnrs=[hnr])
248
249         assert [r.place_id for r in results] == res + [1000, 2000]
250
251     @pytest.mark.parametrize('cc,res', [('es', [2, 1000]), ('pt', [92, 2000])])
252     def test_lookup_with_country_restriction(self, apiobj, frontend, cc, res):
253         lookup = FieldLookup('name_vector', [1, 2], LookupAll)
254         ranking = FieldRanking('name_vector', 0.3, [RankedTokens(0.0, [10])])
255
256         results = run_search(apiobj, frontend, 0.1, [lookup], [ranking], hnrs=['22'],
257                              ccodes=[cc])
258
259         assert [r.place_id for r in results] == res
260
261     def test_lookup_exclude_housenumber_placeid(self, apiobj, frontend):
262         lookup = FieldLookup('name_vector', [1, 2], LookupAll)
263         ranking = FieldRanking('name_vector', 0.3, [RankedTokens(0.0, [10])])
264
265         results = run_search(apiobj, frontend, 0.1, [lookup], [ranking], hnrs=['22'],
266                              details=SearchDetails(excluded=[92]))
267
268         assert [r.place_id for r in results] == [2, 1000, 2000]
269
270     def test_lookup_exclude_street_placeid(self, apiobj, frontend):
271         lookup = FieldLookup('name_vector', [1, 2], LookupAll)
272         ranking = FieldRanking('name_vector', 0.3, [RankedTokens(0.0, [10])])
273
274         results = run_search(apiobj, frontend, 0.1, [lookup], [ranking], hnrs=['22'],
275                              details=SearchDetails(excluded=[1000]))
276
277         assert [r.place_id for r in results] == [2, 92, 2000]
278
279     def test_lookup_only_house_qualifier(self, apiobj, frontend):
280         lookup = FieldLookup('name_vector', [1, 2], LookupAll)
281         ranking = FieldRanking('name_vector', 0.3, [RankedTokens(0.0, [10])])
282
283         results = run_search(apiobj, frontend, 0.1, [lookup], [ranking], hnrs=['22'],
284                              quals=[('place', 'house')])
285
286         assert [r.place_id for r in results] == [2, 92]
287
288     def test_lookup_only_street_qualifier(self, apiobj, frontend):
289         lookup = FieldLookup('name_vector', [1, 2], LookupAll)
290         ranking = FieldRanking('name_vector', 0.3, [RankedTokens(0.0, [10])])
291
292         results = run_search(apiobj, frontend, 0.1, [lookup], [ranking], hnrs=['22'],
293                              quals=[('highway', 'residential')])
294
295         assert [r.place_id for r in results] == [1000, 2000]
296
297     @pytest.mark.parametrize('rank,found', [(26, True), (27, False), (30, False)])
298     def test_lookup_min_rank(self, apiobj, frontend, rank, found):
299         lookup = FieldLookup('name_vector', [1, 2], LookupAll)
300         ranking = FieldRanking('name_vector', 0.3, [RankedTokens(0.0, [10])])
301
302         results = run_search(apiobj, frontend, 0.1, [lookup], [ranking], hnrs=['22'],
303                              details=SearchDetails(min_rank=rank))
304
305         assert [r.place_id for r in results] == ([2, 92, 1000, 2000] if found else [2, 92])
306
307     @pytest.mark.parametrize('geom', [napi.GeometryFormat.GEOJSON,
308                                       napi.GeometryFormat.KML,
309                                       napi.GeometryFormat.SVG,
310                                       napi.GeometryFormat.TEXT])
311     def test_return_geometries(self, apiobj, frontend, geom):
312         lookup = FieldLookup('name_vector', [1, 2], LookupAll)
313
314         results = run_search(apiobj, frontend, 0.1, [lookup], [], hnrs=['20', '21', '22'],
315                              details=SearchDetails(geometry_output=geom))
316
317         assert results
318         assert all(geom.name.lower() in r.geometry for r in results)
319
320
321 def test_very_large_housenumber(apiobj, frontend):
322     apiobj.add_placex(place_id=93, class_='place', type='house',
323                       parent_place_id=2000,
324                       housenumber='2467463524544', country_code='pt')
325     apiobj.add_placex(place_id=2000, class_='highway', type='residential',
326                       rank_search=26, rank_address=26,
327                       country_code='pt')
328     apiobj.add_search_name(2000, names=[1, 2],
329                            search_rank=26, address_rank=26,
330                            country_code='pt')
331
332     lookup = FieldLookup('name_vector', [1, 2], LookupAll)
333
334     results = run_search(apiobj, frontend, 0.1, [lookup], [], hnrs=['2467463524544'],
335                          details=SearchDetails())
336
337     assert results
338     assert [r.place_id for r in results] == [93, 2000]
339
340
341 @pytest.mark.parametrize('wcount,rids', [(2, [990, 991]), (30000, [990])])
342 def test_name_and_postcode(apiobj, frontend, wcount, rids):
343     apiobj.add_placex(place_id=990, class_='highway', type='service',
344                       rank_search=27, rank_address=27,
345                       postcode='11225',
346                       centroid=(10.0, 10.0),
347                       geometry='LINESTRING(9.995 10, 10.005 10)')
348     apiobj.add_search_name(990, names=[111], centroid=(10.0, 10.0),
349                            search_rank=27, address_rank=27)
350     apiobj.add_placex(place_id=991, class_='highway', type='service',
351                       rank_search=27, rank_address=27,
352                       postcode='11221',
353                       centroid=(10.3, 10.3),
354                       geometry='LINESTRING(9.995 10.3, 10.005 10.3)')
355     apiobj.add_search_name(991, names=[111], centroid=(10.3, 10.3),
356                            search_rank=27, address_rank=27)
357     apiobj.add_postcode(place_id=100, country_code='ch', postcode='11225',
358                         geometry='POINT(10 10)')
359
360     lookup = FieldLookup('name_vector', [111], LookupAll)
361
362     results = run_search(apiobj, frontend, 0.1, [lookup], [], pcs=['11225'], count=wcount,
363                          details=SearchDetails())
364
365     assert results
366     assert [r.place_id for r in results] == rids
367
368
369 class TestInterpolations:
370
371     @pytest.fixture(autouse=True)
372     def fill_database(self, apiobj):
373         apiobj.add_placex(place_id=990, class_='highway', type='service',
374                           rank_search=27, rank_address=27,
375                           centroid=(10.0, 10.0),
376                           geometry='LINESTRING(9.995 10, 10.005 10)')
377         apiobj.add_search_name(990, names=[111],
378                                search_rank=27, address_rank=27)
379         apiobj.add_placex(place_id=991, class_='place', type='house',
380                           parent_place_id=990,
381                           rank_search=30, rank_address=30,
382                           housenumber='23',
383                           centroid=(10.0, 10.00002))
384         apiobj.add_osmline(place_id=992,
385                            parent_place_id=990,
386                            startnumber=21, endnumber=29, step=2,
387                            centroid=(10.0, 10.00001),
388                            geometry='LINESTRING(9.995 10.00001, 10.005 10.00001)')
389
390     @pytest.mark.parametrize('hnr,res', [('21', [992]), ('22', []), ('23', [991])])
391     def test_lookup_housenumber(self, apiobj, frontend, hnr, res):
392         lookup = FieldLookup('name_vector', [111], LookupAll)
393
394         results = run_search(apiobj, frontend, 0.1, [lookup], [], hnrs=[hnr])
395
396         assert [r.place_id for r in results] == res + [990]
397
398     @pytest.mark.parametrize('geom', [napi.GeometryFormat.GEOJSON,
399                                       napi.GeometryFormat.KML,
400                                       napi.GeometryFormat.SVG,
401                                       napi.GeometryFormat.TEXT])
402     def test_osmline_with_geometries(self, apiobj, frontend, geom):
403         lookup = FieldLookup('name_vector', [111], LookupAll)
404
405         results = run_search(apiobj, frontend, 0.1, [lookup], [], hnrs=['21'],
406                              details=SearchDetails(geometry_output=geom))
407
408         assert results[0].place_id == 992
409         assert geom.name.lower() in results[0].geometry
410
411
412 class TestTiger:
413
414     @pytest.fixture(autouse=True)
415     def fill_database(self, apiobj):
416         apiobj.add_placex(place_id=990, class_='highway', type='service',
417                           rank_search=27, rank_address=27,
418                           country_code='us',
419                           centroid=(10.0, 10.0),
420                           geometry='LINESTRING(9.995 10, 10.005 10)')
421         apiobj.add_search_name(990, names=[111], country_code='us',
422                                search_rank=27, address_rank=27)
423         apiobj.add_placex(place_id=991, class_='place', type='house',
424                           parent_place_id=990,
425                           rank_search=30, rank_address=30,
426                           housenumber='23',
427                           country_code='us',
428                           centroid=(10.0, 10.00002))
429         apiobj.add_tiger(place_id=992,
430                          parent_place_id=990,
431                          startnumber=21, endnumber=29, step=2,
432                          centroid=(10.0, 10.00001),
433                          geometry='LINESTRING(9.995 10.00001, 10.005 10.00001)')
434
435     @pytest.mark.parametrize('hnr,res', [('21', [992]), ('22', []), ('23', [991])])
436     def test_lookup_housenumber(self, apiobj, frontend, hnr, res):
437         lookup = FieldLookup('name_vector', [111], LookupAll)
438
439         results = run_search(apiobj, frontend, 0.1, [lookup], [], hnrs=[hnr])
440
441         assert [r.place_id for r in results] == res + [990]
442
443     @pytest.mark.parametrize('geom', [napi.GeometryFormat.GEOJSON,
444                                       napi.GeometryFormat.KML,
445                                       napi.GeometryFormat.SVG,
446                                       napi.GeometryFormat.TEXT])
447     def test_tiger_with_geometries(self, apiobj, frontend, geom):
448         lookup = FieldLookup('name_vector', [111], LookupAll)
449
450         results = run_search(apiobj, frontend, 0.1, [lookup], [], hnrs=['21'],
451                              details=SearchDetails(geometry_output=geom))
452
453         assert results[0].place_id == 992
454         assert geom.name.lower() in results[0].geometry
455
456
457 class TestLayersRank30:
458
459     @pytest.fixture(autouse=True)
460     def fill_database(self, apiobj):
461         apiobj.add_placex(place_id=223, class_='place', type='house',
462                           housenumber='1',
463                           rank_address=30,
464                           rank_search=30)
465         apiobj.add_search_name(223, names=[34],
466                                importance=0.0009,
467                                address_rank=30, search_rank=30)
468         apiobj.add_placex(place_id=224, class_='amenity', type='toilet',
469                           rank_address=30,
470                           rank_search=30)
471         apiobj.add_search_name(224, names=[34],
472                                importance=0.0008,
473                                address_rank=30, search_rank=30)
474         apiobj.add_placex(place_id=225, class_='man_made', type='tower',
475                           rank_address=0,
476                           rank_search=30)
477         apiobj.add_search_name(225, names=[34],
478                                importance=0.0007,
479                                address_rank=0, search_rank=30)
480         apiobj.add_placex(place_id=226, class_='railway', type='station',
481                           rank_address=0,
482                           rank_search=30)
483         apiobj.add_search_name(226, names=[34],
484                                importance=0.0006,
485                                address_rank=0, search_rank=30)
486         apiobj.add_placex(place_id=227, class_='natural', type='cave',
487                           rank_address=0,
488                           rank_search=30)
489         apiobj.add_search_name(227, names=[34],
490                                importance=0.0005,
491                                address_rank=0, search_rank=30)
492
493     @pytest.mark.parametrize('layer,res',
494                              [(napi.DataLayer.ADDRESS, [223]),
495                               (napi.DataLayer.POI, [224]),
496                               (napi.DataLayer.ADDRESS | napi.DataLayer.POI, [223, 224]),
497                               (napi.DataLayer.MANMADE, [225]),
498                               (napi.DataLayer.RAILWAY, [226]),
499                               (napi.DataLayer.NATURAL, [227]),
500                               (napi.DataLayer.MANMADE | napi.DataLayer.NATURAL, [225, 227]),
501                               (napi.DataLayer.MANMADE | napi.DataLayer.RAILWAY, [225, 226])])
502     def test_layers_rank30(self, apiobj, frontend, layer, res):
503         lookup = FieldLookup('name_vector', [34], LookupAny)
504
505         results = run_search(apiobj, frontend, 0.1, [lookup], [],
506                              details=SearchDetails(layers=layer))
507
508         assert [r.place_id for r in results] == res