开发者

Document search in RoR

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