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
精彩评论