From 5403276ca12918b59b566e009e2a14d1fab7cfed Mon Sep 17 00:00:00 2001 From: Jon Stovell Date: Mon, 10 Feb 2025 17:58:24 -0700 Subject: [PATCH] Returns correct search results when post moderation is enabled Signed-off-by: Jon Stovell --- Sources/Search.php | 6 ++---- other/install_2-1_mysql.sql | 2 +- other/install_2-1_postgresql.sql | 2 +- other/upgrade_2-1_mysql.sql | 9 +++++++++ other/upgrade_2-1_postgresql.sql | 9 +++++++++ 5 files changed, 22 insertions(+), 6 deletions(-) diff --git a/Sources/Search.php b/Sources/Search.php index 028bf80c15..10e9058fc8 100644 --- a/Sources/Search.php +++ b/Sources/Search.php @@ -1282,6 +1282,7 @@ function PlushSearch2() 'select' => array( 'id_search' => $_SESSION['search_cache']['id_search'], 'relevance' => '0', + 'id_topic' => 't.id_topic', ), 'weights' => array(), 'from' => '{db_prefix}topics AS t', @@ -1306,7 +1307,6 @@ function PlushSearch2() if (empty($search_params['topic']) && empty($search_params['show_complete'])) { - $main_query['select']['id_topic'] = 't.id_topic'; $main_query['select']['id_msg'] = 'MAX(m.id_msg) AS id_msg'; $main_query['select']['num_matches'] = 'COUNT(*) AS num_matches'; @@ -1316,8 +1316,6 @@ function PlushSearch2() } else { - // This is outrageous! - $main_query['select']['id_topic'] = 'm.id_msg AS id_topic'; $main_query['select']['id_msg'] = 'm.id_msg'; $main_query['select']['num_matches'] = '1 AS num_matches'; @@ -1849,7 +1847,7 @@ function PlushSearch2() // *** Retrieve the results to be shown on the page $participants = array(); $request = $smcFunc['db_search_query']('', ' - SELECT ' . (empty($search_params['topic']) ? 'lsr.id_topic' : $search_params['topic'] . ' AS id_topic') . ', lsr.id_msg, lsr.relevance, lsr.num_matches + SELECT lsr.id_topic, lsr.id_msg, lsr.relevance, lsr.num_matches FROM {db_prefix}log_search_results AS lsr' . ($search_params['sort'] == 'num_replies' || !empty($approve_query) ? ' INNER JOIN {db_prefix}topics AS t ON (t.id_topic = lsr.id_topic)' : '') . ' WHERE lsr.id_search = {int:id_search}' . $approve_query . ' diff --git a/other/install_2-1_mysql.sql b/other/install_2-1_mysql.sql index 46968ff55b..02d75253b6 100644 --- a/other/install_2-1_mysql.sql +++ b/other/install_2-1_mysql.sql @@ -556,7 +556,7 @@ CREATE TABLE {$db_prefix}log_search_results ( id_msg INT UNSIGNED NOT NULL DEFAULT '0', relevance SMALLINT UNSIGNED NOT NULL DEFAULT '0', num_matches SMALLINT UNSIGNED NOT NULL DEFAULT '0', - PRIMARY KEY (id_search, id_topic) + PRIMARY KEY (id_search, id_topic, id_msg) ) ENGINE={$engine}; # diff --git a/other/install_2-1_postgresql.sql b/other/install_2-1_postgresql.sql index 54fe2970bb..a0075d5d16 100644 --- a/other/install_2-1_postgresql.sql +++ b/other/install_2-1_postgresql.sql @@ -865,7 +865,7 @@ CREATE TABLE {$db_prefix}log_search_results ( id_msg bigint NOT NULL DEFAULT '0', relevance smallint NOT NULL DEFAULT '0', num_matches smallint NOT NULL DEFAULT '0', - PRIMARY KEY (id_search, id_topic) + PRIMARY KEY (id_search, id_topic, id_msg) ); # diff --git a/other/upgrade_2-1_mysql.sql b/other/upgrade_2-1_mysql.sql index bfb5761fa5..a01da80a36 100644 --- a/other/upgrade_2-1_mysql.sql +++ b/other/upgrade_2-1_mysql.sql @@ -3902,4 +3902,13 @@ foreach($files AS $filename) unset($_GET['last_action_id']); unset($_GET['total_fixes']); ---} +---# + +/******************************************************************************/ +--- Improving search results storage +/******************************************************************************/ + +---# Updating primary key for log_search_results table +ALTER TABLE {$db_prefix}log_search_results DROP PRIMARY KEY; +ALTER TABLE {$db_prefix}log_search_results ADD PRIMARY KEY (id_search, id_topic, id_msg); ---# \ No newline at end of file diff --git a/other/upgrade_2-1_postgresql.sql b/other/upgrade_2-1_postgresql.sql index 6fa581f000..714a48f3f7 100644 --- a/other/upgrade_2-1_postgresql.sql +++ b/other/upgrade_2-1_postgresql.sql @@ -4264,4 +4264,13 @@ foreach($files AS $filename) unset($_GET['last_action_id']); unset($_GET['total_fixes']); ---} +---# + +/******************************************************************************/ +--- Improving search results storage +/******************************************************************************/ + +---# Updating primary key for log_search_results table +ALTER TABLE {$db_prefix}log_search_results DROP PRIMARY KEY; +ALTER TABLE {$db_prefix}log_search_results ADD PRIMARY KEY (id_search, id_topic, id_msg); ---# \ No newline at end of file