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 Oct 29, 2018
1 parent c5044da commit 5d5b0e4
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
15 changes: 15 additions & 0 deletions lib/gds_api/rummager.rb
Expand Up @@ -70,6 +70,21 @@ 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 do |search, index|
url_friendly_searches << { 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 5d5b0e4

Please sign in to comment.