Skip to content

Commit

Permalink
Add request for extra results
Browse files Browse the repository at this point in the history
When users search within a manual (this is doing a "scoped" search)
we want to show them some results from the rest of GOV.UK (ie some
"un-scoped" results) so that they are aware that here are other results
available that might be useful.

This commit adds a third rummager request for scoped searches which
returns three un-scoped results. This request is built by removing
the scoping `filer_manual[]=this/manual` and adding an extra parameter
`reject_manual[]=this/manual` which tells rummager we don't want any
results that occur in this manual.
  • Loading branch information
Alice Bartlett committed Apr 9, 2015
1 parent 318731b commit 5b7049b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
15 changes: 12 additions & 3 deletions lib/search_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@ def search_results
def scope_info
if is_scoped? && scope_object.present?
{
scope: {
title: scope_object.title,
"scope" => {
"title" => scope_object.title,
},
"unscoped_results" => unscoped_results,
}
else
{}
Expand All @@ -53,7 +54,15 @@ def is_scoped?
end

def scope_object_link
params.filter('manual').first
@link ||= params.filter('manual').first
end

def unscoped_results
@unscoped_results ||= api.unified_search(unscoped_rummager_request).to_hash
end

def unscoped_rummager_request
rummager_params.except(:filter_manual).merge(count: "3", reject_manual: scope_object_link)
end
end
end
15 changes: 12 additions & 3 deletions test/unit/search_api_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class SearchAPITest < ActiveSupport::TestCase

setup do
@rummager_api = stub
@rummager_params = stub
@rummager_params = stub(except: {})
@search_params = stub(rummager_parameters: @rummager_params)
@search_api = SearchAPI.new(@rummager_api)
@search_results = stub
Expand All @@ -24,15 +24,19 @@ class SearchAPITest < ActiveSupport::TestCase
end
end

context "given an search scoped to a manual" do
context "given a search scoped to a manual" do
setup do
@manual_link = 'manual/manual-name'
@manual_title = 'Manual Title'

@govuk_result_title = "GOV.UK result"

@search_params.expects(:filtered_by?).with('manual').returns(true)
@search_params.expects(:filter).with('manual').returns([@manual_link])
@manual_search_response = stub(results: [stub(title: @manual_title)])
@unscoped_search_response = stub(to_hash: {title: @govuk_result_title})

@rummager_api.expects(:unified_search).with(count: "3", reject_manual: @manual_link).returns(@unscoped_search_response)
@rummager_api.expects(:unified_search).with(filter_link: @manual_link, count: "1", fields: %w{title}).returns(@manual_search_response)
end

Expand All @@ -43,7 +47,12 @@ class SearchAPITest < ActiveSupport::TestCase

should "return manual from rummager" do
search_response = @search_api.search(@search_params)
assert_equal({title: @manual_title}, search_response.fetch(:scope))
assert_equal({ "title" => @manual_title }, search_response.fetch("scope"))
end

should "return three results for the whole of gov.uk from rummager" do
search_response = @search_api.search(@search_params)
assert_equal({ title: @govuk_result_title }, search_response.fetch("unscoped_results"))
end
end
end

0 comments on commit 5b7049b

Please sign in to comment.