Beispiel #1
0
 /**
  * 关键词搜索
  */
 public function init()
 {
     G('begin_time');
     // 搜索配置
     $setting = S('search/search');
     $search_model = S('search/search_model');
     $type_application = S('search/type_application');
     if (isset($_GET['q'])) {
         if (trim($_GET['q']) == '') {
             header('Location: ' . SITE_URL . 'index.php?app=search');
             exit;
         }
         $typeid = isset($_GET['typeid']) && $_GET['typeid'] > 0 ? intval($_GET['typeid']) : 1;
         $time = empty($_GET['time']) ? 'all' : trim($_GET['time']);
         $page = isset($_GET['page']) ? intval($_GET['page']) : 1;
         $pagesize = 10;
         $q = safe_replace(trim($_GET['q']));
         $q = htmlspecialchars(strip_tags($q));
         $q = str_replace('%', '', $q);
         // 过滤'%',用户全文搜索
         $search_q = $q;
         // 搜索原内容
         // 按时间搜索
         $where = array();
         if ($time == 'day') {
             $search_time = TIME - 86400;
             $where['adddate'] = array('gt', $search_time);
         } elseif ($time == 'week') {
             $search_time = TIME - 604800;
             $where['adddate'] = array('gt', $search_time);
         } elseif ($time == 'month') {
             $search_time = TIME - 2592000;
             $where['adddate'] = array('gt', $search_time);
         } elseif ($time == 'year') {
             $search_time = TIME - 31536000;
             $where['adddate'] = array('gt', $search_time);
         } else {
             $search_time = 0;
         }
         if ($page == 1 && !$setting['sphinxenable']) {
             // 精确搜索
             if ($typeid != 0) {
                 $where['typeid'] = $typeid;
             }
             $where['data'] = array('like', "%{$q}%");
             $commend = $this->db->where($where)->find();
         } else {
             $commend = '';
         }
         // 如果开启sphinx
         if ($setting['sphinxenable']) {
             $sphinx = Loader::lib('search:search_interface', '', 0);
             $sphinx = new search_interface();
             $offset = $pagesize * ($page - 1);
             $res = $sphinx->search($q, array($typeid), array($search_time, TIME), $offset, $pagesize, '@weight desc');
             $totalnums = $res['total'];
             // 如果结果不为空
             if (!empty($res['matches'])) {
                 $result = $res['matches'];
             }
         } else {
             $segment = Loader::lib('Segment');
             // 分词结果
             $segment_q = $segment->get_keyword($segment->split_result($q));
             // 如果分词结果为空
             if (!empty($segment_q)) {
                 $sql = array();
                 $sql['typeid'] = $typeid;
                 $sql = array_merge($where, $sql);
                 $sql = $this->db->where($sql)->to_sql();
                 $sql = "{$sql} AND MATCH (`data`) AGAINST ('{$segment_q}' IN BOOLEAN MODE)";
             } else {
                 $sql = array();
                 $sql['typeid'] = $typeid;
                 $sql = array_merge($where, $sql);
                 $sql['data'] = array('like', "%{$q}%");
             }
             $result = $this->db->where($where)->order('searchid DESC')->listinfo($page, 10);
         }
         // 如果开启相关搜索功能
         if ($setting['relationenble']) {
             // 如果关键词长度在8-16之间,保存关键词作为relation search
             $this->keyword_db = loader::model('search_keyword_model');
             if (strlen($q) < 17 && strlen($q) > 5 && !empty($segment_q)) {
                 $res = $this->keyword_db->where(array('keyword' => $q))->find();
                 if ($res) {
                     // 关键词搜索数+1
                     $this->keyword_db->where(array('keyword' => $q))->update(array('searchnums' => '+=1'));
                 } else {
                     // 关键词转换为拼音
                     $pinyin = string_to_pinyin($q);
                     $this->keyword_db->insert(array('keyword' => $q, 'searchnums' => 1, 'data' => $segment_q, 'pinyin' => $pinyin));
                 }
             }
             // 相关搜索
             if (!empty($segment_q)) {
                 $relation_q = str_replace(' ', '%', $segment_q);
             } else {
                 $relation_q = $q;
             }
             $relation = $this->keyword_db->where("MATCH (`data`) AGAINST ('%{$relation_q}%' IN BOOLEAN MODE)")->order('searchnums DESC')->limit(10)->select();
         }
         // 如果结果不为空
         if (!empty($result) || !empty($commend['id'])) {
             // 开启sphinx后文章id取法不同
             if ($setting['sphinxenable']) {
                 foreach ($result as $_v) {
                     $sids[] = $_v['attrs']['id'];
                 }
             } else {
                 foreach ($result as $_v) {
                     $sids[] = $_v['id'];
                 }
             }
             if (!empty($commend['id'])) {
                 $sids[] = $commend['id'];
             }
             $sids = array_unique($sids);
             $ids = implode(',', $sids);
             $where = array('id' => array('in', $ids));
             // 获取模型id
             $model_type_cache = S('search/type_model');
             $model_type_cache = array_flip($model_type_cache);
             $modelid = $model_type_cache[$typeid];
             // 是否读取其他模块接口
             if ($modelid) {
                 $this->content_db->set_model($modelid);
                 if ($setting['sphinxenable']) {
                     $data = $this->content_db->where($where)->order('id DESC')->listinfo(1, $pagesize);
                     $pages = Page::pages($totalnums, $page, $pagesize);
                 } else {
                     $data = $this->content_db->where($where)->select();
                     $pages = $this->db->pages;
                     $totalnums = $this->db->number;
                 }
                 // 如果分词结果为空
                 if (!empty($segment_q)) {
                     $replace = explode(' ', $segment_q);
                     foreach ($replace as $replace_arr_v) {
                         $replace_arr[] = '<font color=red>' . $replace_arr_v . '</font>';
                     }
                     foreach ($data as $_k => $_v) {
                         $data[$_k]['title'] = str_replace($replace, $replace_arr, $_v['title']);
                         $data[$_k]['description'] = str_replace($replace, $replace_arr, $_v['description']);
                     }
                 } else {
                     foreach ($data as $_k => $_v) {
                         $data[$_k]['title'] = str_replace($q, '<font color=red>' . $q . '</font>', $_v['title']);
                         $data[$_k]['description'] = str_replace($q, '<font color=red>' . $q . '</font>', $_v['description']);
                     }
                 }
             } else {
                 // 读取专辑搜索接口
                 $special_api = Loader::lib('special:search_api');
                 $data = $special_api->get_search_data($sids);
             }
         }
         G('search_time');
         $pages = isset($pages) ? $pages : '';
         $totalnums = isset($totalnums) ? $totalnums : 0;
         $data = isset($data) ? $data : '';
         $execute_time = G('begin_time', 'search_time');
         include template('search', 'list');
     } else {
         include template('search', 'index');
     }
 }
