我正在使用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条答案
按热度按时间kuarbcqp1#
我的解决办法是把字眼缩小: