在ubuntu上装配全文搜索中文分词Coreseek/sphinx 及和Rails集成
在ubuntu上安装全文搜索中文分词Coreseek/sphinx 及和Rails集成
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安装需要预装的软件:
集成到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中加入
然后
可以参考快速实现:http://freelancing-god.github.com/ts/en/quickstart.html
在model中定义索引
记得添加实时索引字段 delta
在controller中加search代码,controler大家自己建啦。
最后再补几点注意事项:
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
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,里面已经包含 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
集成到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::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
最后再补几点注意事项:
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
1 楼
helloqidi
2012-04-17
谢谢博主分享