Skip to content

Commit

Permalink
Merge pull request #1305 from datacite/facet-behavior-tweak
Browse files Browse the repository at this point in the history
Returns no facets when the result set is empty
  • Loading branch information
codycooperross authored Dec 13, 2024
2 parents 5ad6dfb + a169b1a commit 6a00203
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
14 changes: 8 additions & 6 deletions app/controllers/datacite_dois_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -253,12 +253,14 @@ def index
}

aggregations = response.aggregations
facets = facets_to_facet_methods.map do |facet, method|
if aggregations.dig(facet)
buckets = facets_to_bucket_path.dig(facet) ? aggregations.dig(facet, *facets_to_bucket_path[facet]) : aggregations.dig(facet).buckets
[facet.to_s.camelize(:lower), send(method, buckets)]
end
end.compact.to_h
facets = total == 0 ? {} :
facets_to_facet_methods.map do |facet, method|
if aggregations.dig(facet)
buckets = facets_to_bucket_path.dig(facet) ? aggregations.dig(facet, *facets_to_bucket_path[facet]) : aggregations.dig(facet).buckets
[facet.to_s.camelize(:lower), send(method, buckets)]
end
end.compact.to_h

respond_to do |format|
format.json do
options = {}
Expand Down
20 changes: 18 additions & 2 deletions spec/requests/datacite_dois/datacite_dois_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ def clear_doi_index
"downloads"
]

DEFAULT_META_FIELDS = [
"total",
"totalPages",
"page",
]

describe DataciteDoisController, type: :request, vcr: true do
let(:admin) { create(:provider, symbol: "ADMIN") }
let(:admin_bearer) { Client.generate_token(role_id: "staff_admin", uid: admin.symbol, password: admin.password) }
Expand Down Expand Up @@ -265,7 +271,7 @@ def clear_doi_index
expect(json.dig("meta", "total")).to eq(10)

expect(json.dig("meta").length).to eq(DEFAULT_DOIS_FACETS.length + 3)
expect(json.dig("meta").keys).to match_array(DEFAULT_DOIS_FACETS + ["total", "totalPages", "page"])
expect(json.dig("meta").keys).to match_array(DEFAULT_DOIS_FACETS + DEFAULT_META_FIELDS)
end

it "returns default facets when disable-facets is set to false" do
Expand All @@ -276,7 +282,7 @@ def clear_doi_index
expect(json.dig("meta", "total")).to eq(10)

expect(json.dig("meta").length).to eq(DEFAULT_DOIS_FACETS.length + 3)
expect(json.dig("meta").keys).to match_array(DEFAULT_DOIS_FACETS + ["total", "totalPages", "page"])
expect(json.dig("meta").keys).to match_array(DEFAULT_DOIS_FACETS + DEFAULT_META_FIELDS)
end

it "returns no facets when disable-facets is set to true" do
Expand Down Expand Up @@ -319,6 +325,16 @@ def clear_doi_index
expect(json.dig("meta", "madeUpFacet")).to eq(nil)
expect(json.dig("meta", "made_up_facet")).to eq(nil)
end

it "returns no facets when the result set is empty" do
get "/dois?query=creators.name:foo", nil, headers

expect(last_response.status).to eq(200)
expect(json["data"].size).to eq(0)
expect(json.dig("meta", "total")).to eq(0)
expect(json.dig("meta").length).to eq(3)
expect(json.dig("meta").keys).to match_array(DEFAULT_META_FIELDS)
end
end

describe "GET /dois with nil publisher values", elsasticsearch: true, prefix_pool_size: 1 do
Expand Down

0 comments on commit 6a00203

Please sign in to comment.