Full-text search using acts_as_ferret

Source: Internet
Author: User

Acts_as_ferret is a plug-in for full-text search in rails. It is implemented based on ferret and ferret is the ruby interface of Apache Lucene. Acts_as_ferret has many introductions on the Internet and many tutorials. It is the most important full-text search plug-in for rails in the early days. However, it is a waste of time to support Chinese search by foreigners, the implementation of Chinese support in javaeye is also unsatisfactory, and the reference value is increasingly lost due to its long history. Therefore, before giving up using acts_as_ferret, I would like to introduce in detail how to use acts_as_ferret for full-text retrieval in Chinese. It is a record filing and may be used in the future.

Install gem independently

Do not install plug-ins or unpack them into your project. The reason is not clear, but this is my blood lesson!

 
Gem install ferret-V = 0.11.5 -- platform mswin32gem install acts_as_ferretgem install rmmseg

ThenC: \ Ruby \ Lib \ Ruby \ gems \ 1.8 \ gems \ ferret-0.11.5-x86-mswin32 \ extCopying ferret_ext.so in the directoryC: \ Ruby \ Lib \ Ruby \ gems \ 1.8 \ gems \ ferret-0.11.5-x86-mswin32 \ LibNext!

Associated gem

Add in environment. RB of your project

Config. Gem 'ferret 'config. G' rmmseg 'config. G' acts _ as_ferret'
Add index support for models
 
Require 'rmmseg' require 'rmmseg/ferret 'class topic {: Title =>{: store =>: Yes,: boost => 20 # Set weight },: body =>{: boost => 1,: store =>: Yes,: term_vector =>: with_positions_offsets},: Author =>{: store =>: Yes },: created_at_s =>{: index =>: untokenized,: store =>: Yes},: updated_at_s =>{: index =>: untokenized,: store =>: Yes }},: store_class_name => true,: analyzer => rmmseg: ferret: analyzer. new }) Def created_at_s created_at.to_s (: DB) end def updated_at_s updated_at.to_s (: DB) end def body first_post.body end #............................................................ Other implementations ........................................................................ End
 require 'rmmseg' require 'rmmseg/ferret 'class post: topic # If you want to re-create an index, you only need to delete the corresponding folder and restart the service. You can also use the model. rebuild_index method #================================ search part ================ ========= acts_as_ferret ({: fields =>{: title =>{: store =>: Yes,: boost => 20 # Set weight},: Body =>{: boost => 1 ,: store =>: Yes,: term_vector =>: with_positions_offsets},: Author => {: store =>: Yes },: Created_at_s =>{: index =>: untokenized,: store =>: Yes},: updated_at_s =>{: index =>: untokenized,: store =>: yes }},: store_class_name => true,: analyzer => rmmseg: ferret: analyzer. new}) def created_at_s created_at.to_s (: DB) end def updated_at_s updated_at.to_s (: DB) end #............................................................ Other implementations ........................................................................ End 

Here: analyzer => rmmseg: ferret: analyzer. New adds the Chinese word segmentation capability for us.

Create search Module
 
Ruby script/generate controller search index

Add routing rules.

 
Map. Online '/seach',: controller => 'seach ',: Action => 'index'

Modify search_controller.

Class searchcontroller

Modify the corresponding View:

<% Form_tag '/search',: method =>: Get,: Style => "margin-left: 40% "do %> <input type =" radio "name =" class "value =" topic "<% = @ class =" topic "? 'Checked = "checked "': ''%> only subject posts <input type =" radio "name =" class "value =" Post "<% = @ class =" Post "? 'Checked = "checked" ': ''%> All posts <br> <p> <% = text_field_tag: Query, @ query %> <% = submit_tag" Search ",: Name => nil %> </P> <% end %> <% if defined? @ Results %> <style type = "text/CSS">. hilite {color: # 0042bd; Background: # f345cc ;}</style> <Div id = "search_result" ><%@ results. each do | result | %> 

Another highlight scheme.

 
Def hilight (a, B) # A is the string to be highlighted, B is the highlighted part, and the highlighted style is hilite highlight a, B ,'\ 1'End 250), @ query %>

Author: posting time

Paging

Add application_controller.rb

 
Def pages_for (result, Options = {}) page, per_page, total = (options [: Page] | 1), (options [: per_page] | 30 ), (result. total_hits | 0) page_total = page * per_page Index = (page. to_ I-1) * per_page returning willpaginate: collection. new (page, per_page, total) Do | pager. replace result [index, per_page] end

Modify controller:

Class searchcontroller 3,: page => (Params [: Page] | 1) else Results = post. find_with_ferret @ query @ Results = pages_for (results,: per_page => 3,: page => (Params [: Page] | 1) end endend

Add a sentence at the bottom of the corresponding view (using the will_paginate plug-in)

<% Form_tag '/search',: method =>: Get,: Style => "margin-left: 40% "do %> <input type =" radio "name =" class "value =" topic "<% = @ class =" topic "? 'Checked = "checked "': ''%> only subject posts <input type =" radio "name =" class "value =" Post "<% = @ class =" Post "? 'Checked = "checked" ': ''%> All posts <br> <p> <% = text_field_tag: Query, @ query %> <% = submit_tag" Search ",: Name => nil %> </P> <% end %> <% if defined? @ Results %> <style type = "text/CSS">. hilite {color: # 0042bd; Background: # f345cc ;}</style> <Div id = "search_result" ><%@ results. each do | result | %> Product Environment
 
   
    • In the model, acts_as_ferret: Remote => true indicates that remote is true.
    • Copy the ferret_server.yml in the directory of Vendor/plugins/acts_as_ferret/config/to config /.
    • Ruby script/runner vendor/plugins/acts_as_ferret/script/ferret_server-e Production
Some useful links

Http://www.pluitsolutions.com/2007/07/30/acts-as-ferret-drbserver-win32-service/

Http://ferret.davebalmain.com/api/classes/Ferret/Index.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.