为什么我的mysql全文搜索不起作用?

hxzsmxv2  于 2021-06-23  发布在  Mysql
关注(0)|答案(1)|浏览(623)

我正在使用mysql全文搜索,它可以工作,但我不能得到以下结果。
前任:

SELECT * FROM countries WHERE MATCH (name)
     AGAINST ("Chinese*" IN BOOLEAN MODE);

它应该给我一个“中国”的国家记录,但是它不起作用,有什么建议可以让这个功能更好/起作用吗?
我的完整代码:

protected function fullTextWildcards($term)
    {

        // removing symbols used by MySQL
        $reservedSymbols = ['-', '+', '<', '>', '@', '(', ')', '~'];
        $term = str_replace($reservedSymbols, '', $term);

        $words = explode(' ', $term);

        foreach($words as $key => $word) {
            /*
             * applying + operator (required word) only big words
             * because smaller ones are not indexed by MySQL
             */

            if(strlen($word) >= 3) {

                $words[$key] = '' . $word . '*';
            }
        }

        $searchTerm = implode( ' ', $words);

        return $searchTerm;
    }

    /**
     * Scope a query that matches a full text search of term.
     *
     * @param \Illuminate\Database\Eloquent\Builder $query
     * @param string $term
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function scopeSearch($query, $term)
    {
        $columns = implode(',',$this->searchable);

        $query->whereRaw("MATCH ({$columns}) AGAINST (? IN BOOLEAN MODE)" , $this->fullTextWildcards($term));
        return $query;
    }
kuarbcqp

kuarbcqp1#

我的解决办法是把字眼缩小:

if(strlen($word) >= 7){
                    $split_string = str_split($word, 4);

                    $join = implode( '*', $split_string);

                    $words[$key] = '' . $join . '*';
                }else{
                    $words[$key] = '' . $word . '*';
                }

相关问题