diff --git a/app/controllers/dois_controller.rb b/app/controllers/dois_controller.rb index f15154f29..47c1e5bfc 100644 --- a/app/controllers/dois_controller.rb +++ b/app/controllers/dois_controller.rb @@ -49,7 +49,7 @@ def index if params[:id].present? response = Doi.find_by_id(params[:id]) elsif params[:ids].present? - response = Doi.find_by_id(params[:ids], page: page, sort: sort) + response = Doi.find_by_ids(params[:ids], page: page, sort: sort) else response = Doi.query(params[:query], state: params[:state], diff --git a/app/controllers/works_controller.rb b/app/controllers/works_controller.rb index 7814ba81f..f8632009c 100644 --- a/app/controllers/works_controller.rb +++ b/app/controllers/works_controller.rb @@ -31,7 +31,7 @@ def index if params[:id].present? response = Doi.find_by_id(params[:id]) elsif params[:ids].present? - response = Doi.find_by_id(params[:ids], page: page, sort: sort) + response = Doi.find_by_ids(params[:ids], page: page, sort: sort) else response = Doi.query(params[:query], exclude_registration_agencies: true, diff --git a/app/graphql/elasticsearch_loader.rb b/app/graphql/elasticsearch_loader.rb index 6a8714c90..21995b52f 100644 --- a/app/graphql/elasticsearch_loader.rb +++ b/app/graphql/elasticsearch_loader.rb @@ -6,7 +6,7 @@ def initialize(model) end def perform(ids) - @model.find_by_id(ids).results.each { |record| fulfill(record.uid, record) } + @model.find_by_ids(ids).results.each { |record| fulfill(record.uid, record) } ids.each { |id| fulfill(id, nil) unless fulfilled?(id) } end end diff --git a/app/graphql/types/query_type.rb b/app/graphql/types/query_type.rb index f679a526f..1cafadafa 100644 --- a/app/graphql/types/query_type.rb +++ b/app/graphql/types/query_type.rb @@ -154,7 +154,7 @@ def people(query: nil, first: nil) def creative_works(query: nil, ids: nil, client_id: nil, provider_id: nil, first: nil) if ids.present? dois = ids.split(",").map { |i| doi_from_url(i) } - Doi.find_by_id(dois, page: { number: 1, size: first }).results.to_a + Doi.find_by_ids(dois, page: { number: 1, size: first }).results.to_a else Doi.query(query, client_id: client_id, provider_id: provider_id, state: "findable", page: { number: 1, size: first }).results.to_a end diff --git a/app/models/concerns/batch_loader_helper.rb b/app/models/concerns/batch_loader_helper.rb index 52cbe6c0f..cbf2427f1 100644 --- a/app/models/concerns/batch_loader_helper.rb +++ b/app/models/concerns/batch_loader_helper.rb @@ -5,7 +5,7 @@ module BatchLoaderHelper def load_doi(object) BatchLoader.for(object.uuid).batch do |dois, loader| dois = object.doi - results = Doi.find_by_id(dois).results + results = Doi.find_by_ids(dois).results loader.call(object.uuid, results) end end diff --git a/app/models/doi.rb b/app/models/doi.rb index cd7d7011c..840e89138 100644 --- a/app/models/doi.rb +++ b/app/models/doi.rb @@ -597,6 +597,33 @@ def self.query_fields ["uid^50", "related_identifiers.relatedIdentifier^3", "funding_references.relatedIdentifier^3", "container.identifier^3", 'titles.title^3', 'creator_names^3', 'creators.name^3', 'creators.id^3', 'publisher^3', 'descriptions.description^3', 'types.resourceTypeGeneral^3', 'subjects.subject^3', 'client.uid^3', 'provider.uid^3', '_all'] end + # return results for one or more ids + def self.find_by_ids(ids, options={}) + ids = ids.split(",") if ids.is_a?(String) + + options[:page] ||= {} + options[:page][:number] ||= 1 + options[:page][:size] ||= 1000 + options[:sort] ||= { created: { order: "asc" }} + + must = [{ terms: { doi: ids.map(&:upcase) }}] + must << { terms: { aasm_state: options[:state].to_s.split(",") }} if options[:state].present? + must << { terms: { provider_id: options[:provider_id].split(",") }} if options[:provider_id].present? + must << { terms: { client_id: options[:client_id].to_s.split(",") }} if options[:client_id].present? + + __elasticsearch__.search({ + from: (options.dig(:page, :number) - 1) * options.dig(:page, :size), + size: options.dig(:page, :size), + sort: [options[:sort]], + query: { + bool: { + must: must, + } + }, + aggregations: query_aggregations, + }) + end + # return results for one doi def self.find_by_id(id) __elasticsearch__.search(