Skip to content

Commit

Permalink
Add :match option to add_*_field methods to look up fields from the i…
Browse files Browse the repository at this point in the history
…ndex

This is a refinement of the wildcard field name, but also supports arbitrary
regular expressions.

Also, uses the new repository class, so no extra configuration is needed (fixes
  • Loading branch information
cbeer committed Mar 21, 2015
1 parent b386f4e commit da44a99
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 10 deletions.
30 changes: 20 additions & 10 deletions lib/blacklight/configuration/fields.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,17 +82,23 @@ def add_blacklight_field config_key, *args, &block
field_config_from_field_or_hash(config_key, *args)
end

if (field_config.field || field_config.key).to_s =~ /\*/
field_config.match = Regexp.new("^" + (field_config.field || field_config.key).to_s.gsub('*', '.+') + "$")
end

# look up any dynamic fields
if (field_config.field || field_config.key).to_s =~ /\*/ and luke_fields
wildcard_field = (field_config.field || field_config.key).to_s
salient_fields = luke_fields.select do |k,v|
k =~ Regexp.new("^" + wildcard_field.gsub('*', '.+') + "$")
if field_config.match

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

salient_fields.each do |field, luke_config|
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
Expand Down Expand Up @@ -123,15 +129,19 @@ def luke_fields
return nil
end

@table[:luke_fields] ||= begin
if has_key? :blacklight_solr
blacklight_solr.get('admin/luke', params: { fl: '*', 'json.nl' => 'map' })['fields']
@table[:luke_fields] ||= Rails.cache.fetch("blacklight_configuration/admin/luke", expires_in: 1.hour) do
begin
if repository_class == Blacklight::SolrRepository
repository = repository_class.new(self)
repository.send_and_receive('admin/luke', params: { fl: '*', 'json.nl' => 'map' })['fields']
end
rescue => e
Blacklight.logger.warn "Error retrieving field metadata: #{e}"
false
end
rescue
false
end

@table[:luke_fields] || nil
@table[:luke_fields] || {}
end

# Add a solr field by a solr field name and hash
Expand Down
16 changes: 16 additions & 0 deletions spec/lib/blacklight/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -213,11 +213,27 @@
"another_field_facet" => {},
"a_facet_field" => {},
})
expect { |b| @config.add_index_field match: "*_facet", &b }.to yield_control.twice

expect(@config.index_fields.keys).to eq ["some_field_facet", "another_field_facet"]
end

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

expect(@config.index_fields.keys).to eq ["some_field_facet", "another_field_facet"]
end

it "should query solr and get live values for match fields", integration: true do
@config.add_index_field match: /title.+display/
expect(@config.index_fields.keys).to include "subtitle_display", "subtitle_vern_display", "title_display", "title_vern_display"
end

end

describe "add_index_field" do
Expand Down

0 comments on commit da44a99

Please sign in to comment.