gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 17:12:05 25 4
gpt4 key购买 nike

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

最佳答案

我的解决方案是将单词分解得更小:

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

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

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

关于php - 为什么我的 MySQL 全文搜索不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51827046/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com