Plugins & Hacks

Does the Nucleus search function handle exact phrases?

Scenario:
"I've noticed that when I try searching on my blog, using the searchform, for the phrase "search phrase", I get some results with just "search" and some with just "phrase". But if I do this in my admin area, I will get results that only match "search phrase"! Is Nucleus able to do exact matches for a search phrase entered through the front end search as well?"

Solution:
The front end search function used by the searchform is a keyword search and strips quotes from the search query entered. In contrast, the admin area uses a different search function which looks for an exact match on the search phrase entered.

If you would like to use the admin area search function for your site instead of the default one, you can replace the search function inside BLOG.php.

Original forum thread (thanks, xiffy!):
http://forum.nucleuscms.org/viewtopic.php?t=14952

Method:

  • Open /nucleus/libs/BLOG.php and find the search function which starts from line 380:
    	function search($query, $template, $amountMonths, $maxresults, $startpos) {
            global $CONF, $manager;
     
    		$highlight 	= '';
    		$sqlquery	= $this->getSqlSearch($query, $amountMonths, $highlight);
     
    		if ($sqlquery == '')
    		{
    			// no query -> show everything
                $extraquery = '';
    		    $amountfound = $this->readLogAmount($template, $maxresults, $extraQuery, $query, 1, 1);
    		} else {
     
    			// add LIMIT to query (to split search results into pages)
                if (intval($maxresults > 0)) 
    	            $sqlquery .= ' LIMIT ' . intval($startpos).',' . intval($maxresults);
     
    			// show results
    		    $amountfound = $this->showUsingQuery($template, $sqlquery, $highlight, 1, 1);
     
    			// when no results were found, show a message 
        		if ($amountfound == 0) 
        		{
    	    		$template =& $manager->getTemplate($template);
        			$vars = array(
        				'query'		=> htmlspecialchars($query),
        				'blogid'	=> $this->getID()
        			);
    	    		echo TEMPLATE::fill($template['SEARCH_NOTHINGFOUND'],$vars);
        		}
            }
     
    		return $amountfound;
    	}
  • Replace it with the following code:
       function search($search, $template, $amount, $maxresults) {
     
          $extraQuery = "and ((i.ititle LIKE '%" . addslashes($search) . "%') or (i.ibody LIKE '%" . addslashes($search) . "%') or (i.imore LIKE '%" . addslashes($search) . "%'))";
     
          // take into account amount of months to search
          if ($amount > 0) {
                  $localtime = getdate($this->getCorrectTime());
             $timestamp_start = mktime(0,0,0,$localtime['mon'] - $amount,1,$localtime['year']);
             $extraQuery .= ' and UNIX_TIMESTAMP(i.itime)>' . $timestamp_start;
          }
     
          $amountfound = $this->readLogAmount($template, $maxresults, $extraQuery, $search, 1, 1);
     
          if ($amountfound == 0) {
             $template = TEMPLATE::read($template);
             $vars['query'] = htmlspecialchars($search);
             $vars['blogid'] = $this->getID();
     
             echo TEMPLATE::fill($template['SEARCH_NOTHINGFOUND'],$vars);
          }
     
          return $amountfound;
     
       }
section: Plugins & Hacks | submitted by Leng on 2007.Mar.17 | 8230 views

item rate
Total votes: 5 - Rating: 4.00

Please tell us how useful this answer was to you (0 = useless, 10 = very very helpful):

10