我正在尝试使用 Elasticsearch & Tire gem 索引一堆文档。除了记录不完整且没有引用的极少数情况外,索引工作得很好filename
。在这些情况下,记录中的任何内容都不会被索引,并且 Elasticsearch 会抛出一个NullPointerException
.
我不了解 Java,但想知道是否有办法将某些内容传递给 Attachment Type 插件,以便 Elasticsearch 成功索引其余记录并绕过:type => 'attachment'
whereattachment
返回null
?
尝试过"attachment" : {"type" : "attachment", "null_value" : "missing_file"}
(或用轮胎的说法:)indexes :attachment, :type => 'attachment', :null_value => "missing_file"
但这似乎没有任何区别。
#app/models/article.rb
class Article < ActiveRecord::Base
include Tire::Model::Search
include Tire::Model::Callbacks
attr_accessible :title, :content, :published_on, :filename
mapping do
indexes :id, :type =>'integer'
indexes :title
indexes :content
indexes :published_on, :type => 'date'
indexes :attachment, :type => 'attachment', :null_value => 'missing_file' # <== no worky
end
def to_indexed_json
to_json(:methods => [:attachment])
end
def attachment
if filename.present?
path_to_pdf = "/Volumes/HardDisk/sample_pdfs/#{filename}.pdf"
Base64.encode64(open(path_to_pdf) { |pdf| pdf.read })
end
end
end
#Elasticsearch log:
[2012-06-29 09:01:15,515][DEBUG][action.bulk ] [Skunge the Laxidazian Troll] [articles][4] failed to execute bulk item (index) index {[articles][article][10], source[{"content":"this title is fluff","created_at":"2012-06-27T22:02:43Z","filename":null,"id":10,"published_on":"2012-06-27","title":"Test title","updated_at":"2012-06-27T22:02:43Z","attachment":null}]}
java.lang.NullPointerException