From 7f62f09cf377b92b615e456ec4539300b331fd4b Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Sun, 27 Mar 2011 13:16:41 -0700 Subject: Improve search to use wildcard matching, thanks to some code from tempg. Fixes #1663. --- modules/search/controllers/search.php | 3 ++- modules/search/helpers/search.php | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) (limited to 'modules/search') diff --git a/modules/search/controllers/search.php b/modules/search/controllers/search.php index eef009a0..75cbaa29 100644 --- a/modules/search/controllers/search.php +++ b/modules/search/controllers/search.php @@ -29,7 +29,8 @@ class Search_Controller extends Controller { $page = 1; } - list ($count, $result) = search::search($q, $page_size, $offset); + $q_with_more_terms = search::add_query_terms($q); + list ($count, $result) = search::search($q_with_more_terms, $page_size, $offset); $max_pages = max(ceil($count / $page_size), 1); diff --git a/modules/search/helpers/search.php b/modules/search/helpers/search.php index 09e5e83f..bbde8feb 100644 --- a/modules/search/helpers/search.php +++ b/modules/search/helpers/search.php @@ -18,6 +18,22 @@ * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */ class search_Core { + /** + * Add more terms to the query by wildcarding the stem value of the first + * few terms in the query. + */ + static function add_query_terms($q) { + $MAX_TERMS = 5; + $terms = explode(" ", $q, $MAX_TERMS); + for ($i = 0; $i < min(count($terms), $MAX_TERMS - 1); $i++) { + // Don't wildcard quoted or already wildcarded terms + if ((substr($terms[$i], 0, 1) != '"') && (substr($terms[$i], -1, 1) != "*")) { + $terms[] = rtrim($terms[$i], "s") . "*"; + } + } + return implode(" ", $terms); + } + static function search($q, $limit, $offset) { $db = Database::instance(); $q = $db->escape($q); -- cgit v1.2.3