Class: Elasticsearch::DSL::Search::Filters::Bool

Inherits:
Object
  • Object
show all
Includes:
BaseComponent
Defined in:
lib/elasticsearch/dsl/search/filters/bool.rb

Overview

A compound filter which matches documents based on combinations of filters

See the integration test for a working example.

Examples:

Defining a bool filter with multiple conditions


search do
  query do
    filtered do
      filter 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
  end
end

See Also:

Instance Method Summary collapse

Methods included from BaseComponent

included, #initialize

Instance Method Details

#must(*args, &block) ⇒ Object



62
63
64
65
66
67
# File 'lib/elasticsearch/dsl/search/filters/bool.rb', line 62

def must(*args, &block)
  @hash[name][:must] ||= []
  value = args.empty? ? Filter.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) ⇒ Object



69
70
71
72
73
74
# File 'lib/elasticsearch/dsl/search/filters/bool.rb', line 69

def must_not(*args, &block)
  @hash[name][:must_not] ||= []
  value = args.empty? ? Filter.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) ⇒ Object



76
77
78
79
80
81
# File 'lib/elasticsearch/dsl/search/filters/bool.rb', line 76

def should(*args, &block)
  @hash[name][:should] ||= []
  value = args.empty? ? Filter.new(*args, &block).to_hash : args.first.to_hash
  @hash[name][:should].push(value).flatten! unless @hash[name][:should].include?(value)
  self
end

#to_hashObject



83
84
85
86
87
88
89
90
91
92
93
# File 'lib/elasticsearch/dsl/search/filters/bool.rb', line 83

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