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