Search code examples
indexingattachmentelasticsearchtire

Elasticsearch + Tire + attachment-mapper + Paperclip = No Hits


I'm trying to use ElasticSearch + Tire for full-text search in my app.

Currently I have a model Entry that has_attached_file via Paperclip. I've installed and have running the latest version of ElasticSearch/Tire and also installed the Attachment-mapping plugin.

If my query is for anything that can be found in one of the other Entry fields it works flawlessly. I tried to run rake environment tire:import CLASS="Entry" to update the index but i get

** Invoke environment (first_time)
** Execute environment
** Invoke tire:import (first_time)
** Execute tire:import
[IMPORT] Starting import for the 'Entry' class
--------------------------------------------------------------------------------
7/7 | 100% rake aborted!##############################################
stack level too deep
/home/capuser/.rvm/gems/ruby-1.9.2-p320/gems/rake-0.9.2.2/lib/rake/task.rb:162
Tasks: TOP => tire:import

I have a feeling the issue is either in encoding the file or in my to_indexed_json function.

Here's some code:

class Entry < ActiveRecord::Base
  include Tire::Model::Search
  include Tire::Model::Callbacks

  has_attached_file :document,
                :url  => "/assets/entries/:id/:basename.:extension",
                :path => ":rails_root/public/assets/entries/:id/:basename.:extension"

  before_post_process :image?

  validates_presence_of :entry_type

  attr_accessible :description, :title, :url, :category_ids, :subcategory_ids, :entry_type_id, :document

  mapping do
    indexes :title
    indexes :description
    indexes :categories do
      indexes :name
    end
    indexes :subcategories do
      indexes :name
    end
    indexes :entry_type
    indexes :document, :type => 'attachment'
  end

 def to_indexed_json  
    {
    :title          => title,
    :description    => description,
    :categories     => categories.map { |c| { :name => c.name}},
    :subcategories  => subcategories.map { |s| { :name => s.name}},
    :entry_type     => entry_type_name,
    :document       => attachment
    }.to_json
  end


  def self.search(params)
    tire.search(load: true) do
      query { string params[:query], default_operator: "AND" } if params[:query].present?
    end
  end

  def attachment
    if document.present?
      path_to_document = "#{RAILS_ROOT}/app/assets/#{document}"
      Base64.encode64(open(path_to_document) { |pdf| pdf.read})
    end
  end
end

Solution

  • I had a few dumb typos, that was messing things up. I must have read an article where they wrote up the to_indexed_json function in a different format and I got confused. I fixed it right before I wrote up the question so this is what i had before.

     def to_indexed_json  
        {
        :title          => title,
        :description    => description,
        :categories     => categories.map { |c| { :name => c.name}},
        :subcategories  => subcategories.map { |s| { :name => s.name}},
        :entry_type     => entry_type_name,
        :methods        => [:attachment]
        }.to_json
      end