PHP使用Sphinx API对Mysql数据进行全文检索
发布日期:2022-02-19 23:50:34 浏览次数:38 分类:技术文章

本文共 1213 字,大约阅读时间需要 4 分钟。

//使用sphinx进行全文检索function search_by_sphinx($keywords,$offset,$limit) {	include_once app_path() . '/mylibs/sphinxapi.php';	$client = new \SphinxClient();	$host = "127.0.0.1";	$port = 9312;	$client->SetServer ( $host, $port );	$client->SetConnectTimeout ( 1 );	$client->SetArrayResult ( true );	$client->SetLimits($offset,$limit);	//$client->SetSortMode(SPH_SORT_ATTR_DESC,'view_count');		$index_name = "index_demo";		$keywords_clean = $keywords;	$words_list_new = array();	// | 表示或的意思	$words_list = explode('|',$keywords);	foreach ($words_list as $value) {		if (mb_trim($value) == '') continue;		if (mb_strlen($value) > 2) {			//超过2个字符,使用模糊匹配			$words_list_new[] = '(' . $value . ')';		} else {			//小于等于两个字符,使用必须包含符号;双引号表示必须包含;			$words_list_new[] = '("' . $value . '")';		}	}	//重新用空格分割	$keywords_clean = implode('|',$words_list_new);	$keywords = $keywords_clean;	//全文检索	$r = $client->Query($search_str,$index_name);	$total = $r['total'];	$total_found = $r['total_found'];	$matches = $r['matches'];		//组合一下结果	$list_arr = array();	$list_arr['total'] = $total;	$list_arr['total_found'] = $total_found;	$list_arr['matches'] = $matches;	return $list_arr;}

 

PHP使用Sphinx查询速度很快,结果也匹配的很不错,算是轻量级的解决方案。

目前,和都用的不错,接下来有很多地方都能用到。

转载地址:https://blog.csdn.net/lessoft/article/details/108782579 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:VUE动态生成word
下一篇:用Hibernate+Struts做东西,感觉真是舒服(有内容)

发表评论

最新留言

逛到本站,mark一下
[***.202.152.39]2024年04月04日 02时06分42秒