From 0a1d5a44daddccdd5dd72e3e8b8ceb218c3952c5 Mon Sep 17 00:00:00 2001
From: Janne Mareike Koschinski <janne@kuschku.de>
Date: Wed, 17 Feb 2021 23:08:36 +0100
Subject: [PATCH] Unify ranking parameters

---
 database/Database.php                      | 16 ++++++++--------
 database/backends/Backend.php              |  4 +++-
 database/backends/PostgresSmartBackend.php | 20 +++++++++++++++-----
 database/backends/SQLiteSmartBackend.php   |  9 +++++++++
 4 files changed, 35 insertions(+), 14 deletions(-)

diff --git a/database/Database.php b/database/Database.php
index 44bc5cc..66855ab 100644
--- a/database/Database.php
+++ b/database/Database.php
@@ -69,15 +69,15 @@ class Database
 
     private function apply_config(\PDOStatement $stmt)
     {
-        if ($this->enable_ranking) {
-            if (!($this->backend instanceof SQLiteSmartBackend)) {
-                $stmt->bindValue(':config_normalization', 4, PDO::PARAM_INT);
-            }
-            $stmt->bindValue(':weight_content', 14, PDO::PARAM_INT);
+        $values = [
+            ":config_normalizatio" => 4,
+            ":weight_content" => 4,
+            ":weight_type" => 5,
+            ":weight_time" => 1,
+        ];
+        foreach ($this->rankingParameters() as $parameter) {
+            $stmt->bindValue($parameter, $values[$parameter], PDO::PARAM_INT);
         }
-
-        $stmt->bindValue(':weight_type', 32, PDO::PARAM_INT);
-        $stmt->bindValue(':weight_time', 1, PDO::PARAM_INT);
     }
 
     public function find(string $query, string $since = null, string $before = null, string $buffer = null, string $network = null, string $sender = null, int $limitPerBuffer = 4): array
diff --git a/database/backends/Backend.php b/database/backends/Backend.php
index 26656ca..92fe473 100644
--- a/database/backends/Backend.php
+++ b/database/backends/Backend.php
@@ -4,6 +4,8 @@ namespace QuasselRestSearch;
 
 interface Backend
 {
+    function rankingParameters(): array;
+
     public function findUser(): \PDOStatement;
 
     public function findInBuffers(): \PDOStatement;
@@ -17,4 +19,4 @@ interface Backend
     public function loadAfter(): \PDOStatement;
 
     public function loadBefore(): \PDOStatement;
-}
\ No newline at end of file
+}
diff --git a/database/backends/PostgresSmartBackend.php b/database/backends/PostgresSmartBackend.php
index 0d52e93..89b2c03 100644
--- a/database/backends/PostgresSmartBackend.php
+++ b/database/backends/PostgresSmartBackend.php
@@ -33,18 +33,28 @@ class PostgresSmartBackend implements Backend
         return array_key_exists('tsqueryfunction', $this->options) ? $this->options['tsqueryfunction'] : "plainto_tsquery('english', :query)";
     }
 
+    function rankingParameters(): array
+    {
+        return [
+            ":config_normalization",
+            ":weight_content",
+            ":weight_type",
+            ":weight_time"
+        ];
+    }
+
     private function rankingFunction(): string
     {
         if ($this->enable_ranking) {
             return "(
-                      (ts_rank_cd(tsv, query, :config_normalization) ^ :weight_content) *
+                      (ts_rank_cd(tsv, query, :config_normalization) ^ (2 ^ :weight_content)) *
                       ((CASE
                         WHEN TYPE IN (1, 4) THEN 1.0
                         WHEN TYPE IN (2, 1024, 2048, 4096, 16384) THEN 0.8
                         WHEN TYPE IN (32, 64, 128, 256, 512, 32768, 65536) THEN 0.6
                         WHEN TYPE IN (8, 16, 8192, 131072) THEN 0.4
-                        ELSE 0.2 END) ^ :weight_type) *
-                      ((1 / (EXTRACT(EPOCH FROM CURRENT_TIMESTAMP) - EXTRACT(EPOCH FROM time))) ^ :weight_time)
+                        ELSE 0.2 END) ^ (2 ^ :weight_type)) *
+                      ((1 / (EXTRACT(EPOCH FROM CURRENT_TIMESTAMP) - EXTRACT(EPOCH FROM time))) ^ (2 ^ :weight_time))
                     )";
         } else {
             return "(
@@ -53,8 +63,8 @@ class PostgresSmartBackend implements Backend
                         WHEN TYPE IN (2, 1024, 2048, 4096, 16384) THEN 0.8
                         WHEN TYPE IN (32, 64, 128, 256, 512, 32768, 65536) THEN 0.6
                         WHEN TYPE IN (8, 16, 8192, 131072) THEN 0.4
-                        ELSE 0.2 END) ^ :weight_type) *
-                      ((1 / (EXTRACT(EPOCH FROM CURRENT_TIMESTAMP) - EXTRACT(EPOCH FROM time))) ^ :weight_time)
+                        ELSE 0.2 END) ^ (2 ^ :weight_type)) *
+                      ((1 / (EXTRACT(EPOCH FROM CURRENT_TIMESTAMP) - EXTRACT(EPOCH FROM time))) ^ (2 ^ :weight_time))
                     )";
         }
     }
diff --git a/database/backends/SQLiteSmartBackend.php b/database/backends/SQLiteSmartBackend.php
index f414819..86936a2 100644
--- a/database/backends/SQLiteSmartBackend.php
+++ b/database/backends/SQLiteSmartBackend.php
@@ -124,6 +124,15 @@ class SQLiteSmartBackend implements Backend
         ");
     }
 
+    function rankingParameters(): array
+    {
+        return [
+            ":weight_content",
+            ":weight_type",
+            ":weight_time"
+        ];
+    }
+
     private function rankingFunction(): string
     {
         // TODO: Properly port missing support of Pow in sqlite
-- 
GitLab