Beispiel #2
0
 /**
  * 关键词搜索
  */
 public function init()
 {
     //获取siteid
     $siteid = isset($_REQUEST['siteid']) && trim($_REQUEST['siteid']) ? intval($_REQUEST['siteid']) : 1;
     $SEO = seo($siteid);
     //搜索配置
     $search_setting = getcache('search');
     $setting = $search_setting[$siteid];
     $search_model = getcache('search_model_' . $siteid);
     $type_module = getcache('type_module_' . $siteid);
     if (isset($_GET['q'])) {
         if (trim($_GET['q']) == '') {
             header('Location: ' . APP_PATH . 'index.php?m=search');
             exit;
         }
         $typeid = empty($_GET['typeid']) ? 48 : intval($_GET['typeid']);
         $time = empty($_GET['time']) || !in_array($_GET['time'], array('all', 'day', 'month', 'year')) ? 'all' : trim($_GET['time']);
         $page = isset($_GET['page']) ? intval($_GET['page']) : 1;
         $pagesize = 10;
         $q = safe_replace(trim($_GET['q']));
         $q = htmlspecialchars(strip_tags($q));
         $q = str_replace('%', '', $q);
         //过滤'%',用户全文搜索
         $search_q = $q;
         //搜索原内容
         //按时间搜索
         if ($time == 'day') {
             $search_time = SYS_TIME - 86400;
             $sql_time = ' AND adddate > ' . $search_time;
         } elseif ($time == 'week') {
             $search_time = SYS_TIME - 604800;
             $sql_time = ' AND adddate > ' . $search_time;
         } elseif ($time == 'month') {
             $search_time = SYS_TIME - 2592000;
             $sql_time = ' AND adddate > ' . $search_time;
         } elseif ($time == 'year') {
             $search_time = SYS_TIME - 31536000;
             $sql_time = ' AND adddate > ' . $search_time;
         } else {
             $search_time = 0;
             $sql_time = '';
         }
         if ($page == 1 && !$setting['sphinxenable']) {
             //精确搜索
             $commend = $this->db->get_one("`typeid` = '{$typeid}' {$sql_time} AND `data` like '%{$q}%'");
         } else {
             $commend = '';
         }
         //如果开启sphinx
         if ($setting['sphinxenable']) {
             $sphinx = pc_base::load_app_class('search_interface', '', 0);
             $sphinx = new search_interface();
             $offset = $pagesize * ($page - 1);
             $res = $sphinx->search($q, array($siteid), array($typeid), array($search_time, SYS_TIME), $offset, $pagesize, '@weight desc');
             $totalnums = $res['total'];
             //如果结果不为空
             if (!empty($res['matches'])) {
                 $result = $res['matches'];
             }
         } else {
             pc_base::load_sys_class('segment', '', 0);
             $segment = new segment();
             //分词结果
             $segment_q = $segment->get_keyword($segment->split_result($q));
             //如果分词结果为空
             if (!empty($segment_q)) {
                 $sql = "`siteid`= '{$siteid}' AND `typeid` = '{$typeid}' {$sql_time} AND MATCH (`data`) AGAINST ('{$segment_q}' IN BOOLEAN MODE)";
             } else {
                 $sql = "`siteid`= '{$siteid}' AND `typeid` = '{$typeid}' {$sql_time} AND `data` like '%{$q}%'";
             }
             $result = $this->db->listinfo($sql, 'searchid DESC', $page, 10);
         }
         //如果开启相关搜索功能
         if ($setting['relationenble']) {
             //如果关键词长度在8-16之间,保存关键词作为relation search
             $this->keyword_db = pc_base::load_model('search_keyword_model');
             if (strlen($q) < 17 && strlen($q) > 5 && !empty($segment_q)) {
                 $res = $this->keyword_db->get_one(array('keyword' => $q));
                 if ($res) {
                     //关键词搜索数+1
                     //$this->keyword_db->update(array('searchnums'=>'+=1'), array('keyword'=>$q));
                 } else {
                     //关键词转换为拼音
                     pc_base::load_sys_func('iconv');
                     $pinyin = gbk_to_pinyin($q);
                     if (is_array($pinyin)) {
                         $pinyin = implode('', $pinyin);
                     }
                     $this->keyword_db->insert(array('keyword' => $q, 'searchnums' => 1, 'data' => $segment_q, 'pinyin' => $pinyin));
                 }
             }
             //相关搜索
             if (!empty($segment_q)) {
                 $relation_q = str_replace(' ', '%', $segment_q);
             } else {
                 $relation_q = $q;
             }
             $relation = $this->keyword_db->select("MATCH (`data`) AGAINST ('%{$relation_q}%' IN BOOLEAN MODE)", '*', 10, 'searchnums DESC');
         }
         //如果结果不为空
         if (!empty($result) || !empty($commend['id'])) {
             //开启sphinx后文章id取法不同
             if ($setting['sphinxenable']) {
                 foreach ($result as $_v) {
                     $sids[] = $_v['attrs']['id'];
                 }
             } else {
                 foreach ($result as $_v) {
                     $sids[] = $_v['id'];
                 }
             }
             if (!empty($commend['id'])) {
                 $sids[] = $commend['id'];
             }
             $sids = array_unique($sids);
             $where = to_sqls($sids, '', 'id');
             //获取模型id
             $model_type_cache = getcache('type_model_' . $siteid, 'search');
             $model_type_cache = array_flip($model_type_cache);
             $modelid = $model_type_cache[$typeid];
             //是否读取其他模块接口
             if ($modelid) {
                 $this->content_db->set_model($modelid);
                 /**
                  * 如果表名为空,则为黄页模型
                  */
                 if (empty($this->content_db->model_tablename)) {
                     $this->content_db = pc_base::load_model('yp_content_model');
                     $this->content_db->set_model($modelid);
                 }
                 if ($setting['sphinxenable']) {
                     $data = $this->content_db->listinfo($where, 'id DESC', 1, $pagesize);
                     $pages = pages($totalnums, $page, $pagesize);
                 } else {
                     $data = $this->content_db->select($where, '*');
                     $pages = $this->db->pages;
                     $totalnums = $this->db->number;
                 }
                 //如果分词结果为空
                 if (!empty($segment_q)) {
                     $replace = explode(' ', $segment_q);
                     foreach ($replace as $replace_arr_v) {
                         $replace_arr[] = '<font color=red>' . $replace_arr_v . '</font>';
                     }
                     foreach ($data as $_k => $_v) {
                         $data[$_k]['title'] = str_replace($replace, $replace_arr, $_v['title']);
                         $data[$_k]['description'] = str_replace($replace, $replace_arr, $_v['description']);
                     }
                 } else {
                     foreach ($data as $_k => $_v) {
                         $data[$_k]['title'] = str_replace($q, '<font color=red>' . $q . '</font>', $_v['title']);
                         $data[$_k]['description'] = str_replace($q, '<font color=red>' . $q . '</font>', $_v['description']);
                     }
                 }
             } else {
                 //读取专辑搜索接口
                 $special_api = pc_base::load_app_class('search_api', 'special');
                 $data = $special_api->get_search_data($sids);
                 $totalnums = count($data);
             }
         }
         $execute_time = execute_time();
         $pages = isset($pages) ? $pages : '';
         $totalnums = isset($totalnums) ? $totalnums : 0;
         $data = isset($data) ? $data : '';
         include template('search', 'list');
     } else {
         include template('search', 'index');
     }
 }
Beispiel #3
0
 /**
  * 关键词搜索
  */
 public function init()
 {
     //获取siteid
     $siteid = isset($_REQUEST['siteid']) && trim($_REQUEST['siteid']) ? intval($_REQUEST['siteid']) : 1;
     $SEO = seo($siteid);
     //搜索配置
     $search_setting = getcache('search');
     $setting = $search_setting[$siteid];
     $search_model = getcache('search_model_' . $siteid);
     $type_module = getcache('type_module_' . $siteid);
     if (isset($_POST['q'])) {
         if (trim($_POST['q']) == '') {
             header('Location: ' . APP_PATH . 'index.php?m=search');
             exit;
         }
         $typeid = empty($_POST['typeid']) ? 0 : intval($_POST['typeid']);
         $time = empty($_POST['time']) || !in_array($_POST['time'], array('all', 'day', 'month', 'year', 'week')) ? 'all' : trim($_POST['time']);
         $page = isset($_POST['page']) ? intval($_POST['page']) : 1;
         $pagesize = 10;
         $q = safe_replace(trim($_POST['q']));
         $q = new_html_special_chars(strip_tags($q));
         $q = str_replace('%', '', $q);
         //过滤'%',用户全文搜索
         $search_q = $q;
         //搜索原内容
         $sql_time = $sql_tid = '';
         if ($typeid) {
             $sql_tid = ' AND typeid = ' . $typeid;
         }
         //按时间搜索
         if ($time == 'day') {
             $search_time = SYS_TIME - 86400;
             $sql_time = ' AND adddate > ' . $search_time;
         } elseif ($time == 'week') {
             $search_time = SYS_TIME - 604800;
             $sql_time = ' AND adddate > ' . $search_time;
         } elseif ($time == 'month') {
             $search_time = SYS_TIME - 2592000;
             $sql_time = ' AND adddate > ' . $search_time;
         } elseif ($time == 'year') {
             $search_time = SYS_TIME - 31536000;
             $sql_time = ' AND adddate > ' . $search_time;
         } else {
             $search_time = 0;
             $sql_time = '';
         }
         if ($page == 1 && !$setting['sphinxenable']) {
             //精确搜索
             $commend = $this->db->get_one("`siteid`= '{$siteid}' {$sql_tid} {$sql_time} AND `data` like '%{$q}%'");
         } else {
             $commend = '';
         }
         //如果开启sphinx
         if ($setting['sphinxenable']) {
             $sphinx = pc_base::load_app_class('search_interface', '', 0);
             $sphinx = new search_interface();
             $offset = $pagesize * ($page - 1);
             $res = $sphinx->search($q, array($siteid), array($typeid), array($search_time, SYS_TIME), $offset, $pagesize, '@weight desc');
             $totalnums = $res['total'];
             //如果结果不为空
             if (!empty($res['matches'])) {
                 $result = $res['matches'];
             }
         } else {
             $sql = "`siteid`= '{$siteid}' {$sql_tid} {$sql_time} AND `data` like '%{$q}%'";
             $result = $this->db->listinfo($sql, 'searchid DESC', $page, 10);
             //$result = $this->db->listinfo($sql, 'adddate ASC', $page, 10);
         }
         //如果结果不为空
         if (!empty($result) || !empty($commend['id'])) {
             foreach ($result as $_v) {
                 if ($_v['typeid']) {
                     $sids[$_v['typeid']][] = $_v['id'];
                 }
             }
             if (!empty($commend['id'])) {
                 if ($commend['typeid']) {
                     $sids[$commend['typeid']][] = $commend['id'];
                 }
             }
             $model_type_cache = getcache('type_model_' . $siteid, 'search');
             $model_type_cache = array_flip($model_type_cache);
             $data = array();
             foreach ($sids as $_k => $_val) {
                 $tid = $_k;
                 $ids = array_unique($_val);
                 $where = to_sqls($ids, '', 'id');
                 //获取模型id
                 $modelid = $model_type_cache[$tid];
                 //是否读取其他模块接口
                 if ($modelid) {
                     $this->content_db->set_model($modelid);
                     /**
                      * 如果表名为空,则为黄页模型
                      */
                     if (empty($this->content_db->model_tablename)) {
                         $this->content_db = pc_base::load_model('yp_content_model');
                         $this->content_db->set_model($modelid);
                     }
                     $datas = $this->content_db->select($where, '*', '', 'inputtime DESC');
                 }
                 $data = array_merge($data, $datas);
             }
             $pages = $this->db->pages;
             $totalnums = $this->db->number;
             //如果分词结果为空
             if (!empty($segment_q)) {
                 $replace = explode(' ', $segment_q);
                 foreach ($replace as $replace_arr_v) {
                     $replace_arr[] = '' . $replace_arr_v . '';
                 }
                 foreach ($data as $_k => $_v) {
                     $data[$_k]['title'] = str_replace($replace, $replace_arr, $_v['title']);
                     $data[$_k]['description'] = str_replace($replace, $replace_arr, $_v['description']);
                 }
             } else {
                 foreach ($data as $_k => $_v) {
                     $data[$_k]['title'] = str_replace($q, '' . $q . '', $_v['title']);
                     $data[$_k]['description'] = str_replace($q, '' . $q . '', $_v['description']);
                 }
             }
         }
         $execute_time = execute_time();
         $pages = isset($pages) ? $pages : '';
         $totalnums = isset($totalnums) ? $totalnums : 0;
         $data = isset($data) ? $data : '';
         include template('search', 'list');
     } else {
         include template('search', 'index');
     }
 }