aboutsummaryrefslogtreecommitdiff
blob: 10a3bfc709167d4cada56562a4c57fa8a92ff931 (plain)
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
require 'forwardable'
require 'singleton'
require_relative './query_parser/search_query_parser'

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 dynamic: 'strict' do
    indexes :id, type: 'keyword'
    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 do
      indexes :name, type: 'keyword'
      indexes :description, type: 'text'
      indexes :type, type: 'keyword'
      indexes :restrict, type: 'keyword'
      indexes :email, type: 'keyword'
    end
    indexes :useflags do
      indexes :local do
        indexes :scope, type: 'keyword'
        indexes :name, type: 'keyword'
        indexes :description, type: 'text'
        indexes :atom, type: 'keyword'
        indexes :use_expand_prefix, type: 'keyword'
      end
      indexes :global do
        indexes :scope, type: 'keyword'
        indexes :name, type: 'keyword'
        indexes :description, type: 'text'
        indexes :atom, type: 'keyword'
        indexes :use_expand_prefix, type: 'keyword'
      end
      indexes :use_expand do
        indexes :scope, type: 'keyword'
        indexes :name, type: 'keyword'
        indexes :description, type: 'text'
        indexes :atom, type: 'keyword'
        indexes :use_expand_prefix, type: 'keyword'
      end
    end
    indexes :metadata_hash, type: 'keyword'
    indexes :created_at, type: 'date'
    indexes :updated_at, type: 'date'
  end

  def suggest(q)
    search(build_query(q, 20, 0))
  end

  # Tries to resolve a query atom to one or more packages
  def resolve(atom)
    [] if atom.blank?

    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, search_size = default_search_size)
    return [] if q.blank?

    search(build_query(q, search_size, offset))
  end

  def build_query(q, size, offset)
    parser = Object.const_get('SearchQueryParser::QueryParser').new
    transformer = Object.const_get('SearchQueryParser::QueryTransformer').new

    {
      size: size,
      from: offset,
      query: {
        function_score: {
          query: { bool: transformer.apply(parser.parse(q)).to_elasticsearch },
          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

  # Parse the "created_at" and "updated_at" fields in the document
  #
  def deserialize(document)
    hash = document['_source']
    hash['created_at'] = Time.parse(hash['created_at']).utc if hash['created_at']
    hash['updated_at'] = Time.parse(hash['updated_at']).utc if hash['updated_at']
    Package.new hash
  end
end