【问题标题】:Elasticsearch + Tire + attachment-mapper + Paperclip = No HitsElasticsearch + 轮胎 + 附件映射器 + 回形针 = 无命中
【发布时间】:2012-09-24 23:37:45
【问题描述】:

我正在尝试在我的应用中使用 ElasticSearch + Tire 进行全文搜索。

目前我有一个模型 Entry 那个 has_attached_file 通过回形针。我已经安装并运行了最新版本的 ElasticSearch/Tire,还安装了 Attachment-mapping 插件。

如果我的查询是针对可以在其他 Entry 字段之一中找到的任何内容,则它可以完美运行。我试图运行rake environment tire:import CLASS="Entry" 来更新索引,但我得到了

** 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

我感觉问题出在编码文件或我的 to_indexed_json 函数中。

这里有一些代码:

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

【问题讨论】:

    标签: indexing attachment elasticsearch tire


    【解决方案1】:

    我有一些愚蠢的错别字,这把事情搞砸了。我一定读过一篇文章,他们用不同的格式编写了 to_indexed_json 函数,我很困惑。我在写问题之前就修复了它,所以这是我以前的。

     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
    

    【讨论】:

      【解决方案2】:

      如果您希望方法实际命中,您的方法哈希值在 json 中,应该在它之外。像这样:

      def to_indexed_json  
        only: {
          :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]
      end
      

      【讨论】:

        猜你喜欢
        • 2013-07-21
        • 2012-10-26
        • 2013-02-28
        • 2012-06-30
        • 2014-07-11
        • 2013-09-26
        • 2013-07-23
        • 2012-03-29
        • 1970-01-01
        相关资源
        最近更新 更多