Skip to content

Commit

Permalink
Merge pull request #139 from alphagov/boosts-refactor
Browse files Browse the repository at this point in the history
Refactor boost specs for `DiscoveryEngine::Search`
  • Loading branch information
csutter authored Dec 7, 2023
2 parents bd3960a + 56514d5 commit b839032
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 22 deletions.
14 changes: 0 additions & 14 deletions app/services/discovery_engine/best_bets_boost.rb

This file was deleted.

28 changes: 28 additions & 0 deletions app/services/discovery_engine/boosts/best_bets.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
module DiscoveryEngine::Boosts
class BestBets
def initialize(query_string)
@query_string = query_string
end

def boost_specs
return unless best_bets_for_query.any?

[{
boost: 1,
condition: "link: ANY(#{condition_links})",
}]
end

private

attr_reader :query_string

def best_bets_for_query
Array(Rails.configuration.best_bets[query_string])
end

def condition_links
best_bets_for_query.map { "\"#{_1}\"" }.join(",")
end
end
end
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
module DiscoveryEngine
module NewsRecencyBoost
module DiscoveryEngine::Boosts
class NewsRecency
FRESH_AGE = 1.week
RECENT_AGE = 3.months
OLD_AGE = 1.year
ANCIENT_AGE = 4.years

def news_recency_boost_specs
def boost_specs
[
{
boost: 0.2,
Expand Down
7 changes: 2 additions & 5 deletions app/services/discovery_engine/search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ class Search
DEFAULT_PAGE_SIZE = 10
DEFAULT_OFFSET = 0

include BestBetsBoost
include NewsRecencyBoost

def initialize(
query_params,
client: ::Google::Cloud::DiscoveryEngine.search_service(version: :v1)
Expand Down Expand Up @@ -53,8 +50,8 @@ def serving_config
def boost_spec
{
condition_boost_specs: [
*news_recency_boost_specs,
*best_bets_boost_specs(query),
*Boosts::NewsRecency.new.boost_specs,
*Boosts::BestBets.new(query).boost_specs,
],
}
end
Expand Down

0 comments on commit b839032

Please sign in to comment.