[ticket/16976] Fix search results count

PHPBB3-16976
This commit is contained in:
rxu 2022-03-24 20:54:48 +07:00
parent f16929ff9e
commit d44dd404a3
No known key found for this signature in database
GPG key ID: 955F0567380E586A
3 changed files with 14 additions and 5 deletions

View file

@ -508,7 +508,7 @@ class fulltext_mysql extends base implements search_backend_interface
); );
extract($this->phpbb_dispatcher->trigger_event('core.search_mysql_keywords_main_query_before', compact($vars))); extract($this->phpbb_dispatcher->trigger_event('core.search_mysql_keywords_main_query_before', compact($vars)));
$sql_select = ($type == 'posts') ? 'p.post_id' : 'DISTINCT t.topic_id'; $sql_select = ($type == 'posts') ? 'DISTINCT p.post_id' : 'DISTINCT t.topic_id';
$sql_select .= $sort_by_sql[$sort_key] ? ", {$sort_by_sql[$sort_key]}" : ''; $sql_select .= $sort_by_sql[$sort_key] ? ", {$sort_by_sql[$sort_key]}" : '';
$sql_from = ($join_topic) ? TOPICS_TABLE . ' t, ' : ''; $sql_from = ($join_topic) ? TOPICS_TABLE . ' t, ' : '';
$field = ($type == 'posts') ? 'post_id' : 'topic_id'; $field = ($type == 'posts') ? 'post_id' : 'topic_id';
@ -553,7 +553,7 @@ class fulltext_mysql extends base implements search_backend_interface
// if the total result count is not cached yet, retrieve it from the db // if the total result count is not cached yet, retrieve it from the db
if (!$result_count && count($id_ary)) if (!$result_count && count($id_ary))
{ {
$sql_found_rows = str_replace("SELECT $sql_select", "SELECT COUNT(*) as result_count", $sql); $sql_found_rows = str_replace("SELECT $sql_select", "SELECT COUNT($sql_select) as result_count", $sql);
$result = $this->db->sql_query($sql_found_rows); $result = $this->db->sql_query($sql_found_rows);
$result_count = (int) $this->db->sql_fetchfield('result_count'); $result_count = (int) $this->db->sql_fetchfield('result_count');
$this->db->sql_freeresult($result); $this->db->sql_freeresult($result);

View file

@ -605,7 +605,7 @@ class fulltext_native extends base implements search_backend_interface
$w_num = 0; $w_num = 0;
$sql_array = array( $sql_array = array(
'SELECT' => ($type == 'posts') ? 'p.post_id' : 'p.topic_id', 'SELECT' => ($type == 'posts') ? 'DISTINCT p.post_id' : 'DISTINCT p.topic_id',
'FROM' => array( 'FROM' => array(
SEARCH_WORDMATCH_TABLE => array(), SEARCH_WORDMATCH_TABLE => array(),
SEARCH_WORDLIST_TABLE => array(), SEARCH_WORDLIST_TABLE => array(),
@ -955,9 +955,9 @@ class fulltext_native extends base implements search_backend_interface
// If using mysql and the total result count is not calculated yet, get it from the db // If using mysql and the total result count is not calculated yet, get it from the db
if (!$total_results && $is_mysql) if (!$total_results && $is_mysql)
{ {
$sql_count = str_replace("SELECT {$sql_array['SELECT']}", "SELECT COUNT(DISTINCT {$sql_array['SELECT']}) as total_results", $sql); $sql_count = str_replace("SELECT {$sql_array['SELECT']}", "SELECT COUNT({$sql_array['SELECT']})", $sql);
$result = $this->db->sql_query($sql_count); $result = $this->db->sql_query($sql_count);
$total_results = (int) $this->db->sql_fetchfield('total_results'); $total_results = count($this->db->sql_fetchrowset($result));
$this->db->sql_freeresult($result); $this->db->sql_freeresult($result);
if (!$total_results) if (!$total_results)

View file

@ -88,6 +88,9 @@ abstract class phpbb_functional_search_base extends phpbb_functional_test_case
$this->create_search_index('phpbb\\search\\backend\\fulltext_native'); $this->create_search_index('phpbb\\search\\backend\\fulltext_native');
$post = $this->create_topic(2, 'Test Topic 1 foosubject', 'This is a test topic posted by the barsearch testing framework.'); $post = $this->create_topic(2, 'Test Topic 1 foosubject', 'This is a test topic posted by the barsearch testing framework.');
$topic_multiple_results_count1 = $this->create_topic(2, 'Test Topic for multiple search results', 'This is a test topic posted to test multiple results count.');
$this->create_post(2, $topic_multiple_results_count1['topic_id'], 'Re: Test Topic for multiple search results', 'This is a test post 2 posted to test multiple results count.');
$topic_multiple_results_count2 = $this->create_topic(2, 'Test Topic 2 for multiple search results', 'This is a test topic 2 posted to test multiple results count.');
$this->set_flood_interval(15); $this->set_flood_interval(15);
$crawler = self::request('GET', 'adm/index.php?i=acp_search&mode=settings&sid=' . $this->sid); $crawler = self::request('GET', 'adm/index.php?i=acp_search&mode=settings&sid=' . $this->sid);
@ -113,6 +116,8 @@ abstract class phpbb_functional_search_base extends phpbb_functional_test_case
// Search backed is not supported because don't appear in the select // Search backed is not supported because don't appear in the select
$this->delete_topic($post['topic_id']); $this->delete_topic($post['topic_id']);
$this->delete_topic($topic_by_author['topic_id']); $this->delete_topic($topic_by_author['topic_id']);
$this->delete_topic($topic_multiple_results_count1['topic_id']);
$this->delete_topic($topic_multiple_results_count2['topic_id']);
$this->markTestSkipped("Search backend is not supported/running"); $this->markTestSkipped("Search backend is not supported/running");
} }
@ -140,6 +145,8 @@ abstract class phpbb_functional_search_base extends phpbb_functional_test_case
$this->assert_search_found('foosubject+barsearch', 1, 2, $sort_key); $this->assert_search_found('foosubject+barsearch', 1, 2, $sort_key);
$this->assert_search_found('barsearch-testing', 1, 2, $sort_key); // test hyphen ignored $this->assert_search_found('barsearch-testing', 1, 2, $sort_key); // test hyphen ignored
$this->assert_search_found('barsearch+-+testing', 1, 2, $sort_key); // test hyphen wrapped with space ignored $this->assert_search_found('barsearch+-+testing', 1, 2, $sort_key); // test hyphen wrapped with space ignored
$this->assert_search_found('multiple+results+count', 3, 15, $sort_key); // test multiple results count - posts
$this->assert_search_found_topics('multiple+results+count', 2, $sort_key); // test multiple results count - topics
$this->assert_search_found_topics('phpbb3+installation', 1, $sort_key); $this->assert_search_found_topics('phpbb3+installation', 1, $sort_key);
$this->assert_search_found_topics('foosubject+barsearch', 1, $sort_key); $this->assert_search_found_topics('foosubject+barsearch', 1, $sort_key);
@ -156,6 +163,8 @@ abstract class phpbb_functional_search_base extends phpbb_functional_test_case
$this->delete_search_index(); $this->delete_search_index();
$this->delete_topic($post['topic_id']); $this->delete_topic($post['topic_id']);
$this->delete_topic($topic_by_author['topic_id']); $this->delete_topic($topic_by_author['topic_id']);
$this->delete_topic($topic_multiple_results_count1['topic_id']);
$this->delete_topic($topic_multiple_results_count2['topic_id']);
} }
protected function create_search_index($backend = null) protected function create_search_index($backend = null)