Skip to content

Commit

Permalink
Add API endpoint for search suggestions
Browse files Browse the repository at this point in the history
This returns suggestions from the (internal) Search API v2, for
consumption by the autocomplete component soon to be used across GOV.UK
for site search.

- Update `gds-api-adapters` to get access to new `autocomplete` method
  for Search API v2
- Add API endpoint
- Reconfigure autocomplete setup to use v2 `source_key` and the new
  endpoint's path (with an override option for local development)
  • Loading branch information
csutter committed Nov 14, 2024
1 parent 1e0c44b commit 8c1c71f
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ GEM
faker (3.4.1)
i18n (>= 1.8.11, < 2)
ffi (1.17.0)
gds-api-adapters (97.1.0)
gds-api-adapters (97.3.0)
addressable
link_header
null_logger
Expand Down
14 changes: 14 additions & 0 deletions app/controllers/api/autocompletes_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Api::AutocompletesController < ApplicationController
def index
render json: autocomplete_response
end

private

def autocomplete_response
Services
.search_api_v2
.autocomplete(params.require(:q))
.to_hash
end
end
5 changes: 4 additions & 1 deletion app/controllers/finders_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@ def show_page_variables
@pagination = pagination_presenter
@spelling_suggestion_presenter = spelling_suggestion_presenter
@ab_test_search_component = use_autocomplete? ? "search_with_autocomplete" : "search"
@autocomplete_url = ENV.fetch("SEARCH_AUTOCOMPLETE_API_URL", "")
@autocomplete_source_url = ENV.fetch(
"SEARCH_AUTOCOMPLETE_API_URL",
URI.join(Plek.new.website_root, api_search_autocomplete_path).to_s,
)
end

private
Expand Down
4 changes: 2 additions & 2 deletions app/views/finders/show_all_content_finder.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@
wrap_label_in_a_heading: true,
heading_level: 1,
margin_bottom: 0,
source_url: @autocomplete_url,
source_key: "suggested_autocomplete"
source_url: @autocomplete_source_url,
source_key: "suggestions"
} %>
</div>

Expand Down
4 changes: 4 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
get "/healthcheck/live", to: proc { [200, {}, %w[OK]] }
get "/healthcheck/ready", to: proc { [200, {}, [JSON.generate({ status: :ok })]] }

namespace :api do
get "/search/autocomplete" => "autocompletes#index"
end

root to: redirect("/development") unless Rails.env.test?
get "/development" => "development#index"

Expand Down
20 changes: 20 additions & 0 deletions spec/requests/api/autocomplete_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
require "spec_helper"

RSpec.describe "autocomplete API", type: :request do
let(:search_api_v2) { double(:search_api_v2, autocomplete: autocomplete_response) }

let(:suggestions) { %w[blue grey red] }
let(:autocomplete_response) { instance_double(GdsApi::Response, to_hash: { suggestions: }) }

before do
allow(Services).to receive(:search_api_v2).and_return(search_api_v2)
end

it "returns suggestions from Search API v2" do
get "/api/search/autocomplete?q=loving+him+was"

expect(search_api_v2).to have_received(:autocomplete).with("loving him was")
expect(response).to be_successful
expect(JSON.parse(response.body)).to eq("suggestions" => %w[blue grey red])
end
end

0 comments on commit 8c1c71f

Please sign in to comment.