From d8cbaefad29bb5bcc630f7d201625af8ad55378d Mon Sep 17 00:00:00 2001 From: Danang Probo Sayekti Date: Tue, 11 Feb 2014 15:30:52 +0700 Subject: [PATCH] Improve the search Improve the search speed --- system/htmly.php | 7 +- system/includes/functions.php | 116 ++++++++++++---------------------- 2 files changed, 42 insertions(+), 81 deletions(-) diff --git a/system/htmly.php b/system/htmly.php index c0f0e25..a37f48a 100644 --- a/system/htmly.php +++ b/system/htmly.php @@ -939,12 +939,9 @@ get('/search/:keyword', function($keyword){ $page = $page ? (int)$page : 1; $perpage = config('search.perpage'); - $posts = get_keyword($keyword); + $posts = get_keyword($keyword, $page, $perpage); - $total = count($posts); - - // Extract a specific page with results - $posts = array_slice($posts, ($page-1) * $perpage, $perpage); + $total = keyword_count($keyword); if(empty($posts) || $page < 1){ // a non-existing page diff --git a/system/includes/functions.php b/system/includes/functions.php index 69c43b1..5de80cc 100644 --- a/system/includes/functions.php +++ b/system/includes/functions.php @@ -418,92 +418,31 @@ function get_static_post($static){ } // Return search page. -function get_keyword($keyword){ +function get_keyword($keyword, $page, $perpage){ - $posts = get_post_unsorted(); + $posts = get_post_sorted(); $tmp = array(); $words = explode(' ', $keyword); - if(!empty($posts)) { - - foreach($posts as $index => $v){ - foreach ($words as $word) { - if(strpos($v, strtolower($word)) !== false){ - - $post = new stdClass; - - // Extract the date - $arr = explode('_', $v); - - // Replaced string - $replaced = substr($arr[0], 0,strrpos($arr[0], '/')) . '/'; - - // Author string - $str = explode('/', $replaced); - $author = $str[count($str)-3]; - - // The post author + author url - $post->author = $author; - $post->authorurl = site_url() . 'author/' . $author; - - $dt = str_replace($replaced,'',$arr[0]); - $t = str_replace('-', '', $dt); - $time = new DateTime($t); - $timestamp= $time->format("Y-m-d H:i:s"); - - // The post date - $post->date = strtotime($timestamp); - - // The post URL - $post->url = site_url().date('Y/m', $post->date).'/'.str_replace('.md','',$arr[2]); - - $tag = array(); - $url = array(); - $bc = array(); - - $t = explode(',', $arr[1]); - foreach($t as $tt) { - $tag[] = array($tt, site_url(). 'tag/' . $tt); - } - - foreach($tag as $a) { - $url[] = ''. $a[0] .''; - $bc[] = ''. $a[0] .''; - } - - $post->tag = implode(', ', $url); - - $post->tagb = implode(' ยป ', $bc); - - $content = MarkdownExtra::defaultTransform(file_get_contents($v)); - - // Extract the title and body - $arr = explode('t-->', $content); - if(isset($arr[1])) { - $title = str_replace('