在ubuntu上安裝全文搜索中文分詞Coreseek/sphinx 及和Rails集成

Sphinx(獅身人面像) 想必大家都比較瞭解,就不作介紹了,不瞭解的童鞋可以自己Google。

原生Sphinx不支持中文,
所以這裏重點介紹支持中文分詞的 Coreseek。

[color=red]注意:Coreseek 3.2 後,只有安裝 Coreseek 就可以了,它對LibMMSeg和sphinx做了整合,不用再安裝原生Sphinx。(3.2前是要安裝原生Sphinx,還要裝補丁,非常繁瑣)[/color]

[b]安裝coreseek[/b]

下面以coreseek-3.2.14爲例,它基於Sphinx 0.99(不用安裝Sphinx 0.99)

詳細官方手冊:[url]http://www.coreseek.cn/products-install/install_on_bsd_linux/[/url]

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,裏面已經包含 mmseg
cd /tmp
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=/opt/mmseg [color=red]#下面安裝coreseek 需要此路徑[/color]
sudo make && sudo make install

#安裝coreseek
cd ..
cd csft-3.2.14
sh 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 install
cd ..

#然後建立命令快捷方式,方便使用
sudo ln -s /opt/coreseek/bin/indexer /usr/local/bin/indexer
sudo ln -s /opt/coreseek/bin/indextool /usr/local/bin/indextool
sudo ln -s /opt/coreseek/bin/search /usr/local/bin/search
sudo ln -s /opt/coreseek/bin/searchd /usr/local/bin/searchd
sudo ln -s /opt/coreseek/bin/spelldump /usr/local/bin/spelldump



[b]集成到rails項目[/b]

這裏用的是gem thinking-sphinx

如果沒有新項目自己創建Rails項目,這裏不做說明。
可以參考Railscasts: [url]http://railscasts.com/episodes/120-thinking-sphinx?autoplay=true[/url]

安裝 thinking-sphinx
官方手冊:[url]http://freelancing-god.github.com/ts/en/installing_thinking_sphinx.html[/url]

這裏只說明下 rails 3.0

在Gemfile中加入
gem 'thinking-sphinx'


然後
bundle install



可以參考快速實現:[url]http://freelancing-god.github.com/ts/en/quickstart.html[/url]

在model中定義索引
class Post < ActiveRecord::Base
define_index do
indexes :title, :body

#聲明使用實時索引
set_property :delta => true #注意這句一定要加,否則添加了記錄不會自動索引
end
end


記得添加實時索引字段 delta

ruby script/generate migration add_delta_to_posts delta:boolean



class AddDeltaToPosts < ActiveRecord::Migration
def self.up
add_column :posts, :delta,:boolean, :default => true, :null => false
end

def self.down
remove_column :posts, :delta
end
end


rake db:migrate


在controller中加search代碼,controler大家自己建啦。
class FullTextSearchController < ApplicationController

def search
per_page = params[:limit].to_i
page = (params[:start].to_i / per_page) + 1
total_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)}
end
end
end



#創建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/bin
mem_limit: 128M
config_file: config/test.sphinx.conf
charset_type: zh_cn.utf-8
charset_dictpath: <%=::Rails.root.to_s + "/fulltext_search_data "%>
pid_file: "/tmp/searchd.test.pid"
ngram_len: 0
development:
bin_path: /opt/coreseek/bin
mem_limit: 128M
config_file: config/development.sphinx.conf
charset_type: zh_cn.utf-8
charset_dictpath: <%=::Rails.root.to_s + "/fulltext_search_data "%>
pid_file: "/tmp/searchd.development.pid"
ngram_len: 0
production:
bin_path: /opt/coreseek/bin
mem_limit: 128M
config_file: config/production.sphinx.conf
charset_type: zh_cn.utf-8
charset_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



最後再補幾點注意事項:
[color=red]1.定義索引時 "set_property :delta => true", 沒有這句新加的記錄不會索引。
2.coreseek 安裝包中的字典文件一定要複製到Rails項目數據目錄,否則中文無法支持。[/color]

[b]參考資料:[/b]

詳見:[url]https://github.com/freelancing-god/thinking-sphinx[/url]
官方手冊:[url]http://freelancing-god.github.com/ts/en/installing_thinking_sphinx.html[/url]
快速實現:[url]http://freelancing-god.github.com/ts/en/quickstart.html[/url]
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章