doulong2782 2014-08-12 07:28
浏览 358
已采纳

MySQL MATCH AGAINST查询不返回某些请求

I have a search form on my website that is suppose to search article titles in my table for matching words. It seems to be working for certain strings but not others and I'm not sure why. For instance if you search for "assassin's creed" the search function return the right rows from the table. But if you search for "the last of us", there are no results, even though there is an article title with that string in it.

Below is my PHP/SQL command. Is there anything I need to add/change? Do I need to run a FULLTEXT index on the table's column each time I run a query? This is the first time I've used MySQL's MATCH AGAINST functionality. Usually I would use LIKE but after doing a little reading, apparently MATCH AGAINST is a much better approach.

public function fetchSearchArticles($q, $category, $platform, $sort) {

    $q = addslashes($q);
    $category = ($category != 'all')? ' AND `cat_id` = '.$this->fetch_category_id($category).' ' : '';
    $platform = ($platform != 'all')? ' AND `'.str_replace('-', '_', $platform).'` = 1 ': '';
    $sort = ($sort != 'rel')? '`article_timestamp` '.strtoupper($sort).'' : "MATCH(`article_title`) AGAINST('$q')";

    $query = $this->db->prepare ("
             SELECT * 
             FROM articles 
             WHERE `article_is_published` = 1
             $platform
             $category
             AND MATCH(`article_title`) AGAINST('+$q' IN BOOLEAN MODE) 
             ORDER BY $sort
             LIMIT 20
            ");

    try{
        $query->execute();
        $articles = $query->fetchAll(PDO::FETCH_ASSOC);
        $query->closeCursor();
        return $articles;

    } catch(PDOException $e){
        die($e->getMessage());
    }
}
  • 写回答

1条回答 默认 最新

  • douxie7738 2014-08-12 08:15
    关注

    I believe the reason its not working for "the last of us" because, they are all called stop words in context of FULL TEXT search.

    You can verify the same or an alternative solution I could suggest you is to disable stopwords from fulltext. You just need to locate .cnf file and add this,

    ft_stopword_file = ""
    

    Restart mysql engine and rebuild indexes;

    Hope that works

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?