searchable- 搜索字符串

2018-04-21 21:46:56 阅读:3 编辑
 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`

https://github.com/nicolaslopezj/searchable