您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

php中如何使用sphinx搜索引擎

2024/2/29 12:55:56发布17次查看
sphinx是一个高效的搜索引擎,分词搜索的速度比较快,索引建立存储在硬盘文件,不会干扰数据库,有自己内置的一套数据库.希望能帮助到大家。
php中使用sphinx搜索引擎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.配置



1
2



cd /etc/sphinxsearch/
cp sphinx.conf.sample sphinx.conf
修改配置文件如下



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51



source src1
{
type = mysql
sql_host = localhost
sql_user = root
sql_pass = magicmoma
sql_db = coupon_20160901
sql_port = 3306 # optional, default is 3306
sql_query = select couponid,title,description from app_coupon_api
##### 需要包含主键,分词索引字段 ########
}
index test1
{
source = src1
path = /var/lib/sphinxsearch/data/test1 #索引存放目录
docinfo = extern
mlock = 0
morphology = none
min_word_len = 1
charset_type = utf-8
min_prefix_len = 0
min_infix_len = 0
ngram_len = 1
html_strip = 0
}
indexer
{
mem_limit = 2048m
}
searchd
{
listen = 9312
listen = 9306:mysql41
log = /var/log/sphinxsearch/searchd.log
query_log = /var/log/sphinxsearch/query.log
read_timeout = 5
client_timeout = 300
max_children = 30
pid_file = /var/run/sphinxsearch/searchd.pid
max_matches = 1000
seamless_rotate = 1
preopen_indexes = 1
unlink_old = 1
mva_updates_pool = 1m
max_packet_size = 8m
max_filters = 256
max_filter_values = 4096
max_batch_queries = 32
workers = threads # for rt to work
}
执行命令分词,会在/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中使用1.安装php sphinx的依赖库1.安装 aptitudeapt-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
6.在thinkphp中调用搜索



1
2
3
4
5
6
7
8
9
10
11
12
13



public function testsphinx()
{
$s = new \sphinxclient;
$s->setserver(localhost, 9312);
$s->setarrayresult (true );
$s->setmatchmode(sph_match_any);
$s->setmaxquerytime(3);
$result = $s->query(test);
$result = $result['matches'];
$result = array_column($result,'id');
$list = m('couponapi')->field('couponid,title,description')->where(array('couponid'=>array('in',$result)))->select();
dump($list);
}
搜索完毕,返回结果(默认返回20条,修改返回条数用添加 $s->setlimits(0, 1000, 1000);),搜索速度相当快,索引80w条数据的title和description两个字段耗时不到10s,该搜索引擎支持增量索引,多种模式的搜索,网上的资料也非常多
以上就是php中如何使用sphinx搜索引擎的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product