Skip to content

Commit

Permalink
Allow manually calling search-api-v2
Browse files Browse the repository at this point in the history
As a first step to building the integration with `search-api-v2`, allow
`finder-frontend` to call it if and only if an explicit `use_v2` query
parameter is supplied.

- Bump `gds-api-adapters` to get new adapter for `search-api-v2`
- Add `Services#search_api_v2`
- Update `Search::Query` to call `search-api-v2` if `use_v2` parameter
  is present and truthy (accoring to `ActiveModel::Type::Boolean`)
  • Loading branch information
csutter committed Sep 12, 2023
1 parent 5e4c985 commit 91d6b89
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
10 changes: 9 additions & 1 deletion app/lib/search/query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,11 @@ def fetch_search_response(content_item)
override_sort_for_feed:,
).call

if queries.one?
if use_v2_api?
GovukStatsd.time("search_api_v2.finder_search") do
Services.search_api_v2.search(queries.first).to_hash
end
elsif queries.one?
GovukStatsd.time("rummager.finder_search") do
Services.rummager.search(queries.first).to_hash
end
Expand All @@ -99,5 +103,9 @@ def fetch_search_response(content_item)
end
end
end

def use_v2_api?
ActiveModel::Type::Boolean.new.cast(filter_params["use_v2"])
end
end
end
4 changes: 4 additions & 0 deletions app/lib/services.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ def self.rummager
GdsApi::Search.new(Plek.find("search-api"))
end

def self.search_api_v2
GdsApi::SearchApiV2.new(Plek.find("search-api-v2"))
end

def self.email_alert_api
Services::EmailAlertApi.new
end
Expand Down
22 changes: 22 additions & 0 deletions spec/lib/search/query_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ def stub_search
stub_request(:get, %r{#{Plek.find("search-api")}/search.json})
end

def stub_search_v2
stub_request(:get, %r{#{Plek.find("search-api-v2")}/search.json})
end

def stub_batch_search
stub_request(:get, %r{#{Plek.find("search-api")}/batch_search.json})
end
Expand Down Expand Up @@ -45,6 +49,24 @@ def result_item(id, title, score:, popularity:, updated:)
}
end

context "when manually overriding parameters to use the v2 API" do
subject { described_class.new(content_item, { "use_v2" => "true" }).search_results }

before do
stub_search_v2.to_return(body: {
"results" => [
result_item("/i-am-the-v2-api", "I am the v2 API", score: nil, updated: "14-12-19", popularity: 1),
],
}.to_json)
end

it "calls the v2 API" do
results = subject.fetch("results")
expect(results.length).to eq(1)
expect(results.first).to match(hash_including("_id" => "/i-am-the-v2-api"))
end
end

context "when searching using a single query" do
subject { described_class.new(content_item, filter_params).search_results }

Expand Down

0 comments on commit 91d6b89

Please sign in to comment.