From 2ce9d90660ff60ef1c751927f8121bab81e666d8 Mon Sep 17 00:00:00 2001 From: kjgarza Date: Tue, 18 Feb 2020 08:03:34 +0100 Subject: [PATCH] separate methods find_by_id and find_by_ids --- app/controllers/dois_controller.rb | 2 +- app/controllers/works_controller.rb | 2 +- app/graphql/elasticsearch_loader.rb | 2 +- app/graphql/types/query_type.rb | 2 +- app/models/concerns/batch_loader_helper.rb | 2 +- app/models/doi.rb | 13 ++++++++++++- 6 files changed, 17 insertions(+), 6 deletions(-) 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 af830e324..8fcc783a1 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 f9b69a7e3..840e89138 100644 --- a/app/models/doi.rb +++ b/app/models/doi.rb @@ -598,7 +598,7 @@ def self.query_fields end # return results for one or more ids - def self.find_by_id(ids, options={}) + def self.find_by_ids(ids, options={}) ids = ids.split(",") if ids.is_a?(String) options[:page] ||= {} @@ -624,6 +624,17 @@ def self.find_by_id(ids, options={}) }) end + # return results for one doi + def self.find_by_id(id) + __elasticsearch__.search( + query: { + match: { + uid: id, + }, + }, + ) + end + def self.query(query, options={}) # support scroll api # map function is small performance hit