程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> PHP綜合 >> php啟用sphinx全文搜索的實現方法

php啟用sphinx全文搜索的實現方法

編輯:PHP綜合

本文實例講述了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程序設計有所幫助。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved