Skip to content

Commit

Permalink
Merge pull request #1914 from projectblacklight/reflect_fields
Browse files Browse the repository at this point in the history
Extract reflect_fields method to the repository
  • Loading branch information
Jessie Keck committed Jul 25, 2018
2 parents 6ea0849 + 038e382 commit 490932e
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 38 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
57 changes: 31 additions & 26 deletions lib/blacklight/configuration/fields.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ class Configuration
# solr fields configuration
module Fields
extend ActiveSupport::Concern
extend Deprecation
self.deprecation_horizon = 'blacklight version 8.0.0'

module ClassMethods
# Add a configuration block for a collection of solr fields
Expand Down Expand Up @@ -102,24 +104,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,25 +122,45 @@ def add_blacklight_field config_key, *args, &block

private

def luke_fields
if @table[:luke_fields] == false
##
# Using reflection into the index, add any fields in the index that match the field_config
def handle_matching_fields(config_key, field_config, &block)
salient_fields = reflected_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 reflected_fields
if @table[:reflected_fields] == false
return nil
end

@table[:luke_fields] ||= Rails.cache.fetch("blacklight_configuration/admin/luke", expires_in: 1.hour) do
@table[:reflected_fields] ||= Rails.cache.fetch("blacklight_configuration/admin/reflected_fields", 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
end
end

@table[:luke_fields] || {}
@table[:reflected_fields] || {}
end
alias luke_fields reflected_fields
deprecation_deprecate luke_fields: 'use reflected_fields instead'

# Add a solr field by a solr field name and hash
def field_config_from_key_and_hash config_key, field_name, field_or_hash = {}
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 @@ -29,6 +29,13 @@ def suggestions(request_params)
Blacklight::Suggest::Response.new suggest_results, request_params, suggest_handler_path
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
# TODO: Make this private after we have a way to abstract admin/luke and ping
Expand Down
24 changes: 12 additions & 12 deletions spec/models/blacklight/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -251,22 +251,22 @@
end

it "looks up and match field names" do
allow(config).to receive_messages(luke_fields: {
allow(config).to receive(:reflected_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"]
end

it "takes wild-carded field names and dereference them to solr fields" do
allow(config).to receive_messages(luke_fields: {
allow(config).to receive(:reflected_fields).and_return(
"some_field_facet" => {},
"another_field_facet" => {},
"a_facet_field" => {},
})
"a_facet_field" => {}
)
expect { |b| config.add_facet_field "*_facet", &b }.to yield_control.twice

expect(config.facet_fields.keys).to eq ["some_field_facet", "another_field_facet"]
Expand Down Expand Up @@ -321,11 +321,11 @@
end

it "takes wild-carded field names and dereference them to solr fields" do
allow(config).to receive_messages(luke_fields: {
allow(config).to receive(:reflected_fields).and_return(
"some_field_display" => {},
"another_field_display" => {},
"a_facet_field" => {},
})
"a_facet_field" => {}
)
config.add_index_field "*_display"

expect(config.index_fields.keys).to eq ["some_field_display", "another_field_display"]
Expand Down Expand Up @@ -370,11 +370,11 @@
end

it "takes wild-carded field names and dereference them to solr fields" do
allow(config).to receive_messages(luke_fields: {
allow(config).to receive(:reflected_fields).and_return(
"some_field_display" => {},
"another_field_display" => {},
"a_facet_field" => {},
})
"a_facet_field" => {}
)
config.add_show_field "*_display"

expect(config.show_fields.keys).to eq ["some_field_display", "another_field_display"]
Expand Down

0 comments on commit 490932e

Please sign in to comment.