aboutsummaryrefslogtreecommitdiff
blob: 263eb25ccdca40b53eb2cabecdf8e2cb4a39ffe9 (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
# Contains the search logic for packages
module Kkuleomi::Store::Models::PackageSearch
  def self.included(base)
    base.send :include, InstanceMethods
    base.extend ClassMethods
  end

  module ClassMethods
    def suggest(q)
      Package.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?

      Package.find_all_by(:atom, atom) + Package.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)
      Version.search(
        size: 10000, # default limit is 10.
        query: {
          bool: {
            must: { match_all: {} },
            filter: { term: { use: useflag } }
          }
        },
        aggs: {
          group_by_package: {
            terms: {
              field: 'package',
              order: { '_key' => 'asc' }
            }
          }
        },
        size: 0
      ).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

  module InstanceMethods
  end
end