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

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

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

报告相同问题?

悬赏问题

  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作