Class: Chewy::Search::Parameters::QueryStorage::Bool

Inherits:
Object
  • Object
show all
Defined in:
lib/chewy/search/parameters/concerns/query_storage.rb

Overview

Bool storage value object, encapsulates update and query rendering logic.

Constant Summary collapse

KEYS =

Acceptable bool query keys

%i[must should must_not minimum_should_match].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(must: [], should: [], must_not: [], minimum_should_match: nil) ⇒ Bool

Returns a new instance of Bool.

Parameters:

  • must (Array<Hash>, Hash, nil) (defaults to: [])
  • should (Array<Hash>, Hash, nil) (defaults to: [])
  • must_not (Array<Hash>, Hash, nil) (defaults to: [])
  • minimum_should_match (String, Integer, nil) (defaults to: nil)


40
41
42
43
44
45
# File 'lib/chewy/search/parameters/concerns/query_storage.rb', line 40

def initialize(must: [], should: [], must_not: [], minimum_should_match: nil)
  @must = normalize(must)
  @should = normalize(should)
  @must_not = normalize(must_not)
  @minimum_should_match = minimum_should_match
end

Instance Attribute Details

#minimum_should_matchString, ...

Returns:

  • (String, Integer, nil)


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
# File 'lib/chewy/search/parameters/concerns/query_storage.rb', line 30

class Bool
  # Acceptable bool query keys
  KEYS = %i[must should must_not minimum_should_match].freeze
  # @!ignorewarning
  attr_reader(*KEYS)

  # @param must [Array<Hash>, Hash, nil]
  # @param should [Array<Hash>, Hash, nil]
  # @param must_not [Array<Hash>, Hash, nil]
  # @param minimum_should_match [String, Integer, nil]
  def initialize(must: [], should: [], must_not: [], minimum_should_match: nil)
    @must = normalize(must)
    @should = normalize(should)
    @must_not = normalize(must_not)
    @minimum_should_match = minimum_should_match
  end

  # Merges 2 values, returns new value object.
  #
  # @param other [Chewy::Search::Parameters::QueryStorage::Bool]
  # @return [Chewy::Search::Parameters::QueryStorage::Bool]
  def update(other)
    self.class.new(
      must: must + other.must,
      should: should + other.should,
      must_not: must_not + other.must_not,
      minimum_should_match: other.minimum_should_match
    )
  end

  # Renders `bool` query.
  #
  # @return [Hash, nil]
  def query
    if must.one? && should.empty? && must_not.empty?
      must.first
    else
      reduced = reduce
      {bool: reduce} if reduced.present?
    end
  end

  # Just a convention.
  #
  # @return [{Symbol => Array<Hash>, String, Integer, nil}]
  def to_h
    {
      must: must,
      should: should,
      must_not: must_not,
      minimum_should_match: minimum_should_match
    }
  end

private

  def reduce
    value = to_h
      .reject { |_, v| v.blank? }
      .transform_values { |v| v.is_a?(Array) && v.one? ? v.first : v }
    value.delete(:minimum_should_match) if should.empty?
    value
  end

  def normalize(queries)
    Array.wrap(queries).map do |query|
      if query.is_a?(Proc)
        Elasticsearch::DSL::Search::Query.new(&query).to_hash
      else
        query
      end
    end.reject(&:blank?)
  end
end

#mustArray<Hash>, ...

Returns:

  • (Array<Hash>, Hash, nil)


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
# File 'lib/chewy/search/parameters/concerns/query_storage.rb', line 30

class Bool
  # Acceptable bool query keys
  KEYS = %i[must should must_not minimum_should_match].freeze
  # @!ignorewarning
  attr_reader(*KEYS)

  # @param must [Array<Hash>, Hash, nil]
  # @param should [Array<Hash>, Hash, nil]
  # @param must_not [Array<Hash>, Hash, nil]
  # @param minimum_should_match [String, Integer, nil]
  def initialize(must: [], should: [], must_not: [], minimum_should_match: nil)
    @must = normalize(must)
    @should = normalize(should)
    @must_not = normalize(must_not)
    @minimum_should_match = minimum_should_match
  end

  # Merges 2 values, returns new value object.
  #
  # @param other [Chewy::Search::Parameters::QueryStorage::Bool]
  # @return [Chewy::Search::Parameters::QueryStorage::Bool]
  def update(other)
    self.class.new(
      must: must + other.must,
      should: should + other.should,
      must_not: must_not + other.must_not,
      minimum_should_match: other.minimum_should_match
    )
  end

  # Renders `bool` query.
  #
  # @return [Hash, nil]
  def query
    if must.one? && should.empty? && must_not.empty?
      must.first
    else
      reduced = reduce
      {bool: reduce} if reduced.present?
    end
  end

  # Just a convention.
  #
  # @return [{Symbol => Array<Hash>, String, Integer, nil}]
  def to_h
    {
      must: must,
      should: should,
      must_not: must_not,
      minimum_should_match: minimum_should_match
    }
  end

private

  def reduce
    value = to_h
      .reject { |_, v| v.blank? }
      .transform_values { |v| v.is_a?(Array) && v.one? ? v.first : v }
    value.delete(:minimum_should_match) if should.empty?
    value
  end

  def normalize(queries)
    Array.wrap(queries).map do |query|
      if query.is_a?(Proc)
        Elasticsearch::DSL::Search::Query.new(&query).to_hash
      else
        query
      end
    end.reject(&:blank?)
  end
end

#must_not[Array<Hash>, Hash, nil]

Returns [Array, Hash, nil].

