lib/supernova/solr_indexer.rb in supernova-0.4.13 vs lib/supernova/solr_indexer.rb in supernova-0.4.14

- old
+ new

@@ -69,13 +69,11 @@ self.options = options self.ids ||= :all end def index! - index_query(query_to_index) do |row| - map_for_solr(row) - end + index_query(query_to_index) end def map_for_solr(row) map_hash_keys_to_solr( if self.respond_to?(:extra_attributes_from_record) && self.class.clazz @@ -95,11 +93,12 @@ 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 hash.has_key?(field.to_s) + 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 @@ -173,29 +172,33 @@ def query_db(query) db.send(db.respond_to?(:query) ? :query : :select_all, query) end - def index_query(query, &block) - rows = query_db(query) + def solr_rows_to_index_for_query(query) + query_db(query).map do |row| + map_for_solr(row) + end + end + + def index_query(query) + rows = solr_rows_to_index_for_query(query) if self.max_rows_to_direct_index < rows.count - index_with_json_file(rows, &block) + index_with_json_file(rows) else - index_directly(rows, &block) + index_directly(rows) end end def index_directly(rows, &block) rows.each do |row| - row = yield(row) if block_given? row = Supernova::Solr.connection.add(row) end Supernova::Solr.connection.commit if rows.any? end def index_with_json_file(rows, &block) rows.each do |row| - row = yield(row) if block_given? write_to_file(row) end finish end \ No newline at end of file