-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathpopularity_lookup.rb
104 lines (87 loc) · 2.33 KB
/
popularity_lookup.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
module Indexer
class PopularityLookup
def initialize(index_name, search_config)
@index_name = index_name
@search_config = search_config
end
def lookup_popularities(links)
if traffic_index.nil?
return {}
end
results = traffic_index.raw_search({
query: {
terms: {
path_components: links,
},
},
_source: { includes: %w[rank_14 vc_14] },
sort: [
{ rank_14: { order: "asc" } },
],
size: 10 * links.size,
})
default_rank = Hash.new(
rank: traffic_index_size,
view_count: 1,
)
ranks = results["hits"]["hits"].each_with_object(default_rank) do |hit, hsh|
link = hit["_id"]
rank = Array(hit.dig("_source", "rank_14")).first
view_count = hit.dig("_source", "vc_14") || 1
next if rank.nil?
hsh[link] = { rank:, view_count: }
end
Hash[links.map do |link|
if ranks[link][:rank].zero?
popularity_score = 0
popularity_rank = 1
else
popularity_score = 1.0 / (ranks[link][:rank] + SearchConfig.popularity_rank_offset)
popularity_rank = traffic_index_size - (ranks[link][:rank] || traffic_index_size)
end
view_count = ranks[link][:view_count]
[
link,
{
popularity_score:,
popularity_rank:,
view_count:,
},
]
end]
end
private
def traffic_index
if @opened_traffic_index
return @traffic_index
end
@traffic_index = open_traffic_index
@opened_traffic_index = true
@traffic_index
end
def traffic_index_size
@traffic_index_size ||= begin
results = traffic_index.raw_search({
query: { match_all: {} },
size: 0,
})
results["hits"]["total"]
end
end
def open_traffic_index
if @index_name.start_with?("page-traffic")
return nil
end
traffic_index_name = SearchConfig.auxiliary_index_names.find do |index|
index.start_with?("page-traffic")
end
if traffic_index_name
result = @search_config.search_server.index(traffic_index_name)
if result.exists?
return result
end
end
nil
end
end
end