class Ferret::Search::Sort

Summary

A Sort object is used to combine and apply a list of SortFields. The SortFields are applied in the order they are added to the SortObject.

Example

Here is how you would create a Sort object that sorts first by rating and then by title;

sf_rating = SortField.new(:rating, :type => :float, :reverse => true)
sf_title = SortField.new(:title, :type => :string)
sort = Sort.new([sf_rating, sf_title])

Remember that the :type parameter for SortField is set to :auto be default be I strongly recommend you specify a :type value.

Constants

INDEX_ORDER
RELEVANCE

Public Class Methods

new(sort_fields = [SortField::SCORE, SortField::DOC_ID], reverse = false) → Sort click to toggle source

Create a new Sort object. If reverse is true, all sort_fields will be reversed so if any of them are already reversed the will be turned back to their natural order again. By default

static VALUE
frb_sort_init(int argc, VALUE *argv, VALUE self)
{
    int i;
    VALUE rfields, rreverse;
    bool reverse = false;
    bool has_sfd = false;
    GET_SORT();
    switch (rb_scan_args(argc, argv, "02", &rfields, &rreverse)) {
        case 2: reverse = RTEST(rreverse);
        case 1: 
                if (TYPE(rfields) == T_ARRAY) {
                    int i;
                    for (i = 0; i < RARRAY_LEN(rfields); i++) {
                        frb_sort_add(sort, RARRAY_PTR(rfields)[i], reverse);
                    }
                } else {
                    frb_sort_add(sort, rfields, reverse);
                }
                for (i = 0; i < sort->size; i++) {
                    if (sort->sort_fields[i] == &SORT_FIELD_DOC) has_sfd = true;
                }
                if (!has_sfd) {
                    sort_add_sort_field(sort, (SortField *)&SORT_FIELD_DOC);
                }
                break;
        case 0:
                sort_add_sort_field(sort, (SortField *)&SORT_FIELD_SCORE);
                sort_add_sort_field(sort, (SortField *)&SORT_FIELD_DOC);
    }

    return self;
}

Public Instance Methods

fields → Array click to toggle source

Returns an array of the SortFields held by the Sort object.

static VALUE
frb_sort_get_fields(VALUE self)
{
    GET_SORT();
    VALUE rfields = rb_ary_new2(sort->size);
    int i;
    for (i = 0; i < sort->size; i++) {
        rb_ary_store(rfields, i, object_get(sort->sort_fields[i]));
    }
    return rfields;
}
to_s → string click to toggle source

Returns a human readable string representing the sort object.

static VALUE
frb_sort_to_s(VALUE self)
{
    GET_SORT();
    char *str = sort_to_s(sort);
    VALUE rstr = rb_str_new2(str);
    free(str);
    return rstr;
}