Skip to content

Commit

Permalink
Merge pull request #173 from alphagov/did-you-mean
Browse files Browse the repository at this point in the history
Implement `suggested_queries` in search response
  • Loading branch information
csutter authored Jan 10, 2024
2 parents 5660e52 + b6f5ac8 commit b7aec84
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 3 deletions.
2 changes: 1 addition & 1 deletion app/models/result_set.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
class ResultSet
include ActiveModel::Model

attr_accessor :results, :total, :start, :discovery_engine_attribution_token
attr_accessor :results, :total, :start, :discovery_engine_attribution_token, :suggested_queries
end
9 changes: 9 additions & 0 deletions app/services/discovery_engine/query/search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,15 @@ def result_set
Rails.logger.debug { "#{self.class.name}: Query: #{discovery_engine_params}" }
response = client.search(discovery_engine_params).response

# Suggested queries always expects an array on the client side, even if there are no
# suggestions.
suggested_queries = suggest_correction? ? [response.corrected_query].compact_blank : []

ResultSet.new(
results: response.results.map { Result.from_stored_document(_1.document.struct_data.to_h) },
total: response.total_size,
start: offset,
suggested_queries:,
discovery_engine_attribution_token: response.attribution_token,
)
end
Expand Down Expand Up @@ -75,6 +80,10 @@ def filter
Filters.new(query_params).filter_expression
end

def suggest_correction?
query_params[:suggest] == "spelling"
end

def serving_config
Rails.configuration.discovery_engine_serving_config
end
Expand Down
3 changes: 2 additions & 1 deletion spec/requests/search_request_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
RSpec.describe "Making a search request" do
let(:search_service) { instance_double(DiscoveryEngine::Query::Search, result_set:) }
let(:result_set) { ResultSet.new(results:, total: 42, start: 21) }
let(:result_set) { ResultSet.new(results:, total: 42, start: 21, suggested_queries: %w[foo]) }
let(:results) { [Result.new(content_id: "123"), Result.new(content_id: "456")] }

before do
Expand All @@ -19,6 +19,7 @@
],
"total" => 42,
"start" => 21,
"suggested_queries" => %w[foo],
})
end

Expand Down
30 changes: 29 additions & 1 deletion spec/services/discovery_engine/query/search_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,21 @@
let(:query_params) { { q: "garden centres" } }

let(:search_return_value) { double(response: search_response) }
let(:search_response) { double(total_size: 42, attribution_token: "footobar", results:) }
let(:search_response) do
double(
total_size: 42,
attribution_token: "footobar",
results:,
corrected_query:,
)
end
let(:results) do
[
double(document: double(struct_data: { title: "Louth Garden Centre" })),
double(document: double(struct_data: { title: "Cleethorpes Garden Centre" })),
]
end
let(:corrected_query) { nil }

it "calls the client with the expected parameters" do
expect(client).to have_received(:search).with(
Expand Down Expand Up @@ -122,6 +130,26 @@
end
end

context "when searching for a query where the client returns a corrected query" do
let(:corrected_query) { "graden crentres" }

context "and the suggest parameter is 'spelling'" do
let(:query_params) { { q: "garden centres", suggest: "spelling" } }

it "returns the corrected query in the result set" do
expect(result_set.suggested_queries).to eq([corrected_query])
end
end

context "and the suggest parameter is not set" do
let(:query_params) { { q: "garden centres" } }

it "does not return a corrected query in the result set" do
expect(result_set.suggested_queries).to be_empty
end
end
end

context "when searching for a query that has a single best bets defined" do
# see test section in YAML config
let(:query_params) { { q: "i want to test a single best bet" } }
Expand Down

0 comments on commit b7aec84

Please sign in to comment.