Rumah >pembangunan bahagian belakang >tutorial php >php中如何使用sphinx搜索引擎
sphinx是一个高效的搜索引擎,分词搜索的速度比较快,索引建立存储在硬盘文件,不会干扰数据库,有自己内置的一套数据库.希望能帮助到大家。
sphinx是一个高效的搜索引擎,分词搜索的速度比较快,索引建立存储在硬盘文件,不会干扰数据库,有自己内置的一套数据库
如果没有安装aptitude ,需要先安装 aptitude 因为因为用apt get install 安装下面命令会出现问题.<br>sudo apt-get install aptitude
<br>sudo aptitude install sphinx3 sphinx3-doc sphinxsearch sphinx-common -y
<br> 1 2 |
<br> cd /etc/sphinxsearch/ cp sphinx.conf.sample sphinx.conf |
修改配置文件如下<br>
<br> 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 |
<br> 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 } <br> 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 } |
<br>
执行命令分词,会在/var/lib/sphinxsearch/data/test1 目录下生成一堆索引文件<br>sudo indexer -c /etc/sphinxsearch/sphinx.conf test1
test1为上述配置文件的index名字
sudo search -c /etc/sphinxsearch/sphinx.conf google
apt-get install aptitude
<br>sudo aptitude install libsphinxclient-dev libsphinxclient-0.0.1 -y
安装 pecl<br>sudo apt-get install php-pear php5-dev
<br>在安装sphinx<br>sudo pecl install sphinx
我的php.ini文件为<br>sudo vim /etc/php5/fpm/php.ini
<br>获取自己的php.ini文件位置使用<br>php5-fpm -i|grep ini
添加:<br>extension=sphinx.so
<br>4.重启php5-fpm,查看php是否加载sphinx模块<br>sudo /etc/init.d/php5-fpm restart
<br>5.将search程序运行在后台<br>sudo searchd -c /etc/sphinxsearch/sphinx.conf
<br>默认监听配置文件中的端口:9312
6.在thinkphp中调用搜索<br>
<br> 1 2 3 4 5 6 7 8 9 10 11 12 13 |
<br> 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,该搜索引擎支持增量索引,多种模式的搜索,网上的资料也非常多
Atas ialah kandungan terperinci php中如何使用sphinx搜索引擎. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!