class Elasticsearch::DSL::Search::Queries::Bool

A compound query which matches documents based on combinations of queries

@example Defining a bool query with multiple conditions

search do
  query do
    bool do
      must do
        term category: 'men'
      end

      must do
        term size:  'xxl'
      end

      should do
        term color: 'red'
      end

      must_not do
        term manufacturer: 'evil'
      end
    end
  end
end

See the integration test for a working example.

@see www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html

Public Instance Methods

filter(*args, &block) click to toggle source
# File lib/elasticsearch/dsl/search/queries/bool.rb, line 80
def filter(*args, &block)
  @hash[name][:filter] ||= []
  if filter = block ? Filter.new(*args, &block) : args.first
    @hash[name][:filter] << filter.to_hash
  end
  self
end
must(*args, &block) click to toggle source
# File lib/elasticsearch/dsl/search/queries/bool.rb, line 59
def must(*args, &block)
  @hash[name][:must] ||= []
  value = args.empty? ? Query.new(*args, &block).to_hash : args.first.to_hash
  @hash[name][:must].push(value).flatten! unless @hash[name][:must].include?(value)
  self
end
must_not(*args, &block) click to toggle source
# File lib/elasticsearch/dsl/search/queries/bool.rb, line 66
def must_not(*args, &block)
  @hash[name][:must_not] ||= []
  value = args.empty? ? Query.new(*args, &block).to_hash : args.first.to_hash
  @hash[name][:must_not].push(value).flatten! unless @hash[name][:must_not].include?(value)
  self
end
should(*args, &block) click to toggle source
# File lib/elasticsearch/dsl/search/queries/bool.rb, line 73
def should(*args, &block)
  @hash[name][:should] ||= []
  value = args.empty? ? Query.new(*args, &block).to_hash : args.first.to_hash
  @hash[name][:should].push(value).flatten! unless @hash[name][:should].include?(value)
  self
end
to_hash() click to toggle source
# File lib/elasticsearch/dsl/search/queries/bool.rb, line 88
def to_hash
  @hash[name].update(@args.to_hash) if @args.respond_to?(:to_hash)

  if @block
    call
  else
    @hash[name] = @args unless @args.nil? || @args.empty?
  end
  @hash
end