php對于sphinx的擴(kuò)展支持安裝:
Coreseek官方教程中建議php使用直接include一個php文件進(jìn)行操作,事實上php有獨(dú)立的sphinx模塊可以直接操作coreseek(coreseek就是sphinx!)已經(jīng)進(jìn)入了php的官方函數(shù)庫,而且效率的提升不是一點(diǎn)點(diǎn)!但php模塊依賴于libsphinxclient包
如果不安裝他則會報:
?./configure 的時候可能出錯,提示
checking for libsphinxclient headers in default path... not found configure: error: Cannot find libsphinxclient headers ,
解決方法如下:
進(jìn)入coreseek-3.2.14/csft-3.2.14/api/libsphinxclient目錄,編譯安裝libsphinxclient即可
一,那么首先安裝這個依賴:
cd /usr/local/src/coreseek-4.1-beta/csft-4.1/api/libsphinxclient
報如下錯誤;
config.status: creating Makefile
config.status: error: cannot find input file: Makefile.in
這是因為configure.in的文件格式不對(是dos格式),用編輯器打開configure.in文件,將其重新保存為 unix格式,當(dāng)然不先查看下是否是格式不對? :set ff
??? > vi configure.in ?
??? vi > set fileformat=unix ?
??? vi > wq ?
??? > aclocal ?
??? > autoconf ?
??? > automake -a ?
??? > ./configure
如果格式正確:
aclocal
libtoolize --force
automake --add-missing
autoconf
autoheader
make clean
執(zhí)行以上命令再次進(jìn)行安裝
./configure --prefix=/usr/local/sphinxclient
二,
___________________________________________________________________________________________________________________
PHP5版本才適用:
下載并安裝sphinx的php擴(kuò)展
地址 :http://pecl.php.net/package/sphinx
wget http://pecl.php.net/get/sphinx-1.3.3.tgz?? ?
tar zxvf sphinx-1.3.3.tgz?? ?
cd sphinx-1.3.3
./configure --with-php-config=/usr/local/php/bin/php-config --with-sphinx=/usr/local/sphinxclient
上面版本會報錯:
? php_sphinx_client_handlers.read_property = php_sphinx_client_read_property;
?????????????????????????????????????????? ^
make: *** [sphinx.lo] Error 1
這個報錯,修改 sphinx.c 第105行為:
retval = std_hnd->read_property(object, member, type TSRMLS_CC, NULL);
然后編譯即可通過
4 編輯 php.ini
extension=sphinx.so
前提是你的PHP版本不是7.
否則因為版本不符,無法修復(fù),可安裝適配PHP7的sphinx擴(kuò)展。
-----------------------------------------------------------------------------------------------------------------------------------
如果是php7版本:
版本地址:git.php.net/?p=pecl/search_engine/sphinx.git;a=shortlog;h=refs/heads/php7
下載地址:http://git.php.net/?p=pecl/search_engine/sphinx.git;a=snapshot;h=339e123acb0ce7beb2d9d4f9094d6f8bcf15fb54;sf=tgz? ---可能下載下來為index.html文件,那就下載到本地然后rz上去。
接下來接著安裝:
這里現(xiàn)在的版本為:sphinx-339e123.tar.gz
tar -zxf sphinx-339e123.tar.gz
cd sphinx-339e123
./configure --prefix=/usr/local/sphinxextend --with-php-config=/usr/local/php-fpm/bin/php-config --with-sphinx=/usr/local/libsphinxclient/
make && make install
Installing shared extensions:???? /usr/local/php-fpm/lib/php/extensions/no-debug-non-zts-20160303/
然后可以cd到這個目錄去看下結(jié)果:
cd /usr/local/php-fpm/lib/php/extensions/no-debug-non-zts-20160303/
可以看到里面有sphinx.so
然后找到php.ini
在里面添加extension=spninx.so
然后使用php-m 或者看下phpinfo();
能看到sphinx即可。
注:執(zhí)行php腳本之前要先啟動sphinx服務(wù),在shpinx安裝目錄的bin目錄下執(zhí)行./searchd 啟動sphinx服務(wù)
然后可以使用php來調(diào)用sphinx:
sphinx查詢索引出來的結(jié)果默認(rèn)為一個關(guān)聯(lián)數(shù)組,其中id對應(yīng)的為從數(shù)據(jù)庫取出來的數(shù)據(jù)庫主鍵ID,也就是說使用sphinx的表必須存在主鍵,否則不能返回結(jié)果。
實例一:
<?php?? ?
??? $s = new SphinxClient;?? ?
??? $s->setServer("127.0.0.1", 9312);?? ?
??? $s->setMatchMode(SPH_MATCH_PHRASE);?? ?
??? $s->setMaxQueryTime(30);?? ?
??? $res = $s->query("寶馬",'main'); #[寶馬]關(guān)鍵字,[main]索引源source?? ?
??? $err = $s->GetLastError();?? ?
??? var_dump(array_keys($res['matches']));?? ?
??? echo "<br>"."通過獲取的ID來讀取數(shù)據(jù)庫中的值即可。"."<br>";?? ?
??? echo '<pre>';?? ?
??? var_dump($res);?? ?
??? var_dump($err);?? ?
??? echo '</pre>'; ?
實例二:
<?php
$sphinx = new SphinxClient;
$sphinx->setServer("localhost", 9312);
$sphinx->setMatchMode(SPH_MATCH_ANY);?? //匹配模式 ANY為關(guān)鍵詞自動拆詞,ALL為不拆詞匹配(完全匹配)
$sphinx->SetArrayResult ( true );?? ?//返回的結(jié)果集為數(shù)組
$result = $sphinx->query("test","*");?? ?//星號為所有索引源
$count=$result['total'];?? ??? ?//查到的結(jié)果條數(shù)
$time=$result['time'];?? ??? ??? ?//耗時
$arr=$result['matches'];?? ??? ?//結(jié)果集
$id='';
for($i=0;$i<$count;$i++){
?? ?$id.=$arr[$i]['id'].',';
}
$id=substr($id,0,-1);?? ??? ??? ?//結(jié)果集的id字符串
實例三,支持分頁的:
<?php????
????header("Content-type:?text/html;?charset=utf-8");????
????require("./sphinxapi.php");????
????$s?=?new?SphinxClient;????
????$s->setServer("192.168.252.132",?9312);????
????//SPH_MATCH_ALL,?匹配所有查詢詞(默認(rèn)模式);?SPH_MATCH_ANY,?匹配查詢詞中的任意一個;?SPH_MATCH_EXTENDED2,?支持特殊運(yùn)算符查詢????
????$s->setMatchMode(SPH_MATCH_ALL);????
????$s->setMaxQueryTime(30);?????????????????????????????//設(shè)置最大搜索時間????
????$s->SetArrayResult(false);???????????????????????????//是否將Matches的key用ID代替????
????$s->SetSelect?(?"*"?);???????????????????????????????//設(shè)置返回信息的內(nèi)容,等同于SQL????
????$s->SetRankingMode(SPH_RANK_BM25);???????????????????//設(shè)置評分模式,SPH_RANK_BM25可能使包含多個詞的查詢的結(jié)果質(zhì)量下降。?????
????//$s->SetSortMode(SPH_SORT_EXTENDED);????????????????//發(fā)現(xiàn)增加此參數(shù)會使結(jié)果不準(zhǔn)確????
????//$s->SetSortMode(SPH_SORT_EXTENDED,"from_id?asc,id?desc");??//設(shè)置匹配項的排序模式,?SPH_SORT_EXTENDED按一種類似SQL的方式將列組合起來,升序或降序排列。????
????$weights?=?array?('company_name'?=>?20);????
????$s->SetFieldWeights($weights);????????????????????????//設(shè)置字段權(quán)重????
????$s->SetLimits?(?0,?30,?1000,?0?);??????//設(shè)置結(jié)果集偏移量??SetLimits?(便宜量,匹配項數(shù)目,查詢的結(jié)果集數(shù)默認(rèn)1000,閥值達(dá)到后停止)????
????//$s->SetFilter?(?$attribute,?$values,?$exclude=false?);?????//設(shè)置屬性過濾????
????//$s->SetGroupBy?(?$attribute,?$func,?$groupsort="@group?desc"?);????//設(shè)置分組的屬性????
????$res?=?$s->query('@*?"汽車"','main','--single-0-query--');?#[寶馬]關(guān)鍵字,[news]數(shù)據(jù)源source????
????//代碼高亮????
????$tags?=?array();????
????$tags_name?=?array();????
????foreach($res['matches']?as?$key=>$value){????
????????$tags[]?=?$value['attrs'];????
????????$company_name[]?=?$value['attrs']['company_name'];????
????????$description[]?=?$value['attrs']['description'];????
????}????
????$company_name?=?$s->BuildExcerpts?($company_name,?'main',?'汽車',?$opts=array()?);?????//執(zhí)行高亮,這里索引名字千萬不能用*????
????$description?=?$s->BuildExcerpts?($description,?'main',?'汽車',?$opts=array()?);???????//執(zhí)行高亮,這里索引名字千萬不能用*????
????foreach($tags?as?$k=>$v)????
????{????
????????$tags[$k]['company_name']?=?$company_name[$k];??//高亮后覆蓋????
????????$tags[$k]['description']?=?$description[$k];????//高亮后覆蓋????
????}????
????//?高亮后覆蓋????
????$i?=?0;????
????foreach($res['matches']?as?$key=>$value){????
????????$res['matches'][$key]?=?$tags[$i];????
????????$i++;????
????}????
????$err?=?$s->GetLastError();????
????echo?'<pre>';????
????var_export($res);????
????var_export($err);????
????echo?'</pre>';???
---------------------
生成索引時如果出現(xiàn)如下錯誤:
FATAL: failed to lock /usr/local/sphinx/var/data/test1.spl: Resource temporarily unavailable, will not index. Try --rotate option.
請改用命令
?./indexer -c /usr/local/sphinx/etc/sphinx.conf --all --rotate
---------------------
作者:youcijibi
來源:CSDN
原文:https://blog.csdn.net/youcijibi/article/details/76728382
版權(quán)聲明:本文為博主原創(chuàng)文章,轉(zhuǎn)載請附上博文鏈接!