brewster / elastictastic

Object-document mapper and lightweight API adapter for ElasticSearch
MIT License
88 stars 13 forks source link

Attachments support #19

Closed kostia closed 11 years ago

kostia commented 11 years ago

Hello,

me spamming again...

Is there any chance there will be attachment support? If not (or not in the near future): how can one achieve some kind of this functionality?

Thanks a lot!

kostia commented 11 years ago

Responding to my own question: some kind of that can be achieved with Carrierwave. Here is a simple uploader (without caching):

require 'carrierwave'
require 'carrierwave/validations/active_model'

module CarrierWave
  module Elastictastic
    include CarrierWave::Mount

    def mount_uploader(column, uploader=nil, options={}, &block)
      field options[:mount_on] || column

      super

      alias_method :read_uploader, :read_attribute
      alias_method :write_uploader, :write_attribute

      public :read_uploader
      public :write_uploader

      include CarrierWave::Validations::ActiveModel

      if uploader_option column.to_sym, :validate_integrity
        validates_integrity_of column 
      end

      if uploader_option column.to_sym, :validate_processing
        validates_processing_of column
      end

      after_save :"store_#{column}!"
      before_save :"write_#{column}_identifier"
      after_destroy :"remove_#{column}!"
    end 
  end 
end

Unfortunately it will only work for images and will not correctly index them.

outoftime commented 11 years ago

Does ES itself have support for attachments, in particular?

kostia commented 11 years ago

Yes, but optionally (available as a separate plugin). See here: http://www.elasticsearch.org/tutorials/2011/07/18/attachment-type-in-action.html

Great would be following:

class Applicant
  include Elastictastic::Document
  field :cv, type: :attachment
end

alice = Applicant.new cv: Base64.encode64(open('Alice_Johnson_CV.pdf'))
alice.save

Applicant.query(query_string: {query: 'Alice'}).first #=> #<Applicant id="...">
_.cv #=> "Tm93IGlzIHRoZSB0aW1lIGZvciBhbGwgZ29vZCBjb2..."
outoftime commented 11 years ago

I see. Looking over that tutorial, it would appear that attachments are just indexed like any other field -- I'm not sure what Elastictastic would need to do to support it explicitly? I have a feeling the sample code above should work with current Elastictastic...

kostia commented 11 years ago

Sorry, my fault. It works out of the box.