summaryrefslogtreecommitdiff
path: root/lib/chef_zero/solr/query/range_query.rb
blob: 7eed4940b5d3514da62ff5132d4cd721b2eda35a (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
module ChefZero
  module Solr
    module Query
      class RangeQuery
        def initialize(from, to, from_inclusive, to_inclusive)
          @from = from
          @to = to
          @from_inclusive = from_inclusive
          @to_inclusive = to_inclusive
        end

        def to_s
          "#{@from_inclusive ? "[" : "{"}#{@from} TO #{@to}#{@to_inclusive ? "]" : "}"}"
        end

        def matches_values?(values)
          values.any? do |value|
            unless @from == "*"
              case @from <=> value
              when -1
                return false
              when 0
                return false unless @from_inclusive
              end
            end
            unless @to == "*"
              case value <=> @to
              when 1
                return false
              when 0
                return false unless @to_inclusive
              end
            end
            return true
          end
        end

        def matches_doc?(doc)
          matches_values?(doc[DEFAULT_FIELD])
        end

        DEFAULT_FIELD = "text".freeze
      end
    end
  end
end