/*方式一 9312端口*/
$s = new \\sphinx\\SphinxClient();
$s->SetServer('localhost', 9312);
//匹配模式
$s->SetMatchMode(SPH_MATCH_EXTENDED2);
//设置翻页和总匹配数据,与max_matches配置有关
$s->SetLimits(0,10,10000000);
//文本查询
$sql = "((@COMPANY_NAME=$keywords) | (@LEGAL_PERSON_NAME=$keywords)) | (@REG_LOCATION=$keywords)";
$result = $s->Query($sql,'keywords');
//大小区间查询
$s->SetFilterRange('REG_CAPITAL_NUM_INT',0,100);
//group查询
$s->SetGroupBy("company_id", SPH_GROUPBY_ATTR);
//排序模式
$s->SetSortMode('SPH_SORT_EXTENDED',"COMPANY_ID");
$result = $s->Query($sql,'test1');
//匹配总数可以从查询结果中获取
$count = $result['total_found'];
/*方式二 9306端口 类似mysql方式*/
//database.conf配置
'gs'=>[
'type' => 'mysql',
// 服务器地址
'hostport' => '9306',
'hostname' => '127.0.0.1',
'username' => '',
// 密码
'password' => '',
],
//查询
$db =Db::connect('gs');
$start = $current*$pagesize;
//文本查询
$sql = "(@COMPANY_NAME \\"$keywords\\" | @LEGAL_PERSON_NAME \\"$keywords\\")";
//区间查询
$sql2 = " AND REG_CAPITAL_NUM_INT between 100 and 200";
//数值查询
$sql2 .= " AND TOTAL_TAX>=1000000";
$datalist = $db->query("SELECT * FROM tax100 WHERE MATCH('$sql') $sql2 GROUP BY COMPANY_ID ORDER BY REG_CAPITAL_NUM_INT DESC LIMIT $start,$pagesize");//tax100 是索引名称
//获取总数
$count_res = $db->query('show meta');
$count = $count_res[1]['Value'];
<aside> 📒
匹配模式
<aside> 📒
排序模式
确保服务器已经安装C++编译器;
1. 下载sphinx-for-chinese安装包到/usr/local目录下
cd /usr/local/
wget <https://www.codelovers.cn/Public/file/sphinx-for-chinese2.2.1.tar.gz>
2. 解压
tar -zxvf sphinx-for-chinese2.2.1.tar.gz
3. 打开解压文件夹下的configure文件,并编辑
vim sphinx-for-chinese-2.2.1-dev-r4311/configure
将 $as_echo "#define USE_LIBICONV 1" >>confdefs.h 的#去除
4. 编译安装,执行以下命令
./configure --prefix=/usr/local/sphinxforchinese --with-mysql
make
make install
5. 进入文件夹
cd /usr/local/sphinxforchinese
6. 下载字典文件并生成字典
wget <https://www.codelovers.cn/Public/file/xdict_1.1.tar.gz>
tar -zxvf xdict_1.1.tar.gz
/usr/local/sphinxforchinese/bin/mkdict xdict_1.1.txt etc/xdict
7. 编辑配置文件
mv sphinx.conf.dist sphinx.conf
vim sphinx.conf
8. 生成索引
/usr/local/sphinxforchinese/bin/indexer -c /usr/local/sphinxforchinese/etc/sphinx.conf --all --rotate
9. 启动服务
/usr/local/sphinxforchinese/bin/searchd -c /usr/local/sphinxforchinese/etc/sphinx.conf
10. 嵌入程序查询
关闭服务命令 /usr/local/sphinxforchinese/bin/searchd --stop