Skip to content

Commit

Permalink
separate methods find_by_id and find_by_ids
Browse files Browse the repository at this point in the history
  • Loading branch information
kjgarza committed Feb 18, 2020
1 parent a094bea commit 2ce9d90
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 6 deletions.
2 changes: 1 addition & 1 deletion app/controllers/dois_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/works_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion app/graphql/elasticsearch_loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion app/graphql/types/query_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion app/models/concerns/batch_loader_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 12 additions & 1 deletion app/models/doi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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] ||= {}
Expand All @@ -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
Expand Down

0 comments on commit 2ce9d90

Please sign in to comment.