分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 软件开发

php中使用sphinx搜索引擎

发布时间:2023-09-06 01:20责任编辑:郭大石关键词:暂无标签

sphinx是一个高效的搜索引擎,分词搜索的速度比较快,索引建立存储在硬盘文件,不会干扰数据库,有自己内置的一套数据库。

一. 安装、配置

1.ubuntu安装sphinx

如果没有安装aptitude ,需要先安装 aptitude 因为因为用apt get install 安装下面命令会出现问题.
sudo apt-get install aptitude
sudo aptitude install sphinx3 sphinx3-doc sphinxsearch sphinx-common -y

2.配置

cd /etc/sphinxsearch/
cp sphinx.conf.sample sphinx.conf

修改配置文件如下

#配置源source sphinx_t0 #数据库名_数据表名,每配置一个数据表,都需要写上一个配置源{ ???type ???????????????????= mysql #数据库类型 ???sql_host ???????????????= localhost ???sql_user ???????????????= root ???sql_pass ???????????????= 123123 ???sql_db ???????????????????= sphinx #指定数据库 ???sql_port ???????????????= 3306 ???# optional, default is 3306 ???sql_sock ????????????????= /tmp/mysql.sock #mysql接口 ???????#从数据库之中读取数据的SQL语句设置 ???#在这里尽可能不使用where或groupby, ???#将where与groupby的内容交给sphinx,由sphinx进行条件过滤与groupby效率会更高 ???#注意:select的字段必须包括一个唯一主键以及要全文检索的字段(可以有多个)、输出的字段。 ???#where中要用到的字段也要select出来 ???#例: ???#在配置sql语句,可以先写出一句预计要执行的sql语句,然后根据sphinx规定设置 ???#select * from t0 where description like ‘%广州%‘ or name like ‘%s%‘ ???#=> select id,description,name,age from t0 ???????sql_query ???????????????= ????????SELECT id, name, age, description,group_id,date_added ????????FROM t0 ???????????sql_attr_uint ???????????= age ???#使用sql_attr设置的字段(搜索条件),只能作为属性,使用SphinxClient::SetFilter()进行过滤; ???#未被设置的字段,自动作为全文检索的字段,使用SphinxClient::Query("搜索字符串")进行全文搜索 ???#sql_query第一列id需为整数,且被系统使用,无需再设置sql_attr_uint ???????sql_attr_uint ???????????= group_id ???sql_attr_timestamp ???????= date_added #定义不同类型的字段要用不同的属性名,比如上面的sql_attr_timestamp就是时间戳类型 ???#sql_query_info ???????????= SELECT * FROM documents WHERE id=$id ?#命令行查询时,从数据库读取原始数据信息 ????#在执行sql_query前执行的sql命令, 可以有多条 ????sql_query_pre ??????????= SET NAMES utf8 #执行sql字符编码}#索引,每一源需要一个索引index sphinx_t0 #索引名字一般与配置源一致{ ???source ???????????????????= sphinx_t0 #source 关联源 ???path ???????????????????= /usr/local/coreseek/var/data/sphinx_t0 #索引文件存放路径,每个索引文件一个 ???docinfo ????????????????= extern ???charset_dictpath ???????= /usr/local/mmseg/etc/ #指明分词法读取词典文件的位置,当启用分词法时,为必填项。在使用LibMMSeg作为分词 库时,需要确保词典文件uni.lib在指定的目录下 ????charset_type ???????????= zh_cn.utf-8 #字符编码}#索引,控制所有索引indexer{ ???mem_limit ???????????????= 512M #内存}#sphinx守护进程配置 searchd{ ???port ???????????????????= 9312 #端口 ???log ???????????????????????= /usr/local/coreseek/var/log/searchd.log ???query_log ???????????????= /usr/local/coreseek/var/log/query.log ???read_timeout ???????????= 5 #超时 ???max_children ???????????= 30 #最大连接数 ???pid_file ???????????????= /usr/local/csft/var/log/searchd.pid #pid文件路径 ????max_matches ???????????????= 1000 #max_matches最大匹配数,也就是查找的数据再多也只返回这里设置的1000条 ???seamless_rotate ???????????= 1 ???preopen_indexes ???????????= 0 ???unlink_old ???????????????= 1}

3.执行命令分词,会在/var/lib/sphinxsearch/data/test1 目录下生成一堆索引文件


sudo indexer -c /etc/sphinxsearch/sphinx.conf test1

test1为上述配置文件的index名字

4.命令行测试搜索

sudo search -c /etc/sphinxsearch/sphinx.conf google

二.在php中使用,安装php、sphinx的依赖库

1.安装 aptitude

apt-get install aptitude
sudo aptitude install libsphinxclient-dev libsphinxclient-0.0.1 -y

2.安装php sphinx的扩展

安装 pecl
sudo apt-get install php-pear php5-dev
在安装sphinx
sudo pecl install sphinx

3.在配置文件php.ini中添加sphinx的扩展,

我的php.ini文件为
sudo vim /etc/php5/fpm/php.ini
获取自己的php.ini文件位置使用
php5-fpm -i|grep ini

添加:
extension=sphinx.so
4.重启php5-fpm,查看php是否加载sphinx模块
sudo /etc/init.d/php5-fpm restart
5.将search程序运行在后台
sudo searchd -c /etc/sphinxsearch/sphinx.conf
默认监听配置文件中的端口:9312

PHP代码:

 ?<?php ??header(‘Content-Type:text/html;charset=utf-8‘); ???//编码为utf-8 ??$list= array(); ??if(!empty($_POST)){ ??????$sc = new SphinxClient(); // 实例化Api ??????$sc->setServer(‘192.168.169.128‘, 9312); // 设置服务端,第一个参数sphinx服务器地址,第二个sphinx监听端口 ??????$res = $sc->query($_POST[‘key‘], ‘test1‘); // 执行查询,第一个参数查询的关键字,第二个查询的索引名称,mysql索引名 ???称(这个也是在配置文件中定义的),多个索引名称以,分开,也可以用*表示所有索引。 ??????// print_r($sc); ??????echo ‘<pre>‘; ?????print_r($res); ?????echo ‘</pre>‘; ?????exit; ?} ??>
?<form action="" method="post"> ?<input type="text" name="key" /> ?<input type="submit" value="提交" /> ?</form>

php中使用sphinx搜索引擎

原文地址:http://www.cnblogs.com/yuzhoushenqi/p/7736405.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved