[參考資料](https://blog.csdn.net/moqiang02/article/details/42027243)
[關(guān)于排序和分而已,及sphinxapi.php的調(diào)用](https://blog.csdn.net/joyatonce/article/details/52059564)
首先了解sphinx是什么?
他是一個(gè)服務(wù),用于搜索的服務(wù),也可以說(shuō)是一個(gè)搜索引擎,
如果使用搜索引擎,必須開啟他的服務(wù),
其次在api根據(jù)端口號(hào)啟用服務(wù),并傳入關(guān)鍵字
注意在thinkphp中在new對(duì)象的時(shí)候要加\,
如下:
public function test()
{
$cl=new \SphinxClient();
$cl->SetServer('127.0.0.1',9312);
$cl->SetArrayResult(true);
$cl->SetMatchMode(SPH_MATCH_ANY);
$cl->SetLimits(0,12);
$index_name="dizhi";
$key="經(jīng)營(yíng)部";
$this->s_keywords=$key;
$this->s_index=$index_name;
$this->h_cl=$cl;
$this->h_opts= array(
"before_match" => "",
"after_match" => "",
"chunk_separator" => "
",
//? ? ? ? ? ? "limit" => 6,
//? ? ? ? ? ? "around" =>3
? ? ? ? );
$res=$cl->Query($key,$index_name);
$ids=array_column($res['matches'],"id");
//? ? ? ? var_dump($ids);
? ? ? ? $m=M();
$where['id']=array("in",$ids);
$res=$m->table("address")->where($where)->select();
//BuildExcerpts ( $docs, $this->s_index, $this->s_keywords, $this->h_opts);
? ? ? ? foreach ($resas $k=>$v){
$res[$k]=$cl->BuildExcerpts ( $v, $this->s_index, $this->s_keywords, $this->h_opts);
}
//? ? ? ? var_dump($res);
? ? ? ? $this->assign("res",$res);
$this->display("");
}
-------------------------------------方法結(jié)束
第二點(diǎn)要注意的是,在search.exe中是不能搜索中文的,但是可以用英文和數(shù)字測(cè)試是否可用,
返回的weight和size,就有ip,根據(jù)ip去驗(yàn)證;
下面附上常用命令
indexer.exe -c D:\project\coreseek\sphinx.conf zhilian //生成索引文件
search.exe -c D:\mysoftware\coreseek-3.2.14-win32\sphinx.conf redis? ? //查詢r(jià)edis的數(shù)目
D:\mysoftware\coreseek-3.2.14-win32\bin>indexer.exe -c D:\mysoftware\coreseek-3.
2.14-win32\sphinx.conf --merge zhilian? add_zhilian? --rotate //合并索引
-c 執(zhí)行? 配置目錄? 索引文件名稱
D:\project\coreseek\bin>searchd.exe? --install? -c? D:\mysoftware\coreseek-3.2.14-win32\sphinx.conf? ? ? ? //安裝服務(wù)器
sc delete searchd//刪除服務(wù)
replace into a select 1,max(id) from zhilian;//記錄生成的最大id數(shù)
D:\mysoftware\coreseek-3.2.14-win32\bin>indexer.exe -c D:\mysoftware\coreseek-3.
2.14-win32\sphinx.conf --merge zhilian? add_zhilian? --rotate//合并索引
D:\mysoftware\coreseek-3.2.14-win32\bin>indexer.exe -c D:\mysoftware\coreseek-3.
2.14-win32\sphinx.conf add_zhilian? //生成新增數(shù)據(jù)的索引
///////一定要注意? searchd是否啟動(dòng)服務(wù)
sphinx與mysql的配置
創(chuàng)建sphinx統(tǒng)計(jì)表河狐,在coreseek_test庫(kù)中執(zhí)行到推。
CREATETABLEsph_counter
(?
counter_idINTEGERPRIMARYKEYNOTNULL,
max_doc_idINTEGERNOTNULL
);?
創(chuàng)建配置sphinx與mysql的配置文件
# vi /usr/local/coreseek/etc/csft_mysql.conf?
#MySQL數(shù)據(jù)源配置渤早,詳情請(qǐng)查看:http://www.coreseek.cn/docs/coreseek_4.1-sphinx_2.0.1-beta.html#conf-reference?
#源定義?
source main?
{?
? ? type? ? ? ? ? ? ? ? ? ? = mysql?
? ? sql_host? ? ? ? ? ? ? ? = localhost?
? ? sql_user? ? ? ? ? ? ? ? = root?
? ? sql_pass? ? ? ? ? ? ? ? = 123456?
? ? sql_db? ? ? ? ? ? ? ? ? = coreseek_test?
? ? sql_port? ? ? ? ? ? ? ? = 3306?
? ? sql_query_pre? ? ? ? ? = SET NAMES utf8?
? ? sql_query_pre? ? ? ? ? = REPLACE INTO sph_counter SELECT 1,MAX(id) FROM hr_spider_company;?
? ? sql_query? ? ? ? ? ? ? = SELECT * FROM hr_spider_company WHERE id<=( SELECT max_doc_id FROM sph_counter WHERE counter_id=1 ) #sql_query第一列id需為整數(shù)?
? ? #title、content作為字符串/文本字段脯倒,被全文索引 (注:執(zhí)行報(bào)錯(cuò)的話把下面的幾行注釋掉)
? ? sql_attr_uint? ? ? ? ? ? = id? ? ? ? ? ? ? ? ? ? ? ? #從SQL讀取到的值必須為整數(shù)?
? ? sql_attr_uint? ? ? ? ? ? = from_id? ? ? ? ? ? ? ? #從SQL讀取到的值必須為整數(shù)办悟,不支持全文檢索?
? ? sql_attr_uint? ? ? ? ? ? = link_id? ? ? ? ? ? ? ? #從SQL讀取到的值必須為整數(shù)躬厌,不支持全文檢索?
? ? sql_attr_uint? ? ? ? ? ? = add_time? ? ? ? ? ? ? ? #從SQL讀取到的值必須為整數(shù)铛碑,不支持全文檢索?
? ? sql_field_string? ? ? ? = link_url? ? ? ? ? ? ? ? #字符串字段(可全文搜索狠裹,可返回原始文本信息)?
? ? sql_field_string? ? ? ? = company_name? ? ? ? ? #字符串字段(可全文搜索,可返回原始文本信息)?
? ? sql_field_string? ? ? ? = type_name? ? ? ? ? ? #字符串字段(可全文搜索汽烦,可返回原始文本信息)?
? ? sql_field_string? ? ? ? = trade_name? ? ? ? ? ? #字符串字段(可全文搜索涛菠,可返回原始文本信息)?
? ? sql_field_string? ? ? ? = email? ? ? ? ? ? ? ? #字符串字段(可全文搜索,可返回原始文本信息)?
? ? sql_field_string? ? ? ? = description? ? ? ? ? ? #字符串字段(可全文搜索撇吞,可返回原始文本信息)?
? ? sql_query_info_pre? ? ? = SET NAMES utf8? ? ? ? #命令行查詢時(shí)俗冻,設(shè)置正確的字符集?
? ? sql_query_info? ? ? ? ? = SELECT id,from_id,link_id,company_name,type_name,trade_name,address,description, FROM_UNIXTIME(add_time) AS add_time? FROM hr_spider_company? WHERE id=$id? ? ? ? ? ? ? ? ? ? #命令行查詢時(shí),從數(shù)據(jù)庫(kù)讀取原始數(shù)據(jù)信息?
}?
source delta : main? ?
{? ?
? ? sql_query_pre? ? ? ? ? = SET NAMES utf8? ?
? ? sql_query? ? ? ? ? ? ? = SELECT * FROM hr_spider_company WHERE id>( SELECT max_doc_id FROM sph_counter WHERE counter_id=1 )?
? ? sql_query_post_index? ? = REPLACE INTO sph_counter SELECT 1,MAX(id) FROM hr_spider_company?
}? ?
#index定義?
index main?
{?
? ? source? ? ? ? ? ? ? ? = main? #對(duì)應(yīng)的source名稱?
? ? path? ? ? ? ? ? ? ? ? = /usr/local/coreseek/var/data/mysql? #請(qǐng)修改為實(shí)際使用的絕對(duì)路徑牍颈,例如:/usr/local/coreseek/var/...?
? ? docinfo? ? ? ? ? ? ? = extern?
? ? mlock? ? ? ? ? ? ? ? = 0?
? ? morphology? ? ? ? ? ? = none?
? ? min_word_len? ? ? ? ? = 1?
? ? html_strip? ? ? ? ? ? = 0?
? ? #中文分詞配置迄薄,詳情請(qǐng)查看:http://www.coreseek.cn/products-install/coreseek_mmseg/?
? ? charset_dictpath? ? = /usr/local/mmseg3/etc/? ? ? ? ? #BSD、Linux環(huán)境下設(shè)置煮岁,/符號(hào)結(jié)尾?
? ? charset_type? ? ? ? = zh_cn.utf-8?
}?
index delta : main? ?
{? ?
? ? source? ? ? ? ? = delta? ?
? ? path? ? ? ? ? ? = /usr/local/coreseek/var/data/delta?
}?
#全局index定義?
indexer?
{?
? ? mem_limit? ? ? ? ? ? = 128M?
}?
#searchd服務(wù)定義?
searchd?
{?
? ? listen? ? ? ? ? ? ? = 9312?
? ? read_timeout? ? ? ? = 5?
? ? max_children? ? ? ? = 30?
? ? max_matches? ? ? ? = 1000?
? ? seamless_rotate? ? = 0?
? ? preopen_indexes? ? = 0?
? ? unlink_old? ? ? ? ? = 1?
? ? pid_file? ? = /usr/local/coreseek/var/log/searchd_mysql.pid? #請(qǐng)修改為實(shí)際使用的絕對(duì)路徑讥蔽,例如:/usr/local/coreseek/var/...?
? ? log? ? ? ? ? = /usr/local/coreseek/var/log/searchd_mysql.log? #請(qǐng)修改為實(shí)際使用的絕對(duì)路徑,例如:/usr/local/coreseek/var/...?
? ? query_log? ? = /usr/local/coreseek/var/log/query_mysql.log? ? #請(qǐng)修改為實(shí)際使用的絕對(duì)路徑画机,例如:/usr/local/coreseek/var/...?
? ? binlog_path? =? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? #關(guān)閉binlog日志?
}
我的測(cè)試表名為hr_spider_company冶伞,你只需要根據(jù)實(shí)際需求更改為自己的表名即可。
調(diào)用命令列表:
啟動(dòng)后臺(tái)服務(wù)(必須開啟)
# /usr/local/coreseek/bin/searchd -c /usr/local/coreseek/etc/csft_mysql.conf
執(zhí)行索引(查詢步氏、測(cè)試前必須執(zhí)行一次)
/usr/local/coreseek/bin/indexer -c /usr/local/coreseek/etc/csft_mysql.conf --all --rotate
執(zhí)行增量索引
/usr/local/coreseek/bin/indexer -c /usr/local/coreseek/etc/csft_mysql.conf delta --rotate
合并索引
/usr/local/coreseek/bin/indexer -c /usr/local/coreseek/etc/csft_mysql.conf --merge main delta --rotate --merge-dst-range deleted 0 0
(為了防止多個(gè)關(guān)鍵字指向同一個(gè)文檔加上--merge-dst-range deleted 0 0)
后臺(tái)服務(wù)測(cè)試
# /usr/local/coreseek/bin/search -c /usr/local/coreseek/etc/csft_mysql.conf? aaa
關(guān)閉后臺(tái)服務(wù)
# /usr/local/coreseek/bin/searchd -c /usr/local/coreseek/etc/csft_mysql.conf --stop
自動(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
以下任務(wù)計(jì)劃的意思是:每隔一分鐘執(zhí)行一遍增量索引碰缔,每五分鐘執(zhí)行一遍合并索引,每天1:30執(zhí)行整體索引戳护。
Coreseek官方教程中建議php使用直接include一個(gè)php文件進(jìn)行操作金抡,事實(shí)上php有獨(dú)立的sphinx模塊可以直接操作coreseek(coreseek就是sphinx!)已經(jīng)進(jìn)入了php的官方函數(shù)庫(kù)腌且,而且效率的提升不是一點(diǎn)點(diǎn)梗肝!但php模塊依賴于libsphinxclient包。
[第一步] 安裝依賴libsphinxclient
# cd /var/install/coreseek-4.1-beta/csft-4.1/api/libsphinxclient/?
# ./configure? --prefix=/usr/local/sphinxclient?
configure: creating ./config.status?
config.status: creating Makefile?
config.status: error: cannot find input file: Makefile.in? #報(bào)錯(cuò)configure失敗? ? ?
//處理configure報(bào)錯(cuò)?
編譯過(guò)程中報(bào)了一個(gè)config.status: error: cannot find input file: src/Makefile.in這個(gè)的錯(cuò)誤铺董,然后運(yùn)行下列指令再次編譯就能通過(guò)了:?
# aclocal?
# libtoolize --force?
# automake --add-missing?
# autoconf?
# autoheader?
# make clean?
//從新configure編譯?
# ./configure?
# make && make install?
[第二步] 安裝sphinx的PHP擴(kuò)展
http://pecl.php.net/package/sphinx?
# wget http://pecl.php.net/get/sphinx-1.3.0.tgz?
# tar zxvf sphinx-1.3.0.tgz?
# cd sphinx-1.3.0?
# phpize?
# ./configure --with-php-config=/usr/bin/php-config --with-sphinx=/usr/local/sphinxclient?
# make && make install?
# cd /etc/php.d/?
# cp gd.ini? sphinx.ini?
# vi sphinx.ini?
extension=sphinx.so?
# service php-fpm restart?
打開phpinfo看一下是否已經(jīng)支持了sphinx模塊巫击。
$s =newSphinxClient;
$s->setServer("127.0.0.1",9312);
$s->setMatchMode(SPH_MATCH_PHRASE);?
$s->setMaxQueryTime(30);
$res = $s->query("寶馬",'main');#[寶馬]關(guān)鍵字,[main]數(shù)據(jù)源source?
$err = $s->GetLastError();?
var_dump(array_keys($res['matches']));
echo"
"."通過(guò)獲取的ID來(lái)讀取數(shù)據(jù)庫(kù)中的值即可精续。"."
";
echo'
';
var_dump($res);?
var_dump($err);?
echo'';
調(diào)用示例二:支持分頁(yè)
header("Content-type: text/html; charset=utf-8");
require("./sphinxapi.php");
$s =newSphinxClient;
$s->setServer("192.168.252.132",9312);
//SPH_MATCH_ALL, 匹配所有查詢?cè)~(默認(rèn)模式); SPH_MATCH_ANY, 匹配查詢?cè)~中的任意一個(gè); SPH_MATCH_EXTENDED2, 支持特殊運(yùn)算符查詢?
$s->setMatchMode(SPH_MATCH_ALL);?
$s->setMaxQueryTime(30);//設(shè)置最大搜索時(shí)間?
$s->SetArrayResult(false);//是否將Matches的key用ID代替?
$s->SetSelect ("*");//設(shè)置返回信息的內(nèi)容,等同于SQL?
$s->SetRankingMode(SPH_RANK_BM25);//設(shè)置評(píng)分模式坝锰,SPH_RANK_BM25可能使包含多個(gè)詞的查詢的結(jié)果質(zhì)量下降。?
//$s->SetSortMode(SPH_SORT_EXTENDED);? ? ? ? ? ? ? ? //發(fā)現(xiàn)增加此參數(shù)會(huì)使結(jié)果不準(zhǔn)確?
//$s->SetSortMode(SPH_SORT_EXTENDED,"from_id asc,id desc");? //設(shè)置匹配項(xiàng)的排序模式, SPH_SORT_EXTENDED按一種類似SQL的方式將列組合起來(lái)重付,升序或降序排列顷级。?
$weights =array('company_name'=>20);
$s->SetFieldWeights($weights);//設(shè)置字段權(quán)重?
$s->SetLimits (0,30,1000,0);//設(shè)置結(jié)果集偏移量? SetLimits (便宜量,匹配項(xiàng)數(shù)目,查詢的結(jié)果集數(shù)默認(rèn)1000,閥值達(dá)到后停止)?
//$s->SetFilter ( $attribute, $values, $exclude=false );? ? //設(shè)置屬性過(guò)濾?
//$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í)行高亮确垫,這里索引名字千萬(wàn)不能用*?
$description = $s->BuildExcerpts ($description,'main','汽車', $opts=array() );//執(zhí)行高亮弓颈,這里索引名字千萬(wàn)不能用*?
foreach($tagsas$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'
';
var_export($res);?
var_export($err);?
echo'';
******************************************************************************************************************
錯(cuò)誤:FATAL: failedto lock pid file '/home/zhangjie_z.pt/local/sphinx/var/log/searchd.pid':Resource temporarily unavailable (searchd alrecoreseek+php之sphinx擴(kuò)展安裝+php調(diào)用示例 - CSDN博客ady running?)
意思就是你已經(jīng)打開了一個(gè)search進(jìn)程帽芽,你需要找到相關(guān)進(jìn)程號(hào)ps -e|grep searchd,然后kill PID殺死之前的進(jìn)程翔冀,再啟動(dòng)新進(jìn)程)
啟動(dòng) searchd 服務(wù)時(shí)提示如下錯(cuò)誤:
index 'test1': search error: failed to open /var/lib/sphinx/test1.sph: sphinx 錯(cuò)誤解決 index 'test1': search error: failed to open /var/lib/sphinx/test.sph:No such file or directory; NOT SERVING
這個(gè)錯(cuò)誤可能是沒有建立好索引導(dǎo)致的导街,所以 indexer --all 很重要
CentOS6.x安裝Coreseek和Sphinx擴(kuò)展for PHP - 簡(jiǎn)書
安裝coreseek與sphinx遇見的問(wèn)題 - sphinx- - ITkeyowrd
搭建coreseek(sphinx+mmseg3)詳細(xì)安裝配置+php之sphinx擴(kuò)展安裝+php調(diào)用示例 - CSDN博客
關(guān)于coressek的相關(guān)筆記整理如下:
Linux下Coreseek的安裝配置 - 為程序員服務(wù)
訊搜可以替代coreseek
迅搜(xunsearch) - 開源免費(fèi)中文全文搜索引擎|PHP全文檢索|mysql全文檢索|站內(nèi)搜索