2012-10-04 20 views
0

我試圖在我的應用中使用ElasticSearch + Tire進行全文搜索。Elasticsearch + Tire + attachment-mapper + Paperclip = No Hits

目前,我有一個模型Entryhas_attached_file通過回形針。我已安裝並運行最新版本的ElasticSearch/Tire,並且還安裝了附件映射插件。

如果我的查詢是任何可以在其他領域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 

回答

0

我有幾個錯別字啞,這是搞亂事情了。我必須閱讀一篇文章,他們以不同的格式編寫了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 
0

您的方法散列位於json中,如果您希望該方法實際命中,應該在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 
相關問題