Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to batch search Rummager #855

Merged
merged 1 commit into from Nov 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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