]> git.openstreetmap.org Git - nominatim.git/blob - test/python/api/search/test_search_places.py
Merge pull request #3190 from lonvia/fix-internal-server-errors
[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 pytest
11
12 import nominatim.api as napi
13 from nominatim.api.types import SearchDetails
14 from nominatim.api.search.db_searches import PlaceSearch
15 from nominatim.api.search.db_search_fields import WeightedStrings, WeightedCategories,\
16                                                   FieldLookup, FieldRanking, RankedTokens
17
18 def run_search(apiobj, global_penalty, lookup, ranking, count=2,
19                hnrs=[], pcs=[], ccodes=[], quals=[],
20                details=SearchDetails()):
21     class MySearchData:
22         penalty = global_penalty
23         postcodes = WeightedStrings(pcs, [0.0] * len(pcs))
24         countries = WeightedStrings(ccodes, [0.0] * len(ccodes))
25         housenumbers = WeightedStrings(hnrs, [0.0] * len(hnrs))
26         qualifiers = WeightedCategories(quals, [0.0] * len(quals))
27         lookups = lookup
28         rankings = ranking
29
30     search = PlaceSearch(0.0, MySearchData(), count)
31
32     async def run():
33         async with apiobj.api._async_api.begin() as conn:
34             return await search.lookup(conn, details)
35
36     results = apiobj.async_to_sync(run())
37     results.sort(key=lambda r: r.accuracy)
38
39     return results
40
41
42 class TestNameOnlySearches:
43
44     @pytest.fixture(autouse=True)
45     def fill_database(self, apiobj):
46         apiobj.add_placex(place_id=100, country_code='us',
47                           centroid=(5.6, 4.3))
48         apiobj.add_search_name(100, names=[1,2,10,11], country_code='us',
49                                centroid=(5.6, 4.3))
50         apiobj.add_placex(place_id=101, country_code='mx',
51                           centroid=(-10.3, 56.9))
52         apiobj.add_search_name(101, names=[1,2,20,21], country_code='mx',
53                                centroid=(-10.3, 56.9))
54
55
56     @pytest.mark.parametrize('lookup_type', ['lookup_all', 'restrict'])
57     @pytest.mark.parametrize('rank,res', [([10], [100, 101]),
58                                           ([20], [101, 100])])
59     def test_lookup_all_match(self, apiobj, lookup_type, rank, res):
60         lookup = FieldLookup('name_vector', [1,2], lookup_type)
61         ranking = FieldRanking('name_vector', 0.9, [RankedTokens(0.0, rank)])
62
63         results = run_search(apiobj, 0.1, [lookup], [ranking])
64
65         assert [r.place_id for r in results] == res
66
67
68     @pytest.mark.parametrize('lookup_type', ['lookup_all', 'restrict'])
69     def test_lookup_all_partial_match(self, apiobj, lookup_type):
70         lookup = FieldLookup('name_vector', [1,20], lookup_type)
71         ranking = FieldRanking('name_vector', 0.9, [RankedTokens(0.0, [21])])
72
73         results = run_search(apiobj, 0.1, [lookup], [ranking])
74
75         assert len(results) == 1
76         assert results[0].place_id == 101
77
78     @pytest.mark.parametrize('rank,res', [([10], [100, 101]),
79                                           ([20], [101, 100])])
80     def test_lookup_any_match(self, apiobj, rank, res):
81         lookup = FieldLookup('name_vector', [11,21], 'lookup_any')
82         ranking = FieldRanking('name_vector', 0.9, [RankedTokens(0.0, rank)])
83
84         results = run_search(apiobj, 0.1, [lookup], [ranking])
85
86         assert [r.place_id for r in results] == res
87
88
89     def test_lookup_any_partial_match(self, apiobj):
90         lookup = FieldLookup('name_vector', [20], 'lookup_all')
91         ranking = FieldRanking('name_vector', 0.9, [RankedTokens(0.0, [21])])
92
93         results = run_search(apiobj, 0.1, [lookup], [ranking])
94
95         assert len(results) == 1
96         assert results[0].place_id == 101
97
98
99     @pytest.mark.parametrize('cc,res', [('us', 100), ('mx', 101)])
100     def test_lookup_restrict_country(self, apiobj, cc, res):
101         lookup = FieldLookup('name_vector', [1,2], 'lookup_all')
102         ranking = FieldRanking('name_vector', 0.9, [RankedTokens(0.0, [10])])
103
104         results = run_search(apiobj, 0.1, [lookup], [ranking], ccodes=[cc])
105
106         assert [r.place_id for r in results] == [res]
107
108
109     def test_lookup_restrict_placeid(self, apiobj):
110         lookup = FieldLookup('name_vector', [1,2], 'lookup_all')
111         ranking = FieldRanking('name_vector', 0.9, [RankedTokens(0.0, [10])])
112
113         results = run_search(apiobj, 0.1, [lookup], [ranking],
114                              details=SearchDetails(excluded=[101]))
115
116         assert [r.place_id for r in results] == [100]
117
118
119     @pytest.mark.parametrize('geom', [napi.GeometryFormat.GEOJSON,
120                                       napi.GeometryFormat.KML,
121                                       napi.GeometryFormat.SVG,
122                                       napi.GeometryFormat.TEXT])
123     def test_return_geometries(self, apiobj, geom):
124         lookup = FieldLookup('name_vector', [20], 'lookup_all')
125         ranking = FieldRanking('name_vector', 0.9, [RankedTokens(0.0, [21])])
126
127         results = run_search(apiobj, 0.1, [lookup], [ranking],
128                              details=SearchDetails(geometry_output=geom))
129
130         assert geom.name.lower() in results[0].geometry
131
132
133     @pytest.mark.parametrize('viewbox', ['5.0,4.0,6.0,5.0', '5.7,4.0,6.0,5.0'])
134     def test_prefer_viewbox(self, apiobj, viewbox):
135         lookup = FieldLookup('name_vector', [1, 2], 'lookup_all')
136         ranking = FieldRanking('name_vector', 0.9, [RankedTokens(0.0, [21])])
137
138         results = run_search(apiobj, 0.1, [lookup], [ranking])
139         assert [r.place_id for r in results] == [101, 100]
140
141         results = run_search(apiobj, 0.1, [lookup], [ranking],
142                              details=SearchDetails.from_kwargs({'viewbox': viewbox}))
143         assert [r.place_id for r in results] == [100, 101]
144
145
146     def test_force_viewbox(self, apiobj):
147         lookup = FieldLookup('name_vector', [1, 2], 'lookup_all')
148
149         details=SearchDetails.from_kwargs({'viewbox': '5.0,4.0,6.0,5.0',
150                                            'bounded_viewbox': True})
151
152         results = run_search(apiobj, 0.1, [lookup], [], details=details)
153         assert [r.place_id for r in results] == [100]
154
155
156     def test_prefer_near(self, apiobj):
157         lookup = FieldLookup('name_vector', [1, 2], 'lookup_all')
158         ranking = FieldRanking('name_vector', 0.9, [RankedTokens(0.0, [21])])
159
160         results = run_search(apiobj, 0.1, [lookup], [ranking])
161         assert [r.place_id for r in results] == [101, 100]
162
163         results = run_search(apiobj, 0.1, [lookup], [ranking],
164                              details=SearchDetails.from_kwargs({'near': '5.6,4.3'}))
165         results.sort(key=lambda r: -r.importance)
166         assert [r.place_id for r in results] == [100, 101]
167
168
169     def test_force_near(self, apiobj):
170         lookup = FieldLookup('name_vector', [1, 2], 'lookup_all')
171
172         details=SearchDetails.from_kwargs({'near': '5.6,4.3',
173                                            'near_radius': 0.11})
174
175         results = run_search(apiobj, 0.1, [lookup], [], details=details)
176
177         assert [r.place_id for r in results] == [100]
178
179
180 class TestStreetWithHousenumber:
181
182     @pytest.fixture(autouse=True)
183     def fill_database(self, apiobj):
184         apiobj.add_placex(place_id=1, class_='place', type='house',
185                           parent_place_id=1000,
186                           housenumber='20 a', country_code='es')
187         apiobj.add_placex(place_id=2, class_='place', type='house',
188                           parent_place_id=1000,
189                           housenumber='21;22', country_code='es')
190         apiobj.add_placex(place_id=1000, class_='highway', type='residential',
191                           rank_search=26, rank_address=26,
192                           country_code='es')
193         apiobj.add_search_name(1000, names=[1,2,10,11],
194                                search_rank=26, address_rank=26,
195                                country_code='es')
196         apiobj.add_placex(place_id=91, class_='place', type='house',
197                           parent_place_id=2000,
198                           housenumber='20', country_code='pt')
199         apiobj.add_placex(place_id=92, class_='place', type='house',
200                           parent_place_id=2000,
201                           housenumber='22', country_code='pt')
202         apiobj.add_placex(place_id=93, class_='place', type='house',
203                           parent_place_id=2000,
204                           housenumber='24', country_code='pt')
205         apiobj.add_placex(place_id=2000, class_='highway', type='residential',
206                           rank_search=26, rank_address=26,
207                           country_code='pt')
208         apiobj.add_search_name(2000, names=[1,2,20,21],
209                                search_rank=26, address_rank=26,
210                                country_code='pt')
211
212
213     @pytest.mark.parametrize('hnr,res', [('20', [91, 1]), ('20 a', [1]),
214                                          ('21', [2]), ('22', [2, 92]),
215                                          ('24', [93]), ('25', [])])
216     def test_lookup_by_single_housenumber(self, apiobj, hnr, res):
217         lookup = FieldLookup('name_vector', [1,2], 'lookup_all')
218         ranking = FieldRanking('name_vector', 0.3, [RankedTokens(0.0, [10])])
219
220         results = run_search(apiobj, 0.1, [lookup], [ranking], hnrs=[hnr])
221
222         assert [r.place_id for r in results] == res + [1000, 2000]
223
224
225     @pytest.mark.parametrize('cc,res', [('es', [2, 1000]), ('pt', [92, 2000])])
226     def test_lookup_with_country_restriction(self, apiobj, cc, res):
227         lookup = FieldLookup('name_vector', [1,2], 'lookup_all')
228         ranking = FieldRanking('name_vector', 0.3, [RankedTokens(0.0, [10])])
229
230         results = run_search(apiobj, 0.1, [lookup], [ranking], hnrs=['22'],
231                              ccodes=[cc])
232
233         assert [r.place_id for r in results] == res
234
235
236     def test_lookup_exclude_housenumber_placeid(self, apiobj):
237         lookup = FieldLookup('name_vector', [1,2], 'lookup_all')
238         ranking = FieldRanking('name_vector', 0.3, [RankedTokens(0.0, [10])])
239
240         results = run_search(apiobj, 0.1, [lookup], [ranking], hnrs=['22'],
241                              details=SearchDetails(excluded=[92]))
242
243         assert [r.place_id for r in results] == [2, 1000, 2000]
244
245
246     def test_lookup_exclude_street_placeid(self, apiobj):
247         lookup = FieldLookup('name_vector', [1,2], 'lookup_all')
248         ranking = FieldRanking('name_vector', 0.3, [RankedTokens(0.0, [10])])
249
250         results = run_search(apiobj, 0.1, [lookup], [ranking], hnrs=['22'],
251                              details=SearchDetails(excluded=[1000]))
252
253         assert [r.place_id for r in results] == [2, 92, 2000]
254
255
256     @pytest.mark.parametrize('geom', [napi.GeometryFormat.GEOJSON,
257                                       napi.GeometryFormat.KML,
258                                       napi.GeometryFormat.SVG,
259                                       napi.GeometryFormat.TEXT])
260     def test_return_geometries(self, apiobj, geom):
261         lookup = FieldLookup('name_vector', [1, 2], 'lookup_all')
262
263         results = run_search(apiobj, 0.1, [lookup], [], hnrs=['20', '21', '22'],
264                              details=SearchDetails(geometry_output=geom))
265
266         assert results
267         assert all(geom.name.lower() in r.geometry for r in results)
268
269
270 def test_very_large_housenumber(apiobj):
271     apiobj.add_placex(place_id=93, class_='place', type='house',
272                       parent_place_id=2000,
273                       housenumber='2467463524544', country_code='pt')
274     apiobj.add_placex(place_id=2000, class_='highway', type='residential',
275                       rank_search=26, rank_address=26,
276                       country_code='pt')
277     apiobj.add_search_name(2000, names=[1,2],
278                            search_rank=26, address_rank=26,
279                            country_code='pt')
280
281     lookup = FieldLookup('name_vector', [1, 2], 'lookup_all')
282
283     results = run_search(apiobj, 0.1, [lookup], [], hnrs=['2467463524544'],
284                          details=SearchDetails())
285
286     assert results
287     assert [r.place_id for r in results] == [93, 2000]
288
289
290 class TestInterpolations:
291
292     @pytest.fixture(autouse=True)
293     def fill_database(self, apiobj):
294         apiobj.add_placex(place_id=990, class_='highway', type='service',
295                           rank_search=27, rank_address=27,
296                           centroid=(10.0, 10.0),
297                           geometry='LINESTRING(9.995 10, 10.005 10)')
298         apiobj.add_search_name(990, names=[111],
299                                search_rank=27, address_rank=27)
300         apiobj.add_placex(place_id=991, class_='place', type='house',
301                           parent_place_id=990,
302                           rank_search=30, rank_address=30,
303                           housenumber='23',
304                           centroid=(10.0, 10.00002))
305         apiobj.add_osmline(place_id=992,
306                            parent_place_id=990,
307                            startnumber=21, endnumber=29, step=2,
308                            centroid=(10.0, 10.00001),
309                            geometry='LINESTRING(9.995 10.00001, 10.005 10.00001)')
310
311
312     @pytest.mark.parametrize('hnr,res', [('21', [992]), ('22', []), ('23', [991])])
313     def test_lookup_housenumber(self, apiobj, hnr, res):
314         lookup = FieldLookup('name_vector', [111], 'lookup_all')
315
316         results = run_search(apiobj, 0.1, [lookup], [], hnrs=[hnr])
317
318         assert [r.place_id for r in results] == res + [990]
319
320
321 class TestTiger:
322
323     @pytest.fixture(autouse=True)
324     def fill_database(self, apiobj):
325         apiobj.add_placex(place_id=990, class_='highway', type='service',
326                           rank_search=27, rank_address=27,
327                           country_code='us',
328                           centroid=(10.0, 10.0),
329                           geometry='LINESTRING(9.995 10, 10.005 10)')
330         apiobj.add_search_name(990, names=[111], country_code='us',
331                                search_rank=27, address_rank=27)
332         apiobj.add_placex(place_id=991, class_='place', type='house',
333                           parent_place_id=990,
334                           rank_search=30, rank_address=30,
335                           housenumber='23',
336                           country_code='us',
337                           centroid=(10.0, 10.00002))
338         apiobj.add_tiger(place_id=992,
339                          parent_place_id=990,
340                          startnumber=21, endnumber=29, step=2,
341                          centroid=(10.0, 10.00001),
342                          geometry='LINESTRING(9.995 10.00001, 10.005 10.00001)')
343
344
345     @pytest.mark.parametrize('hnr,res', [('21', [992]), ('22', []), ('23', [991])])
346     def test_lookup_housenumber(self, apiobj, hnr, res):
347         lookup = FieldLookup('name_vector', [111], 'lookup_all')
348
349         results = run_search(apiobj, 0.1, [lookup], [], hnrs=[hnr])
350
351         assert [r.place_id for r in results] == res + [990]
352
353
354 class TestLayersRank30:
355
356     @pytest.fixture(autouse=True)
357     def fill_database(self, apiobj):
358         apiobj.add_placex(place_id=223, class_='place', type='house',
359                           housenumber='1',
360                           rank_address=30,
361                           rank_search=30)
362         apiobj.add_search_name(223, names=[34],
363                                importance=0.0009,
364                                address_rank=30, search_rank=30)
365         apiobj.add_placex(place_id=224, class_='amenity', type='toilet',
366                           rank_address=30,
367                           rank_search=30)
368         apiobj.add_search_name(224, names=[34],
369                                importance=0.0008,
370                                address_rank=30, search_rank=30)
371         apiobj.add_placex(place_id=225, class_='man_made', type='tower',
372                           rank_address=0,
373                           rank_search=30)
374         apiobj.add_search_name(225, names=[34],
375                                importance=0.0007,
376                                address_rank=0, search_rank=30)
377         apiobj.add_placex(place_id=226, class_='railway', type='station',
378                           rank_address=0,
379                           rank_search=30)
380         apiobj.add_search_name(226, names=[34],
381                                importance=0.0006,
382                                address_rank=0, search_rank=30)
383         apiobj.add_placex(place_id=227, class_='natural', type='cave',
384                           rank_address=0,
385                           rank_search=30)
386         apiobj.add_search_name(227, names=[34],
387                                importance=0.0005,
388                                address_rank=0, search_rank=30)
389
390
391     @pytest.mark.parametrize('layer,res', [(napi.DataLayer.ADDRESS, [223]),
392                                            (napi.DataLayer.POI, [224]),
393                                            (napi.DataLayer.ADDRESS | napi.DataLayer.POI, [223, 224]),
394                                            (napi.DataLayer.MANMADE, [225]),
395                                            (napi.DataLayer.RAILWAY, [226]),
396                                            (napi.DataLayer.NATURAL, [227]),
397                                            (napi.DataLayer.MANMADE | napi.DataLayer.NATURAL, [225, 227]),
398                                            (napi.DataLayer.MANMADE | napi.DataLayer.RAILWAY, [225, 226])])
399     def test_layers_rank30(self, apiobj, layer, res):
400         lookup = FieldLookup('name_vector', [34], 'lookup_any')
401
402         results = run_search(apiobj, 0.1, [lookup], [],
403                              details=SearchDetails(layers=layer))
404
405         assert [r.place_id for r in results] == res