Class: Elasticsearch::DSL::Search::Filters::Nested

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

Overview

A filter which executes another filter in the context of a nested document

Examples:


search do
  query do
    filtered do
      filter do
        nested do
          path 'comments'
          filter do
            term 'comments.title' => 'Ruby'
          end
        end
      end
    end
  end
end

See Also:

Instance Method Summary collapse

Methods included from BaseComponent

included, #initialize

Instance Method Details

#filter(*args, &block) ⇒ self

DSL method for building the ‘filter` part of the query definition

Returns:

  • (self)


40
41
42
43
# File 'lib/elasticsearch/dsl/search/filters/nested.rb', line 40

def filter(*args, &block)
  @filter = block ? Filter.new(*args, &block) : args.first
  self
end

#query(*args, &block) ⇒ Object



45
46
47
48
# File 'lib/elasticsearch/dsl/search/filters/nested.rb', line 45

def query(*args, &block)
  @query = block ? Elasticsearch::DSL::Search::Query.new(*args, &block) : args.first
  self
end

#to_hashHash

Converts the query definition to a Hash

Returns:

  • (Hash)


54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/elasticsearch/dsl/search/filters/nested.rb', line 54

def to_hash
  hash = super
  if @filter
    _filter = @filter.respond_to?(:to_hash) ? @filter.to_hash : @filter
    hash[self.name].update(filter: _filter)
  end
  if @query
    _query = @query.respond_to?(:to_hash) ? @query.to_hash : @query
    hash[self.name].update(query: _query)
  end
  hash
end