php中如何使用sphinx搜索引擎

小云云
发布: 2018-03-21 14:12:10
原创
2736人浏览过

 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

}


  1. 执行命令分词,会在/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.安装 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

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,该搜索引擎支持增量索引,多种模式的搜索,网上的资料也非常多

<br/>
登录后复制
登录后复制
登录后复制
<br/>
登录后复制
登录后复制
登录后复制
<br/>
登录后复制
登录后复制
登录后复制
<br/>
登录后复制
登录后复制
登录后复制
<br/>
登录后复制
登录后复制
登录后复制
<br/>
登录后复制
登录后复制
登录后复制

以上就是php中如何使用sphinx搜索引擎的详细内容,更多请关注php中文网其它相关文章!

PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号