class Chewy::Search::Parameters::QueryStorage::Bool

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

@!attribute must

@return [Array<Hash>, Hash, nil]

@!attribute should

@return [Array<Hash>, Hash, nil]

@!attribute must_not

@return [[Array<Hash>, Hash, nil]

@!attribute minimum_should_match

@return [String, Integer, nil]

Constants

KEYS

Acceptable bool query keys

Public Class Methods

new(must: [], should: [], must_not: [], minimum_should_match: nil) click to toggle source

@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]

# 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

Public Instance Methods

query() click to toggle source

Renders ‘bool` query.

@return [Hash, nil]

# 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() click to toggle source

Just a convention.

@return [{Symbol => Array<Hash>, String, Integer, nil}]

# 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) click to toggle source

Merges 2 values, returns new value object.

@param other [Chewy::Search::Parameters::QueryStorage::Bool] @return [Chewy::Search::Parameters::QueryStorage::Bool]

# 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

Private Instance Methods

normalize(queries) click to toggle source
# File lib/chewy/search/parameters/concerns/query_storage.rb, line 94
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
reduce() click to toggle source
# File lib/chewy/search/parameters/concerns/query_storage.rb, line 86
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