开发者

Document search in RoR

开发者 https://www.devze.com 2023-04-11 04:09 出处:网络
Here\'s the deal: I have a RoR application with bunch of database items (indexed via Sunspot), some of them with file attachments (typically PDFs and plaintext files). What\'s the easiest way to inclu

Here's the deal: I have a RoR application with bunch of database items (indexed via Sunspot), some of them with file attachments (typically PDFs and plaintext files). What's the easiest way to include content of those docu开发者_C百科ments in fulltext search?


Use a PDF reader gem like pdf-reader, and index it in Sunspot.

class Item < ActiveRecord::Base
  searchable if: proc{ |topic| topic.try(:price).try(:>,0) } do
    text    :attachment_text  # index result returned from attachment() method
  end

  # getting text out of attachment
  def attachment_text
    # pseudo code of determining attachment format
    case attachment.extension
    when :pdf
      # Use pdf-reader gem get all the text from all pages
    when :txt
      return open(attachment).read()
    end
  end
end
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号