module Jekyll::Algolia::Indexer
def self.update_records(index_name, old_records_ids, new_records)
update
Note: All operations will be done in one batch, assuring an atomic
new_records - Records to add to the index
old_records_ids - Ids of records to delete from the index
index_name - The Algolia index
Public: Update records of the index
def self.update_records(index_name, old_records_ids, new_records) # Stop if nothing to change if old_records_ids.empty? && new_records.empty? Logger.log('I:Nothing to index. Your content is already up to date.') return end Logger.log("I:Updating records in index #{index_name}...") Logger.log("I:Records to delete: #{old_records_ids.length}") Logger.log("I:Records to add: #{new_records.length}") return if Configurator.dry_run? operations = new_records.map do |new_record| { action: 'addObject', indexName: index_name, body: new_record } end old_records_ids.each do |object_id| operations << { action: 'deleteObject', indexName: index_name, body: { objectID: object_id } } end # Run the batches in slices if they are too large batch_size = Configurator.algolia('indexing_batch_size') operations.each_slice(batch_size) do |slice| begin ::Algolia.batch!(slice) rescue StandardError => error records = slice.map do |record| record[:body] end ErrorHandler.stop(error, records: records) end end end