-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #317 from alphagov/autocomp
Add autocomplete API
- Loading branch information
Showing
10 changed files
with
132 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
class AutocompletesController < ApplicationController | ||
def show | ||
render json: DiscoveryEngine::Autocomplete::Complete.new(query).completion_result | ||
end | ||
|
||
private | ||
|
||
def query | ||
params[:q] | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# Represents a set of query completion results for an autocomplete feature | ||
class CompletionResult | ||
include ActiveModel::Model | ||
|
||
attr_accessor :suggestions | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
module DiscoveryEngine::Autocomplete | ||
class Complete | ||
QUERY_MODEL = "user-event".freeze | ||
|
||
def initialize( | ||
query, | ||
client: ::Google::Cloud::DiscoveryEngine.completion_service(version: :v1) | ||
) | ||
@query = query | ||
@client = client | ||
end | ||
|
||
def completion_result | ||
CompletionResult.new(suggestions:) | ||
end | ||
|
||
private | ||
|
||
attr_reader :query, :client | ||
|
||
def suggestions | ||
# Discovery Engine returns an error on an empty query, so we need to handle it ourselves | ||
return [] if query.blank? | ||
|
||
Metrics::Exported.increment_counter(:autocomplete_requests) | ||
|
||
client | ||
.complete_query(complete_query_request) | ||
.query_suggestions | ||
.map(&:suggestion) | ||
end | ||
|
||
def complete_query_request | ||
{ | ||
data_store:, | ||
query:, | ||
query_model: QUERY_MODEL, | ||
} | ||
end | ||
|
||
def data_store | ||
Rails.configuration.discovery_engine_datastore | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
RSpec.describe "Making an autocomplete request" do | ||
let(:autocomplete_service) { instance_double(DiscoveryEngine::Autocomplete::Complete, completion_result:) } | ||
let(:completion_result) { CompletionResult.new(suggestions: %w[foo foobar foobaz]) } | ||
|
||
before do | ||
allow(DiscoveryEngine::Autocomplete::Complete).to receive(:new) | ||
.with("foo").and_return(autocomplete_service) | ||
end | ||
|
||
describe "GET /autocomplete.json" do | ||
it "returns a set of suggestions as JSON" do | ||
get "/autocomplete.json?q=foo" | ||
|
||
expect(response).to have_http_status(:ok) | ||
expect(JSON.parse(response.body)).to eq({ | ||
"suggestions" => %w[foo foobar foobaz], | ||
}) | ||
end | ||
end | ||
end |
40 changes: 40 additions & 0 deletions
40
spec/services/discovery_engine/autocomplete/complete_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
RSpec.describe DiscoveryEngine::Autocomplete::Complete do | ||
subject(:completion) { described_class.new(query, client:) } | ||
|
||
let(:client) { double("completion service", complete_query:) } | ||
|
||
before do | ||
allow(Rails.configuration).to receive(:discovery_engine_datastore).and_return("/the/datastore") | ||
end | ||
|
||
describe "#completion_result" do | ||
subject(:completion_result) { completion.completion_result } | ||
|
||
let(:query) { "foo" } | ||
let(:complete_query) { double("response", query_suggestions:) } | ||
let(:query_suggestions) { %w[foo foobar foobaz].map { double("suggestion", suggestion: _1) } } | ||
|
||
it "returns the suggestions from the search response" do | ||
expect(completion_result.suggestions).to eq(%w[foo foobar foobaz]) | ||
end | ||
|
||
it "makes a request to the completion service with the right parameters" do | ||
completion_result | ||
|
||
expect(client).to have_received(:complete_query).with( | ||
data_store: "/the/datastore", | ||
query:, | ||
query_model: "user-event", | ||
) | ||
end | ||
|
||
context "when the query is empty" do | ||
let(:query) { "" } | ||
|
||
it "returns an empty array and does not make a request" do | ||
expect(completion_result.suggestions).to eq([]) | ||
expect(client).not_to have_received(:complete_query) | ||
end | ||
end | ||
end | ||
end |