controllers/SearchController.php
/**
     * Perform natural language search in database for given search query
     * 
     * @param string $searchStr
     * @return array $searchResults
     */
Perform natural language search in database for given search query

@param string $searchStr
@return array $searchResults
Perform natural language search in database for given search query @param string $searchStr @return array $searchResults
private function performSimpleSearch($searchStr) {
        $keywords = explode(" ", $searchStr);
        $keywords = array_unique($keywords);
        $dataset = $this->getModel()->getMatchedDataset($keywords);
        if (!empty($dataset)) {
            foreach ($dataset as $key => $row) {
                $relevenceScore = 0;
                foreach ($keywords as $keyword) {
                    $keyword = strtolower(trim($keyword));
                    $relevenceScore += (substr_count(strtolower($row['title']), $keyword)) * 2;
                    $relevenceScore += substr_count(strtolower($row['description']), $keyword);
                }
                if ($searchStr === $row['title'] || $searchStr === $row['description']) {
                    $relevenceScore += 20;
                }
                $dataset[$key]['relevenceScore'] = $relevenceScore;
            }
        }
        $sortedResults = $this->getSortedSearchResults($dataset);
        $sortedResults = $this->formatSearchResults($sortedResults, $keywords);
        return $sortedResults;
    }
private function performSimpleSearch ( $ searchStr ) { $ keywords = explode ( " " , $ searchStr ) ; $ keywords = array_unique ( $ keywords ) ; $ dataset = $ this -> getModel ( ) -> getMatchedDataset ( $ keywords ) ; if ( ! empty ( $ dataset ) ) { foreach ( $ dataset as $ key => $ row ) { $ relevenceScore = 0 ; foreach ( $ keywords as $ keyword ) { $ keyword = strtolower ( trim ( $ keyword ) ) ; $ relevenceScore += ( substr_count ( strtolower ( $ row [ 'title' ] ) , $ keyword ) ) * 2 ; $ relevenceScore += substr_count ( strtolower ( $ row [ 'description' ] ) , $ keyword ) ; } if ( $ searchStr === $ row [ 'title' ] || $ searchStr === $ row [ 'description' ] ) { $ relevenceScore += 20 ; } $ dataset [ $ key ] [ 'relevenceScore' ] = $ relevenceScore ; } } $ sortedResults = $ this -> getSortedSearchResults ( $ dataset ) ; $ sortedResults = $ this -> formatSearchResults ( $ sortedResults , $ keywords ) ; return $ sortedResults ; }
Perform natural language search in database for given search query
Perform natural language search in database for given search query