Quell NullPointerException errors when 'attachment' not present

(Meltemi) #1

Using Tire gem to interface with Elasticsearch from w/in a Rails
application. FWIW- I'm new to both Tire & ES.

As I'm exploring Tire & ES I frequently make changes to the Index which I
reload with:

$ rake environment tire:import CLASS=Article FORCE=true

which appears to do a bulk import of some sort. Anyway, things seem to be
working fine and attachments are indexed (along with attributes from the
model) but some of the records don't, yet, have attachments associated with
them and the 'attachment' attribute in their instance currently returns
'null'. Problem (at least according to the log it's a problem) is that when
trying to index one of those records ES writes this in the 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

Is there something I could be returning (instead of a null value) when
'filename' is not present in the instance to make ES ignore the indexing of
the field or at least not complain about it?

appreciate any help!


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'

def to_indexed_json
to_json(:methods => [:attachment])

def attachment
if filename.present?
path_to_pdf = "/Volumes/Disk41402/Internal/IRRAD_large/sample_pdfs/#{filename}.pdf"
Base64.encode64(open(path_to_pdf) { |pdf| pdf.read })

(system) #2