Class: Elasticsearch::DSL::Search::Filter

Inherits:
Object
  • Object
show all
Defined in:
lib/elasticsearch/dsl/search/filter.rb

Overview

Wraps the ‘filter` part of a search definition, aggregation, etc

Instance Method Summary collapse

Constructor Details

#initialize(*args, &block) ⇒ Filter

Returns a new instance of Filter.



18
19
20
# File 'lib/elasticsearch/dsl/search/filter.rb', line 18

def initialize(*args, &block)
  @block = block
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object

Looks up the corresponding class for a method being invoked, and initializes it

Raises:

  • (NoMethodError)

    When the corresponding class cannot be found



26
27
28
29
30
31
32
33
34
35
# File 'lib/elasticsearch/dsl/search/filter.rb', line 26

def method_missing(name, *args, &block)
  klass = Utils.__camelize(name)
  if Filters.const_defined? klass
    @value = Filters.const_get(klass).new *args, &block
  elsif @block
    @block.binding.eval('self').send(name, *args, &block)
  else
    super
  end
end

Instance Method Details

#callself

Evaluates any block passed to the query

Returns:

  • (self)


41
42
43
44
# File 'lib/elasticsearch/dsl/search/filter.rb', line 41

def call
  @block.arity < 1 ? self.instance_eval(&@block) : @block.call(self) if @block
  self
end

#to_hash(options = {}) ⇒ Hash

Converts the query definition to a Hash

Returns:

  • (Hash)


50
51
52
53
54
55
56
57
# File 'lib/elasticsearch/dsl/search/filter.rb', line 50

def to_hash(options={})
  call
  if @value
    @value.to_hash
  else
    {}
  end
end