Skip to content

Commit

Permalink
Merge pull request #174 from alphagov/suggest-2
Browse files Browse the repository at this point in the history
Fix suggested queries; refactor `Search` service
  • Loading branch information
csutter authored Jan 10, 2024
2 parents b7aec84 + 0d645a7 commit 628a051
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 18 deletions.
40 changes: 25 additions & 15 deletions app/services/discovery_engine/query/search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,11 @@ def initialize(
)
@query_params = query_params
@client = client

Rails.logger.debug { "Instantiated #{self.class.name}: Query: #{discovery_engine_params}" }
end

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,
Expand All @@ -33,6 +28,10 @@ def result_set

attr_reader :query_params, :client

def response
@response ||= client.search(discovery_engine_params).response
end

def discovery_engine_params
{
query:,
Expand All @@ -49,6 +48,10 @@ def query
query_params[:q].presence || ""
end

def serving_config
Rails.configuration.discovery_engine_serving_config
end

def page_size
query_params[:count].presence&.to_i || DEFAULT_PAGE_SIZE
end
Expand Down Expand Up @@ -80,14 +83,6 @@ 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

def boost_spec
{
condition_boost_specs: [
Expand All @@ -96,5 +91,20 @@ def boost_spec
],
}
end

def suggested_queries
# TODO: Highlighting isn't actually supported by Discovery Engine, and this _always_ returns a
# single suggestion, but we need to do this for API compatibility with Finder Frontend.
# Eventually this should be improved.
return [] unless query_params[:suggest] == "spelling_with_highlighting"

# Gotcha: Discovery Engine returns an empty string rather than null if there is no correction.
return [] if response.corrected_query.blank?

[{
text: response.corrected_query,
highlighted: "<mark>#{response.corrected_query}</mark>",
}]
end
end
end
9 changes: 6 additions & 3 deletions spec/services/discovery_engine/query/search_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,14 @@
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" } }
context "and the suggest parameter is 'spelling_with_highlighting'" do
let(:query_params) { { q: "garden centres", suggest: "spelling_with_highlighting" } }

it "returns the corrected query in the result set" do
expect(result_set.suggested_queries).to eq([corrected_query])
expect(result_set.suggested_queries).to eq([{
text: "graden crentres",
highlighted: "<mark>graden crentres</mark>",
}])
end
end

Expand Down

0 comments on commit 628a051

Please sign in to comment.