]> git.openstreetmap.org Git - nominatim.git/commitdiff
replace CASE construct with plpgsql function
authorSarah Hoffmann <lonvia@denofr.de>
Wed, 28 Jun 2023 12:27:35 +0000 (14:27 +0200)
committerSarah Hoffmann <lonvia@denofr.de>
Sat, 1 Jul 2023 16:15:22 +0000 (18:15 +0200)
lib-sql/functions/ranking.sql
nominatim/api/search/db_search_fields.py
nominatim/version.py

index af23335a862cbbb511f133937c149aa6fecd4e85..0b18954cedb985ab71430b20762958f7571dd6da 100644 (file)
@@ -284,3 +284,26 @@ BEGIN
 END;
 $$
 LANGUAGE plpgsql IMMUTABLE;
+
+
+CREATE OR REPLACE FUNCTION weigh_search(search_vector INT[],
+                                        term_vectors TEXT[],
+                                        weight_vectors FLOAT[],
+                                        def_weight FLOAT)
+  RETURNS FLOAT
+  AS $$
+DECLARE
+  pos INT := 1;
+  terms TEXT;
+BEGIN
+  FOREACH terms IN ARRAY term_vectors
+  LOOP
+    IF search_vector @> terms::INTEGER[] THEN
+      RETURN weight_vectors[pos];
+    END IF;
+    pos := pos + 1;
+  END LOOP;
+  RETURN def_weight;
+END;
+$$
+LANGUAGE plpgsql IMMUTABLE;
index 325e08df3559f9afd362242c69ec043c4ff617b0..13f1c56eb09493261a10ece0e728c9e7cf0f252d 100644 (file)
@@ -129,10 +129,11 @@ class FieldRanking:
         """
         assert self.rankings
 
-        col = table.c[self.column]
-
-        return sa.case(*((col.contains(r.tokens),r.penalty) for r in self.rankings),
-                       else_=self.default)
+        return sa.func.weigh_search(table.c[self.column],
+                                    [f"{{{','.join((str(s) for s in r.tokens))}}}"
+                                     for r in self.rankings],
+                                    [r.penalty for r in self.rankings],
+                                    self.default)
 
 
 @dataclasses.dataclass
index 346af5eb651f969bd8bd5694c34df4bc0840171e..beec32a53c366f573ff835fc555934e58ce50cf3 100644 (file)
@@ -34,7 +34,7 @@ class NominatimVersion(NamedTuple):
         return f"{self.major}.{self.minor}.{self.patch_level}-{self.db_patch_level}"
 
 
-NOMINATIM_VERSION = NominatimVersion(4, 2, 99, 1)
+NOMINATIM_VERSION = NominatimVersion(4, 2, 99, 2)
 
 POSTGRESQL_REQUIRED_VERSION = (9, 6)
 POSTGIS_REQUIRED_VERSION = (2, 2)