Rails elasticsearch:存储回形针图像URL

时间:2018-12-06 19:51:43

标签: ruby-on-rails elasticsearch paperclip

我想将图像URL直接存储在我的Elasticsearch记录上,因此该程序无需通过ActiveRecrods进行其他调用。但是我在导入这些链接时遇到了问题。我的代码:

class Lead < ApplicationRecord
...
 has_attached_file :product_image, styles: { default: "650x400>", thumb: "150x120>" },
  default_url: "/images/:style/default_product_image.png"
  validates_attachment_content_type :product_image, content_type: /\Aimage\/.*\z/
end

映射:

settings index: { number_of_shards: 1 } do
        mappings dynamic: false do
          indexes :id, type: 'long'
          indexes :lead_status, type: 'keyword'
          indexes :country, type: 'keyword'
          indexes :city, type: 'keyword'
          indexes :title, analyzer: 'english', index_options: 'offsets'
          indexes :description_short, type: 'keyword'
          indexes :created_at, type: 'date'
          indexes :category do 
            indexes :name, type: 'keyword'
          end
          indexes :user do
            indexes :user_name, type: 'keyword'
            indexes :id, type: 'long'
          end
        end
     end

      #includes relations to elasticsearch indexed model
      def as_indexed_json(options = {})
        self.as_json(
          options.merge(
            only: [:id, :lead_status, :city, :title, :description_short, :created_at,
                :country, :product_image_file_name],
            include: { 
                category: { only: :name },
                user: { only: [:user_name, :id] },
                image: product_image.url // here is my url 
            }   
          )
        )
      end

尝试在我的rails控制台上调用“ Lead.import”会给我这个错误:

Traceback (most recent call last):
        2: from (irb):2
        1: from app/models/concerns/searchable.rb:80:in `as_indexed_json'
NoMethodError (undefined method `image' for #<Lead:0x00007ff7f447b820>)

我该如何解决?

0 个答案:

没有答案