Skip to content

Commit b839032

Browse files
authored
Merge pull request #139 from alphagov/boosts-refactor
Refactor boost specs for `DiscoveryEngine::Search`
2 parents bd3960a + 56514d5 commit b839032

File tree

4 files changed

+33
-22
lines changed

4 files changed

+33
-22
lines changed

app/services/discovery_engine/best_bets_boost.rb

Lines changed: 0 additions & 14 deletions
This file was deleted.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
module DiscoveryEngine::Boosts
2+
class BestBets
3+
def initialize(query_string)
4+
@query_string = query_string
5+
end
6+
7+
def boost_specs
8+
return unless best_bets_for_query.any?
9+
10+
[{
11+
boost: 1,
12+
condition: "link: ANY(#{condition_links})",
13+
}]
14+
end
15+
16+
private
17+
18+
attr_reader :query_string
19+
20+
def best_bets_for_query
21+
Array(Rails.configuration.best_bets[query_string])
22+
end
23+
24+
def condition_links
25+
best_bets_for_query.map { "\"#{_1}\"" }.join(",")
26+
end
27+
end
28+
end

app/services/discovery_engine/news_recency_boost.rb renamed to app/services/discovery_engine/boosts/news_recency.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
module DiscoveryEngine
2-
module NewsRecencyBoost
1+
module DiscoveryEngine::Boosts
2+
class NewsRecency
33
FRESH_AGE = 1.week
44
RECENT_AGE = 3.months
55
OLD_AGE = 1.year
66
ANCIENT_AGE = 4.years
77

8-
def news_recency_boost_specs
8+
def boost_specs
99
[
1010
{
1111
boost: 0.2,

app/services/discovery_engine/search.rb

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@ class Search
33
DEFAULT_PAGE_SIZE = 10
44
DEFAULT_OFFSET = 0
55

6-
include BestBetsBoost
7-
include NewsRecencyBoost
8-
96
def initialize(
107
query_params,
118
client: ::Google::Cloud::DiscoveryEngine.search_service(version: :v1)
@@ -53,8 +50,8 @@ def serving_config
5350
def boost_spec
5451
{
5552
condition_boost_specs: [
56-
*news_recency_boost_specs,
57-
*best_bets_boost_specs(query),
53+
*Boosts::NewsRecency.new.boost_specs,
54+
*Boosts::BestBets.new(query).boost_specs,
5855
],
5956
}
6057
end

0 commit comments

Comments
 (0)