Fulltext Searching Now Available

That's right ladies and gentlemen, the search box in the upper right, which until this evening was decoration only, is now fully active.  I implemented what I thought was the last item on my TODO list for Linuxphile.  I have added another since, but well that might wait for another evening. 

Tonight's productivity is brought to you in part by Creative Punch. Using the howto there I was able to expand upon that to implement full text searching as I wanted it, with ordering of the score in descending order and then order by creation date, again in descending order. The major difference between what was posted on Creative Punch and what I have done is in the controller.  In order to order by the score I needed to specifiy the fields I wanted, including a DB::raw field of the match clause aliased as score. 

For interested parties, the code for my controller is below:


    public function search() {
        $searchTerm = Input::get('search');
        $posts = DB::table('posts')->select('id', 'title', 'summary', 'created_at', 'category_id', 'title_slug',
                                            DB::raw('(match (title,content) against (\''.$searchTerm.'\' in boolean mode)) as score'))
                ->whereRaw('match (title,content) against (\''.$searchTerm.'\' in boolean mode)')
                ->where('publish','=','1')
                ->orderBy('score', 'desc')
                ->orderBy('created_at', 'desc');
        return view('welcome')->with('posts', $posts->paginate(10));
    }


Leave a comment below if you have any suggestions for improving the search...And now perhaps I will begin that other item I have just added to the TODO list.  No rest for the wicked. 

0 Comments



Post Comment