zoukankan      html  css  js  c++  java
  • 在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安装需要预装的软件
    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
  • 相关阅读:
    matplotlib实战
    matplotlib常用操作2
    matplotlib 常用操作
    pandas总结
    朴素贝叶斯算法python实现
    什么叫“回归”——“回归”名词的由来&&回归与拟合、分类的区别 && 回归分析
    Latex常用整理
    准备尝试openFrameworks
    常用工具库总结
    K-Means和K Nearest Neighbor
  • 原文地址:https://www.cnblogs.com/wycg1984/p/2214547.html
Copyright © 2011-2022 走看看