《PHP學習:php啟用sphinx全文搜索的實現方法》要點:
本文介紹了PHP學習:php啟用sphinx全文搜索的實現方法,希望對您有用。如果有疑問,可以聯系我們。
PHP應用本文實例講述了php啟用sphinx全文搜索的實現辦法.分享給大家供大家參考.具體分析如下:
PHP應用在編譯安裝 sphinx 的時候出現很多中文亂碼,最后拋出錯誤卡住了,我去到官方直接下載一個 rpm 包,安裝就很爽,具體錯誤不想研究了,忙開發呢.
PHP應用安裝兩個包,一個是 mmseg 這個是生成中文字典的程序,一個是? csft 也就是中國版的sphinx .
PHP應用rpm -ivh 安裝完以后,很順利~~不到半分鐘就裝完了.
PHP應用中文字典庫,我直接去 csft 官方下載了,挺好的想得很周到.
PHP應用unigram.txt? uni.lib
PHP應用unigram.txt? 字典文本,可以在里面添加你自己的關鍵字.
PHP應用然后使用:mmseg -u unigram.txt 生成字典文件:unigram.txt.uni 然后重命名一下? uni.lib 這個就是sphinx 認識的字典了.
PHP應用放哪里?放你在 sphinx.conf 里面配置的字典路徑里面,等會說到,然后基本就差不多了,在看下sphinx 幾個實用的程序:
代碼如下:
[root@beihai365 /]# csft-
PHP應用csft-indexer? csft-search?? csft-searchd
PHP應用csft-indexer? 是生成全文搜索索引的 程序
PHP應用csft-search? 是測試搜索是否生效用的,也很好用,不如我還沒用客戶端腳本開發,就可以用這個來查看全文搜索是否成功
PHP應用csft-searchd? 這個就是 sphinx 搜索的守護程序了. 啟動以后,就可以用腳本 php python 等,開查詢了.
PHP應用就那么簡單,在看下關鍵的兩部分東西.
PHP應用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 停止搜索守護
PHP應用[root@beihai365 /]# csft-indexer --all 針對所有節點生成索引,你也可以針對某個節點生成索引比如:csft-indexer? xx
PHP應用[root@beihai365 /]# csft-search App 搜索關鍵字 App,不過看下面信息沒有搜到和沒有命中任何的文檔.
PHP應用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應用希望本文所述對大家的php程序設計有所贊助.
歡迎參與《PHP學習:php啟用sphinx全文搜索的實現方法》討論,分享您的想法,維易PHP學院為您提供專業教程。
轉載請注明本頁網址:
http://www.snjht.com/jiaocheng/13209.html