Skip to content

Commit

Permalink
Merge 79b470c into ac2ad4a
Browse files Browse the repository at this point in the history
  • Loading branch information
jcoyne committed Jun 27, 2018
2 parents ac2ad4a + 79b470c commit 23207df
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 25 deletions.
7 changes: 7 additions & 0 deletions lib/blacklight/abstract_repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ def search(_params = {})
raise NotImplementedError
end

##
# Query the fields that exist from the index
# @return [Hash]
def reflect_fields
raise NotImplementedError
end

private

def connection_config
Expand Down
45 changes: 23 additions & 22 deletions lib/blacklight/configuration/fields.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,24 +102,7 @@ def add_blacklight_field config_key, *args, &block

# look up any dynamic fields
if field_config.match

salient_fields = luke_fields.select do |k, _v|
k =~ field_config.match
end

salient_fields.each_key do |field|
config = field_config.dup
config.match = nil
config.field = field
config.key = field

if self[config_key.pluralize][config.key]
self[config_key.pluralize][config.key] = config.merge(self[config_key.pluralize][config.key])
else
add_blacklight_field(config_key, config, &block)
end
end

handle_matching_fields(config_key, field_config, &block)
return
end

Expand All @@ -137,17 +120,35 @@ def add_blacklight_field config_key, *args, &block

private

##
# Using the Luke handler see add any fields in the index that match the field_config
def handle_matching_fields(config_key, field_config, &block)
salient_fields = luke_fields.select do |k, _v|
k =~ field_config.match
end

salient_fields.each_key do |field|
config = field_config.dup
config.match = nil
config.field = field
config.key = field
if self[config_key.pluralize][config.key]
self[config_key.pluralize][config.key] = config.merge(self[config_key.pluralize][config.key])
else
add_blacklight_field(config_key, config, &block)
end
end
end

def luke_fields
if @table[:luke_fields] == false
return nil
end

@table[:luke_fields] ||= Rails.cache.fetch("blacklight_configuration/admin/luke", expires_in: 1.hour) do
begin
if repository_class <= Blacklight::Solr::Repository
repository = repository_class.new(self)
repository.send_and_receive('admin/luke', params: { fl: '*', 'json.nl' => 'map' })['fields']
end
repository = repository_class.new(self)
repository.reflect_fields
rescue => e
Blacklight.logger.warn "Error retrieving field metadata: #{e}"
false
Expand Down
7 changes: 7 additions & 0 deletions lib/blacklight/solr/repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ def search params = {}
send_and_receive blacklight_config.solr_path, params.reverse_merge(qt: blacklight_config.qt)
end

##
# Gets a list of available fields
# @return [Hash]
def reflect_fields
send_and_receive('admin/luke', params: { fl: '*', 'json.nl' => 'map' })['fields']
end

##
# Execute a solr query
# @see [RSolr::Client#send_and_receive]
Expand Down
6 changes: 3 additions & 3 deletions spec/models/blacklight/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -251,11 +251,11 @@
end

it "looks up and match field names" do
allow(config).to receive_messages(luke_fields: {
allow(config).to receive(:luke_fields).and_return(
"some_field_facet" => {},
"another_field_facet" => {},
"a_facet_field" => {},
})
"a_facet_field" => {}
)
expect { |b| config.add_facet_field match: /_facet$/, &b }.to yield_control.twice

expect(config.facet_fields.keys).to eq ["some_field_facet", "another_field_facet"]
Expand Down

0 comments on commit 23207df

Please sign in to comment.