首页 > 运营 > 帮助中心 > 正文

discuz全文检索文章的实现方法

2020-01-27 17:20:11
字体:
来源:转载
供稿:网友

首先说明:这个检索是直接用like来实现的,所以,如果你的站数据量大,这样很吃系统,自己掂量着办。搜索门户中的文章,并不是按这个走的,要么只能分中文要么只能分英文(学艺不精没细了解啊,个人测试是这样的)。而我目前碰到的要求是需要对文章也执行like。所以,经过研究,类比了下搜索文章标题的功能,成功实现了discuzX3对门户中的文章进行全文检索的功能,以下操作方法discuz版本为20140101的X3.1。具体方法如下:

1)用notepad++或其他文本编辑器打开下述文件

网站目录sourceclasstabletable_portal_article_content.php

在下面的

  1. class table_portal_article_content extends discuz_table
  2. {

后添加

  1. public function fetch_all_by_sql($where, $order = '', $start = 0, $limit = 0, $count = 0, $alias = '') {
  2. $where = $where && !is_array($where) ? " WHERE $where" : '';
  3. if(is_array($order)) {
  4. $order = '';
  5. }
  6. if($count) {
  7. return DB::result_first('SELECT count(*) FROM '.DB::table($this->_table).' %i %i %i '.DB::limit($start, $limit), array($alias, $where, $order));
  8. }
  9. return DB::fetch_all('SELECT * FROM '.DB::table($this->_table).' %i %i %i '.DB::limit($start, $limit), array($alias, $where, $order));
  10. }

变为:

  1. class table_portal_article_content extends discuz_table
  2. {
  3. public function fetch_all_by_sql($where, $order = '', $start = 0, $limit = 0, $count = 0, $alias = '') {
  4. $where = $where && !is_array($where) ? " WHERE $where" : '';
  5. if(is_array($order)) {
  6. $order = '';
  7. }
  8. if($count) {
  9. 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。

2)打开

网站目录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,大功告成~

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表