lib/supernova/solr_indexer.rb in supernova-0.3.12 vs lib/supernova/solr_indexer.rb in supernova-0.3.13

- old
+ new

@@ -70,18 +70,47 @@ self.ids ||= :all end def index! index_query(query_to_index) do |row| - row_to_solr(row) + map_for_solr(row) end end - def row_to_solr(row) + def map_for_solr(row) + map_hash_keys_to_solr( + if self.respond_to?(:extra_attributes_from_record) && self.class.clazz + row.merge(self.extra_attributes_from_record(Supernova.build_ar_like_record(self.class.clazz, row)).stringify_keys) + elsif self.respond_to?(:row_to_solr) + puts "DEPRECATION WARNING: use before_index instead of row_to_solr! in #{caller.first}" + self.row_to_solr(row) + else + self.before_index(row) + end + ) + end + + def before_index(row) row end + def map_hash_keys_to_solr(hash) + hash["indexed_at_dt"] = Time.now.utc.iso8601 + hash["id_s"] = [self.class.table_name, hash["id"]].compact.join("/") if hash["id"] + self.class.field_definitions.each do |field, options| + if value = hash.delete(field.to_s) + if options[:type] == :date + value = Time.utc(value.year, value.month, value.day) if value.is_a?(Date) + value = value.utc.iso8601 + end + hash["#{field}_#{self.class.suffix_from_type(options[:type])}"] = value + end + end + hash["type"] = self.class.clazz.to_s if self.class.clazz + hash + end + def table_name self.class.table_name || (self.class.clazz && self.class.clazz.respond_to?(:table_name) ? self.class.clazz.table_name : nil) end def query_to_index @@ -97,11 +126,11 @@ fields end def defined_fields self.class.field_definitions.map do |field, options| - sql_column_from_field_and_type(field, options[:type]) if options[:virtual] != true + field.to_s if options[:virtual] != true end.compact end def select_fields default_fields + defined_fields @@ -217,9 +246,10 @@ %(curl -s '#{solr_url}/update/json?commit=true\\&stream.file=#{index_file_path}') else %(cd #{File.dirname(index_file_path)} && curl -s '#{solr_url}/update/json?commit=true' --data-binary @#{File.basename(index_file_path)} -H 'Content-type:application/json') end out = Kernel.send(:`, cmd) - FileUtils.rm_f(self.index_file_path) if out.to_s.include?(%(<int name=\"status\">0</int>)) + raise "unable to index #{index_file_path}: #{out}" if !out.to_s.include?(%(<int name=\"status\">0</int>)) + FileUtils.rm_f(self.index_file_path) out end end \ No newline at end of file