class Elasticsearch::DSL::Search::Search

Wraps the whole search definition (queries, filters, aggregations, sorting, etc)

Attributes

aggregations[R]

Public Class Methods

new(*args, &block) click to toggle source
# File lib/elasticsearch/dsl/search.rb, line 59
def initialize(*args, &block)
  @options = Options.new *args
  @block = block
  @block.arity < 1 ? self.instance_eval(&@block) : @block.call(self) if @block
end

Public Instance Methods

aggregation(*args, &block) click to toggle source

DSL method for building the `aggregations` part of a search definition

@return [self]

# File lib/elasticsearch/dsl/search.rb, line 138
def aggregation(*args, &block)
  @aggregations ||= AggregationsCollection.new

  if block
    @aggregations.update args.first => Aggregation.new(*args, &block)
  else
    name = args.shift
    @aggregations.update name => args.shift
  end
  self
end
aggregations=(value) click to toggle source

Set the aggregations part of a search definition

# File lib/elasticsearch/dsl/search.rb, line 152
def aggregations=(value)
  @aggregations = value
end
collapse(*args, &block) click to toggle source

DSL method for building the `collapse` part of a search definition

@return [self, Collapse]

# File lib/elasticsearch/dsl/search.rb, line 173
def collapse(*args, &block)
  if !args.empty? || block
    @collapse = Collapse.new(*args, &block)
    self
  else
    @collapse
  end
end
filter(*args, &block) click to toggle source

DSL method for building the `filter` part of a search definition

@return [self]

# File lib/elasticsearch/dsl/search.rb, line 92
def filter(*args, &block)
  case
    when block
      @filter = Filter.new(*args, &block)
      self
    when !args.empty?
      @filter = args.first
      self
    else
      @filter
  end
end
filter=(value) click to toggle source

Set the filter part of a search definition

# File lib/elasticsearch/dsl/search.rb, line 107
def filter=(value)
  filter value
end
from(value=nil) click to toggle source

DSL method for building the `from` part of a search definition

@return [self]

# File lib/elasticsearch/dsl/search.rb, line 231
def from(value=nil)
  if value
    @from = value
    self
  else
    @from
  end
end
Also aliased as: from=
from=(value=nil)
Alias for: from
highlight(*args, &block) click to toggle source

DSL method for building the `highlight` part of a search definition

@return [self]

# File lib/elasticsearch/dsl/search.rb, line 160
def highlight(*args, &block)
  if !args.empty? || block
    @highlight = Highlight.new(*args, &block)
    self
  else
    @highlight
  end
end
method_missing(name, *args, &block) click to toggle source

Delegates to the methods provided by the {Options} class

Calls superclass method
# File lib/elasticsearch/dsl/search.rb, line 263
def method_missing(name, *args, &block)
  if @options.respond_to? name
    @options.__send__ name, *args, &block
    self
  elsif @block
    @block.binding.eval('self').send(name, *args, &block)
  else
    super
  end
end
post_filter(*args, &block) click to toggle source

DSL method for building the `post_filter` part of a search definition

@return [self]

# File lib/elasticsearch/dsl/search.rb, line 115
def post_filter(*args, &block)
  case
    when block
      @post_filter = Filter.new(*args, &block)
      self
    when !args.empty?
      @post_filter = args.first
      self
    else
      @post_filter
  end
end
post_filter=(value) click to toggle source

Set the post_filter part of a search definition

# File lib/elasticsearch/dsl/search.rb, line 130
def post_filter=(value)
  post_filter value
end
query(*args, &block) click to toggle source

DSL method for building or accessing the `query` part of a search definition

@return [self, {Query}]

# File lib/elasticsearch/dsl/search.rb, line 69
def query(*args, &block)
  case
    when block
      @query = Query.new(*args, &block)
      self
    when !args.empty?
      @query = args.first
      self
    else
      @query
  end
end
query=(value) click to toggle source

Set the query part of a search definition

# File lib/elasticsearch/dsl/search.rb, line 84
def query=(value)
  query value
end
size(value=nil) click to toggle source

DSL method for building the `size` part of a search definition

@return [self]

# File lib/elasticsearch/dsl/search.rb, line 218
def size(value=nil)
  if value
    @size = value
    self
  else
    @size
  end
end
Also aliased as: size=
size=(value=nil)
Alias for: size
sort(*args, &block) click to toggle source

DSL method for building the `sort` part of a search definition

@return [self]

# File lib/elasticsearch/dsl/search.rb, line 186
def sort(*args, &block)
  if !args.empty? || block
    @sort = Sort.new(*args, &block)
    self
  else
    @sort
  end
end
sort=(value) click to toggle source

Set the sort part of a search definition

# File lib/elasticsearch/dsl/search.rb, line 197
def sort=(value)
  @sort = value
end
stored_fields(value=nil) click to toggle source

DSL method for building the `stored_fields` part of a search definition

@return [self]

# File lib/elasticsearch/dsl/search.rb, line 205
def stored_fields(value=nil)
  if value
    @stored_fields = value
    self
  else
    @stored_fields
  end
end
Also aliased as: stored_fields=
stored_fields=(value=nil)
Alias for: stored_fields
suggest(*args, &block) click to toggle source

DSL method for building the `suggest` part of a search definition

@return [self]

# File lib/elasticsearch/dsl/search.rb, line 244
def suggest(*args, &block)
  if !args.empty? || block
    @suggest ||= {}
    key, options = args
    @suggest.update key => Suggest.new(key, options, &block)
    self
  else
    @suggest
  end
end
suggest=(value) click to toggle source

Set the suggest part of a search definition

# File lib/elasticsearch/dsl/search.rb, line 257
def suggest=(value)
  @suggest = value
end
to_hash() click to toggle source

Converts the search definition to a Hash

@return [Hash]

# File lib/elasticsearch/dsl/search.rb, line 278
def to_hash
  hash = {}
  hash.update(query: @query.to_hash)   if @query
  hash.update(filter: @filter.to_hash) if @filter
  hash.update(post_filter: @post_filter.to_hash) if @post_filter
  hash.update(aggregations: @aggregations.reduce({}) { |sum,item| sum.update item.first => item.last.to_hash }) if @aggregations
  hash.update(sort: @sort.to_hash) if @sort
  hash.update(size: @size) if @size
  hash.update(stored_fields: @stored_fields) if @stored_fields
  hash.update(from: @from) if @from
  hash.update(suggest: @suggest.reduce({}) { |sum,item| sum.update item.last.to_hash }) if @suggest
  hash.update(highlight: @highlight.to_hash) if @highlight
  hash.update(collapse: @collapse.to_hash) if @collapse
  hash.update(@options) unless @options.empty?
  hash
end