-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathbooster.rb
63 lines (56 loc) · 1.63 KB
/
booster.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
module QueryComponents
class Booster < BaseComponent
BOOST_CONFIG = YAML.load_file("config/query/boosting.yml")["base"]
DEFAULT_BOOST = 1
def wrap(core_query)
return core_query if search_params.disable_boosting?
{
function_score: {
boost_mode: :multiply,
score_mode: :multiply,
query: {
bool: {
should: [core_query],
},
},
functions: boost_filters,
},
}
end
private
def boost_filters
property_boosts + [time_boost]
end
def property_boosts
BOOST_CONFIG.flat_map do |property, boosts|
boosts.map do |value, boost|
{
filter: { term: { property.to_sym => value } },
weight: boost,
}
end
end
end
# An implementation of http://wiki.apache.org/solr/FunctionQuery#recip
# Curve for 2 months: http://www.wolframalpha.com/share/clip?f=d41d8cd98f00b204e9800998ecf8427e5qr62u0si
#
# Behaves as a freshness boost for newer documents with a public_timestamp and search_format_types announcement
def time_boost
{
filter: { term: { search_format_types: "announcement" } },
script_score: {
script: {
lang: "painless",
source: "((0.05 / ((3.16*Math.pow(10,-11)) * Math.abs(params.now - doc['public_timestamp'].date.getMillis()) + 0.05)) + 0.12)",
params: {
now: time_in_millis_to_nearest_minute,
},
},
},
}
end
def time_in_millis_to_nearest_minute
(Time.now.to_i / 60) * 60_000
end
end
end