Skip to content

Commit

Permalink
Convert autocomplete to use blacklight's search fields
Browse files Browse the repository at this point in the history
  • Loading branch information
cbeer committed Mar 24, 2015
1 parent 2b69417 commit be9ef80
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 16 deletions.
2 changes: 1 addition & 1 deletion app/controllers/spotlight/catalog_controller.rb
Expand Up @@ -42,7 +42,7 @@ def show
# setup within their index analyzer. This will ensure that this method returns
# results when a partial match is passed in the "q" parameter.
def autocomplete
(_, @document_list) = get_search_results(params, blacklight_config.default_autocomplete_solr_params.merge(fq: ["-#{Spotlight::SolrDocument.visibility_field(current_exhibit)}:false"]))
(_, @document_list) = get_search_results(params.merge(search_field: Spotlight::Engine.config.autocomplete_search_field), facet: false, "facet.field" => [], fq: ["-#{Spotlight::SolrDocument.visibility_field(current_exhibit)}:false"])

respond_to do |format|
format.json do
Expand Down
11 changes: 9 additions & 2 deletions app/models/spotlight/blacklight_configuration.rb
Expand Up @@ -67,8 +67,6 @@ def blacklight_config

config.add_results_collection_tool 'save_search', if: :render_save_this_search?

config.default_autocomplete_solr_params[:fl] ||= "#{config.document_model.unique_key} #{config.view_config(:show).title_field} #{spotlight_image_version_fields.join(' ')}"

config.default_solr_params = config.default_solr_params.merge(default_solr_params)

config.show.partials.insert(2, "spotlight/catalog/tags")
Expand Down Expand Up @@ -202,6 +200,15 @@ def add_exhibit_specific_fields config
config.add_index_field key, options
end
end

if Spotlight::Engine.config.autocomplete_search_field and !config.search_fields[Spotlight::Engine.config.autocomplete_search_field]
config.add_search_field(Spotlight::Engine.config.autocomplete_search_field) do |field|
field.include_in_simple_select = false
field.solr_parameters = Spotlight::Engine.config.default_autocomplete_params.deep_dup
field.solr_parameters[:fl] ||= ""
field.solr_parameters[:fl] += " #{config.document_model.unique_key} #{config.view_config(:show).title_field} #{spotlight_image_version_fields.join(' ')}"
end
end
end

def spotlight_image_version_fields
Expand Down
3 changes: 2 additions & 1 deletion lib/spotlight/engine.rb
Expand Up @@ -111,7 +111,8 @@ def self.blacklight_config
Blacklight::Engine.config.inject_blacklight_helpers = false

# Query parameters for autocomplete requests
Blacklight::Configuration.default_values[:default_autocomplete_solr_params] = {qf: 'id^1000 full_title_tesim^100 id_ng full_title_ng'}
Spotlight::Engine.config.autocomplete_search_field = "autocomplete"
Spotlight::Engine.config.default_autocomplete_params = {qf: 'id^1000 full_title_tesim^100 id_ng full_title_ng'}

# Field containing the last modified date for a Solr document
Blacklight::Configuration.default_values[:index].timestamp_field ||= 'timestamp'
Expand Down
45 changes: 33 additions & 12 deletions spec/models/spotlight/blacklight_configuration_spec.rb
Expand Up @@ -361,18 +361,6 @@
end
end

describe "default_autocomplete_solr_params" do
it "should include all the fields we need to render the autocomplete widget" do
blacklight_config.show.title_field = "some_field"
expect(subject.blacklight_config.default_autocomplete_solr_params).to have_key :fl
expect(subject.blacklight_config.default_autocomplete_solr_params[:fl]).to include "id"
expect(subject.blacklight_config.default_autocomplete_solr_params[:fl]).to include "some_field"
expect(subject.blacklight_config.default_autocomplete_solr_params[:fl]).to include "full_image_url_ssm"
expect(subject.blacklight_config.default_autocomplete_solr_params[:fl]).to include "thumbnail_url_ssm"
expect(subject.blacklight_config.default_autocomplete_solr_params[:fl]).to include "thumbnail_square_url_ssm"
end
end

describe "show" do
it "should have show view configuration" do
expect(subject.show).to be_empty
Expand Down Expand Up @@ -418,4 +406,37 @@
expect(subject.custom_index_fields['abc'].a).to eq 1
end
end

describe "autocomplete configuration" do
before do
# undo the stubbing we've used elsewhere..
allow(subject).to receive(:default_blacklight_config).and_call_original
blacklight_config.show.title_field = "x"
allow(Spotlight::Engine).to receive_messages blacklight_config: blacklight_config.deep_copy
end

context "with the default search field" do
let(:search_field) do
subject.blacklight_config.search_fields[Spotlight::Engine.config.autocomplete_search_field]
end

it "should be hidden from the search field selector" do
expect(search_field.if).to eq false
end

it "should use the engine's autocomplete parameters" do
expect(search_field.solr_parameters).to include Spotlight::Engine.config.default_autocomplete_params
end

it "should include the relevant fields" do
Spotlight::Engine.blacklight_config.show.title_field = "some_field"
expect(search_field.solr_parameters).to have_key :fl
expect(search_field.solr_parameters[:fl]).to include "id"
expect(search_field.solr_parameters[:fl]).to include "some_field"
expect(search_field.solr_parameters[:fl]).to include "full_image_url_ssm"
expect(search_field.solr_parameters[:fl]).to include "thumbnail_url_ssm"
expect(search_field.solr_parameters[:fl]).to include "thumbnail_square_url_ssm"
end
end
end
end

0 comments on commit be9ef80

Please sign in to comment.