From: runntb Date: Tue, 1 May 2018 20:44:27 +0000 (-0400) Subject: search: Add result ranking in MySQL X-Git-Tag: 1.34.0-rc.0~5426^2~1 X-Git-Url: http://git.cyclocoop.org/%7D%7Cconcat%7B?a=commitdiff_plain;h=b033d3dc34fd29c698b33e91a83b5e25ffec41a4;p=lhc%2Fweb%2Fwiklou.git search: Add result ranking in MySQL MySQL normally attempts to rank the results when performing a full- text search. However, this behavior is disabled when using BOOLEAN MODE. While BOOLEAN MODE is needed in the WHERE clause, the default NATURAL LANGUAGE MODE can be used in an ORDER BY clause to reenable ranking. Bug: T192458 Change-Id: I09462c339432927efead58fb543a10aed2c53195 --- diff --git a/includes/search/SearchMySQL.php b/includes/search/SearchMySQL.php index 8e705c1fe8..c98f7e337c 100644 --- a/includes/search/SearchMySQL.php +++ b/includes/search/SearchMySQL.php @@ -34,13 +34,13 @@ class SearchMySQL extends SearchDatabase { private static $mMinSearchLength; /** - * Parse the user's query and transform it into an SQL fragment which will - * become part of a WHERE clause + * Parse the user's query and transform it into two SQL fragments: + * a WHERE condition and an ORDER BY expression * * @param string $filteredText * @param string $fulltext * - * @return string + * @return array */ function parseQuery( $filteredText, $fulltext ) { global $wgContLang; @@ -127,7 +127,10 @@ class SearchMySQL extends SearchDatabase { $searchon = $this->db->addQuotes( $searchon ); $field = $this->getIndexField( $fulltext ); - return " MATCH($field) AGAINST($searchon IN BOOLEAN MODE) "; + return [ + " MATCH($field) AGAINST($searchon IN BOOLEAN MODE) ", + " MATCH($field) AGAINST($searchon IN NATURAL LANGUAGE MODE) DESC " + ]; } function regexTerm( $string, $wildcard ) { @@ -303,7 +306,8 @@ class SearchMySQL extends SearchDatabase { $query['fields'][] = 'page_namespace'; $query['fields'][] = 'page_title'; $query['conds'][] = 'page_id=si_page'; - $query['conds'][] = $match; + $query['conds'][] = $match[0]; + $query['options']['ORDER BY'] = $match[1]; } /** @@ -318,7 +322,7 @@ class SearchMySQL extends SearchDatabase { $query = [ 'tables' => [ 'page', 'searchindex' ], 'fields' => [ 'COUNT(*) as c' ], - 'conds' => [ 'page_id=si_page', $match ], + 'conds' => [ 'page_id=si_page', $match[0] ], 'options' => [], 'joins' => [], ];