利用acts_as_ferret實現全文檢索索引

來源:互聯網
上載者:User

acts_as_ferret是在Rails中實現全文檢索索引的外掛程式,它的實現基於Ferret,Ferret是Apache Lucene的ruby介面。有關acts_as_ferret的介紹網上很多,教程也很多,是早期rails最重要的全文檢索索引外掛程式,不過要老外支援中文檢索可謂是天荒地潭,javaeye中講述中文支援的實現也不盡人意,而且都因年代久遠,日益失去參考價值了。鑒此,我在放棄使用acts_as_ferret之前,詳細介紹一下如何利用acts_as_ferret實現中文的全文檢索索引吧,算是一個備案,或許未來會有用到它的時候。

獨立安裝gem

不要以外掛程式方式安裝,也不要unpack到你的項目下,原因不大清楚,但這是我血的教訓總結出來的!

gem install ferret -v=0.11.5 --platform mswin32
gem install acts_as_ferret
gem install rmmseg

然後把C:\ruby\lib\ruby\gems\1.8\gems\ferret-0.11.5-x86-mswin32\ext目錄下的ferret_ext.so複製到C:\ruby\lib\ruby\gems\1.8\gems\ferret-0.11.5-x86-mswin32\lib下!

關聯gem

在你的項目的environment.rb添加

config.gem 'ferret'
  config.gem 'rmmseg'
  config.gem 'acts_as_ferret'

為模型添加索引支援require 'rmmseg'
require 'rmmseg/ferret'
class Topic < ActiveRecord::Base
  #……………………………………………………其他實現………………………………………………………………
  #如果想重建立立索引,只需要刪除對應的檔案夾,並重啟服務,也可以使用Model.rebuild_index方法
  #=======================搜尋部分=====================
  acts_as_ferret({
    :fields => {
     :title => {
      :store => :yes,
      :boost=> 20 #設定權重
     },
     :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
#……………………………………………………其他實現………………………………………………………………
end

require 'rmmseg'
require 'rmmseg/ferret'
class Post < ActiveRecord::Base
  #……………………………………………………其他實現………………………………………………………………
  delegate :title, :to => :topic

  #如果想重建立立索引,只需要刪除對應的檔案夾,並重啟服務,也可以使用Model.rebuild_index方法
  #=======================搜尋部分=====================
  acts_as_ferret({
    :fields => {
     :title => {
      :store => :yes,
      :boost=> 20 #設定權重
     },
     :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
#……………………………………………………其他實現………………………………………………………………
end

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.