]> git.openstreetmap.org Git - nominatim.git/commitdiff
do not use index when searching in large areas
authorSarah Hoffmann <lonvia@denofr.de>
Sat, 12 Aug 2023 14:12:44 +0000 (16:12 +0200)
committerSarah Hoffmann <lonvia@denofr.de>
Sat, 12 Aug 2023 14:12:44 +0000 (16:12 +0200)
This concerns viewboxes as well as radius search.

nominatim/api/search/db_searches.py
nominatim/api/search/geocoder.py
nominatim/api/search/token_assignment.py
nominatim/db/sqlalchemy_types.py

index 77d9f848b77231f065141c33883e1bc1c6b3676c..02a45e7c2d4d9f7db707dc3d69f8cc6051691199 100644 (file)
@@ -622,7 +622,10 @@ class PlaceSearch(AbstractSearch):
 
         if details.viewbox is not None:
             if details.bounded_viewbox:
-                sql = sql.where(tsearch.c.centroid.intersects(VIEWBOX_PARAM))
+                if details.viewbox.area < 0.2:
+                    sql = sql.where(tsearch.c.centroid.intersects(VIEWBOX_PARAM))
+                else:
+                    sql = sql.where(tsearch.c.centroid.ST_Intersects_no_index(VIEWBOX_PARAM))
             else:
                 penalty += sa.case((t.c.geometry.intersects(VIEWBOX_PARAM), 0.0),
                                    (t.c.geometry.intersects(VIEWBOX2_PARAM), 1.0),
@@ -630,7 +633,11 @@ class PlaceSearch(AbstractSearch):
 
         if details.near is not None:
             if details.near_radius is not None:
-                sql = sql.where(tsearch.c.centroid.ST_DWithin(NEAR_PARAM, NEAR_RADIUS_PARAM))
+                if details.near_radius < 0.1:
+                    sql = sql.where(tsearch.c.centroid.ST_DWithin(NEAR_PARAM, NEAR_RADIUS_PARAM))
+                else:
+                    sql = sql.where(tsearch.c.centroid.ST_DWithin_no_index(NEAR_PARAM,
+                                                                           NEAR_RADIUS_PARAM))
             sql = sql.add_columns(-tsearch.c.centroid.ST_Distance(NEAR_PARAM)
                                       .label('importance'))
             sql = sql.order_by(sa.desc(sa.text('importance')))
index 311840a9e9bcf7bd36c9d48bd543b086b7e7b115..564e3d8dadc1df4181c5f3a2a5c4bfb86ade0c77 100644 (file)
@@ -152,7 +152,8 @@ class ForwardGeocoder:
 # pylint: disable=invalid-name,too-many-locals
 def _dump_searches(searches: List[AbstractSearch], query: QueryStruct,
                    start: int = 0) -> Iterator[Optional[List[Any]]]:
-    yield ['Penalty', 'Lookups', 'Housenr', 'Postcode', 'Countries', 'Qualifier', 'Catgeory', 'Rankings']
+    yield ['Penalty', 'Lookups', 'Housenr', 'Postcode', 'Countries',
+           'Qualifier', 'Catgeory', 'Rankings']
 
     def tk(tl: List[int]) -> str:
         tstr = [f"{query.find_lookup_word_by_id(t)}({t})" for t in tl]
index 0ae2cd4306c9e4ca1c78cbd9acd9c0363aa7b9c8..3f0e737b004d623f5eef758a40b1a59e1d495806 100644 (file)
@@ -253,6 +253,8 @@ class _TokenSequence:
                 priors = sum(1 for t in self.seq[hnrpos+1:] if t.ttype == qmod.TokenType.PARTIAL)
                 if not self._adapt_penalty_from_priors(priors, 1):
                     return False
+            if any(t.ttype == qmod.TokenType.CATEGORY for t in self.seq):
+                self.penalty += 1.0
 
         return True
 
index 7d3789aa0d3b44854583aeb09686d8edaa1c421e..9e1f9fcecf8c1dc819e616bbbe708f40ccfd7949 100644 (file)
@@ -66,7 +66,15 @@ class Geometry(types.UserDefinedType): # type: ignore[type-arg]
 
 
         def ST_DWithin(self, other: SaColumn, distance: SaColumn) -> SaColumn:
-            return sa.func.ST_DWithin(self, other, distance, type_=sa.Float)
+            return sa.func.ST_DWithin(self, other, distance, type_=sa.Boolean)
+
+
+        def ST_DWithin_no_index(self, other: SaColumn, distance: SaColumn) -> SaColumn:
+            return sa.func._ST_DWithin(self, other, distance, type_=sa.Boolean)
+
+
+        def ST_Intersects_no_index(self, other: SaColumn) -> SaColumn:
+            return sa.func._ST_Intersects(self, other, type_=sa.Boolean)
 
 
         def ST_Distance(self, other: SaColumn) -> SaColumn: