discuzx文章进行全文检索的实现技巧
发布时间:2021-11-30 17:51:23 所属栏目:PHP教程 来源:互联网
导读:首先说明啊,这个检索是直接用like来弄的,所以,如果你的站数据量大,这样很吃系统,自己掂量着办,我研究了下利用sphinx的,结果搞定了才发现这个只是针对论坛的帖子。搜索门户中的文章,并不是按这个走的,而且利用sphinx这个啊,要么只能分中文要么只能
首先说明啊,这个检索是直接用like来弄的,所以,如果你的站数据量大,这样很吃系统,自己掂量着办,我研究了下利用sphinx的,结果搞定了才发现这个只是针对论坛的帖子。搜索门户中的文章,并不是按这个走的,而且利用sphinx这个啊,要么只能分中文要么只能分英文(学艺不精没细了解啊,个人测试是这样的)。而我目前碰到的要求是需要对文章也执行like。所以,经过研究,类比了下搜索文章标题的功能,成功实现了discuzX3对门户中的文章进行全文检索的功能,以下操作方法discuz版本为20140101的X3.1。具体方法如下: 1.用notepad++或其他文本编辑器打开下述文件 网站目录sourceclasstabletable_portal_article_content.php 2.在下面的 复制代码代码如下: class table_portal_article_content extends discuz_table { 后添加 复制代码代码如下: 01 public function fetch_all_by_sql($where, $order = '', $start = 0, $limit = 0, $count = 0, $alias = '') { 02 $where = $where && !is_array($where) ? " WHERE $where" : ''; 03 if(is_array($order)) { 04 $order = ''; 05 } 06 if($count) { 07 return DB::result_first('SELECT count(*) FROM '.DB::table($this->_table).' %i %i %i '.DB::limit($start, $limit), array($alias, $where, $order)); 08 } 09 return DB::fetch_all('SELECT * FROM '.DB::table($this->_table).' %i %i %i '.DB::limit($start, $limit), array($alias, $where, $order)); 10 } 变为: 复制代码代码如下: 01 class table_portal_article_content extends discuz_table 02 { 03 public function fetch_all_by_sql($where, $order = '', $start = 0, $limit = 0, $count = 0, $alias = '') { 04 $where = $where && !is_array($where) ? " WHERE $where" : ''; 05 if(is_array($order)) { 06 $order = ''; 07 } 08 if($count) { 09 return DB::result_first('SELECT count(*) FROM '.DB::table($this->_table).' %i %i %i '.DB::limit($start, $limit), array($alias, $where, $order)); 10 } 11 return DB::fetch_all('SELECT * FROM '.DB::table($this->_table).' %i %i %i '.DB::limit($start, $limit), array($alias, $where, $order)); 12 } 上面添加那个方法才能用$query = C::t(‘portal_article_content’)->fetch_all_by_sql。 3.打开 网站目录sourcemodulesearchsearch_portal.php 搜索 复制代码代码如下: 1 </p> <p> foreach($query as $article) { 2 $ids .= ','.$article['aid']; 3 $num++; 4 } 在其后添加如下代码: 复制代码代码如下: 1 if($num==0){ 2 list($srchtxt, $srchtxtsql) = searchkey($keyword, "content LIKE '%{text}%'", true); 3 $query = C::t('portal_article_content')->fetch_all_by_sql(' 1 '.$srchtxtsql, 'ORDER BY aid DESC ', 0, $_G['setting']['search']['portal']['maxsearchresults']); 4 foreach($query as $article) { 5 $ids .= ','.$article['aid']; 6 $num++; 7 } 8 } 上面代码的意思是,如果搜标题没搜到,那就用like来搜文章的内容。 保存后,更新下discuz的缓存,搜文章里的内容试试,如果能搜到,OK,大功告成~ ![]() (编辑:云计算网_泰州站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |