Closed kostia closed 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.
Does ES itself have support for attachments, in particular?
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..."
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...
Sorry, my fault. It works out of the box.
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!