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