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

时间:2018-08-13 16:46:47

标签: php mysql laravel full-text-indexing

我正在使用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;
    }

1 个答案:

答案 0 :(得分:0)

“我的解决方案”的字眼较小:

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

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

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