]> git.openstreetmap.org Git - nominatim.git/blob - test/python/api/search/test_search_places.py
only print non-empty 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 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 class TestInterpolations:
271
272     @pytest.fixture(autouse=True)
273     def fill_database(self, apiobj):
274         apiobj.add_placex(place_id=990, class_='highway', type='service',
275                           rank_search=27, rank_address=27,
276                           centroid=(10.0, 10.0),
277                           geometry='LINESTRING(9.995 10, 10.005 10)')
278         apiobj.add_search_name(990, names=[111],
279                                search_rank=27, address_rank=27)
280         apiobj.add_placex(place_id=991, class_='place', type='house',
281                           parent_place_id=990,
282                           rank_search=30, rank_address=30,
283                           housenumber='23',
284                           centroid=(10.0, 10.00002))
285         apiobj.add_osmline(place_id=992,
286                            parent_place_id=990,
287                            startnumber=21, endnumber=29, step=2,
288                            centroid=(10.0, 10.00001),
289                            geometry='LINESTRING(9.995 10.00001, 10.005 10.00001)')
290
291
292     @pytest.mark.parametrize('hnr,res', [('21', [992]), ('22', []), ('23', [991])])
293     def test_lookup_housenumber(self, apiobj, hnr, res):
294         lookup = FieldLookup('name_vector', [111], 'lookup_all')
295
296         results = run_search(apiobj, 0.1, [lookup], [], hnrs=[hnr])
297
298         assert [r.place_id for r in results] == res + [990]
299
300
301 class TestTiger:
302
303     @pytest.fixture(autouse=True)
304     def fill_database(self, apiobj):
305         apiobj.add_placex(place_id=990, class_='highway', type='service',
306                           rank_search=27, rank_address=27,
307                           country_code='us',
308                           centroid=(10.0, 10.0),
309                           geometry='LINESTRING(9.995 10, 10.005 10)')
310         apiobj.add_search_name(990, names=[111], country_code='us',
311                                search_rank=27, address_rank=27)
312         apiobj.add_placex(place_id=991, class_='place', type='house',
313                           parent_place_id=990,
314                           rank_search=30, rank_address=30,
315                           housenumber='23',
316                           country_code='us',
317                           centroid=(10.0, 10.00002))
318         apiobj.add_tiger(place_id=992,
319                          parent_place_id=990,
320                          startnumber=21, endnumber=29, step=2,
321                          centroid=(10.0, 10.00001),
322                          geometry='LINESTRING(9.995 10.00001, 10.005 10.00001)')
323
324
325     @pytest.mark.parametrize('hnr,res', [('21', [992]), ('22', []), ('23', [991])])
326     def test_lookup_housenumber(self, apiobj, hnr, res):
327         lookup = FieldLookup('name_vector', [111], 'lookup_all')
328
329         results = run_search(apiobj, 0.1, [lookup], [], hnrs=[hnr])
330
331         assert [r.place_id for r in results] == res + [990]
332
333
334 class TestLayersRank30:
335
336     @pytest.fixture(autouse=True)
337     def fill_database(self, apiobj):
338         apiobj.add_placex(place_id=223, class_='place', type='house',
339                           housenumber='1',
340                           rank_address=30,
341                           rank_search=30)
342         apiobj.add_search_name(223, names=[34],
343                                importance=0.0009,
344                                address_rank=30, search_rank=30)
345         apiobj.add_placex(place_id=224, class_='amenity', type='toilet',
346                           rank_address=30,
347                           rank_search=30)
348         apiobj.add_search_name(224, names=[34],
349                                importance=0.0008,
350                                address_rank=30, search_rank=30)
351         apiobj.add_placex(place_id=225, class_='man_made', type='tower',
352                           rank_address=0,
353                           rank_search=30)
354         apiobj.add_search_name(225, names=[34],
355                                importance=0.0007,
356                                address_rank=0, search_rank=30)
357         apiobj.add_placex(place_id=226, class_='railway', type='station',
358                           rank_address=0,
359                           rank_search=30)
360         apiobj.add_search_name(226, names=[34],
361                                importance=0.0006,
362                                address_rank=0, search_rank=30)
363         apiobj.add_placex(place_id=227, class_='natural', type='cave',
364                           rank_address=0,
365                           rank_search=30)
366         apiobj.add_search_name(227, names=[34],
367                                importance=0.0005,
368                                address_rank=0, search_rank=30)
369
370
371     @pytest.mark.parametrize('layer,res', [(napi.DataLayer.ADDRESS, [223]),
372                                            (napi.DataLayer.POI, [224]),
373                                            (napi.DataLayer.ADDRESS | napi.DataLayer.POI, [223, 224]),
374                                            (napi.DataLayer.MANMADE, [225]),
375                                            (napi.DataLayer.RAILWAY, [226]),
376                                            (napi.DataLayer.NATURAL, [227]),
377                                            (napi.DataLayer.MANMADE | napi.DataLayer.NATURAL, [225, 227]),
378                                            (napi.DataLayer.MANMADE | napi.DataLayer.RAILWAY, [225, 226])])
379     def test_layers_rank30(self, apiobj, layer, res):
380         lookup = FieldLookup('name_vector', [34], 'lookup_any')
381
382         results = run_search(apiobj, 0.1, [lookup], [],
383                              details=SearchDetails(layers=layer))
384
385         assert [r.place_id for r in results] == res