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 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
逛到本站,mark一下
[***.202.152.39]2024年04月04日 02时06分42秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
Spark入门(二)多主standalone安装
2019-04-28
Spark入门(三)Idea构建spark项目
2019-04-28
Spark入门(四)Idea远程提交项目到spark集群
2019-04-28
Spark入门(五)Spark SQL shell启动方式(元数据存储在derby)
2019-04-28
Spark入门(六)Spark SQL shell启动方式(元数据存储在mysql)
2019-04-28
Spark入门(七)Spark SQL thriftserver/beeline启动方式
2019-04-28
MyBatis Generator分页插件RowBoundsPlugin坑
2019-04-28
MyBatis中的RowBounds
2019-04-28
Maven的pom.xml文件详解------Build Settings
2019-04-28
Maven的pom.xml文件详解------Environment Settings
2019-04-28
Maven的pom.xml文件详解------The Basics
2019-04-28
Lombok的@Data生成的hashCode和equals方法坑
2019-04-28
vue-cli2、vue-cli3脚手架详细讲解
2019-04-28
Redis 性能问题分析
2019-04-28
OAuth2 实现单点登录 SSO
2019-04-28
Spring Boot Elasticsearch 入门
2019-04-28
Spring Boot MongoDB 入门
2019-04-28
Spring框架-事务管理注意事项
2019-04-28
Spring Boot之程序性能监控
2019-04-28
Spring Boot 热部署入门
2019-04-28