protected $searchable = [
/**
* Columns and their priority in search results.
* Columns with higher values are more important.
* Columns with equal values have equal importance.
*
* @var array
*/
'columns' => ['title' => 10,
'intro' => 5,
'body' => 2,
]
];
select `id`, `title` from (select `posts`.*, max ((case when LOWER (`title`) LIKE 'id' then 150 else 0 end)
+ (case when LOWER (`title`) LIKE 'id%' then 50 else 0 end)
+ (case when LOWER (`title`) LIKE '%id%' then 10 else 0 end)
+ (case when LOWER (`intro`) LIKE 'id' then 75 else 0 end)
+ (case when LOWER (`intro`) LIKE 'id%' then 25 else 0 end)
+ (case when LOWER (`intro`) LIKE '%id%' then 5 else 0 end)
+ (case when LOWER (`body`) LIKE 'id' then 30 else 0 end)
+ (case when LOWER (`body`) LIKE 'id%' then 10 else 0 end)
+ (case when LOWER (`body`) LIKE '%id%' then 2 else 0 end))
as relevance from `posts` group by `posts`.`id`
having relevance >= 5.25 order by `relevance` desc) as `posts`
1.search ($query_str, $threshold = null, $entireText = false, $entireTextOnly = false);
2.$threshold=null 时,则 $threshold= (10 + 5 + 2) /4 = 4.25, 当 $threshold 设的越高,匹配度就越高。
4.LIKE '%id%' 则,relevance 1;LIKE 'id%' 则,relevance 5;LIKE 'id' 则,relevance * 15;
5.$entireText=true, 表示当 $query_str 有空格时,优先匹配整个字符串
6.$entireTextOnly =true, 当 $query_str 有空格时,表示只匹配整个字符串,匹配 $query_str 与 %$query_str%