class Ferret::Search::SortField
Summary¶ ↑
A SortField is used to sort the result-set of a search be the contents of a field. The following types of sort_field are available;
-
:auto
-
:integer
-
:float
-
:string
-
:byte
-
:doc_id
-
:score
The type of the SortField is set by passing it
as a parameter to the constructor. The :auto
type specifies
that the SortField should detect the sort type
by looking at the data in the field. This is the default :type value
although it is recommended that you explicitly specify the fields type.
Example¶ ↑
title_sf = SortField.new(:title, :type => :string) rating_sf = SortField.new(:rating, :type => float, :reverse => true)
Note 1: Care should be taken when using the :auto sort-type since numbers will occur before other strings in the index so if you are sorting a field with both numbers and strings (like a title field which might have “24” and “Prison Break”) then the sort_field will think it is sorting integers when it really should be sorting strings.
Note 2: When sorting by integer, integers are only 4 bytes so anything larger will cause strange sorting behaviour.
Constants
- DOC_ID
- DOC_ID_REV
- SCORE
- SCORE_REV
Public Class Methods
Create a new SortField which can be used to
sort the result-set by the value in field field
.
Options¶ ↑
- :type
-
Default:
:auto
. Specifies how a field should be sorted. Choose from one of;:auto
,:integer
,:float
,:string
,:byte
,:doc_id
or:score
.:auto
will check the datatype of the field by trying to parse it into either a number or a float before settling on a string sort. String sort is locale dependent and works for multibyte character sets like UTF-8 if you have your locale set correctly.
:reverse Default: false. Set to true if you want to reverse the
sort.
static VALUE frb_sf_init(int argc, VALUE *argv, VALUE self) { SortField *sf; VALUE rfield, roptions; VALUE rval; int type = SORT_TYPE_AUTO; int is_reverse = false; Symbol field; if (rb_scan_args(argc, argv, "11", &rfield, &roptions) == 2) { if (Qnil != (rval = rb_hash_aref(roptions, sym_type))) { type = get_sort_type(rval); } if (Qnil != (rval = rb_hash_aref(roptions, sym_reverse))) { is_reverse = RTEST(rval); } if (Qnil != (rval = rb_hash_aref(roptions, sym_comparator))) { rb_raise(rb_eArgError, "Unsupported argument ':comparator'"); } } if (NIL_P(rfield)) rb_raise(rb_eArgError, "must pass a valid field name"); field = frb_field(rfield); sf = sort_field_new(field, type, is_reverse); if (sf->field == NULL) { sf->field = field; } Frt_Wrap_Struct(self, NULL, &frb_sf_free, sf); object_add(sf, self); return self; }
Public Instance Methods
TODO: currently unsupported
static VALUE frb_sf_get_comparator(VALUE self) { return Qnil; }
Returns the name of the field to be sorted.
static VALUE frb_sf_get_name(VALUE self) { GET_SF(); return sf->field ? FSYM2SYM(sf->field) : Qnil; }
Return true if the field is to be reverse sorted. This attribute is set when you create the sort_field.
static VALUE frb_sf_is_reverse(VALUE self) { GET_SF(); return sf->reverse ? Qtrue : Qfalse; }
Return a human readable string describing this sort_field
.
static VALUE frb_sf_to_s(VALUE self) { GET_SF(); char *str = sort_field_to_s(sf); VALUE rstr = rb_str_new2(str); free(str); return rstr; }
Return the type of sort. Should be one of; :auto
,
:integer
, :float
, :string
,
:byte
, :doc_id
or :score
.
static VALUE frb_sf_get_type(VALUE self) { GET_SF(); switch (sf->type) { case SORT_TYPE_BYTE: return sym_byte; case SORT_TYPE_INTEGER: return sym_integer; case SORT_TYPE_FLOAT: return sym_float; case SORT_TYPE_STRING: return sym_string; case SORT_TYPE_AUTO: return sym_auto; case SORT_TYPE_DOC: return sym_doc_id; case SORT_TYPE_SCORE: return sym_score; } return Qnil; }