Skip to content

Commit

Permalink
Merge pull request #117 from alphagov/best-bets
Browse files Browse the repository at this point in the history
Add simple ability to define best bets
  • Loading branch information
csutter authored Nov 19, 2023
2 parents 0202458 + 7cd526d commit c3e685d
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 2 deletions.
14 changes: 14 additions & 0 deletions app/services/discovery_engine/best_bets_boost.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module DiscoveryEngine
module BestBetsBoost
def best_bets_boost_specs(query_string)
best_bets_for_query = Array(Rails.configuration.best_bets[query_string])
return unless best_bets_for_query.any?

condition_links = best_bets_for_query.map { "\"#{_1}\"" }.join(",")
[{
boost: 1,
condition: "link: ANY(#{condition_links})",
}]
end
end
end
6 changes: 4 additions & 2 deletions app/services/discovery_engine/search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ class Search
DEFAULT_COUNT = 10
DEFAULT_START = 0

include BestBetsBoost
include NewsRecencyBoost

def initialize(client: ::Google::Cloud::DiscoveryEngine.search_service(version: :v1))
Expand All @@ -18,7 +19,7 @@ def call(query_string, start: nil, count: nil)
serving_config:,
page_size: count,
offset: start,
boost_spec:,
boost_spec: boost_spec(query_string),
).response

ResultSet.new(
Expand All @@ -36,10 +37,11 @@ def serving_config
Rails.configuration.discovery_engine_serving_config
end

def boost_spec
def boost_spec(query_string)
{
condition_boost_specs: [
*news_recency_boost_specs,
*best_bets_boost_specs(query_string),
],
}
end
Expand Down
3 changes: 3 additions & 0 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,8 @@ class Application < Rails::Application
config.document_type_ignorelist_path_overrides = config_for(
:document_type_ignorelist_path_overrides,
)

# Query configuration
config.best_bets = config_for(:best_bets)
end
end
13 changes: 13 additions & 0 deletions config/best_bets.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
shared:
# TODO: Test data that we can delete once demo'ed
"i want to test best bets":
- /government/people/test-person
- /government/publications/test--150
- /service-manual/technology/test-your-services-performance

test:
"i want to test a single best bet": /here/you/go
"i want to test multiple best bets":
- /i-am-important
- /i-am-also-important
- /also-me
44 changes: 44 additions & 0 deletions spec/services/discovery_engine/search_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,50 @@
expect(result_set.start).to eq(11)
end
end

context "when searching for a query that has a single best bets defined" do
# see test section in YAML config
subject!(:result_set) { search.call("i want to test a single best bet") }

let(:expected_boost_specs) do
super() + [{
boost: 1,
condition: 'link: ANY("/here/you/go")',
}]
end

it "calls the client with the expected parameters" do
expect(client).to have_received(:search).with(
serving_config: "serving-config-path",
query: "i want to test a single best bet",
offset: 0,
page_size: 10,
boost_spec: { condition_boost_specs: expected_boost_specs },
)
end
end

context "when searching for a query that has multiple best bets defined" do
# see test section in YAML config
subject!(:result_set) { search.call("i want to test multiple best bets") }

let(:expected_boost_specs) do
super() + [{
boost: 1,
condition: 'link: ANY("/i-am-important","/i-am-also-important","/also-me")',
}]
end

it "calls the client with the expected parameters" do
expect(client).to have_received(:search).with(
serving_config: "serving-config-path",
query: "i want to test multiple best bets",
offset: 0,
page_size: 10,
boost_spec: { condition_boost_specs: expected_boost_specs },
)
end
end
end
end
end

0 comments on commit c3e685d

Please sign in to comment.