From 91d6b8913c0311bb8b838bbc176127546e5a7310 Mon Sep 17 00:00:00 2001 From: Christian Sutter Date: Tue, 12 Sep 2023 10:34:48 +0000 Subject: [PATCH] Allow manually calling `search-api-v2` 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`) --- app/lib/search/query.rb | 10 +++++++++- app/lib/services.rb | 4 ++++ spec/lib/search/query_spec.rb | 22 ++++++++++++++++++++++ 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/app/lib/search/query.rb b/app/lib/search/query.rb index 7f10b0bff..f4df88edb 100644 --- a/app/lib/search/query.rb +++ b/app/lib/search/query.rb @@ -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 @@ -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 diff --git a/app/lib/services.rb b/app/lib/services.rb index 657ad5ebc..6b0676003 100644 --- a/app/lib/services.rb +++ b/app/lib/services.rb @@ -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 diff --git a/spec/lib/search/query_spec.rb b/spec/lib/search/query_spec.rb index 5c5c1f7de..ef12cab19 100644 --- a/spec/lib/search/query_spec.rb +++ b/spec/lib/search/query_spec.rb @@ -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 @@ -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 }