diff --git a/lib/blacklight/abstract_repository.rb b/lib/blacklight/abstract_repository.rb index fad2d4f7dc..274663803c 100644 --- a/lib/blacklight/abstract_repository.rb +++ b/lib/blacklight/abstract_repository.rb @@ -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 diff --git a/lib/blacklight/configuration/fields.rb b/lib/blacklight/configuration/fields.rb index 49b3ce45be..da5f9862a5 100644 --- a/lib/blacklight/configuration/fields.rb +++ b/lib/blacklight/configuration/fields.rb @@ -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 @@ -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 @@ -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 = {} diff --git a/lib/blacklight/solr/repository.rb b/lib/blacklight/solr/repository.rb index fc7608ad23..ee49a2adff 100644 --- a/lib/blacklight/solr/repository.rb +++ b/lib/blacklight/solr/repository.rb @@ -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 diff --git a/spec/models/blacklight/configuration_spec.rb b/spec/models/blacklight/configuration_spec.rb index 1cf704205a..9e66454db7 100644 --- a/spec/models/blacklight/configuration_spec.rb +++ b/spec/models/blacklight/configuration_spec.rb @@ -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"] @@ -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"] @@ -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"]