您好,登錄后才能下訂單哦!
怎么在php項目中啟用sphinx全文搜索功能?相信很多沒有經驗的人對此束手無策,為此本文總結了問題出現的原因和解決方法,通過這篇文章希望你能解決這個問題。
在編譯安裝 sphinx 的時候出現很多中文亂碼,最后拋出錯誤卡住了,我去到官方直接下載一個 rpm 包,安裝就很爽,具體錯誤不想研究了,忙開發呢.
安裝兩個包,一個是 mmseg 這個是生成中文字典的程序,一個是 csft 也就是中國版的sphinx .
rpm -ivh 安裝完以后,很順利~~不到半分鐘就裝完了.
中文字典庫,我直接去 csft 官方下載了,挺好的想得很周到.
unigram.txt uni.lib
unigram.txt 字典文本,可以在里面添加你自己的關鍵字.
然后使用:mmseg -u unigram.txt 生成字典文件:unigram.txt.uni 然后重命名一下 uni.lib 這個就是sphinx 認識的字典了.
放哪里?放你在 sphinx.conf 里面配置的字典路徑里面,等會說到,然后基本就差不多了,在看下sphinx 幾個實用的程序:
復制代碼 代碼如下:
[root@beihai365 /]# csft-
csft-indexer csft-search csft-searchd
csft-indexer 是生成全文搜索索引的 程序
csft-search 是測試搜索是否生效用的,也很好用,不如我還沒用客戶端腳本開發,就可以用這個來查看全文搜索是否成功
csft-searchd 這個就是 sphinx 搜索的守護程序了。 啟動以后,就可以用腳本 php python 等,開查詢了。
就那么簡單,在看下關鍵的兩部分東西.
sphinx.conf 配置文件:
復制代碼 代碼如下:
source tmsgs
{
type = mysql
sql_host = localhost
sql_user = root
sql_pass = 1
sql_db = phpwind75sp3
sql_port = 3306 # optional, default is 3306
#sql_sock = /tmp/mysql3307.sock
sql_query_pre = SET NAMES gbk
sql_query = SELECT id,name,type,stock FROM pw_tools
#sql_attr_uint = id
sql_attr_uint = stock
}
index tmsgsindex
{
source = tmsgs
path = /var/mmseg/searchdata/beihai365
docinfo = extern
charset_type = zh_cn.gbk
#min_prefix_len = 0
#min_infix_len = 2
#ngram_len = 2
charset_dictpath = /var/mmseg/data
#min_prefix_len = 0
#min_infix_len = 0
#min_word_len = 2
}
indexer
{
mem_limit = 128M
}
searchd
{
#listen = 3312
log = /var/log/searchd.log
query_log = /var/log/query.log
read_timeout = 5
max_children = 30
pid_file = /var/log/searchd.pid
max_matches = 1000
#seamless_rotate = 1
#preopen_indexes = 0
#unlink_old = 1
}
source tmsgs
{
type = mysql
sql_host = localhost
sql_user = root
sql_pass = 1
sql_db = phpwind75sp3
sql_port = 3306 # optional, default is 3306
#sql_sock = /tmp/mysql3307.sock
sql_query_pre = SET NAMES gbk
sql_query = SELECT id,name,type,stock FROM pw_tools
#sql_attr_uint = id
sql_attr_uint = stock
}
index tmsgsindex
{
source = tmsgs
path = /var/mmseg/searchdata/beihai365
docinfo = extern
charset_type = zh_cn.gbk
#min_prefix_len = 0
#min_infix_len = 2
#ngram_len = 2
charset_dictpath = /var/mmseg/data
#min_prefix_len = 0
#min_infix_len = 0
#min_word_len = 2
}
indexer
{
mem_limit = 128M
}
searchd
{
#listen = 3312
log = /var/log/searchd.log
query_log = /var/log/query.log
read_timeout = 5
max_children = 30
pid_file = /var/log/searchd.pid
max_matches = 1000
#seamless_rotate = 1
#preopen_indexes = 0
#unlink_old = 1
}
再看一下,測試客戶端代碼:
復制代碼 代碼如下:
<?php
header("Content-type:text/html;charset=utf-8");
include 'sphinxapi.php';
$cl = new SphinxClient();
$cl->SetServer('localhost',3312);
$cl->SetMatchMode(SPH_MATCH_ALL);
$cl->SetArrayResult(true);
$res = $cl->Query("名卡","*");
print_r($res);
?>
<?php
header("Content-type:text/html;charset=utf-8");
include 'sphinxapi.php';
$cl = new SphinxClient();
$cl->SetServer('localhost',3312);
$cl->SetMatchMode(SPH_MATCH_ALL);
$cl->SetArrayResult(true);
$res = $cl->Query("名卡","*");
print_r($res);
?>
“名卡”這個關鍵字是我自己手動在字典里面添加的,看是否能真的搜到,實例代碼如下:
復制代碼 代碼如下:
Array
(
[error] =>
[warning] =>
[status] => 0
[fields] => Array
(
[0] => name
[1] => type
)
[attrs] => Array
(
[stock] => 1
)
[matches] => Array
(
[0] => Array
(
[id] => 8
[weight] => 1
[attrs] => Array
(
[stock] => 100
)
)
)
[total] => 1
[total_found] => 1
[time] => 0.018
[words] => Array
(
[名卡] => Array
(
[docs] => 1
[hits] => 1
)
)
)
Array
(
[error] =>
[warning] =>
[status] => 0
[fields] => Array
(
[0] => name
[1] => type
)
[attrs] => Array
(
[stock] => 1
)
[matches] => Array
(
[0] => Array
(
[id] => 8
[weight] => 1
[attrs] => Array
(
[stock] => 100
)
)
)
[total] => 1
[total_found] => 1
[time] => 0.018
[words] => Array
(
[名卡] => Array
(
[docs] => 1
[hits] => 1
)
)
)
完全沒問題,搜索出來了,幾個關鍵的操作:
[root@beihai365 /]# csft-searchd --stop 停止搜索守護
[root@beihai365 /]# csft-indexer --all 針對所有節點生成索引,你也可以針對某個節點生成索引比如:csft-indexer xx
[root@beihai365 /]# csft-search App 搜索關鍵字 App,不過看下面信息沒有搜到和沒有命中任何的文檔.
Coreseek Full Text Server 3.1
Copyright (c) 2006-2008 coreseek.com
using config file './csft.conf'...
1,
pt:1, 1; index 'tmsgsindex': query 'App ': returned 0 matches of 0 total in 0.017 sec
words:
1. 'app': 0 documents, 0 hits
當大家在運行這些命令的時候發現,需要你自己手動的置頂 --config sphinx.conf 配置文件的路徑,很不方便,所以我干脆 ln -s 一個在 ./,這樣不用每次都去敲入 --config.
看完上述內容,你們掌握怎么在php項目中啟用sphinx全文搜索功能的方法了嗎?如果還想學到更多技能或想了解更多相關內容,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。