Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solr doc children fix #3143

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 27 additions & 7 deletions app/models/solr_document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def self.find(ids)

solr_query = Hyrax::SolrQueryBuilderService.construct_query_for_ids(ids)
path = repository.blacklight_config.document_solr_path || repository.blacklight_config.solr_path
docs = repository.send_and_receive path, { fq: solr_query }
docs = repository.send_and_receive path, { fq: solr_query, rows: ids.count }
docs.documents
end

Expand Down Expand Up @@ -85,9 +85,28 @@ def parents?

# Find and return child works (excluding FileSets)
def children
@children ||= SolrDocument.find(member_ids).map do |document|
document['has_model_ssim'].first.include?('FileSet') ? nil : document
end.compact
return @children unless @children.nil?

sort_members
@children
end

def sort_members
@children = []
@file_sets = []
member_ids.each_slice(OD2::Application.config.max_members_query) do |chunk|
process_chunk(chunk)
end
end

def process_chunk(chunk)
SolrDocument.find(chunk).map do |document|
if document['has_model_ssim'].first.include?('FileSet')
@file_sets << document
else
@children << document
end
end
end

def children?
Expand All @@ -96,9 +115,10 @@ def children?

# Find and return file sets
def file_sets
@file_sets ||= SolrDocument.find(member_ids).map do |document|
document['has_model_ssim'].first.include?('FileSet') ? document : nil
end.compact
return @file_sets unless @file_sets.nil?

sort_members
@file_sets
end

def file_sets?
Expand Down
1 change: 1 addition & 0 deletions config/environments/development.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,5 +90,6 @@
config.verify_services = [
OregonDigital::VerifyDerivativesService
]
config.max_members_query = ENV.fetch('MAX_MEMBERS_QUERY', 5).to_i

end
1 change: 1 addition & 0 deletions config/environments/production.rb
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,5 @@
config.verify_services = [
OregonDigital::VerifyDerivativesService
]
config.max_members_query = ENV.fetch('MAX_MEMBERS_QUERY', 100).to_i
end
1 change: 1 addition & 0 deletions config/environments/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,5 @@
config.verify_services = [
OregonDigital::VerifyDerivativesService
]
config.max_members_query = ENV.fetch('MAX_MEMBERS_QUERY', 5).to_i
end
54 changes: 54 additions & 0 deletions spec/models/solr_document_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# frozen_string_literal: true

RSpec.describe SolrDocument do
let(:sd) { described_class.new }
let(:sd1) { described_class.new({ 'id' => 'bcde12345', 'has_model_ssim' => ['FileSet'] }) }
let(:sd2) { described_class.new({ 'id' => 'cdef23456', 'has_model_ssim' => ['Image'] }) }
let(:docs) { [sd1, sd2] }
let(:ids) { [sd1['id'], sd2['id']] }

describe 'process_chunk' do
before do
allow(sd).to receive(:member_ids).and_return(ids)
allow(described_class).to receive(:find).and_return(docs)
OD2::Application.config.max_members_query = 2
sd.sort_members
end

it 'populates children and file_sets' do
expect(sd.children).to eq [sd2]
expect(sd.file_sets).to eq [sd1]
end
end

describe 'max_members_query' do
let(:ids) do
i = []
(0..1000).each do |num|
i << "abc#{num}"
end
i
end

before do
allow(sd).to receive(:member_ids).and_return(ids)
OD2::Application.config.max_members_query = max_members_query
end

context 'when chunk size is low' do
let(:max_members_query) { 100 }

it 'returns without throwing an error' do
expect { sd.sort_members }.not_to raise_error
end
end

context 'when chunk size is high' do
let(:max_members_query) { 1000 }

it 'throws error' do
expect { sd.sort_members }.to raise_error(Blacklight::Exceptions::InvalidRequest)
end
end
end
end