From d1b2a9d7087ecc171f941755b4c3a420cd202842 Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Thu, 19 Mar 2009 11:01:58 -0400 Subject: [PATCH] Somewhat better behaviour with mixed caps in search Deal somewhat better with mixed caps in people and notice search. --- actions/groupsearch.php | 11 +++-------- actions/noticesearch.php | 2 +- actions/noticesearchrss.php | 3 --- actions/peoplesearch.php | 6 +++--- lib/search_engines.php | 34 +++++++++++++++++++++++++--------- 5 files changed, 32 insertions(+), 24 deletions(-) diff --git a/actions/groupsearch.php b/actions/groupsearch.php index 9b0026db94..109a53ce11 100644 --- a/actions/groupsearch.php +++ b/actions/groupsearch.php @@ -1,9 +1,4 @@ terms = array_map('preg_quote', + $this->terms = array_map('preg_quote', array_map('htmlspecialchars', $terms)); $this->pattern = '/('.implode('|',$terms).')/i'; } - + function highlight($text) { return preg_replace($this->pattern, '\\1', htmlspecialchars($text)); diff --git a/actions/noticesearch.php b/actions/noticesearch.php index 83e59dd9ae..eb4a072def 100644 --- a/actions/noticesearch.php +++ b/actions/noticesearch.php @@ -103,7 +103,7 @@ class NoticesearchAction extends SearchAction function showResults($q, $page) { $notice = new Notice(); - $q = strtolower($q); + $search_engine = $notice->getSearchEngine('identica_notices'); $search_engine->set_sort_mode('chron'); // Ask for an extra to see if there's more. diff --git a/actions/noticesearchrss.php b/actions/noticesearchrss.php index 0f98ed04bb..ba5276d06e 100644 --- a/actions/noticesearchrss.php +++ b/actions/noticesearchrss.php @@ -62,9 +62,6 @@ class NoticesearchrssAction extends Rss10Action $notice = new Notice(); - # lcase it for comparison - $q = strtolower($q); - $search_engine = $notice->getSearchEngine('identica_notices'); $search_engine->set_sort_mode('chron'); diff --git a/actions/peoplesearch.php b/actions/peoplesearch.php index 14177fcf0d..9e515ade1a 100644 --- a/actions/peoplesearch.php +++ b/actions/peoplesearch.php @@ -63,13 +63,13 @@ class PeoplesearchAction extends SearchAction $profile = new Profile(); - # lcase it for comparison - $q = strtolower($q); + // lcase it for comparison + // $q = strtolower($q); $search_engine = $profile->getSearchEngine('identica_people'); $search_engine->set_sort_mode('chron'); - # Ask for an extra to see if there's more. + // Ask for an extra to see if there's more. $search_engine->limit((($page-1)*PROFILES_PER_PAGE), PROFILES_PER_PAGE + 1); if (false === $search_engine->query($q)) { $cnt = 0; diff --git a/lib/search_engines.php b/lib/search_engines.php index 559107910c..7b9dbb6182 100644 --- a/lib/search_engines.php +++ b/lib/search_engines.php @@ -74,7 +74,7 @@ class SphinxSearch extends SearchEngine { //FIXME without LARGEST_POSSIBLE, the most recent results aren't returned // this probably has a large impact on performance - $LARGEST_POSSIBLE = 1e6; + $LARGEST_POSSIBLE = 1e6; if ($rss) { $this->sphinx->setLimits($offset, $count, $count, $LARGEST_POSSIBLE); @@ -109,12 +109,25 @@ class MySQLSearch extends SearchEngine { function query($q) { - if ('identica_people' === $this->table) - return $this->target->whereAdd('MATCH(nickname, fullname, location, bio, homepage) ' . - 'against (\''.addslashes($q).'\')'); - if ('identica_notices' === $this->table) - return $this->target->whereAdd('MATCH(content) ' . - 'against (\''.addslashes($q).'\')'); + if ('identica_people' === $this->table) { + $this->target->whereAdd('MATCH(nickname, fullname, location, bio, homepage) ' . + 'AGAINST (\''.addslashes($q).'\' IN BOOLEAN MODE)'); + if (strtolower($q) != $q) { + $this->target->whereAdd('MATCH(nickname, fullname, location, bio, homepage) ' . + 'AGAINST (\''.addslashes(strtolower($q)).'\' IN BOOLEAN MODE)', 'OR'); + } + return true; + } else if ('identica_notices' === $this->table) { + $this->target->whereAdd('MATCH(content) ' . + 'AGAINST (\''.addslashes($q).'\' IN BOOLEAN MODE)'); + if (strtolower($q) != $q) { + $this->target->whereAdd('MATCH(content) ' . + 'AGAINST (\''.addslashes(strtolower($q)).'\' IN BOOLEAN MODE)', 'OR'); + } + return true; + } else { + throw new ServerException('Unknown table: ' . $this->table); + } } } @@ -122,10 +135,13 @@ class PGSearch extends SearchEngine { function query($q) { - if ('identica_people' === $this->table) + if ('identica_people' === $this->table) { return $this->target->whereAdd('textsearch @@ plainto_tsquery(\''.addslashes($q).'\')'); - if ('identica_notices' === $this->table) + } else if ('identica_notices' === $this->table) { return $this->target->whereAdd('to_tsvector(\'english\', content) @@ plainto_tsquery(\''.addslashes($q).'\')'); + } else { + throw new ServerException('Unknown table: ' . $this->table); + } } }