CoreSeek詳細入門教程
centos6作業系統
Coreseek 是一款中文全文檢索/搜尋軟體,以GPLv2許可協議開源釋出,基於Sphinx研發並獨立釋出,專攻中文搜尋和資訊處理領域,適用於行業/垂直搜尋、論壇/站內搜尋、資料庫搜尋、文件/文獻檢索、資訊檢索、資料探勘等應用場景,使用者可以免費下載使用。
coreseek安裝需要預裝的軟體:
yum install make gcc g++ gcc-c++ libtool autoconf automake imake mysql-devel libxml2-devel expat-devel
cd /usr/local/src wget http://www.coreseek.cn/uploads/csft/3.2/coreseek-3.2.14.tar.gz tar -xzvf coreseek-3.2.14.tar.gz cd coreseek-3.2.14 ##安裝mmseg cd mmseg-3.2.14 ./bootstrap #輸出的warning資訊可以忽略,如果出現error則需要解決 ./configure --prefix=/usr/local/mmseg3 make && make install cd .. ## 安裝完成後,mmseg使用的詞典和配置檔案將自動安裝到/usr/local/mmseg3/etc中 ##安裝coreseek cd csft-3.2.14 sh buildconf.sh #輸出的warning資訊可以忽略,如果出現error則需要解決 ./configure --prefix=/usr/local/coreseek --without-unixodbc --with-mmseg --with-mmseg-includes=/usr/local/mmseg3/include/mmseg/ --with-mmseg-libs=/usr/local/mmseg3/lib/ --with-mysql ##如果提示mysql問題,可以檢視MySQL資料來源安裝說明 make && make install cd .. cd /usr/local/coreseek/etc cp sphinx-min.conf.dist sphinx.conf vi sphinx.conf 內容示例如下(localhost,DB_USER,DB_PASSWORD,DB_NAME自行修改) # # Minimal Sphinx configuration sample (clean, simple, functional) # source content { type = mysql sql_host = localhost sql_user = DB_USER sql_pass = DB_PASSWORD sql_db = DB_NAME sql_port = 3306 # optional, default is 3306 sql_query_pre = SET NAMES utf8 sql_query = \ SELECT id, title, pub_time, group_id, content FROM contents where status = '1' sql_attr_uint = group_id sql_attr_timestamp = pub_time sql_query_info = SELECT * FROM contents WHERE id=$id } index content { source = content path = /usr/local/coreseek/var/data/content docinfo = extern charset_dictpath = /usr/local/mmseg3/etc/ charset_type = zh_cn.utf-8 ngram_len = 0 } indexer { mem_limit = 32M } searchd { port = 9312 log = /usr/local/coreseek/var/log/searchd.log query_log = /usr/local/coreseek/var/log/query.log read_timeout = 5 max_children = 30 pid_file = /usr/local/coreseek/var/log/searchd.pid max_matches = 1000 seamless_rotate = 1 preopen_indexes = 1 unlink_old = 1 }
然後根據以上配置建立索引檔案
/usr/local/coreseek/bin/indexer -c /usr/local/coreseek/etc/sphinx.conf --all --rotate
啟動命令
/usr/local/coreseek/bin/searchd -c /usr/local/coreseek/etc/sphinx.conf
然後在coreseek目錄下,新建3個sh指令碼,以便操作
停止服務stop.sh
#!/bin/bash
/usr/local/coreseek/bin/searchd -c /usr/local/coreseek/etc/sphinx.conf --stop
建立索引build.sh
#!/bin/bash
/usr/local/coreseek/bin/indexer -c /usr/local/coreseek/etc/sphinx.conf --all --rotate
啟動服務start.sh
#!/bin/bash
/usr/local/coreseek/bin/searchd -c /usr/local/coreseek/etc/sphinx.conf
新增可執行許可權
chmod +x start.sh
chmod +x stop.sh
chmod +x build.sh
執行start.sh後,使用crontab定時執行build.sh,就可更新索引。(注:因為資料量小且更新不算很頻繁,未使用增量索引,只是定時重建主索引,新版本CoreSeek全文搜尋 4.1 支援實時索引)
crontab -e
0 2 * * * sh /usr/local/coreseek/build.sh >/dev/null 2>&1
每天凌晨2點重建一次索引,忽略日誌輸出。
在/usr/local/src/coreseek.3.2.14/csft-3.2.14/api目錄下提供了PHP的介面檔案 sphinxapi.php,這個檔案包含一個SphinxClient的類,copy到自己的web目錄下
通過如下方式進行搜尋
$s_key = trim($s_key);
if(strpos($s_key,'\'') || strpos($s_key,'\"') || strpos($s_key,'\;')) {
exit('非法字元');
}
require("sphinxapi.php");
$page_nums = 20;
$offset_start = ($page_index-1)*$page_nums;
$offset_end = $offset_start + $page_nums;
$cl = new SphinxClient();
$cl->SetServer('localhost', 9312);
$cl->SetArrayResult(true);
$cl->SetMatchMode(SPH_MATCH_ALL);
$cl->SetLimits($offset_start,$offset_end);
$cl->SetSortMode(SPH_SORT_RELEVANCE);
$res = $cl->Query($s_key,"content");
安裝包括兩個部分,mmseg和csft
安裝成功會在/usr/local資料夾下面出現coreseek資料夾
source bt
{
sql_pass = **** #如果密碼裡面有#號需要使用轉意字元,否則連線不了資料庫
sql_query_pre = SET NAMES utf8 #要根據你自己資料庫的編碼改變,比如如果編碼是utf8mb4而編碼寫的是utf8 會出現沒有搜尋結果的問題
}
index bt
{
source = bt #這個地方的值要和前面配置的source名對應
}
/usr/local/coreseek/bin/searchd -c /usr/local/coreseek/etc/sphinx.conf --stop 停止服務
/usr/local/coreseek/bin/indexer -c /usr/local/coreseek/etc/sphinx.conf --all --rotate 建立索引
/usr/local/coreseek/bin/searchd -c /usr/local/coreseek/etc/sphinx.conf 開啟服務
預設配置檔案是csft.conf 如果配置檔案是其他名字的話,需要-c 來制定配置檔案路徑
---------------
配置檔案中
sql_query = xxxx
xxxx代表一個sql語句,sql語句select的第一個欄位將被sphinx認作表的主鍵來進行索引,所以資料表的主鍵欄位不是int型別也沒有關係,選一個是int型別的欄位排在select語句的第一個就行了,但是這個欄位要保證唯一性,否則會導致搜尋結果不完整,計算出來的值也可以被當做主鍵來進行索引 比如SELECT unix_timestamp(time),name, age .......unix_timestamp(time)是計算出來的,它排在第一個的時候,就會被sphinx當做表的主鍵來進行索引。
---------------------