Skip to content

Commit

Permalink
Add ability to batch search Rummager
Browse files Browse the repository at this point in the history
This commit enables the new batch search endpoint to be called on Rummager, where each of the individual queries to be batched is encoded in a readable string as part of the query string.

Co-authored-by: Oscar Wyatt <oscar.wyatt@digital.cabinet-office.gov.uk>
  • Loading branch information
2 people authored and Vanita Barrett committed Nov 1, 2018
1 parent f043764 commit cc3286f
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
@@ -1,3 +1,7 @@
# 54.1.0

* Add ability to batch search Rummager

# 54.0.0

* Expect `GdsApi::TestHelpers::Organisations` to be using the public API instead of Whitehall.
Expand Down
14 changes: 14 additions & 0 deletions lib/gds_api/rummager.rb
Expand Up @@ -70,6 +70,20 @@ def search(args, additional_headers = {})
get_json(request_url, additional_headers)
end

# Perform a batch search.
#
# @param searches [Array] An array valid search queries. Maximum of 6. See Rummager documentation for options.
#
# # @see https://github.com/alphagov/rummager/blob/master/doc/search-api.md
def batch_search(searches, additional_headers = {})
url_friendly_searches = searches.each_with_index.map do |search, index|
{ index => search }
end
searches_query = { search: url_friendly_searches }
request_url = "#{base_url}/batch_search?.json?search=#{Rack::Utils.build_nested_query(searches_query)}"
get_json(request_url, additional_headers)
end

# Perform a search, returning the results as an enumerator.
#
# The enumerator abstracts away rummager's pagination and fetches new pages when
Expand Down
15 changes: 15 additions & 0 deletions test/rummager_test.rb
Expand Up @@ -5,6 +5,7 @@
before(:each) do
stub_request(:get, /example.com\/advanced_search/).to_return(body: "[]")
stub_request(:get, /example.com\/search/).to_return(body: "[]")
stub_request(:get, /example.com\/batch_search/).to_return(body: "[]")
end

# tests for #advanced_search
Expand Down Expand Up @@ -138,6 +139,20 @@
assert_requested :get, /.*/, headers: { "authorization" => "token" }
end

it "#batch_search should issue a single request containing all queries" do
GdsApi::Rummager.new("http://example.com").batch_search([{ q: 'self assessment' }, { q: 'tax return' }])

assert_requested :get, /\[\]\[0\]\[q\]=self assessment/
assert_requested :get, /\[\]\[1\]\[q\]=tax return/
end

it "#batch_search should return the search deserialized from json" do
batch_search_results = [{ "title" => "document-title" }, { "title" => "document-title-2" }]
stub_request(:get, /example.com\/batch_search/).to_return(body: batch_search_results.to_json)
results = GdsApi::Rummager.new("http://example.com").batch_search([{ q: 'self assessment' }, { q: 'tax return' }])
assert_equal batch_search_results, results.to_hash
end

# tests for search_enum
it "#search_enum returns two pages - last page is half full" do
stub_request(:get, /example.com\/search/)
Expand Down

0 comments on commit cc3286f

Please sign in to comment.