Class: OpenSearch::DSL::Search::Query

Inherits:
Object
  • Object
show all
Defined in:
lib/opensearch/dsl/search/query.rb

Overview

Wraps the ‘query` part of a search definition

Instance Method Summary collapse

Constructor Details

#initialize(*args, &block) ⇒ Query

Returns a new instance of Query.



39
40
41
# File 'lib/opensearch/dsl/search/query.rb', line 39

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



47
48
49
50
51
52
53
54
# File 'lib/opensearch/dsl/search/query.rb', line 47

def method_missing(name, *args, &block)
  klass = Utils.__camelize(name)
  if Queries.const_defined? klass
    @value = Queries.const_get(klass).new *args, &block
  else
    raise NoMethodError, "undefined method '#{name}' for #{self}"
  end
end

Instance Method Details

#callself

Evaluates any block passed to the query

Returns:

  • (self)


60
61
62
63
# File 'lib/opensearch/dsl/search/query.rb', line 60

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)


69
70
71
72
73
74
75
76
# File 'lib/opensearch/dsl/search/query.rb', line 69

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