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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
|
require 'forwardable'
require 'singleton'
class PackageRepository < BaseRepository
include Singleton
class << self
extend Forwardable
def_delegators :instance, :suggest, :resolve, :find_atoms_by_useflag, :default_search_size, :default_search,
:build_query, :match_wildcard, :match_phrase, :match_description, :match_category, :scoring_functions
end
index_name "packages-#{Rails.env}"
klass Package
mapping do
indexes :category, type: 'keyword'
indexes :name, type: 'keyword'
indexes :name_sort, type: 'keyword'
indexes :atom, type: 'keyword'
indexes :description, type: 'text'
indexes :longdescription, type: 'text'
indexes :homepage, type: 'keyword'
indexes :license, type: 'keyword'
indexes :licenses, type: 'keyword'
indexes :herds, type: 'keyword'
indexes :maintainers, type: 'object'
indexes :useflags, type: 'object'
indexes :metadata_hash, type: 'keyword'
indexes :created_at, type: 'date'
indexes :updated_at, type: 'date'
end
def suggest(q)
PackageRepository.search(
size: 20,
query: {
wildcard: {
name_sort: {
wildcard: q.downcase + '*'
}
}
}
)
end
# Tries to resolve a query atom to one or more packages
def resolve(atom)
[] if atom.nil? || atom.empty?
PackageRepository.find_all_by(:atom, atom) + PackageRepository.find_all_by(:name, atom)
end
# Searches the versions index for versions using a certain USE flag.
# Results are aggregated by package atoms.
def find_atoms_by_useflag(useflag)
VersionRepository.search(
size: 0, # collect all packages.
query: {
bool: {
must: { match_all: {} },
filter: { term: { use: useflag } }
}
},
aggs: {
group_by_package: {
terms: {
field: 'package',
order: { '_key' => 'asc' },
# https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html
# ES actually dislikes large sizes like this (it defines 10k buckets basically) and it will be *very* expensive but lets try it and see.
# Other limits in this app are also 10k mostly to 'make things fit kinda'.
size: 10000,
}
}
},
).response.aggregations['group_by_package'].buckets
end
def default_search_size
25
end
def default_search(q, offset)
return [] if q.nil? || q.empty?
part1, part2 = q.split('/', 2)
if part2.nil?
search(build_query(part1, nil, default_search_size, offset))
else
search(build_query(part2, part1, default_search_size, offset))
end
end
def build_query(q, category, size, offset)
{
size: size,
from: offset,
query: {
function_score: {
query: { bool: bool_query_parts(q, category) },
functions: scoring_functions
}
}
}
end
def bool_query_parts(q, category = nil)
q_dwncsd = q.downcase
query = {
must: [
match_wildcard(q_dwncsd)
],
should: [
match_phrase(q_dwncsd),
match_description(q)
]
}
query[:must] << [match_category(category)] if category
query
end
def match_wildcard(q)
q = ('*' + q + '*') unless q.include? '*'
q.tr!(' ', '*')
{
wildcard: {
name_sort: {
wildcard: q,
boost: 4
}
}
}
end
def match_phrase(q)
{
match_phrase: {
name: {
query: q,
boost: 5
}
}
}
end
def match_description(q)
{
match: {
description: {
query: q,
boost: 0.1
}
}
}
end
def match_category(cat)
{
match: {
category: {
query: cat,
boost: 2
}
}
}
end
def scoring_functions
[
{
filter: {
term: {
category: 'virtual'
}
},
weight: 0.6
}
]
end
end
|