Class: Elasticsearch::DSL::Search::Sort

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

Overview

Wraps the ‘sort` part of a search definition

Instance Method Summary collapse

Methods included from BaseComponent

included

Constructor Details

#initialize(*args, &block) ⇒ Sort

Returns a new instance of Sort.



16
17
18
19
# File 'lib/elasticsearch/dsl/search/sort.rb', line 16

def initialize(*args, &block)
  @value ||= []
  super
end

Instance Method Details

#by(name, direction = nil) ⇒ Object

DSL method to specify sorting item

Examples:


search do
  sort do
    by :category
    by :clicks, order: 'desc'
  end
end


32
33
34
35
# File 'lib/elasticsearch/dsl/search/sort.rb', line 32

def by(name, direction=nil)
  @value << ( direction ? { name => direction } : name )
  self
end

#empty?Boolean

Return whether the definition is empty

Returns:

  • (Boolean)


57
58
59
# File 'lib/elasticsearch/dsl/search/sort.rb', line 57

def empty?
  to_hash.empty?
end

#to_hashHash

Convert the definition to a Hash

Returns:

  • (Hash)


41
42
43
44
45
46
47
48
49
50
51
# File 'lib/elasticsearch/dsl/search/sort.rb', line 41

def to_hash
  if @block
    call unless @block_called
    @block_called = true
  else
    @value << @args if @args && !@args.empty? && ! @value.include?(@args)
  end

  @hash = @value.flatten
  @hash
end