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

execSearch() public static method

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 execSearch ( mixed $term, integer $limit = 20, integer $offset ) : array
$term mixed 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).
$limit integer The number of articles to get.
$offset integer The offset.
return array
    public static function execSearch($term, $limit = 20, $offset = 0)
    {
        $limit = (int) $limit;
        $offset = (int) $offset;
        // advanced search
        if (is_array($term)) {
            // init vars
            $where = array();
            $order = array();
            $join = array();
            $params1 = array();
            $params2 = 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 = ?';
                $order[$queryNr] = '(' . mb_substr(str_repeat('MATCH (i' . $queryNr . '.value) AGAINST (? IN BOOLEAN MODE) + ', count($terms)), 0, -3) . ') * m' . $queryNr . '.weight';
                $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
                $params1 = array_merge($params1, $terms);
                $params2 = array_merge($params2, $terms, array((string) $field, LANGUAGE, 'Y', 'Y'));
            }
            // prepare query and params
            $query = 'SELECT i0.module, i0.other_id, ' . implode(' + ', $order) . ' AS score
                 FROM ' . implode(' INNER JOIN ', $join) . '
                 WHERE ' . implode(' AND ', $where) . '
                 ORDER BY score DESC
                 LIMIT ?, ?';
            $params = array_merge($params1, $params2, array($offset, $limit));
        } 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 i.module, i.other_id, SUM(' . mb_substr(str_repeat('MATCH (i.value) AGAINST (? IN BOOLEAN MODE) + ', count($terms)), 0, -3) . ') * m.weight AS score
                 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 module, other_id
                 ORDER BY score DESC
                 LIMIT ?, ?';
            $params = array_merge($terms, $terms, array(LANGUAGE, 'Y', 'Y', $offset, $limit));
        }
        return (array) FrontendModel::getContainer()->get('database')->getRecords($query, $params);
    }