Skip to content

Commit

Permalink
Merge pull request #832 from datacite/facetable-authors-error
Browse files Browse the repository at this point in the history
Addresses error where some author aggs with multiple nameIdentifiers result in array index error
  • Loading branch information
codycooperross authored Jul 6, 2022
2 parents e9d55ad + 78be741 commit 787dde0
Show file tree
Hide file tree
Showing 5 changed files with 624 additions and 24 deletions.
28 changes: 16 additions & 12 deletions app/controllers/concerns/facetable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -408,8 +408,12 @@ def facet_by_fos(arr)
end

def facet_by_authors(arr)
arr.map do |hsh|
orcid_id = hsh["key"]
arr.map { |hsh|
orcid_id = %r{\A(?:(http|https)://(orcid.org)/)(.+)\z}.match?(hsh["key"]) && hsh["key"]

if orcid_id.nil?
next
end

# The aggregation query should only return 1 hit, so hence the index
# into first element
Expand All @@ -418,20 +422,20 @@ def facet_by_authors(arr)
# Filter through creators to find creator that matches the key
matched_creator = creators.select do |creator|
if creator.key?("nameIdentifiers")
creator["nameIdentifiers"].each do |ni|
break ni["nameIdentifier"] == orcid_id
end
creator["nameIdentifiers"].any? { |ni| ni["nameIdentifier"] == orcid_id }
end
end

title = matched_creator[0]["name"]
if matched_creator.any?
title = matched_creator[0]["name"]

{
"id" => orcid_id,
"title" => title,
"count" => hsh["doc_count"],
}
end
{
"id" => orcid_id,
"title" => title,
"count" => hsh["doc_count"],
}
end
}.compact
end
end
end
28 changes: 16 additions & 12 deletions app/graphql/types/base_connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,12 @@ def facet_by_language(arr)
end

def facet_by_authors(arr)
arr.map do |hsh|
orcid_id = hsh["key"]
arr.map { |hsh|
orcid_id = %r{\A(?:(http|https)://(orcid.org)/)(.+)\z}.match?(hsh["key"]) && hsh["key"]

if orcid_id.nil?
next
end

# The aggregation query should only return 1 hit, so hence the index
# into first element
Expand All @@ -208,19 +212,19 @@ def facet_by_authors(arr)
# Filter through creators to find creator that matches the key
matched_creator = creators.select do |creator|
if creator.key?("nameIdentifiers")
creator["nameIdentifiers"].each do |ni|
break ni["nameIdentifier"] == orcid_id
end
creator["nameIdentifiers"].any? { |ni| ni["nameIdentifier"] == orcid_id }
end
end

title = matched_creator[0]["name"]
if matched_creator.any?
title = matched_creator[0]["name"]

{
"id" => orcid_id,
"title" => title,
"count" => hsh["doc_count"],
}
end
{
"id" => orcid_id,
"title" => title,
"count" => hsh["doc_count"],
}
end
}.compact
end
end
49 changes: 49 additions & 0 deletions spec/concerns/facetable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,62 @@

describe "Facetable", type: :controller do
let(:author_aggs) { JSON.parse(file_fixture("authors_aggs.json").read) }
let(:author_aggs_with_multiple_name_identifiers) { JSON.parse(file_fixture("authors_aggs_with_multiple_name_identifiers.json").read) }
let(:model) { DataciteDoisController.new }
it "facet by author" do
authors = model.facet_by_authors(author_aggs)

expected_result = [{ "id" => "https://orcid.org/0000-0003-1419-2405", "title" => "Fenner, Martin", "count" => 244 }, { "id" => "https://orcid.org/0000-0001-9570-8121", "title" => "Lambert, Simon", "count" => 23 }]
expect(authors).to eq (expected_result)
end

it "facet by author where author may have multiple nameIdentifiers" do
authors = model.facet_by_authors(author_aggs_with_multiple_name_identifiers)

expected_result = [
{
"id" => "https://orcid.org/0000-0002-0429-5446",
"title" => "Nam, Hyung-song",
"count" => 28,
},
{
"id" => "https://orcid.org/0000-0003-4973-3128",
"title" => "Casares, Ramón",
"count" => 12,
},
{
"id" => "https://orcid.org/0000-0002-3776-4755",
"title" => "Gomeseria, Ronald",
"count" => 4,
},
{
"id" => "https://orcid.org/0000-0002-6014-2161",
"title" => "Kartha, Sivan",
"count" => 4,
},
{
"id" => "https://orcid.org/0000-0003-1026-5865",
"title" => "Willemen, Louise",
"count" => 4,
},
{
"id" => "https://orcid.org/0000-0003-4624-488X",
"title" => "Schwarz, Nina",
"count" => 4,
},
{
"id" => "https://orcid.org/0000-0002-2149-9897",
"title" => "A, Subaveerapandiyan",
"count" => 3,
},
{
"id" => "https://orcid.org/0000-0002-4541-7294",
"title" => "Puntiroli, Michael",
"count" => 3,
},
]
expect(authors).to eq (expected_result)
end
end


Expand Down
Loading

0 comments on commit 787dde0

Please sign in to comment.