Skip to content

Commit

Permalink
Use the original user query as the first element in the opensearch su…
Browse files Browse the repository at this point in the history
…ggestion response; fixes #1682
  • Loading branch information
cbeer committed Apr 27, 2017
1 parent b27a537 commit 182d82b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
2 changes: 1 addition & 1 deletion app/services/blacklight/search_service.rb
Expand Up @@ -78,7 +78,7 @@ def opensearch_response(field = nil, extra_controller_params = {})
query = search_builder.with(user_params).merge(solr_opensearch_params(field)).merge(extra_controller_params)
response = repository.search(query)

[response.params[:q], response.documents.flat_map { |doc| doc[field] }.uniq]
[user_params[:q], response.documents.flat_map { |doc| doc[field] }.uniq]
end

##
Expand Down
25 changes: 24 additions & 1 deletion spec/services/blacklight/search_service_spec.rb
Expand Up @@ -11,6 +11,7 @@
RSpec.describe Blacklight::SearchService do

let(:service) { described_class.new(blacklight_config, user_params) }
let(:repository) { Blacklight::Solr::Repository.new(blacklight_config) }
let(:user_params) { {} }
subject { service }
let(:blacklight_config) { Blacklight::Configuration.new }
Expand All @@ -23,7 +24,7 @@
let(:single_facet) { { format: 'Book' } }

before do
allow(service).to receive(:repository).and_return(Blacklight::Solr::Repository.new(blacklight_config))
allow(service).to receive(:repository).and_return(repository)
service.repository.connection = blacklight_solr
end

Expand Down Expand Up @@ -414,4 +415,26 @@
expect(docs.first).to be_nil
end
end

describe '#opensearch_response' do
let(:user_params) { { q: 'Book' } }
let(:mock_response) {
instance_double(Blacklight::Solr::Response, documents: [
{ field: 'A' }, { field: 'B' }, { field: 'C' }
])
}

before do
blacklight_config.view.opensearch.title_field = :field
allow(repository).to receive(:search).and_return(mock_response)
end

it 'contains the original user query as the first element in the response' do
expect(service.opensearch_response.first).to eq 'Book'
end

it 'contains the search suggestions as the second element in the response' do
expect(service.opensearch_response.last).to match_array %w(A B C)
end
end
end

0 comments on commit 182d82b

Please sign in to comment.