我正在尝试在我的应用程序中使用ElasticSearch + Tire进行全文搜索。
目前,我有一个模型Entry,通过回形针has_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发布于 2012-11-06 08:25:21
我犯了几个愚蠢的打字错误,把事情搞砸了。我一定读过一篇文章,他们用不同的格式编写了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发布于 2016-04-08 22:23:21
你的方法散列在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]
endhttps://stackoverflow.com/questions/12718717
复制相似问题