discuzx3.1文章进行全文检索的实现方法

时间:2023-09-18 03:42:55 作者:csyr0010 综合材料 收藏本文 下载本文

【导语】“csyr0010”通过精心收集,向本站投稿了7篇discuzx3.1文章进行全文检索的实现方法,下面是小编帮大家整理后的discuzx3.1文章进行全文检索的实现方法,希望对大家带来帮助,欢迎大家分享。

篇1:discuzx3.1文章进行全文检索的实现方法

这篇文章主要介绍了discuzx3.1文章进行全文检索的实现方法,需要的朋友可以参考下

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

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

网站目录\\source\\class\\table\\table_portal_article_content.php

2.在下面的

代码如下:

class table_portal_article_content extends discuz_table

{

后添加

代码如下:

public function fetch_all_by_sql($where, $order = ‘‘, $start = 0, $limit = 0, $count = 0, $alias = ‘‘) {

$where = $where && !is_array($where) ? “ WHERE $where” : ‘‘;

if(is_array($order)) {

$order = ‘‘;

}

if($count) {

return DB::result_first(‘SELECT count(*) FROM ‘.DB::table($this->_table).‘ %i %i %i ‘.DB::limit($start, $limit), array($alias, $where, $order));

}

return DB::fetch_all(‘SELECT * FROM ‘.DB::table($this->_table).‘ %i %i %i ‘.DB::limit($start, $limit), array($alias, $where, $order));

}

变为:

代码如下:

class table_portal_article_content extends discuz_table

{

public function fetch_all_by_sql($where, $order = ‘‘, $start = 0, $limit = 0, $count = 0, $alias = ‘‘) {

$where = $where && !is_array($where) ? “ WHERE $where” : ‘‘;

if(is_array($order)) {

$order = ‘‘;

}

if($count) {

return DB::result_first(‘SELECT count(*) FROM ‘.DB::table($this->_table).‘ %i %i %i ‘.DB::limit($start, $limit), array($alias, $where, $order));

}

return DB::fetch_all(‘SELECT * FROM ‘.DB::table($this->_table).‘ %i %i %i ‘.DB::limit($start, $limit), array($alias, $where, $order));

}

上面添加那个方法才能用$query = C::t(‘portal_article_content’)->fetch_all_by_sql,

3.打开

网站目录\\source\\module\\search\\search_portal.php

搜索

代码如下:

foreach($query as $article) {

$ids .= ‘,‘.$article[‘aid‘];

$num++;

}

在其后添加如下代码:

代码如下:

if($num==0){

list($srchtxt, $srchtxtsql) = searchkey($keyword, “content LIKE ‘%{text}%‘”, true);

$query = C::t(‘portal_article_content‘)->fetch_all_by_sql(‘ 1 ‘.$srchtxtsql, ‘ORDER BY aid DESC ‘, 0, $_G[‘setting‘][‘search‘][‘portal‘][‘maxsearchresults‘]);

foreach($query as $article) {

$ids .= ‘,‘.$article[‘aid‘];

$num++;

}

}

上面代码的意思是,如果搜标题没搜到,那就用like来搜文章的内容。

保存后,更新下discuz的缓存,搜文章里的内容试试,如果能搜到,OK,大功告成~

篇2:WordPress实现相关文章功能代码方法

WordPress有很多实现相关文章功能的插件,插件的优点是配置简单,但是可能会对网站的速度造成一些小的影响,所以很多人还是比较喜欢用代码实现需要的功能,但是话又说回来了,代码实现也有缺点,就是配置复杂,不懂代码的人完全摸不着头脑或者只能照搬别人的代码,还不如用插件,

* 文章标题1

* 文章标题2

......

方法一:标签相关

首先获取文章的所有标签,接着获取这些标签下的 n 篇文章,那么这 n 篇文章就是与该文章相关的文章了。现在可以见到的WordPress相关文章插件都是使用的这个方法。下面是实现的代码:

$post_tags = wp_get_post_tags($post->ID);

if ($post_tags) {

foreach ($post_tags as $tag)

{

// 获取标签列表

$tag_list[] .= $tag->term_id;

}

// 随机获取标签列表中的一个标签

$post_tag = $tag_list[ mt_rand(0, count($tag_list) - 1) ];

// 该方法使用 query_posts 函数来调用相关文章,以下是参数列表

$args = array(

'tag__in' =>array($post_tag),

'category__not_in' =>array(NULL),      // 不包括的分类ID

'post__not_in' =>array($post->ID),

'showposts' =>6,               // 显示相关文章数量

'caller_get_posts' =>1

);

query_posts($args);

if (have_posts()) :

while (have_posts()) : the_post(); update_post_caches($posts); ?>

* “ rel=”bookmark“ title=”

* 暂无相关文章

使用说明:”不包括的分类ID” 指的是相关文章不显示该分类下的文章,将同行的 NULL 改成文章分类的ID即可,多个ID就用半角逗号隔开。因为这里限制只显示6篇相关文章,所以不管给 query_posts() 的参数 tag__in 赋多少个值,都是只显示一个标签下的 6 篇文章,除非第一个标签有1篇,第二个标签有2篇,第三个有3篇。。。。。。所以如果这篇文章有多个标签,那么我们采取的做法是随机获取一个标签的id,赋值给 tag__in 这个参数,获取该标签下的6篇文章。

方法二:分类相关

本方法是通过获取该文章的分类id,然后获取该分类下的文章,来达到获取相关文章的目的。

$cats = wp_get_post_categories($post->ID);

if ($cats) {

$cat = get_category( $cats[0] );

$first_cat = $cat->cat_ID;

$args = array(

'category__in' =>array($first_cat),

'post__not_in' =>array($post->ID),

'showposts' =>6,

'caller_get_posts' =>1);

query_posts($args);

if (have_posts()) :

while (have_posts()) : the_post(); update_post_caches($posts); ?>

* “ rel=”bookmark“ title=”

?>“>

* 暂无相关文章

方法三:标签相关,SQL获取

获取相关文章的原理与方法一相似,不过在获取文章的时候是以SQL语句来直接读取数据库,从而随机获取6篇相关文章记录,而不是WordPress的函数query_posts().

$post_tags = wp_get_post_tags($post->ID);

if ($post_tags) {

foreach ($post_tags as $tag)

{

// 获取标签列表

$tag_list[] .= $tag->term_id;

}

// 随机获取标签列表中的一个标签

$post_tag = $tag_list[ mt_rand(0, count($tag_list) - 1) ];

$related = $wpdb->get_results(”

SELECT {$wpdb->prefix}posts.post_title, {$wpdb->prefix}posts.guid

FROM {$wpdb->prefix}posts, {$wpdb->prefix}term_relationships, {$wpdb->prefix}term_taxonomy

WHERE {$wpdb->prefix}posts.ID = {$wpdb->prefix}term_relationships.object_id

AND {$wpdb->prefix}term_taxonomy.taxonomy = 'post_tag'

AND {$wpdb->prefix}term_taxonomy.term_taxonomy_id = {$wpdb->prefix}

term_relationships.term_taxonomy_id

AND {$wpdb->prefix}posts.post_status = 'publish'

AND {$wpdb->prefix}posts.post_type = 'post'

AND {$wpdb->prefix}term_taxonomy.term_id = '“ . $post_tag . ”'

AND {$wpdb->prefix}posts.ID != '“ . $post->ID . ”'

ORDER BY RAND( )

LIMIT 6“);

// 以上代码中的 6 为限制只获取6篇相关文章

// 通过修改数字 6,可修改你想要的文章数量

if ( $related ) {

foreach ($related as $related_post) {

?>

* guid; ?>” rel=“bookmark”

title=“

* 暂无相关文章

方法四:分类相关,SQL获取

获取相关文章的原理与方法二相似,不过在获取文章的时候是以SQL语句来直接读取数据库,从而随机获取6篇相关文章记录,而不是WordPress的函数query_posts().

$cats = wp_get_post_categories($post->ID);

if ($cats) {

$cat = get_category( $cats[0] );

$first_cat = $cat->cat_ID;

$related = $wpdb->get_results(”

SELECT wp_posts.post_title, wp_posts.guid

FROM wp_posts, wp_term_relationships, wp_term_taxonomy

WHERE wp_posts.ID = wp_term_relationships.object_id

AND {$wpdb->prefix}term_taxonomy.taxonomy = 'category'

AND {$wpdb->prefix}term_taxonomy.term_taxonomy_id = {$wpdb->prefix}term_relationships.term_taxonomy_id

AND {$wpdb->prefix}posts.post_status = 'publish'

AND {$wpdb->prefix}posts.post_type = 'post'

AND {$wpdb->prefix}term_taxonomy.term_id = '“ . $first_cat . ”'

AND {$wpdb->prefix}posts.ID != '“ . $post->ID . ”'

ORDER BY RAND( )

LIMIT 6“);

if ( $related ) {

foreach ($related as $related_post) {

?>

* guid; ?>” rel=“bookmark”

title=“

* 暂无相关文章

方法五:作者相关

该方法是获取该文章作者的其他文章来充当相关文章,代码如下:

$post_author = get_the_author_meta( 'user_login' );

$args = array(

'author_name' =>$post_author,

'post__not_in' =>array($post->ID),

'showposts' =>6,               // 显示相关文章数量

'orderby' =>date,          // 按时间排序

'caller_get_posts' =>1

);

query_posts($args);

if (have_posts()) :

while (have_posts()) : the_post(); update_post_caches($posts); ?>

* ” rel=“bookmark”

title=“

* 暂无相关文章

时间效率对比

我们将用之前的一个php代码对以上各个相关文章代码执行时间进行测算,以便对以上各个的方法进行效率,给你的选择提供参考,

以下是在同一篇文章中获取6篇相关文章,以上各方法最终测算的时间如下:

方法一:0.18067908287048 秒

方法二:0.057158946990967 秒

方法三:0.037126064300537 秒

方法四:0.045628070831299 秒

方法五:0.023991823196411 秒

篇3:WordPress代码实现相关文章的几种方法

WordPress有很多实现相关文章功能的插件,插件的优点是配置简 单,但是可能会对网站的速度造成一些小的影响,所以很多人还是比较喜欢用代码实现需要的功能,但是话又说回来了,代码实现也有缺点,就是配置复杂,不懂代 码的人完全摸不着头脑或者只能照搬别人的代码,还不如用插件,

* 文章标题1

* 文章标题2

......

方法一:标签相关

首先获取文章的所有标签,接着获取这些标签下的 n 篇文章,那么这 n 篇文章就是与该文章相关的文章了。现在可以见到的WordPress相关文章插件都是使用的这个方法。下面是实现的代码:

$post_tags = wp_get_post_tags($post->ID);

if ($post_tags) {

foreach ($post_tags as $tag)

{

// 获取标签列表

$tag_list[] .= $tag->term_id;

}

// 随机获取标签列表中的一个标签

$post_tag = $tag_list[ mt_rand(0, count($tag_list) - 1) ];

// 该方法使用 query_posts() 函数来调用相关文章,以下是参数列表

$args = array(

'tag__in' =>array($post_tag),

'category__not_in' =>array(NULL),      // 不包括的分类ID

'post__not_in' =>array($post->ID),

'showposts' =>6,               // 显示相关文章数量

'caller_get_posts' =>1

);

query_posts($args);

if (have_posts()) :

while (have_posts()) : the_post(); update_post_caches($posts); ?>

* ” rel=“bookmark” title=“

* 暂无相关文章

使用说明:”不包括的分类ID” 指的是相关文章不显示该分类下的文章,将同行的 NULL 改成文章分类的ID即可,多个ID就用半角逗号隔开,

因为这里限制只显示6篇相关文章,所以不管给 query_posts() 的参数 tag__in 赋多少个值,都是只显示一个标签下的 6 篇文章,除非第一个标签有1篇,第二个标签有2篇,第三个有3篇。。。。。。所以如果这篇文章有多个标签,那么我们采取的做法是随机获取一个标签的id, 赋值给 tag__in 这个参数,获取该标签下的6篇文章。

方法二:分类相关

本方法是通过获取该文章的分类id,然后获取该分类下的文章,来达到获取相关文章的目的。

$cats = wp_get_post_categories($post->ID);

if ($cats) {

$cat = get_category( $cats[0] );

$first_cat = $cat->cat_ID;

$args = array(

'category__in' =>array($first_cat),

'post__not_in' =>array($post->ID),

'showposts' =>6,

'caller_get_posts' =>1);

query_posts($args);

if (have_posts()) :

while (have_posts()) : the_post(); update_post_caches($posts); ?>

* ” rel=“bookmark” title=“

?>”>

* 暂无相关文章

篇4:dedecms5.7文章二次开发实现阅读全文功能的方法

这篇文章主要为大家介绍了dedecms5.7文章二次开发实现阅读全文功能的方法,涉及文章模型相关类文件功能的修改与完善,是一个比较典型的二次开发技巧,需要的朋友可以参考下

阅读全文功能其实在很多的流行站点都有的,比如网易,新浪等,随着文章内容的增加,当一个页面有多个分页的时候,就会显示出这个“在本页阅读全文”的链接,点击这个链接之后出现的,将是这篇文章以没有分页出现的型式,那么在dedecms5.7如何在文章内容页添加阅读全文功能呢?

这个阅读全文有什么用呢?说白了,也就是提高用户体验,下面让我们看看,怎么简单现实这个功能.

修改文件:include/arc.archives.class.php

注意:做任何修改前都要备份好原文件.

第一步:打开include/arc.archives.class.php

文件查找://issystem==-1

往下 大概 145行 找到:

代码如下:

$this->Fields[‘userip‘] = $this->addTableRow[‘userip‘];

在下面一行添加:

代码如下:

$this->Fields[‘body2‘] = $this->addTableRow[‘body‘];

第二步查找:

代码如下:

$this->dsql->ExecuteNoneQuery(“Update `dede_archives` SET ismake=1 WHERE id=‘”.$this->ArcID.“‘”);

在上一行添加以下代码:

代码如下:

//阅读全文开始

if($this->TotalPage >1) {

//用正则匹配把分页符去掉

$this->Fields[‘body2‘] = preg_replace(‘/# p#副标题# e#/U‘, ‘‘,$this->Fields[‘body2‘]);

$this->SplitFields = explode(“#p2222#”,$this->Fields[‘body2‘]);

$this->Fields[‘tmptitle‘] = (emptyempty($this->Fields[‘tmptitle‘]) ? $this->Fields[‘title‘] : $this->Fields[‘tmptitle‘]);

$this->Fields[‘title‘] = $this->Fields[‘tmptitle‘];

$this->TotalPage = count($this->SplitFields);

$this->Fields[‘totalpage‘] = $this->TotalPage;

$TRUEfilenameall = $this->GetTruePath.$fileFirst.“_all.”.$this->ShortName;

$this->ParseDMFields(1,0);

$this->dtp->SaveTo($TRUEfilenameall);

if($cfg_remote_site==‘Y‘ && $isremote == 1)

{

//分析远程文件路径

$remotefile = str_replace(DEDEROOT, ‘‘, $TRUEfilename);

$localfile = ‘..‘.$remotefile;

//创建远程文件夹

$remotedir = preg_replace(“#[^\\/]*\\.html#”, ‘‘, $remotefile);

$this->ftp->rmkdir($remotedir);

$this->ftp->upload($localfile, $remotefile, ‘ascii‘);

}

}

//阅读全文结束

第三步:查找 获得静态页面分页列表,代码如下:

代码如下:

/**

* 获得静态页面分页列表

*

* @access public

* @param int $totalPage 总页数

* @param int $nowPage 当前页数

* @param int $aid 文档id

* @return string

*/

function GetPagebreak($totalPage, $nowPage, $aid)

{

if($totalPage==1)

{

return “”;

}

//$PageList = “

共”.$totalPage.“页:

”;

$PageList = “”;

$nPage = $nowPage-1;

$lPage = $nowPage+1;

if($nowPage==1)

{

$PageList.=“<”;

}

else

{

if($nPage==1)

{

$PageList.=“NameFirst.”.“.$this->ShortName.”‘ target=‘_self‘><“;

}

else

{

$PageList.=”NameFirst.“_”.$nPage.“.”.$this->ShortName.“‘ target=‘_self‘><”;

}

}

for($i=1;$i<=$totalPage;$i++)

{

if($i==1)

{

if($nowPage!=1)

{

$PageList.=“NameFirst.”.“.$this->ShortName.”‘ target=‘_self‘>1“;

}

else

{

$PageList.=”1“;

}

}

else

{

$n = $i;

if($nowPage!=$i)

{

$PageList.=”NameFirst.“_”.$i.“.”.$this->ShortName.“‘ target=‘_self‘>”.$n.“”;

}

else

{

$PageList.=“{$n}”;

}

}

}

if($lPage <= $totalPage)

{

$PageList.=“NameFirst.”_“.$lPage.”.“.$this->ShortName.”‘ target=‘_self‘>>“;

}

else

{

$PageList.= ”>“;

}

$PageList.= ”NameFirst.“_all.”.$this->ShortName.“‘>阅读全文”;

return $PageList;

}

也就是在return $PageList 上一行添加了一行代码:

代码如下:

$PageList.= “NameFirst.”_all.“.$this->ShortName.”‘>阅读全文“;

修改完成后,保存文件,更新一下页面就可以看到效果了.

希望本文所述对大家的dedecms建站有所帮助,

篇5:WordPress自动给文章添加nofollow属性的实现方法

这篇文章主要为大家介绍了WordPress自动给文章添加nofollow属性的实现方法,可通过Nofollow for external link 插件实现文章页自动添加nofollow属性的功能,是非常实用的技巧,需要的朋友可以参考下

nofollow属性是告诉搜索引擎不传权重过去,但WordPressk中如果我们要nofollow属性就需要手工加了,现在我来告诉大家利用 Nofollow for external link就可以自动给文章添加nofollow属性了.

直接安装启用 Nofollow for external link 插件,或者将下面的代码添加到当前主题的 functions.php 文件即可.

实例代码如下:

代码如下:

add_filter( ‘the_content‘, ‘cn_nf_url_parse‘);

function cn_nf_url_parse( $content ) {

$regexp = ”]*href=(“??)([^” >]*?)\\1[^>]*>“;

if(preg_match_all(”/$regexp/siU“, $content, $matches, PREG_SET_ORDER)) {

if( !emptyempty($matches) ) {

$srcUrl = get_option(‘siteurl‘);

for ($i=0; $i < count($matches); $i++)

{

$tag = $matches[$i][0];

$tag2 = $matches[$i][0];

$url = $matches[$i][0];

$noFollow = ‘‘;

$pattern = ‘/targets*=s*”s*_blanks*“/‘;

preg_match($pattern, $tag2, $match, PREG_OFFSET_CAPTURE);

if( count($match) < 1 )

$noFollow .= ‘ target=”_blank“ ‘;

$pattern = ‘/rels*=s*”s*[n|d]ofollows*“/‘;

preg_match($pattern, $tag2, $match, PREG_OFFSET_CAPTURE);

if( count($match) < 1 )

$noFollow .= ‘ rel=”nofollow“ ‘;

$pos = strpos($url,$srcUrl);

if ($pos === false) {

$tag = rtrim ($tag,‘>‘);

$tag .= $noFollow.‘>‘;

$content = str_replace($tag2,$tag,$content);

}

}

}

}

$content = str_replace(‘]]>‘, ‘]]>‘, $content);

return $content;

}

最终效果:自动给文章/页面的站外链接添加nofollow属性(rel=”nofollow”),并且在新窗口打开这些链接(即添加 target=”_blank”属性),如果已经手动给链接添加了 rel=”dofollow”,就不会添加 rel=”nofollow”,如果手动添加了 target=”_blank”,就不会重复添加.

为指定分类的所有链接添加nofollow属性,那你可以将下面的代码添加到主题的 functions.php 文件即可:

代码如下:

function nofollow_cat_posts($text) {

global $post;

if( in_category(1) ) { // 修改这里的分类ID

$text = stripslashes(wp_rel_nofollow($text));

}

return $text;

}

add_filter(‘the_content‘, ‘nofollow_cat_posts‘);

希望本文所述对大家的WordPress建站有所帮助,

篇6:WordPress实现搜索结果包括自定义文章类型内容的方法

这篇文章主要为大家介绍了WordPress实现搜索结果包括自定义文章类型内容的方法,通过自定义函数的设置来实现WordPress搜索结果包括自定义文章类型,是非常实用的技巧,需要的朋友可以参考下

我们知道如果你在WordPress站点添加了自定义文章类型,这样在搜索结果中是看不到自定义文章类型的内容的,但要让搜索结果中看到自定义文章类型的内容也不难.

如果你的WordPress站点添加了自定义文章类型,请记得让WordPress默认搜索支持自定义文章类型,即可以搜索自定义文章类型的内容,实现的方法很简单,将下面的代码添加到主题的 functions.php 文件中即可.

让搜索支持自定义文章类型代码如下:

代码如下:

function searchAll( $query ) {

if ( $query->is_search ) { $query->set( ‘post_type‘, array( ‘post‘,‘books‘, ‘product‘,‘works‘ )); }

return $query;

}

add_filter( ‘the_search_query‘, ‘searchAll‘ );

注意根据自己的实际修改第 3 行数组(array)中的文章类型别名.

希望本文所述对大家的WordPress建站有所帮助,

篇7:WordPress实现搜索结果只有文章时自动跳转到文章的方法

这篇文章主要为大家介绍了WordPress实现搜索结果只有一篇文章时自动跳转到文章的方法,通过一个简单的自定义函数实现搜索结果只有一篇时跳转到文章的技巧,可提高用户体验,非常具有实用价值,需要的朋友可以参考下

当读者通过WordPress自身的搜索功能来搜索文章时,如果返回的结果只有一篇文章,我们可以直接让它跳转到这篇文章,提高用户体验,实现的方法很简单,只需要在你主题的 functions.php 文件中添加下面的代码:

代码如下:

add_action(‘template_redirect‘, ‘redirect_single_post‘);

function redirect_single_post() {

if (is_search()) {

global $wp_query;

if ($wp_query->post_count == 1 && $wp_query->max_num_pages == 1) {

wp_redirect( get_permalink( $wp_query->posts[‘0‘]->ID ) );

exit;

}

}

}

希望本文所述对大家的WordPress建站有所帮助,

检索报告

实现高效学习的方法

信息检索心得体会

信息检索总结范文

信息检索心得

php实现cookie加密的方法

励志文章:梦想要用努力去实现

初中生励志文章,实现梦想的行动

打扮文章的几大方法

写综述文章的方法

discuzx3.1文章进行全文检索的实现方法(锦集7篇)

欢迎下载DOC格式的discuzx3.1文章进行全文检索的实现方法,但愿能给您带来参考作用!
推荐度: 推荐 推荐 推荐 推荐 推荐
点击下载文档 文档为doc格式
点击下载本文文档