Install full-text search Chinese Word Segmentation coreseek/sphek on Ubuntu and integrate with rails

Source: Internet
Author: User
Sphinx () should be well understood by everyone. I will not introduce it any more. If you do not know about it, You can Google your own children's shoes.

The native sphinx only supports Chinese characters,
Therefore, we will focus on coreseek that supports Chinese word segmentation.

Note: After coreseek 3.2, you only need to install coreseek. It integrates libmmseg and sphg and does not need to install native sphinx. (Install native sphenders and install patches before 3.2. This is very cumbersome)

Install coreseek

The following uses the coreseek-3.2.14 as an example, it is based on sph00000.99 (without installing sph00000.99)

Detailed official manual: http://www.coreseek.cn/products-install/install_on_bsd_linux/

Ubuntu-10.04 installation coreseek installation requires preinstalled software:

 
Sudo apt-Get install make GCC g ++ automake libtool mysql-client libmysqlclient15-dev libxml2-dev libexpat1-dev
 # download the coreseek-3.2.14, which already contains the mmsegcd/tmpwget http://www.coreseek.cn/uploads/csft/3.2/coreseek-3.2.14.tar.gztar xzvf coreseek-3.2.14.tar.gzcd # first install the mmsegcd coreseek-3.2.14. /Bootstrap # The output warning information can be ignored. If an error occurs, it must be resolved. /configure -- prefix =/opt/mmseg [color = Red] # This path [/color] sudo make & sudo make install # Install coreseekcd is required for coreseek installation .. CD csft-3.2.14sh buildconf. sh # The output warning information can be ignored. If an error occurs, it must be resolved. /configure -- prefix =/opt/coreseek -- without-unixodbc -- With-mmseg-separated des =/opt/mmseg/include/mmseg/-- With-mmseg-libs = /opt/mmseg/lib/-- With-mysql # If a MySQL problem is prompted, you can view the MySQL data source installation instructions sudo make & sudo make installcd .. # create a command shortcut, 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 

Integrated into rails Project

Here we use the gem thinking-sphking

If you do not have a new project to create your own rails project, it is not described here.
Can refer to railscasts: http://railscasts.com/episodes/120-thinking-sphinx? Autoplay = true

Install thinking-sphking
Official Manual: http://freelancing-god.github.com/ts/en/installing_thinking_sphinx.html

Here we will only describe rails 3.0

Add in gemfile

 
Gem 'thinking-sphsecret'

Then

 
Bundle install

See quick implementation: http://freelancing-god.github.com/ts/en/quickstart.html

Define indexes in Model

 
Class post <activerecord: basedefine_index doindexes: title,: body # declare to use real-time index set_property: Delta => true # note that this sentence must be added; otherwise, the added record will not automatically index endend.

Remember to add the real-time index field 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

Add search in ControllerCode, Controler.

 class fulltextsearchcontroller 
  
    [Post],: page => page,: per_page => per_page) @ Results = thinkingsphinx. search (Params [: Query],: classes => [Post],: page => page,: per_page => per_page) respond_to do | 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 
  
# Create a rails project full text search data directory CD your rails directory mkdir fulltext_search_data # copy the dictionary from the installation package to the rails project CP/tmp/coreseek-3.2.14/mmseg-3.2.14/data /*. * Your rails directory/fulltext_search_data # create a configuration file: VI config/sphinx. yml # Content: Test: bin_path:/opt/coreseek/binmem_limit: 128mconfig_file: config/test. sphworkflow. confcharset_type: zh_cn.utf-8charset_dictpath: <% =: rails. root. to_s + "/fulltext_search_data" %> pid_file: "/tmp/searchd. test. PID "ngram_len: 0 development: bin_path:/opt/coreseek/binmem_limit: 128mconfig_file: config/development. sphworkflow. confcharset_type: zh_cn.utf-8charset_dictpath: <% =: rails. root. to_s + "/fulltext_search_data" %> pid_file: "/tmp/searchd. development. PID "ngram_len: 0 Production: bin_path:/opt/coreseek/binmem_limit: 128mconfig_file: config/production. sphworkflow. confcharset_type: zh_cn.utf-8charset_dictpath: <% =: rails. root. to_s + "/fulltext_search_data" %> pid_file: "/tmp/searchd. production. PID "ngram_len: 0 # create the configuration file rake thinking_sphure: Configure # create the index rake thinking_sphinx: Index # enable the Service rake thinking_sphinx: start # add some data first, access your controller in the browser to test the search # You can also use the following command to test the full-text search engine 'search' keyword '-C your rsils Project/config/development. sphworkflow. conf

Finally, add the following considerations:
1. When the index is defined, "set_property: Delta => true" will not be indexed without the newly added record.
2. The dictionary file in the coreseek installation package must be copied to the rails project data directory. Otherwise, Chinese characters cannot be supported.

References:

See: https://github.com/freelancing-god/thinking-sphinx
Official Manual: http://freelancing-god.github.com/ts/en/installing_thinking_sphinx.html
Rapid implementation: http://freelancing-god.github.com/ts/en/quickstart.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.