【问题标题】:Full text search in MySQL (PHP) with letters under ft_min_word_len在 MySQL (PHP) 中使用 ft_min_word_len 下的字母进行全文搜索
【发布时间】:2016-06-29 09:46:08
【问题描述】:

我在 MySQL 中的 ft_min_word_len 设置为 4。当我尝试对包含 2 或 3 个字母的内容进行全文搜索时,我没有得到任何结果。所以我使用这段代码来获取这些短词的全文搜索:

$table = $this->db->shop_products();

$where = '';
preg_match_all("~[\\pL\\pN_]+('[\\pL\\pN_]+)*~u", stripslashes($find), $matches);

foreach($matches[0] as $part) {
    $isFirst = empty($where);
    $regexp = "REGEXP '" . addslashes($part) . "'";

    if(!$isFirst) $where .= " AND (";
    $where .= "name {$regexp} OR content {$regexp}";
    if(!$isFirst) $where .= ")";
}

return $table->where($where)->limit(5)->fetchAll();

这段代码可以正常工作,但它的变音符号有问题,比如č, á, é, í...。例如,我有一个名为 的产品,当我只想查找 be 时,它不会显示该产品,因为它没有相同的字母。

注意:我使用 NotORM 进行 MySQL 查询,但我希望您知道 $table->where(... 是如何工作的。

【问题讨论】:

  • 为什么不将ft_min_word_len 设置为2?
  • 在我的网站托管上我不能做这样的事情。

标签: php mysql


【解决方案1】:

所以我找到了解决方案,但使用的是 PHP,而不是 MySQL。就在foreach 我把这行$part = diagriticToNormal($part); - 所以它会将特殊字符更改为经典字符,或者如何命名它。在这个答案下有那个功能。我知道它很蹩脚,不利于性能等......但作为一个开始就足够了。

function diagriticToNormal($text) {
    $charsMap = array(
        'a' => array('á', 'ä'),
        'c' => array('č'),
        'd' => array('ď'),
        'e' => array('é', 'ě'),
        'i' => array('í'),
        'l' => array('ľ', 'ĺ'),
        'n' => array('ň'),
        'o' => array('ô', 'ó'),
        'r' => array('ŕ', 'ř'),
        's' => array('š'),
        't' => array('ť'),
        'u' => array('ú', 'ů'),
        'y' => array('ý'),
        'z' => array('ž'),
    );

    foreach($charsMap as $real => $alt) {
        $alt = implode('|', $alt);
        $text = str_replace(array(strtolower($real), strtoupper($real)), '[' . $real . '|' . $alt . ']', $text);
    }

    return $text;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-29
    • 2016-06-06
    • 2012-04-16
    • 2010-11-02
    • 2019-11-16
    • 1970-01-01
    相关资源
    最近更新 更多