Class: Chewy::Search::Parameters::QueryStorage::Bool
- Inherits:
-
Object
- Object
- Chewy::Search::Parameters::QueryStorage::Bool
- 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
- #minimum_should_match ⇒ String, ...
- #must ⇒ Array<Hash>, ...
-
#must_not ⇒ [Array<Hash>, Hash, nil]
[Array<Hash>, Hash, nil].
- #should ⇒ Array<Hash>, ...
Instance Method Summary collapse
-
#initialize(must: [], should: [], must_not: [], minimum_should_match: nil) ⇒ Bool
constructor
A new instance of Bool.
-
#query ⇒ Hash?
Renders ‘bool` query.
-
#to_h ⇒ {Symbol => Array<Hash>, String, Integer, nil}
Just a convention.
-
#update(other) ⇒ Chewy::Search::Parameters::QueryStorage::Bool
Merges 2 values, returns new value object.
Constructor Details
#initialize(must: [], should: [], must_not: [], minimum_should_match: nil) ⇒ Bool
Returns a new instance of Bool.
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_match ⇒ String, ...
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 ⇒ Array<Hash>, ...
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>, 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 |
#should ⇒ Array<Hash>, ...
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
#query ⇒ Hash?
Renders ‘bool` query.
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.
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 |