##################### 一? mariadb 安裝 ##############
?? ??? ??? ??? ??? ?使用的是yum安裝 略
#####################################################
##################### 二? php 環(huán)境搭建 ##############
?? ??? ??? ??? ??? ?使用的是yum安裝 略
#####################################################
##################### 三 mmseg3? 安裝? ##############
?
?1)?安裝autoconf[root@D1?software]#?tar?-jxvf?autoconf-2.64.tar.bz2[root@D1?software]#?cd?autoconf-2.64[root@D1?autoconf-2.64]#?./configure?[root@D1?autoconf-2.64]#?make[root@D1?autoconf-2.64]#?make?install2)?安裝mmseg[root@D1?software]#?tar?-zxvf?coreseek-3.2.14.tar.gz?[root@D1?software]#?cd?coreseek-3.2.14[root@D1?coreseek-3.2.14]#?cd?mmseg-3.2.14[root@D1?mmseg-3.2.14]#?./bootstrap??#?輸出warning信息可以忽略?如果輸出error信息則需要解決[root@D1?mmseg-3.2.14]#?./configure?--prefix=/usr/local/mmseg3???#?出現(xiàn)一個(gè)錯(cuò)誤?config.status:?error:?cannot?find?input?file:?'src/Makefile.in'?則?升級(jí)安裝一些軟件[root@D1?mmseg-3.2.14]#?yum?-y?install?gcc?gcc-c++?libtool?autoconf?automake?imake?libxml2-devel?expat-devel?#?升級(jí)安裝一些軟件[root@D1?mmseg-3.2.14]#?aclocal[root@D1?mmseg-3.2.14]#?libtoolize?--force[root@D1?mmseg-3.2.14]#?automake?--add-missing[root@D1?mmseg-3.2.14]#?autoconf[root@D1?mmseg-3.2.14]#?autoheader[root@D1?mmseg-3.2.14]#?make?clean[root@D1?mmseg-3.2.14]#?./configure?--prefix=/usr/local/mmseg3#?重新編譯[root@D1?mmseg-3.2.14]#?make[root@D1?mmseg-3.2.14]#?make?install
################################################################
?
#################### 四 安裝coreseek #################
??
[root@D1?mmseg-3.2.14]#?cd?../csft-3.2.14/[root@D1?csft-3.2.14]#?sh?buildconf.sh???#?warning?信息可以忽略?error信息需要解決[root@D1?csft-3.2.14]#?./configure?--prefix=/usr/local/coreseek??--without-unixodbc?--with-mmseg?--with-mmseg-includes=/usr/local/mmseg3/include/mmseg/?--with-mmseg-libs=/usr/local/mmseg3/lib/?--with-mysql[root@D1?csft-3.2.14]#?make?&&?make?install
########################################################### ?
#################### 五 測(cè)試coreseek 分詞 #################
??
?[root@D1?coreseek-3.2.14]#?cd?testpack/[root@D1?testpack]#?cat??var/test/test.xml??#?此時(shí)應(yīng)該正確顯示中文[root@D1?testpack]#?/usr/local/mmseg3/bin/mmseg?-d?/usr/local/mmseg3/etc?var/test/test.xml[root@D1?testpack]#?/usr/local/coreseek/bin/indexer?-c?etc/csft.conf?--all[root@D1?testpack]#?/usr/local/coreseek/bin/search?-c?etc/csft.conf?網(wǎng)絡(luò)搜索words:1.?'網(wǎng)絡(luò)':?1?documents,?1?hits2.?'搜索':?2?documents,?5?hits#?返回上面的內(nèi)容說明拆詞成功
############################################################
################## 六 配置coreseek數(shù)據(jù)源mysql ######################
?
?1)?創(chuàng)建測(cè)試用數(shù)據(jù)庫(kù)和數(shù)據(jù)表,做為coreseek的測(cè)試用的數(shù)據(jù)源MariaDB?[(none)]>?create?database?test;Query?OK,?1?row?affected?(0.10?sec)MariaDB?[(none)]>?use?test;MariaDB?[test]>?source?/root/v9_news.sql;??#?其中有兩萬(wàn)多條數(shù)據(jù)?供測(cè)試MariaDB?[test]>?show?create?table?v9_news\G;
***************************?1.?row?***************************Table:?v9_news
Create?Table:?CREATE?TABLE?`v9_news`?(`id`?mediumint(8)?unsigned?NOT?NULL?AUTO_INCREMENT,`catid`?smallint(5)?unsigned?NOT?NULL?DEFAULT?'0',`typeid`?smallint(5)?unsigned?NOT?NULL,`title`?varchar(120)?NOT?NULL?DEFAULT?'',`style`?char(24)?NOT?NULL?DEFAULT?'',`thumb`?varchar(100)?NOT?NULL?DEFAULT?'',`keywords`?varchar(60)?NOT?NULL?DEFAULT?'',`description`?mediumtext?NOT?NULL,`posids`?tinyint(1)?unsigned?NOT?NULL?DEFAULT?'0',`url`?char(100)?NOT?NULL,`listorder`?tinyint(3)?unsigned?NOT?NULL?DEFAULT?'0',`status`?tinyint(2)?unsigned?NOT?NULL?DEFAULT?'1',`sysadd`?tinyint(1)?unsigned?NOT?NULL?DEFAULT?'0',`islink`?tinyint(1)?unsigned?NOT?NULL?DEFAULT?'0',`username`?char(20)?NOT?NULL,`inputtime`?int(10)?unsigned?NOT?NULL?DEFAULT?'0',`updatetime`?int(10)?unsigned?NOT?NULL?DEFAULT?'0',`subtitle`?varchar(120)?NOT?NULL?DEFAULT?'',`lable`?varchar(150)?NOT?NULL?DEFAULT?'',`userid`?int(10)?unsigned?NOT?NULL?DEFAULT?'0',PRIMARY?KEY?(`id`),KEY?`status`?(`status`,`listorder`,`id`),KEY?`listorder`?(`catid`,`status`,`listorder`,`id`),KEY?`catid`?(`catid`,`status`,`id`)
)?ENGINE=MyISAM?AUTO_INCREMENT=29471?DEFAULT?CHARSET=utf8
1?row?in?set?(0.00?sec)
?2) 修改coreseek 的配置文件
?
[root@D1?testpack]#?vim?/usr/local/coreseek/etc/csft_mysql.conf#?源定義source?news_src{type????????????????????????????????????=?mysql?#####數(shù)據(jù)源類型sql_host????????????????????????????????=?localhost?######mysql主機(jī)sql_user????????????????????????????????=?root?########mysql用戶名sql_pass????????????????????????????????=?123456?############mysql密碼sql_db??????????????????????????????????=?test?#########mysql數(shù)據(jù)庫(kù)名sql_port????????????????????????????????=?3306??###########mysql端口sql_query_pre???=?SET?NAMES?UTF8??###mysql檢索編碼,特別要注意這點(diǎn),很多人中文檢索不到是數(shù)據(jù)庫(kù)的編碼是GBK或其他非UTF8sql_query???????????????????????????????=?\SELECT?id,?catid,?keywords,?title,?description?\FROM?v9_news?#######?獲取數(shù)據(jù)的sqlsql_attr_uint???????????????????=?id?########?無(wú)符號(hào)整數(shù)屬性sql_attr_uint???????????????????=?catidsql_query_info??????????=?SELECT?*?FROM?v9_news?WHERE?id=$id?#########?用于命令界面端(CLI)調(diào)用的測(cè)試}#######?索引定義?##########index?news{source?=?news_src?####聲明索引源path????????????????????=?/usr/local/coreseek/var/data/news?#######索引文件存放路徑及索引的文件名docinfo?=?extern?#####?文檔信息存儲(chǔ)方式mlock?=?0?###緩存數(shù)據(jù)內(nèi)存鎖定morphology?=?none??####?形態(tài)學(xué)(對(duì)中文無(wú)效)min_word_len?=?1??####?索引的詞最小長(zhǎng)度html_strip????????????=?0?charset_dictpath?????=?/usr/local/mmseg3/etc/??#BSD、Linux環(huán)境下設(shè)置,/符號(hào)結(jié)尾?分詞庫(kù)路徑定義?charset_type????????=?zh_cn.utf-8?#####數(shù)據(jù)編碼}#########?全局index定義?##############indexer{mem_limit?=?128M?#######?內(nèi)存限制}######?searchd服務(wù)定義?###searchd{#?listen??????????????=?9312???###?監(jiān)聽端口,在此版本開始,官方已在IANA獲得正式授權(quán)的9312端口,以前版本默認(rèn)的是3312log?=?/usr/local/coreseek/var/log/searchd.logquery_log?=?/usr/local/coreseek/var/log/query.log?###?客戶端查詢?nèi)罩?#xff0c;筆者注:若欲對(duì)一些關(guān)鍵詞進(jìn)行統(tǒng)計(jì),可以分析此日志文件read_timeout?=?5?##?請(qǐng)求超時(shí)max_children?=?30?###?同時(shí)可執(zhí)行的最大searchd?進(jìn)程數(shù)pid_file?=?/usr/local/coreseek/var/log/searchd.pid?#######進(jìn)程ID文件max_matches?=?1000??###?查詢結(jié)果的最大返回?cái)?shù)seamless_rotate?=?1??###?是否支持無(wú)縫切換,做增量索引時(shí)通常需要}#?注?配置文件中源和索引都可以針對(duì)不同的數(shù)據(jù)庫(kù)或不同的查詢條件配置多個(gè)
#############################################################
?
######################## 七? coreseek服務(wù)管理 ###############
?
?1?建立索引[root@D1?testpack]#?/usr/local/coreseek/bin/indexer?-c?/usr/local/coreseek/etc/csft_mysql.conf?--all?--rotate2?啟動(dòng)后臺(tái)服務(wù)[root@D1?testpack]#?/usr/local/coreseek/bin/searchd?-c?/usr/local/coreseek/etc/csft_mysql.conf??3?執(zhí)行增量索引[root@D1?testpack]#?/usr/local/coreseek/bin/indexer?-c?/usr/local/coreseek/etc/csft_mysql.conf?delta?--rotate??4?合并索引[root@D1?testpack]#?/usr/local/coreseek/bin/indexer?-c?/usr/local/coreseek/etc/csft_mysql.conf?--merge?main?delta?--rotate?--merge-dst-range?deleted?0?0??5?后臺(tái)服務(wù)測(cè)試/usr/local/coreseek/bin/search?-c?/usr/local/coreseek/etc/csft_mysql.conf??北京6?關(guān)閉后臺(tái)服務(wù)/usr/local/coreseek/bin/searchd?-c?/usr/local/coreseek/etc/csft_mysql.conf?--stop??7?自動(dòng)化管理crontab?-e?*/1?*?*?*?*?/bin/sh?/usr/local/coreseek/bin/indexer?-c?/usr/local/coreseek/etc/csft_mysql.conf?delta?--rotate??*/5?*?*?*?*?/bin/sh?/usr/local/coreseek/bin/indexer?-c?/usr/local/coreseek/etc/csft_mysql.conf?--merge?main?delta?--rotate?--merge-dst-range?deleted?0?0??30?1?*?*?*??/bin/sh?/usr/local/coreseek/bin/indexer?-c?/usr/local/coreseek/etc/csft_mysql.conf?--all?--rotate
############ 八 安裝php的sphinx擴(kuò)展 #####################
?
1?安裝libsphinxclient[root@D1?sphinx-1.0.4]#?cd?/data/software/coreseek-3.2.14/csft-3.2.14/api/libsphinxclient/[root@D1?libsphinxclient]#??./configure??--prefix=/usr/local/sphinxclient?[root@D1?libsphinxclient]#?make[root@D1?libsphinxclient]#?make?install2?安裝?sphinx-1.0.4.tgz[root@D1?sphinx]#?cd?/data/software/[root@D1?software]#?tar?xvzf?sphinx-1.0.4.tgz[root@D1?software]#?cd?sphinx-1.0.4[root@D1?sphinx-1.0.4]#?yum?-y?install?php-devel?#?如果沒有phpize則?安裝phpize[root@D1?sphinx-1.0.4]#?phpize[root@D1?sphinx-1.0.4]#?./configure?--with-php-config=/usr/bin/php-config?--with-sphinx=/usr/local/sphinxclient[root@D1?sphinx-1.0.4]#?make?#?如果出現(xiàn)錯(cuò)誤信息??The?test-suite?requires?that?proc_open()?is?available.????||?Please?check?if?you?disabled?it?in?php.ini.?出現(xiàn)上述錯(cuò)誤信息,解決方法vi?/data/conf/php.ini(即php的配置文件)找到disable_functions這一行?把proc_open去除即可??其他出現(xiàn)的錯(cuò)誤信息則把相應(yīng)的函數(shù)的禁止去除即可?[root@D1?sphinx-1.0.4]#?make?test[root@D1?sphinx-1.0.4]#?make?installInstalling?shared?extensions:?????/usr/lib64/php/modules/?#?這一行顯示sphinx.so模塊已經(jīng)被安裝到/usr/lib64/php/modules/?目錄下[root@D1?sphinx-1.0.4]#?vi?/data/conf/php.ini添加extension=/usr/lib64/php/modules/sphinx.so
? ?
?
?[root@D1?sphinx-1.0.4]#?service?httpd?restart??(重啟apache)
###################################################################
############# 九 php程序?qū)崿F(xiàn)分詞 #################################
? 1 查看當(dāng)前環(huán)境是否已經(jīng)支持sphinx
? [root@D1 sphinx-1.0.4]# vim sphinx.php
??? <?php
?? ??? ?phpinfo();
?? ?
??? ?>
? 通過瀏覽器訪問
? 是否出現(xiàn)以下信息 (如果出現(xiàn)則說明sphinx的擴(kuò)展已經(jīng)安裝成功)
? sphinx
? sphinx support?? ?enabled
? Version?? ?1.0.3
? Revision?? ?$Revision: 294591 $
? 2) 編寫測(cè)試代碼測(cè)試
? [root@D1 sphinx-1.0.4]# vim sphinx.php
???<?php$sphinx?=?new?SphinxClient();$sphinx->setServer("localhost",?9312);$sphinx->setMatchMode(SPH_MATCH_ANY);$sphinx->setMaxQueryTime(100);$result?=?$sphinx->query("北京");echo?"<pre>";var_dump($result);echo?"</pre>";?>??訪問后可得到數(shù)據(jù)其中["matches"]字段中出現(xiàn)的內(nèi)容就是在數(shù)據(jù)庫(kù)中查找到的內(nèi)容?查找的數(shù)據(jù)項(xiàng)如catid是在coreseek的配置文件中定義的。具體如下[root@D1?testpack]#?vim?/usr/local/coreseek/etc/csft_mysql.confsql_attr_uint???????????????????=?idsql_attr_uint???????????????????=?catid其中matches?中每一條數(shù)據(jù)的下標(biāo)志為id?是sql_query???????????????????????????????=?\SELECT?id,?catid,?keywords,?title,?description?\FROM?v9_news中select?中后面的第一個(gè)值#?注?如果此時(shí)我們想重新建立索引會(huì)出現(xiàn)一個(gè)問題FATAL:?failed?to?lock?/usr/local/sphinx/var/data/news.spl:?Resource?temporarily?unavailable,?will?not?index.?Try?--rotate?option.導(dǎo)致這個(gè)問題的原因是我在后臺(tái)啟動(dòng)了coreseek,所以要先停掉
################################################################
################## 十 sphinx 中類似mysql的一些特性 ##########
?
1)?限制條數(shù),起始位置和索引源vim?sphinx2.php<?php$sphinx?=?new?SphinxClient();$sphinx->setServer(?'localhost',?9312?);$sphinx->setArrayResult(?true?);$sphinx->setLimits(0,?10,?1000);??//?限制數(shù)據(jù)的條數(shù)10和起始位置0,最大條數(shù)1000?如果這一行不設(shè)置的話默認(rèn)顯示的是前20條$sphinx->SetMaxQueryTime(10);$index?=?'news';?//索引源是配置文件中的?index?類,如果有多個(gè)索引源可使用,號(hào)隔開:'email,diary'?或者使用'*'??我在上面使用的是news,所以這里也使用news$result?=?$sphinx->query?('北京',?$index);echo?"<pre>";var_dump($result);echo?"</pre>";2)?限制id范圍$sphinx->setIdRange(100,?3000);??//?限制id的范圍3)?屬性過濾$sphinx->SetFilter('catid',?array(60,65));??//?此時(shí)查出來(lái)的數(shù)據(jù)catid全部是60或者65。如果使用$sphinx->SetFilter('catid',?array(60,65),?false);?則說明查詢catid不為60和65的?。如果使用$sphinx->SetFilterRange('catid',?60,?65);?則說明catid在60到65這個(gè)范圍里面的。?如果使用?$sphinx->SetFilterRange('catid',?60,?65,false);??說明catid不在60到65的范圍4)?排序模式$sphinx->setSortMode?(?"SPH_SORT_ATTR_DESC",?'id');?//?排序方式,按照id升序排列。注:可使用的排序模式有:SPH_SORT_RELEVANCE?模式,?按相關(guān)度降序排列(最好的匹配排在最前面)SPH_SORT_ATTR_DESC?模式,?按屬性降序排列?(屬性值越大的越是排在前面)SPH_SORT_ATTR_ASC?模式,?按屬性升序排列(屬性值越小的越是排在前面)SPH_SORT_TIME_SEGMENTS?模式,?先按時(shí)間段(最近一小時(shí)/天/周/月)降序,再按相關(guān)度降序SPH_SORT_EXTENDED?模式,?按一種類似SQL的方式將列組合起來(lái),升序或降序排列。
SPH_SORT_EXPR?模式,按某個(gè)算術(shù)表達(dá)式排序6)匹配模式$sphinx->setMatchMode(SPH_MATCH_ANY);?//?采用這一種模式的話會(huì)把超級(jí)可愛分成兩個(gè)詞?‘超級(jí)’?‘可愛’?并分別把相應(yīng)的數(shù)據(jù)查詢出來(lái)達(dá)到拆詞的效果$result?=?$sphinx->query?('超級(jí)可愛',?$index);??注:有如下可選的匹配模式:SPH_MATCH_ALL,?匹配所有查詢?cè)~(默認(rèn)模式);SPH_MATCH_ANY,?匹配查詢?cè)~中的任意一個(gè);SPH_MATCH_PHRASE,?將整個(gè)查詢看作一個(gè)詞組,要求按順序完整匹配;SPH_MATCH_BOOLEAN,?將查詢看作一個(gè)布爾表達(dá)式?SPH_MATCH_EXTENDED,?將查詢看作一個(gè)CoreSeek/Sphinx內(nèi)部查詢語(yǔ)言的表達(dá)式?.?從版本Coreseek?3/Sphinx?0.9.9開始,?這個(gè)選項(xiàng)被選項(xiàng)SPH_MATCH_EXTENDED2代替,它提供了更多功能和更佳的性能。保留這個(gè)選項(xiàng)是為了與遺留的舊代碼兼容——這樣即使Sphinx及其組件包括API升級(jí)的時(shí)候,舊的應(yīng)用程序代碼還能夠繼續(xù)工作。SPH_MATCH_EXTENDED2,?使用第二版的“擴(kuò)展匹配模式”對(duì)查詢進(jìn)行匹配.
SPH_MATCH_FULLSCAN,?強(qiáng)制使用下文所述的“完整掃描”模式來(lái)對(duì)查詢進(jìn)行匹配。注意,在此模式下,所有的查詢?cè)~都被忽略,盡管過濾器、過濾器范圍以及分組仍然起作用,但任何文本匹配都不會(huì)發(fā)生.
我們要關(guān)注的主要是SPH_MATCH_EXTENDED2擴(kuò)展匹配模式,擴(kuò)展匹配模式允許使用一些像mysql的條件語(yǔ)句
#####################################################################
############### 十一 注意事項(xiàng) #######################################
?
sphinx是查詢數(shù)據(jù)庫(kù)中相印數(shù)據(jù)的id值,具體的數(shù)據(jù)還應(yīng)該根據(jù)id值到數(shù)據(jù)庫(kù)中去查找
?
########################################################################
?
轉(zhuǎn)載于:https://blog.51cto.com/bingdonghuoyan/1616977
總結(jié)
以上是生活随笔為你收集整理的sphinx+coreseek+php扩展--用php程序实现中文拆词的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。