Module: SearchFlip::Sortable

Included in:
Aggregation, Criteria
Defined in:
lib/search_flip/sortable.rb

Overview

The SearchFlip::Sortable mixin provides the chainable methods #sort as well as #resort

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



6
7
8
9
10
11
12
# File 'lib/search_flip/sortable.rb', line 6

def self.included(base)
  base.class_eval do
    attr_accessor :sort_values

    alias_method :order, :sort
  end
end

Instance Method Details

#resort(*args) ⇒ SearchFlip::Criteria Also known as: reorder

Specify the sort order you want Elasticsearch to use for sorting the results with already existing sort orders being removed.

Examples:

CommentIndex.sort(user_id: "asc").resort(id: "desc")

# Same as

CommentIndex.sort(id: "desc")

Returns:

See Also:



61
62
63
64
65
# File 'lib/search_flip/sortable.rb', line 61

def resort(*args)
  fresh.tap do |criteria|
    criteria.sort_values = args
  end
end

#sort(*args) ⇒ SearchFlip::Criteria

Specify the sort order you want Elasticsearch to use for sorting the results. When you call this multiple times, the sort orders are appended to the already existing ones. The sort arguments get passed to Elasticsearch without modifications, such that you can use sort by script, etc here as well.

Examples:

Default usage

CommentIndex.sort(:user_id, :id)

# Same as

CommentIndex.sort(:user_id).sort(:id)

Default hash usage

CommentIndex.sort(user_id: "asc").sort(id: "desc")

# Same as

CommentIndex.sort({ user_id: "asc" }, { id: "desc" })

Sort by native script

CommentIndex.sort("_script" => "sort_script", lang: "native", order: "asc", type: "number")

Parameters:

  • args

    The sort values that get passed to Elasticsearch

Returns:



41
42
43
44
45
# File 'lib/search_flip/sortable.rb', line 41

def sort(*args)
  fresh.tap do |criteria|
    criteria.sort_values = (sort_values || []) + args
  end
end