X-Git-Url: https://git.openstreetmap.org./nominatim.git/blobdiff_plain/4a8b78985bc0b20b67204633a776a8eb60eba02c..HEAD:/test/python/api/search/test_search_places.py diff --git a/test/python/api/search/test_search_places.py b/test/python/api/search/test_search_places.py index 8d17ec2d..c6ff16b8 100644 --- a/test/python/api/search/test_search_places.py +++ b/test/python/api/search/test_search_places.py @@ -2,20 +2,25 @@ # # This file is part of Nominatim. (https://nominatim.org) # -# Copyright (C) 2023 by the Nominatim developer community. +# Copyright (C) 2024 by the Nominatim developer community. # For a full list of authors see the git log. """ Tests for running the generic place searcher. """ +import json + import pytest -import nominatim.api as napi -from nominatim.api.types import SearchDetails -from nominatim.api.search.db_searches import PlaceSearch -from nominatim.api.search.db_search_fields import WeightedStrings, WeightedCategories,\ +import nominatim_api as napi +from nominatim_api.types import SearchDetails +from nominatim_api.search.db_searches import PlaceSearch +from nominatim_api.search.db_search_fields import WeightedStrings, WeightedCategories,\ FieldLookup, FieldRanking, RankedTokens +from nominatim_api.search.db_search_lookups import LookupAll, LookupAny, Restrict + +APIOPTIONS = ['search'] -def run_search(apiobj, global_penalty, lookup, ranking, count=2, +def run_search(apiobj, frontend, global_penalty, lookup, ranking, count=2, hnrs=[], pcs=[], ccodes=[], quals=[], details=SearchDetails()): class MySearchData: @@ -29,11 +34,16 @@ def run_search(apiobj, global_penalty, lookup, ranking, count=2, search = PlaceSearch(0.0, MySearchData(), count) + if frontend is None: + api = apiobj + else: + api = frontend(apiobj, options=APIOPTIONS) + async def run(): - async with apiobj.api._async_api.begin() as conn: + async with api._async_api.begin() as conn: return await search.lookup(conn, details) - results = apiobj.async_to_sync(run()) + results = api._loop.run_until_complete(run()) results.sort(key=lambda r: r.accuracy) return results @@ -53,64 +63,64 @@ class TestNameOnlySearches: centroid=(-10.3, 56.9)) - @pytest.mark.parametrize('lookup_type', ['lookup_all', 'restrict']) + @pytest.mark.parametrize('lookup_type', [LookupAll, Restrict]) @pytest.mark.parametrize('rank,res', [([10], [100, 101]), ([20], [101, 100])]) - def test_lookup_all_match(self, apiobj, lookup_type, rank, res): + def test_lookup_all_match(self, apiobj, frontend, lookup_type, rank, res): lookup = FieldLookup('name_vector', [1,2], lookup_type) - ranking = FieldRanking('name_vector', 0.9, [RankedTokens(0.0, rank)]) + ranking = FieldRanking('name_vector', 0.4, [RankedTokens(0.0, rank)]) - results = run_search(apiobj, 0.1, [lookup], [ranking]) + results = run_search(apiobj, frontend, 0.1, [lookup], [ranking]) assert [r.place_id for r in results] == res - @pytest.mark.parametrize('lookup_type', ['lookup_all', 'restrict']) - def test_lookup_all_partial_match(self, apiobj, lookup_type): + @pytest.mark.parametrize('lookup_type', [LookupAll, Restrict]) + def test_lookup_all_partial_match(self, apiobj, frontend, lookup_type): lookup = FieldLookup('name_vector', [1,20], lookup_type) - ranking = FieldRanking('name_vector', 0.9, [RankedTokens(0.0, [21])]) + ranking = FieldRanking('name_vector', 0.4, [RankedTokens(0.0, [21])]) - results = run_search(apiobj, 0.1, [lookup], [ranking]) + results = run_search(apiobj, frontend, 0.1, [lookup], [ranking]) assert len(results) == 1 assert results[0].place_id == 101 @pytest.mark.parametrize('rank,res', [([10], [100, 101]), ([20], [101, 100])]) - def test_lookup_any_match(self, apiobj, rank, res): - lookup = FieldLookup('name_vector', [11,21], 'lookup_any') - ranking = FieldRanking('name_vector', 0.9, [RankedTokens(0.0, rank)]) + def test_lookup_any_match(self, apiobj, frontend, rank, res): + lookup = FieldLookup('name_vector', [11,21], LookupAny) + ranking = FieldRanking('name_vector', 0.4, [RankedTokens(0.0, rank)]) - results = run_search(apiobj, 0.1, [lookup], [ranking]) + results = run_search(apiobj, frontend, 0.1, [lookup], [ranking]) assert [r.place_id for r in results] == res - def test_lookup_any_partial_match(self, apiobj): - lookup = FieldLookup('name_vector', [20], 'lookup_all') - ranking = FieldRanking('name_vector', 0.9, [RankedTokens(0.0, [21])]) + def test_lookup_any_partial_match(self, apiobj, frontend): + lookup = FieldLookup('name_vector', [20], LookupAll) + ranking = FieldRanking('name_vector', 0.4, [RankedTokens(0.0, [21])]) - results = run_search(apiobj, 0.1, [lookup], [ranking]) + results = run_search(apiobj, frontend, 0.1, [lookup], [ranking]) assert len(results) == 1 assert results[0].place_id == 101 @pytest.mark.parametrize('cc,res', [('us', 100), ('mx', 101)]) - def test_lookup_restrict_country(self, apiobj, cc, res): - lookup = FieldLookup('name_vector', [1,2], 'lookup_all') - ranking = FieldRanking('name_vector', 0.9, [RankedTokens(0.0, [10])]) + def test_lookup_restrict_country(self, apiobj, frontend, cc, res): + lookup = FieldLookup('name_vector', [1,2], LookupAll) + ranking = FieldRanking('name_vector', 0.4, [RankedTokens(0.0, [10])]) - results = run_search(apiobj, 0.1, [lookup], [ranking], ccodes=[cc]) + results = run_search(apiobj, frontend, 0.1, [lookup], [ranking], ccodes=[cc]) assert [r.place_id for r in results] == [res] - def test_lookup_restrict_placeid(self, apiobj): - lookup = FieldLookup('name_vector', [1,2], 'lookup_all') - ranking = FieldRanking('name_vector', 0.9, [RankedTokens(0.0, [10])]) + def test_lookup_restrict_placeid(self, apiobj, frontend): + lookup = FieldLookup('name_vector', [1,2], LookupAll) + ranking = FieldRanking('name_vector', 0.4, [RankedTokens(0.0, [10])]) - results = run_search(apiobj, 0.1, [lookup], [ranking], + results = run_search(apiobj, frontend, 0.1, [lookup], [ranking], details=SearchDetails(excluded=[101])) assert [r.place_id for r in results] == [100] @@ -120,59 +130,87 @@ class TestNameOnlySearches: napi.GeometryFormat.KML, napi.GeometryFormat.SVG, napi.GeometryFormat.TEXT]) - def test_return_geometries(self, apiobj, geom): - lookup = FieldLookup('name_vector', [20], 'lookup_all') - ranking = FieldRanking('name_vector', 0.9, [RankedTokens(0.0, [21])]) + def test_return_geometries(self, apiobj, frontend, geom): + lookup = FieldLookup('name_vector', [20], LookupAll) + ranking = FieldRanking('name_vector', 0.4, [RankedTokens(0.0, [21])]) - results = run_search(apiobj, 0.1, [lookup], [ranking], + results = run_search(apiobj, frontend, 0.1, [lookup], [ranking], details=SearchDetails(geometry_output=geom)) assert geom.name.lower() in results[0].geometry + @pytest.mark.parametrize('factor,npoints', [(0.0, 3), (1.0, 2)]) + def test_return_simplified_geometry(self, apiobj, frontend, factor, npoints): + apiobj.add_placex(place_id=333, country_code='us', + centroid=(9.0, 9.0), + geometry='LINESTRING(8.9 9.0, 9.0 9.0, 9.1 9.0)') + apiobj.add_search_name(333, names=[55], country_code='us', + centroid=(5.6, 4.3)) + + lookup = FieldLookup('name_vector', [55], LookupAll) + ranking = FieldRanking('name_vector', 0.4, [RankedTokens(0.0, [21])]) + + results = run_search(apiobj, frontend, 0.1, [lookup], [ranking], + details=SearchDetails(geometry_output=napi.GeometryFormat.GEOJSON, + geometry_simplification=factor)) + + assert len(results) == 1 + result = results[0] + geom = json.loads(result.geometry['geojson']) + + assert result.place_id == 333 + assert len(geom['coordinates']) == npoints + + @pytest.mark.parametrize('viewbox', ['5.0,4.0,6.0,5.0', '5.7,4.0,6.0,5.0']) - def test_prefer_viewbox(self, apiobj, viewbox): - lookup = FieldLookup('name_vector', [1, 2], 'lookup_all') - ranking = FieldRanking('name_vector', 0.9, [RankedTokens(0.0, [21])]) + @pytest.mark.parametrize('wcount,rids', [(2, [100, 101]), (20000, [100])]) + def test_prefer_viewbox(self, apiobj, frontend, viewbox, wcount, rids): + lookup = FieldLookup('name_vector', [1, 2], LookupAll) + ranking = FieldRanking('name_vector', 0.2, [RankedTokens(0.0, [21])]) - results = run_search(apiobj, 0.1, [lookup], [ranking]) + api = frontend(apiobj, options=APIOPTIONS) + results = run_search(api, None, 0.1, [lookup], [ranking]) assert [r.place_id for r in results] == [101, 100] - results = run_search(apiobj, 0.1, [lookup], [ranking], + results = run_search(api, None, 0.1, [lookup], [ranking], count=wcount, details=SearchDetails.from_kwargs({'viewbox': viewbox})) - assert [r.place_id for r in results] == [100, 101] + assert [r.place_id for r in results] == rids - def test_force_viewbox(self, apiobj): - lookup = FieldLookup('name_vector', [1, 2], 'lookup_all') + @pytest.mark.parametrize('viewbox', ['5.0,4.0,6.0,5.0', '5.55,4.27,5.62,4.31']) + def test_force_viewbox(self, apiobj, frontend, viewbox): + lookup = FieldLookup('name_vector', [1, 2], LookupAll) - details=SearchDetails.from_kwargs({'viewbox': '5.0,4.0,6.0,5.0', + details=SearchDetails.from_kwargs({'viewbox': viewbox, 'bounded_viewbox': True}) - results = run_search(apiobj, 0.1, [lookup], [], details=details) + results = run_search(apiobj, frontend, 0.1, [lookup], [], details=details) assert [r.place_id for r in results] == [100] - def test_prefer_near(self, apiobj): - lookup = FieldLookup('name_vector', [1, 2], 'lookup_all') - ranking = FieldRanking('name_vector', 0.9, [RankedTokens(0.0, [21])]) + def test_prefer_near(self, apiobj, frontend): + lookup = FieldLookup('name_vector', [1, 2], LookupAll) + ranking = FieldRanking('name_vector', 0.4, [RankedTokens(0.0, [21])]) - results = run_search(apiobj, 0.1, [lookup], [ranking]) + api = frontend(apiobj, options=APIOPTIONS) + results = run_search(api, None, 0.1, [lookup], [ranking]) assert [r.place_id for r in results] == [101, 100] - results = run_search(apiobj, 0.1, [lookup], [ranking], + results = run_search(api, None, 0.1, [lookup], [ranking], details=SearchDetails.from_kwargs({'near': '5.6,4.3'})) results.sort(key=lambda r: -r.importance) assert [r.place_id for r in results] == [100, 101] - def test_force_near(self, apiobj): - lookup = FieldLookup('name_vector', [1, 2], 'lookup_all') + @pytest.mark.parametrize('radius', [0.09, 0.11]) + def test_force_near(self, apiobj, frontend, radius): + lookup = FieldLookup('name_vector', [1, 2], LookupAll) details=SearchDetails.from_kwargs({'near': '5.6,4.3', - 'near_radius': 0.11}) + 'near_radius': radius}) - results = run_search(apiobj, 0.1, [lookup], [], details=details) + results = run_search(apiobj, frontend, 0.1, [lookup], [], details=details) assert [r.place_id for r in results] == [100] @@ -213,61 +251,92 @@ class TestStreetWithHousenumber: @pytest.mark.parametrize('hnr,res', [('20', [91, 1]), ('20 a', [1]), ('21', [2]), ('22', [2, 92]), ('24', [93]), ('25', [])]) - def test_lookup_by_single_housenumber(self, apiobj, hnr, res): - lookup = FieldLookup('name_vector', [1,2], 'lookup_all') + def test_lookup_by_single_housenumber(self, apiobj, frontend, hnr, res): + lookup = FieldLookup('name_vector', [1,2], LookupAll) ranking = FieldRanking('name_vector', 0.3, [RankedTokens(0.0, [10])]) - results = run_search(apiobj, 0.1, [lookup], [ranking], hnrs=[hnr]) + results = run_search(apiobj, frontend, 0.1, [lookup], [ranking], hnrs=[hnr]) assert [r.place_id for r in results] == res + [1000, 2000] @pytest.mark.parametrize('cc,res', [('es', [2, 1000]), ('pt', [92, 2000])]) - def test_lookup_with_country_restriction(self, apiobj, cc, res): - lookup = FieldLookup('name_vector', [1,2], 'lookup_all') + def test_lookup_with_country_restriction(self, apiobj, frontend, cc, res): + lookup = FieldLookup('name_vector', [1,2], LookupAll) ranking = FieldRanking('name_vector', 0.3, [RankedTokens(0.0, [10])]) - results = run_search(apiobj, 0.1, [lookup], [ranking], hnrs=['22'], + results = run_search(apiobj, frontend, 0.1, [lookup], [ranking], hnrs=['22'], ccodes=[cc]) assert [r.place_id for r in results] == res - def test_lookup_exclude_housenumber_placeid(self, apiobj): - lookup = FieldLookup('name_vector', [1,2], 'lookup_all') + def test_lookup_exclude_housenumber_placeid(self, apiobj, frontend): + lookup = FieldLookup('name_vector', [1,2], LookupAll) ranking = FieldRanking('name_vector', 0.3, [RankedTokens(0.0, [10])]) - results = run_search(apiobj, 0.1, [lookup], [ranking], hnrs=['22'], + results = run_search(apiobj, frontend, 0.1, [lookup], [ranking], hnrs=['22'], details=SearchDetails(excluded=[92])) assert [r.place_id for r in results] == [2, 1000, 2000] - def test_lookup_exclude_street_placeid(self, apiobj): - lookup = FieldLookup('name_vector', [1,2], 'lookup_all') + def test_lookup_exclude_street_placeid(self, apiobj, frontend): + lookup = FieldLookup('name_vector', [1,2], LookupAll) ranking = FieldRanking('name_vector', 0.3, [RankedTokens(0.0, [10])]) - results = run_search(apiobj, 0.1, [lookup], [ranking], hnrs=['22'], + results = run_search(apiobj, frontend, 0.1, [lookup], [ranking], hnrs=['22'], details=SearchDetails(excluded=[1000])) assert [r.place_id for r in results] == [2, 92, 2000] + def test_lookup_only_house_qualifier(self, apiobj, frontend): + lookup = FieldLookup('name_vector', [1,2], LookupAll) + ranking = FieldRanking('name_vector', 0.3, [RankedTokens(0.0, [10])]) + + results = run_search(apiobj, frontend, 0.1, [lookup], [ranking], hnrs=['22'], + quals=[('place', 'house')]) + + assert [r.place_id for r in results] == [2, 92] + + + def test_lookup_only_street_qualifier(self, apiobj, frontend): + lookup = FieldLookup('name_vector', [1,2], LookupAll) + ranking = FieldRanking('name_vector', 0.3, [RankedTokens(0.0, [10])]) + + results = run_search(apiobj, frontend, 0.1, [lookup], [ranking], hnrs=['22'], + quals=[('highway', 'residential')]) + + assert [r.place_id for r in results] == [1000, 2000] + + + @pytest.mark.parametrize('rank,found', [(26, True), (27, False), (30, False)]) + def test_lookup_min_rank(self, apiobj, frontend, rank, found): + lookup = FieldLookup('name_vector', [1,2], LookupAll) + ranking = FieldRanking('name_vector', 0.3, [RankedTokens(0.0, [10])]) + + results = run_search(apiobj, frontend, 0.1, [lookup], [ranking], hnrs=['22'], + details=SearchDetails(min_rank=rank)) + + assert [r.place_id for r in results] == ([2, 92, 1000, 2000] if found else [2, 92]) + + @pytest.mark.parametrize('geom', [napi.GeometryFormat.GEOJSON, napi.GeometryFormat.KML, napi.GeometryFormat.SVG, napi.GeometryFormat.TEXT]) - def test_return_geometries(self, apiobj, geom): - lookup = FieldLookup('name_vector', [1, 2], 'lookup_all') + def test_return_geometries(self, apiobj, frontend, geom): + lookup = FieldLookup('name_vector', [1, 2], LookupAll) - results = run_search(apiobj, 0.1, [lookup], [], hnrs=['20', '21', '22'], + results = run_search(apiobj, frontend, 0.1, [lookup], [], hnrs=['20', '21', '22'], details=SearchDetails(geometry_output=geom)) assert results assert all(geom.name.lower() in r.geometry for r in results) -def test_very_large_housenumber(apiobj): +def test_very_large_housenumber(apiobj, frontend): apiobj.add_placex(place_id=93, class_='place', type='house', parent_place_id=2000, housenumber='2467463524544', country_code='pt') @@ -278,15 +347,43 @@ def test_very_large_housenumber(apiobj): search_rank=26, address_rank=26, country_code='pt') - lookup = FieldLookup('name_vector', [1, 2], 'lookup_all') + lookup = FieldLookup('name_vector', [1, 2], LookupAll) - results = run_search(apiobj, 0.1, [lookup], [], hnrs=['2467463524544'], + results = run_search(apiobj, frontend, 0.1, [lookup], [], hnrs=['2467463524544'], details=SearchDetails()) assert results assert [r.place_id for r in results] == [93, 2000] +@pytest.mark.parametrize('wcount,rids', [(2, [990, 991]), (30000, [990])]) +def test_name_and_postcode(apiobj, frontend, wcount, rids): + apiobj.add_placex(place_id=990, class_='highway', type='service', + rank_search=27, rank_address=27, + postcode='11225', + centroid=(10.0, 10.0), + geometry='LINESTRING(9.995 10, 10.005 10)') + apiobj.add_search_name(990, names=[111], centroid=(10.0, 10.0), + search_rank=27, address_rank=27) + apiobj.add_placex(place_id=991, class_='highway', type='service', + rank_search=27, rank_address=27, + postcode='11221', + centroid=(10.3, 10.3), + geometry='LINESTRING(9.995 10.3, 10.005 10.3)') + apiobj.add_search_name(991, names=[111], centroid=(10.3, 10.3), + search_rank=27, address_rank=27) + apiobj.add_postcode(place_id=100, country_code='ch', postcode='11225', + geometry='POINT(10 10)') + + lookup = FieldLookup('name_vector', [111], LookupAll) + + results = run_search(apiobj, frontend, 0.1, [lookup], [], pcs=['11225'], count=wcount, + details=SearchDetails()) + + assert results + assert [r.place_id for r in results] == rids + + class TestInterpolations: @pytest.fixture(autouse=True) @@ -310,14 +407,29 @@ class TestInterpolations: @pytest.mark.parametrize('hnr,res', [('21', [992]), ('22', []), ('23', [991])]) - def test_lookup_housenumber(self, apiobj, hnr, res): - lookup = FieldLookup('name_vector', [111], 'lookup_all') + def test_lookup_housenumber(self, apiobj, frontend, hnr, res): + lookup = FieldLookup('name_vector', [111], LookupAll) - results = run_search(apiobj, 0.1, [lookup], [], hnrs=[hnr]) + results = run_search(apiobj, frontend, 0.1, [lookup], [], hnrs=[hnr]) assert [r.place_id for r in results] == res + [990] + @pytest.mark.parametrize('geom', [napi.GeometryFormat.GEOJSON, + napi.GeometryFormat.KML, + napi.GeometryFormat.SVG, + napi.GeometryFormat.TEXT]) + def test_osmline_with_geometries(self, apiobj, frontend, geom): + lookup = FieldLookup('name_vector', [111], LookupAll) + + results = run_search(apiobj, frontend, 0.1, [lookup], [], hnrs=['21'], + details=SearchDetails(geometry_output=geom)) + + assert results[0].place_id == 992 + assert geom.name.lower() in results[0].geometry + + + class TestTiger: @pytest.fixture(autouse=True) @@ -343,14 +455,28 @@ class TestTiger: @pytest.mark.parametrize('hnr,res', [('21', [992]), ('22', []), ('23', [991])]) - def test_lookup_housenumber(self, apiobj, hnr, res): - lookup = FieldLookup('name_vector', [111], 'lookup_all') + def test_lookup_housenumber(self, apiobj, frontend, hnr, res): + lookup = FieldLookup('name_vector', [111], LookupAll) - results = run_search(apiobj, 0.1, [lookup], [], hnrs=[hnr]) + results = run_search(apiobj, frontend, 0.1, [lookup], [], hnrs=[hnr]) assert [r.place_id for r in results] == res + [990] + @pytest.mark.parametrize('geom', [napi.GeometryFormat.GEOJSON, + napi.GeometryFormat.KML, + napi.GeometryFormat.SVG, + napi.GeometryFormat.TEXT]) + def test_tiger_with_geometries(self, apiobj, frontend, geom): + lookup = FieldLookup('name_vector', [111], LookupAll) + + results = run_search(apiobj, frontend, 0.1, [lookup], [], hnrs=['21'], + details=SearchDetails(geometry_output=geom)) + + assert results[0].place_id == 992 + assert geom.name.lower() in results[0].geometry + + class TestLayersRank30: @pytest.fixture(autouse=True) @@ -396,10 +522,10 @@ class TestLayersRank30: (napi.DataLayer.NATURAL, [227]), (napi.DataLayer.MANMADE | napi.DataLayer.NATURAL, [225, 227]), (napi.DataLayer.MANMADE | napi.DataLayer.RAILWAY, [225, 226])]) - def test_layers_rank30(self, apiobj, layer, res): - lookup = FieldLookup('name_vector', [34], 'lookup_any') + def test_layers_rank30(self, apiobj, frontend, layer, res): + lookup = FieldLookup('name_vector', [34], LookupAny) - results = run_search(apiobj, 0.1, [lookup], [], + results = run_search(apiobj, frontend, 0.1, [lookup], [], details=SearchDetails(layers=layer)) assert [r.place_id for r in results] == res