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

摘要:
斯芬克斯必须让每个人都理解,所以我们就不介绍了。我们不理解的童鞋可以自己谷歌。Native Sphinx只支持中文,所以这里我们重点关注Coresearch,它支持中文分词。注意:在Corescape 3.2之后,只能安装Corescape。它集成了LibMMSeg和sphinx,无需安装原生sphinx。
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

免责声明:文章转载自《在ubuntu上安装全文搜索中文分词Coreseek/sphinx及和Rails集成》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇C# 32位程序,申请大内存,附dome(wpf),亲测可用Linq的使用下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

MongoDB索引(7)

索引是对数据库表中一列或多列的值进行排序的一种结构,可以让我们查询数据库变得 更快。MongoDB 的索引几乎与传统的关系型数据库一模一样,这其中也包括一些基本的查询优化技巧。 增加检索的效率. mongodb提供了索引的支持.(越来越倾向于关系型数据库) 通常建立索引的, 基本要求, 就是文档的结构要尽可能一致. 普通单列索引 语法: db.集合名.en...

设计一个文本搜索引擎

博客中的文章均为meelo原创,请务必以链接形式注明本文地址 搜索引擎是一个十分神秘的事物,因为它铸造了google和百度两大传奇互联网公司。过去流传一种说法,世界上只有4个国家掌握了搜索引擎的核心技术,那就是美国、中国、俄罗斯和韩国,分别对应Google、百度、naver和yandex。曾经有国有背景的即刻搜索想承担起国家战略,国有企业在能源、基础设施...

【SQL server初级】数据库性能优化三:程序操作优化

  数据库优化包含以下三部分,数据库自身的优化,数据库表优化,程序操作优化.此文为第三部分   数据库性能优化三:程序操作优化 概述:程序访问优化也可以认为是访问SQL语句的优化,一个好的SQL语句是可以减少非常多的程序性能的,下面列出常用错误习惯,并且提出相应的解决方案     一、操作符优化   1. IN、NOT IN 操作符   IN和EXIS...

Elasticsearch中文分词加拼音

网上可能有很多教程,我写这个只是记录一下自己学习的过程,给自己看的 。 中文分司网上搜了一下,用的IK分词器(https://github.com/medcl/elasticsearch-analysis-ik),拼音搜索插件用的是拼音分词器(https://github.com/medcl/elasticsearch-analysis-pinyin)。...

Mongodb 笔记03 查询、索引

查询 1. MongoDB使用find来进行查询。find的第一个参数决定了要返回哪些文档,这个参数是一个文档,用于指定查询条件。空的查询会匹配集合的全部内容。要是不指定查询,默认是{}。 2.可以通过find的第二个参数来指定想要的键。这样即会节省传输的数量,又能节省客户端解码文档的时间和内存消耗。举例:db.users.find({},{"userna...

ElasticSearch 安装与配置 (windows)

新工作需要所以开始学一下。。。 elasticsearch的概念和基本原理: elasticsearch是一个类似于nosql数据库的东西,基于Lucene,以json格式储存数据,采用了倒序索引(也叫反向索引),主要用于信息抓取(我的感觉就是字符串查找),所以主要应用在搜索引擎和自然语言处理方面。 比如有30篇文章,我想找一个字符串出现的位置,普通情况我...