Class: Ferret::Search::SortField

Inherits:
Object
  • Object
show all
Defined in:
lib/ferret/search/sort_field.rb

Overview

Stores information about how to sort documents by terms in an individual field. Fields must be indexed in order to sort by them.

Defined Under Namespace

Classes: SortType

Constant Summary collapse

FIELD_SCORE =

Represents sorting by document score (relevancy).

SortField.new(nil, {:sort_type => SortType::SCORE})
FIELD_DOC =

Represents sorting by document number (order).

SortField.new(nil, {:sort_type => SortType::DOC})

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name = nil, args = {}) ⇒ SortField

Creates a SortField which specifies which field the data is sorted on and how that field is sorted. See SortType.

name

Name of field to sort by. Can be nil if sort_type is SCORE or

DOC.

A hash with the followind values can also be supplied;

sort_type

Type of values in the terms.

reverse

True if natural order should be reversed.

comparator

a proc used to compare two values from the index. You can

also give this value to the SortType object that you pass.


64
65
66
67
68
69
70
71
72
73
# File 'lib/ferret/search/sort_field.rb', line 64

def initialize(name = nil, args= {})
  @name = name
  @sort_type = args[:sort_type]||SortType::AUTO
  @reverse = args[:reverse]||false
  @comparator = args[:comparator]||@sort_type.comparator
  if (@name == nil and @sort_type != SortType::DOC and
      @sort_type != SortType::SCORE)
    raise ArgumentError, "You must supply a field name for your sort field"
  end
end

Instance Attribute Details

#comparatorObject (readonly)

Returns the value of attribute comparator.



47
48
49
# File 'lib/ferret/search/sort_field.rb', line 47

def comparator
  @comparator
end

#nameObject (readonly)

Returns the value of attribute name.



47
48
49
# File 'lib/ferret/search/sort_field.rb', line 47

def name
  @name
end

#sort_typeObject (readonly)

Returns the value of attribute sort_type.



47
48
49
# File 'lib/ferret/search/sort_field.rb', line 47

def sort_type
  @sort_type
end

Instance Method Details

#reverse?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/ferret/search/sort_field.rb', line 49

def reverse?
  return @reverse
end

#to_sObject



81
82
83
84
85
# File 'lib/ferret/search/sort_field.rb', line 81

def to_s() 
  buffer = '"' + (@name||"<#{@sort_type}>") + '"'
  buffer << '!' if @reverse
  return buffer
end