论坛首页 入门讨论版 rails

请教ferret高亮问题?

浏览 218 次
该帖已经被评为新手帖
作者 正文
最后更新时间:2008-07-24 关键字: ferret 高亮

  目前现状,使用ferret + RMMSeg做全文搜索,目前可以搜索出结果,但是不知道怎么加入高亮的功能.请教大家了:

  Controller的代码:

  def search
    #说明:FriendInfo是一个实体,对应表:friend_info
    #   查询所有姓"崔"的人员信息
    @friend = FriendInfo.find_by_contents("崔~")       
  end 

    ActiveRecord的代码:

  #说明:使用rmmseg做分词进行检索
 analyzer = RMMSeg::Ferret::Analyzer.new { |tokenizer|
    Ferret::Analysis::LowerCaseFilter.new(tokenizer)
  } 
  $index = Ferret::Index::Index.new(:analyzer => analyzer)
  acts_as_ferret :field => {:username => {:store => :yes}} 

    目前可以查询出结果,参考了很多文档也不知道到底在哪里加入highlight,请教各位大侠,是在 ActiveRecord当中加呢,还是在其它地方(view),在网上找的资料多半没有完成我的需求.

  另外感觉ferret做索引的时间超长,跟Lucene真的无法相比,大家有什么好办法吗?

 

   
最后更新时间:2008-07-24
http://ferret.davebalmain.com/api/files/TUTORIAL.html

参考Highlighting一节
   
0 请登录后投票
最后更新时间:2008-07-24
index.search_each('date:( >= 20050905) content:(ruby OR rails)') do |id, score|
    puts "Document #{index[id][:title]} found with a score of #{score}"
    highlights = index.highlight("content:(ruby OR rails)", 0,
                                 :field => :content,
                                 :pre_tag = "\033[36m",
                                 :post_tag = "\033[m")
    puts highlights
  end

能不能请教一下:
其中search_each('date:( >= 20050905) content:(ruby OR rails)') do |id, score| 其中的变量是什么呀,比如我只有查询FriendInfo.username这个字段,我应该对以上的语句如何修改.
另外是在model也就是ActiveRecord当中加入这个代码吗?多谢呀.
   
0 请登录后投票
最后更新时间:2008-07-25

在view里面直接高亮搜索结果也可以:

 <p><%= highlight(@friend.contents,'崔~') %></p>
 

 

   
0 请登录后投票
最后更新时间:2008-07-25
首先感谢phoenix520的回复,不过可能不是我想要的结果.
这是利用TextHelper.highlight(text,phrase,'style格式'),这样的话,我只能够标示出符合phrase的结果,但是没有分词后的结果,参考一下我的实现:
 <p><%= highlight(friend.username,'崔燕','<span style="color:red;">\1</span>') %></p>

只能够标红:崔燕 的字段,但是如果把 崔 燕 崔燕(分词结果)标红的方法如何做呢?呵呵,请教大家中....
   
0 请登录后投票
论坛首页 入门讨论版 rails

跳转论坛:
JavaEye推荐