Frontend\Modules\Search\Engine\Model::getTotal PHP Method

getTotal() public static method

Note: please be aware that this is an approximate amount. It IS possible that this is not the exact amount of search results, since search results may vary in time (entries may not yet/no longer be shown) and we will not rebuild the entire search index on every search (would be a great performance killer and huge scalability loss) This function can be called with either a string as parameter (simple search) or an array (advanced search) Simple search: all search index fields will be searched for the given term Advanced search: only the given fields (keys in the array) will be matched to the corresponding values (corresponding values in the array)
public static getTotal ( string $term ) : integer
$term string The search term (simple search) or the fields to search for (advanced search - please note that the field names may not be consistent throughout several modules).
return integer
    public static function getTotal($term)
    {
        // advanced search
        if (is_array($term)) {
            // init vars
            $where = array();
            $join = array();
            $params = array();
            // loop all searches
            foreach ($term as $field => $value) {
                // get all terms to search for (including synonyms)
                $terms = self::getSynonyms((string) $value);
                // build search terms
                $terms = self::buildTerm($terms);
                $queryNr = count($where);
                // add query
                $where[$queryNr] = '(' . mb_substr(str_repeat('MATCH (i' . $queryNr . '.value) AGAINST (? IN BOOLEAN MODE) OR ', count($terms)), 0, -4) . ') AND i' . $queryNr . '.field = ? AND i' . $queryNr . '.language = ? AND i' . $queryNr . '.active = ? AND m' . $queryNr . '.searchable = ?';
                $join[$queryNr] = 'search_index AS i' . $queryNr . ($join ? ' ON i' . $queryNr . '.module = i0.module AND i' . $queryNr . '.other_id = i0.other_id' : '') . ' INNER JOIN search_modules AS m' . $queryNr . ' ON m' . $queryNr . '.module = i' . $queryNr . '.module';
                // add params
                $params = array_merge($params, $terms, array((string) $field, LANGUAGE, 'Y', 'Y'));
            }
            // prepare query and params
            $query = 'SELECT COUNT(module)
                 FROM
                 (
                     SELECT i0.module, i0.other_id
                     FROM ' . implode(' INNER JOIN ', $join) . '
                     WHERE ' . implode(' AND ', $where) . '
                 ) AS results';
        } else {
            // simple search
            // get all terms to search for (including synonyms)
            $terms = self::getSynonyms((string) $term);
            // build search terms
            $terms = self::buildTerm($terms);
            // prepare query and params
            $query = 'SELECT COUNT(module)
                 FROM
                 (
                     SELECT i.module
                     FROM search_index AS i
                     INNER JOIN search_modules AS m ON i.module = m.module
                     WHERE (' . mb_substr(str_repeat('MATCH (i.value) AGAINST (? IN BOOLEAN MODE) OR ', count($terms)), 0, -4) . ') AND i.language = ? AND i.active = ? AND m.searchable = ?
                GROUP BY i.module, i.other_id
            ) AS results';
            $params = array_merge($terms, array(LANGUAGE, 'Y', 'Y'));
        }
        // get the search results
        return (int) FrontendModel::getContainer()->get('database')->getVar($query, $params);
    }

Usage Example

Beispiel #1
0
 /**
  * Execute the action
  */
 public function execute()
 {
     parent::execute();
     // get parameters
     $charset = $this->getContainer()->getParameter('kernel.charset');
     $searchTerm = \SpoonFilter::getPostValue('term', null, '');
     $term = $charset == 'utf-8' ? \SpoonFilter::htmlspecialchars($searchTerm) : \SpoonFilter::htmlentities($searchTerm);
     // validate search term
     if ($term == '') {
         $this->output(self::BAD_REQUEST, null, 'term-parameter is missing.');
     } else {
         // previous search result
         $previousTerm = \SpoonSession::exists('searchTerm') ? \SpoonSession::get('searchTerm') : '';
         \SpoonSession::set('searchTerm', '');
         // save this term?
         if ($previousTerm != $term) {
             // format data
             $this->statistics = array();
             $this->statistics['term'] = $term;
             $this->statistics['language'] = LANGUAGE;
             $this->statistics['time'] = FrontendModel::getUTCDate();
             $this->statistics['data'] = serialize(array('server' => $_SERVER));
             $this->statistics['num_results'] = FrontendSearchModel::getTotal($term);
             // save data
             FrontendSearchModel::save($this->statistics);
         }
         // save current search term in cookie
         \SpoonSession::set('searchTerm', $term);
         // output
         $this->output(self::OK);
     }
 }
All Usage Examples Of Frontend\Modules\Search\Engine\Model::getTotal