Returns:

  • ([Array<Hash>, Hash, nil])

    [Array, Hash, nil]



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
# File 'lib/chewy/search/parameters/concerns/query_storage.rb', line 30

class Bool
  # Acceptable bool query keys
  KEYS = %i[must should must_not minimum_should_match].freeze
  # @!ignorewarning
  attr_reader(*KEYS)

  # @param must [Array<Hash>, Hash, nil]
  # @param should [Array<Hash>, Hash, nil]
  # @param must_not [Array<Hash>, Hash, nil]
  # @param minimum_should_match [String, Integer, nil]
  def initialize(must: [], should: [], must_not: [], minimum_should_match: nil)
    @must = normalize(must)
    @should = normalize(should)
    @must_not = normalize(must_not)
    @minimum_should_match = minimum_should_match
  end

  # Merges 2 values, returns new value object.
  #
  # @param other [Chewy::Search::Parameters::QueryStorage::Bool]
  # @return [Chewy::Search::Parameters::QueryStorage::Bool]
  def update(other)
    self.class.new(
      must: must + other.must,
      should: should + other.should,
      must_not: must_not + other.must_not,
      minimum_should_match: other.minimum_should_match
    )
  end

  # Renders `bool` query.
  #
  # @return [Hash, nil]
  def query
    if must.one? && should.empty? && must_not.empty?
      must.first
    else
      reduced = reduce
      {bool: reduce} if reduced.present?
    end
  end

  # Just a convention.
  #
  # @return [{Symbol => Array<Hash>, String, Integer, nil}]
  def to_h
    {
      must: must,
      should: should,
      must_not: must_not,
      minimum_should_match: minimum_should_match
    }
  end

private

  def reduce
    value = to_h
      .reject { |_, v| v.blank? }
      .transform_values { |v| v.is_a?(Array) && v.one? ? v.first : v }
    value.delete(:minimum_should_match) if should.empty?
    value
  end

  def normalize(queries)
    Array.wrap(queries).map do |query|
      if query.is_a?(Proc)
        Elasticsearch::DSL::Search::Query.new(&query).to_hash
      else
        query
      end
    end.reject(&:blank?)
  end
end

#shouldArray<Hash>, ...

Returns:

  • (Array<Hash>, Hash, nil)


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
# File 'lib/chewy/search/parameters/concerns/query_storage.rb', line 30

class Bool
  # Acceptable bool query keys
  KEYS = %i[must should must_not minimum_should_match].freeze
  # @!ignorewarning
  attr_reader(*KEYS)

  # @param must [Array<Hash>, Hash, nil]
  # @param should [Array<Hash>, Hash, nil]
  # @param must_not [Array<Hash>, Hash, nil]
  # @param minimum_should_match [String, Integer, nil]
  def initialize(must: [], should: [], must_not: [], minimum_should_match: nil)
    @must = normalize(must)
    @should = normalize(should)
    @must_not = normalize(must_not)
    @minimum_should_match = minimum_should_match
  end

  # Merges 2 values, returns new value object.
  #
  # @param other [Chewy::Search::Parameters::QueryStorage::Bool]
  # @return [Chewy::Search::Parameters::QueryStorage::Bool]
  def update(other)
    self.class.new(
      must: must + other.must,
      should: should + other.should,
      must_not: must_not + other.must_not,
      minimum_should_match: other.minimum_should_match
    )
  end

  # Renders `bool` query.
  #
  # @return [Hash, nil]
  def query
    if must.one? && should.empty? && must_not.empty?
      must.first
    else
      reduced = reduce
      {bool: reduce} if reduced.present?
    end
  end

  # Just a convention.
  #
  # @return [{Symbol => Array<Hash>, String, Integer, nil}]
  def to_h
    {
      must: must,
      should: should,
      must_not: must_not,
      minimum_should_match: minimum_should_match
    }
  end

private

  def reduce
    value = to_h
      .reject { |_, v| v.blank? }
      .transform_values { |v| v.is_a?(Array) && v.one? ? v.first : v }
    value.delete(:minimum_should_match) if should.empty?
    value
  end

  def normalize(queries)
    Array.wrap(queries).map do |query|
      if query.is_a?(Proc)
        Elasticsearch::DSL::Search::Query.new(&query).to_hash
      else
        query
      end
    end.reject(&:blank?)
  end
end

Instance Method Details

#queryHash?

Renders bool query.

Returns:

  • (Hash, nil)


63
64
65
66
67
68
69
70
# File 'lib/chewy/search/parameters/concerns/query_storage.rb', line 63

def query
  if must.one? && should.empty? && must_not.empty?
    must.first
  else
    reduced = reduce
    {bool: reduce} if reduced.present?
  end
end

#to_h{Symbol => Array<Hash>, String, Integer, nil}

Just a convention.

Returns:

  • ({Symbol => Array<Hash>, String, Integer, nil})


75
76
77
78
79
80
81
82
# File 'lib/chewy/search/parameters/concerns/query_storage.rb', line 75

def to_h
  {
    must: must,
    should: should,
    must_not: must_not,
    minimum_should_match: minimum_should_match
  }
end

#update(other) ⇒ Chewy::Search::Parameters::QueryStorage::Bool

Merges 2 values, returns new value object.



51
52
53
54
55
56
57
58
# File 'lib/chewy/search/parameters/concerns/query_storage.rb', line 51

def update(other)
  self.class.new(
    must: must + other.must,
    should: should + other.should,
    must_not: must_not + other.must_not,
    minimum_should_match: other.minimum_should_match
  )
end