Skip to content

Commit

Permalink
automatically update doi indexed column. #116
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Fenner committed Oct 15, 2018
1 parent 3372dea commit 9562477
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 7 deletions.
9 changes: 3 additions & 6 deletions app/models/concerns/indexable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,9 @@ module Indexable
after_commit on: [:create, :update] do
# use index_document instead of update_document to also update virtual attributes
IndexJob.perform_later(self)
if self.class.name == "Doi" && !Rails.env.test?
index_interval = (Time.zone.now - updated_at)
logger = Logger.new(STDOUT)
logger.info "[Elasticsearch] Indexing of DOI #{doi} finished #{index_interval.to_s} seconds after DOI update."

send_import_message(self.to_jsonapi) if aasm_state == "findable"
if self.class.name == "Doi"
update_column(:indexed, Time.zone.now)
send_import_message(self.to_jsonapi) if aasm_state == "findable" unless Rails.env.test?
end
end

Expand Down
3 changes: 2 additions & 1 deletion app/models/doi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -250,14 +250,15 @@ def self.index_by_day(options={})

logger = Logger.new(STDOUT)

Doi.where("created >= ?", from_date.strftime("%F") + " 00:00:00").where("created < ?", until_date.strftime("%F") + " 00:00:00").find_in_batches(batch_size: 100) do |dois|
Doi.where("created >= ?", from_date.strftime("%F") + " 00:00:00").where("created < ?", until_date.strftime("%F") + " 00:00:00").where("updated > indexed").find_in_batches(batch_size: 100) do |dois|
response = Doi.__elasticsearch__.client.bulk \
index: Doi.index_name,
type: Doi.document_type,
body: dois.map { |doi| { index: { _id: doi.id, data: doi.as_indexed_json } } }

errors += response['items'].map { |k, v| k.values.first['error'] }.compact.length
count += dois.length
dois.each { |doi| doi.update_column(:indexed, Time.zone.now) }
end

logger.info "[Elasticsearch] #{errors} errors indexing #{count} DOIs created on #{from_date.strftime("%F")}."
Expand Down

0 comments on commit 9562477

Please sign in to comment.