Class: Solrizer::Fedora::Indexer

Inherits:
Object
  • Object
show all
Defined in:
lib/solrizer/fedora/indexer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Indexer

This method performs initialization tasks



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/solrizer/fedora/indexer.rb', line 19

def initialize( opts={} )
  @@index_list = false unless defined?(@@index_list)
  @extractor = ::Solrizer::Extractor.new
  
  if opts[:index_full_text] == true || opts[:index_full_text] == "true"
    @index_full_text = true 
  else
    @index_full_text = false 
  end
  
  connect
end

Instance Attribute Details

#extractorObject

The extractor to use. This is usually Solrizer::Extractor



11
12
13
# File 'lib/solrizer/fedora/indexer.rb', line 11

def extractor
  @extractor
end

#index_full_textObject

Boolean or “true” or “false”

tells the indexer whether to index full text or just field values



14
15
16
# File 'lib/solrizer/fedora/indexer.rb', line 14

def index_full_text
  @index_full_text
end

#solrObject

The instance of solr that updates will be written to



8
9
10
# File 'lib/solrizer/fedora/indexer.rb', line 8

def solr
  @solr
end

Instance Method Details

#create_document(obj) ⇒ Object

This method creates a Solr-formatted XML document



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/solrizer/fedora/indexer.rb', line 141

def create_document( obj )        
  
  solr_doc = Hash.new
  
  model_klazz_array = ActiveFedora::ContentModel.known_models_for( obj )
  model_klazz_array.delete(ActiveFedora::Base)
  
  # If the object was passed in as an ActiveFedora::Base, call to_solr in order to get the base field entries from ActiveFedora::Base
  # Otherwise, the object was passed in as a model instance other than ActiveFedora::Base,so call its to_solr method & allow it to insert the fields from ActiveFedora::Base
  if obj.class == ActiveFedora::Base
    solr_doc = obj.to_solr(solr_doc)
    logger.debug "  added base fields from #{obj.class.to_s}"
  else
    solr_doc = obj.to_solr(solr_doc)
    model_klazz_array.delete(obj.class)
    logger.debug "    added base fields from #{obj.class.to_s} and model fields from #{obj.class.to_s}"
  end
 
  # Load the object as an instance of each of its other models and get the corresponding solr fields
  # Include :model_only=>true in the options in order to avoid adding the metadata from ActiveFedora::Base every time.
  model_klazz_array.each do |klazz|
    instance = obj.adapt_to(klazz)
    solr_doc = instance.to_solr(solr_doc, :model_only=>true)
    logger.debug "  added solr fields from #{klazz.to_s}"
  end
  
  ::Solrizer::Extractor.insert_solr_field_value(solr_doc, :id_t, "#{obj.pid}" )
  ::Solrizer::Extractor.insert_solr_field_value(solr_doc, :id, "#{obj.pid}" ) unless solr_doc[:id]
  
  return solr_doc
end

#generate_dates(solr_doc) ⇒ Object

This method generates the month and day facets from the date_t in solr_doc



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/solrizer/fedora/indexer.rb', line 100

def generate_dates(solr_doc)
  
  # This will check for valid dates, but it seems most of the dates are currently invalid....
  #date_check =  /^(19|20)\d\d([- \/.])(0[1-9]|1[012])\2(0[1-9]|[12][0-9]|3[01])/

 #if there is not date_t, add on with easy-to-find value
 if solr_doc[:date_t].nil?
      ::Solrizer::Extractor.insert_solr_field_value(solr_doc, :date_t, "9999-99-99")
 end #if

  # Grab the date value from date_t regardless of wheter it is inside of an array
  # then convert it to a Date object
  date_value =    solr_doc[:date_t]
  if date_value.kind_of? Array
    date_value = date_value.first
  end
  date_obj = Date._parse(date_value)
  
  if date_obj[:mon].nil? 
     ::Solrizer::Extractor.insert_solr_field_value(solr_doc, :month_facet, "99")
  elsif 0 < date_obj[:mon] && date_obj[:mon] < 13
    ::Solrizer::Extractor.insert_solr_field_value(solr_doc, :month_facet, date_obj[:mon].to_s.rjust(2, '0'))
  else
    ::Solrizer::Extractor.insert_solr_field_value(solr_doc, :month_facet, "99")
  end
    
  if  date_obj[:mday].nil?
    ::Solrizer::Extractor.insert_solr_field_value(solr_doc, :day_facet, "99")
  elsif 0 < date_obj[:mday] && date_obj[:mday] < 32   
    ::Solrizer::Extractor.insert_solr_field_value(solr_doc, :day_facet, date_obj[:mday].to_s.rjust(2, '0'))
  else
     ::Solrizer::Extractor.insert_solr_field_value(solr_doc, :day_facet, "99")
  end
  
  return solr_doc
      
end

#index(obj) ⇒ Object

This method adds a document to the Solr search index



176
177
178
179
# File 'lib/solrizer/fedora/indexer.rb', line 176

def index( obj )
  solr.add( create_document( obj ) )
  solr.commit
end

#load_fallback_configObject



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/solrizer/fedora/indexer.rb', line 77

def load_fallback_config
  config_path = File.join("config","solr.yml")
  unless File.exist?(config_path)
    config_path = File.join(File.dirname(__FILE__), "..", "..", "..", "config", "solr.yml")
  end
  logger.debug "SOLRIZER: reading config from " + config_path.inspect 
  yaml = YAML.load(File.open(config_path))

  if ENV["environment"].nil?
    environment = "development"
  else
    environment = ENV["environment"]
  end #if

  solr_config = yaml[environment].symbolize_keys
  logger.debug "SOLRIZER solr_config:" + solr_config.inspect
  solr_config
end

#load_rails_configObject



69
70
71
72
73
74
75
# File 'lib/solrizer/fedora/indexer.rb', line 69

def load_rails_config
  config_path = File.join(Rails.root.to_s, "config", "solr.yml")
  yaml = YAML.load(File.open(config_path))
  solr_config = yaml[Rails.env].symbolize_keys
  logger.debug solr_config.inspect
  solr_config
end