Sphinx(獅身人面像) 想必大家都比較瞭解,就不作介紹了,不瞭解的童鞋可以自己Google。
原生的Sphinx只支援中文,
所以這裡重點介紹支援中文分詞的 Coreseek。
注意:Coreseek 3.2 後,只有安裝 Coreseek 就可以了,它對LibMMSeg和sphinx做了整合,不用再安裝原生Sphinx。(3.2前是要安裝原生Sphinx,還要裝補丁,非常繁瑣)
安裝coreseek
下面以coreseek-3.2.14為例,它基於Sphinx 0.99(不用安裝Sphinx 0.99)
詳細官方手冊:http://www.coreseek.cn/products-install/install_on_bsd_linux/
ubuntu-10.04 安裝 coreseek安裝需要預裝的軟體:
sudo apt-get install make gcc g++ automake libtool mysql-client libmysqlclient15-dev libxml2-dev libexpat1-dev
#下載 coreseek-3.2.14,裡面已經包含 mmsegcd /tmpwget http://www.coreseek.cn/uploads/csft/3.2/coreseek-3.2.14.tar.gztar xzvf coreseek-3.2.14.tar.gzcd coreseek-3.2.14# 先安裝mmsegcd mmseg-3.2.14./bootstrap #輸出的warning資訊可以忽略,如果出現error則需要解決./configure --prefix=/opt/mmseg [color=red]#下面安裝coreseek 需要此路徑[/color]sudo make && sudo make install#安裝coreseekcd ..cd csft-3.2.14sh buildconf.sh #輸出的warning資訊可以忽略,如果出現error則需要解決./configure --prefix=/opt/coreseek --without-unixodbc --with-mmseg --with-mmseg-includes=/opt/mmseg/include/mmseg/ --with-mmseg-libs=/opt/mmseg/lib/ --with-mysql ##如果提示mysql問題,可以查看MySQL資料來源安裝說明sudo make && sudo make installcd ..#然後建立命令捷徑,方便使用sudo ln -s /opt/coreseek/bin/indexer /usr/local/bin/indexersudo ln -s /opt/coreseek/bin/indextool /usr/local/bin/indextoolsudo ln -s /opt/coreseek/bin/search /usr/local/bin/searchsudo ln -s /opt/coreseek/bin/searchd /usr/local/bin/searchdsudo ln -s /opt/coreseek/bin/spelldump /usr/local/bin/spelldump
整合到rails項目
這裡用的是gem thinking-sphinx
如果沒有新項目自己建立Rails項目,這裡不做說明。
可以參考Railscasts: http://railscasts.com/episodes/120-thinking-sphinx?autoplay=true
安裝 thinking-sphinx
官方手冊:http://freelancing-god.github.com/ts/en/installing_thinking_sphinx.html
這裡只說明下 rails 3.0
在Gemfile中加入
gem 'thinking-sphinx'
然後
bundle install
可以參考快速實現:http://freelancing-god.github.com/ts/en/quickstart.html
在model中定義索引
class Post < ActiveRecord::Basedefine_index doindexes :title, :body#聲明使用即時索引set_property :delta => true #注意這句一定要加,否則添加了記錄不會自動索引endend
記得添加即時索引欄位 delta
ruby script/generate migration add_delta_to_posts delta:boolean
class AddDeltaToPosts < ActiveRecord::Migrationdef self.upadd_column :posts, :delta,:boolean, :default => true, :null => falseenddef self.downremove_column :posts, :deltaendend
rake db:migrate
在controller中加search代碼,controler大家自己建啦。
class FullTextSearchController < ApplicationControllerdef searchper_page = params[:limit].to_ipage = (params[:start].to_i / per_page) + 1total_count = ThinkingSphinx.count(params[:query], :classes => [Post], :page => page, :per_page => per_page)@results = ThinkingSphinx.search(params[:query], :classes => [Post], :page => page, :per_page => per_page)respond_to do |format|format.json { render(:json => {:total => total_count, :success => true,:items => @results.map{ |i| {:id => i.id, :title => i.title, :body => i.body} unless i.blank? },}.to_json)}endendend
#建立rails項目全文檢索搜尋資料目錄cd 你的rails目錄mkdir fulltext_search_data#從安裝包中複製字典到Rails項目cp /tmp/coreseek-3.2.14/mmseg-3.2.14/data/*.* 你的rails目錄/fulltext_search_data#建立設定檔:vi config/sphinx.yml#內容如下:test:bin_path: /opt/coreseek/binmem_limit: 128Mconfig_file: config/test.sphinx.confcharset_type: zh_cn.utf-8charset_dictpath: <%=::Rails.root.to_s + "/fulltext_search_data "%>pid_file: "/tmp/searchd.test.pid"ngram_len: 0development:bin_path: /opt/coreseek/binmem_limit: 128Mconfig_file: config/development.sphinx.confcharset_type: zh_cn.utf-8charset_dictpath: <%=::Rails.root.to_s + "/fulltext_search_data "%>pid_file: "/tmp/searchd.development.pid"ngram_len: 0production:bin_path: /opt/coreseek/binmem_limit: 128Mconfig_file: config/production.sphinx.confcharset_type: zh_cn.utf-8charset_dictpath: <%=::Rails.root.to_s + "/fulltext_search_data "%>pid_file: "/tmp/searchd.production.pid"ngram_len: 0#建立設定檔rake thinking_sphinx:configure#建立索引rake thinking_sphinx:index#開啟服務rake thinking_sphinx:start#現在可以先加些資料,在瀏覽器中訪問你的controller測試搜尋#也可以用如下命令來測試全文檢索搜尋引擎search '關鍵字' -c 你的Rsils項目/config/development.sphinx.conf
最後再補幾點注意事項:
1.定義索引時 "set_property :delta => true", 沒有這句新加的記錄不會索引。
2.coreseek 安裝包中的字典檔案一定要複製到Rails項目資料目錄,否則中文無法支援。
參考資料:
詳見:https://github.com/freelancing-god/thinking-sphinx
官方手冊:http://freelancing-god.github.com/ts/en/installing_thinking_sphinx.html
快速實現:http://freelancing-god.github.com/ts/en/quickstart.html