class Ferret::Search::TopDocs

Summary

A TopDocs object holds a result set for a search. The number of documents that matched the query his held in TopDocs#total_hits. The actual results are in the Array TopDocs#hits. The number of hits returned is limited by the :limit option so the size of the hits array will not always be equal to the value of total_hits. Finally TopDocs#max_score holds the maximum score of any match (not necessarily the maximum score contained in the hits array) so it can be used to normalize scores. For example, to print doc ids with scores out of 100.0 you could do this;

top_docs.hits.each do |hit|
  puts "#{hit.doc} scored #{hit.score * 100.0 / top_docs.max_score}"
end

Public Instance Methods

to_json() → string click to toggle source

Returns a json representation of the top_doc.

static VALUE
frb_td_to_json(VALUE self)
{
        int i;
        VALUE rhits = rb_funcall(self, id_hits, 0);
        VALUE rhit;
        LazyDoc *lzd;
        Searcher *sea = (Searcher *)DATA_PTR(rb_funcall(self, id_searcher, 0));
        const int num_hits = RARRAY_LEN(rhits);
        int doc_id;
    int len = 32768;
        char *str = ALLOC_N(char, len);
    char *s = str;
        VALUE rstr;

    *(s++) = '[';
        for (i = 0; i < num_hits; i++) {
        if (i) *(s++) = ',';
        *(s++) = '{';
                rhit = RARRAY_PTR(rhits)[i];
                doc_id = FIX2INT(rb_funcall(rhit, id_doc, 0));
                lzd = sea->get_lazy_doc(sea, doc_id);
                s = frb_lzd_load_to_json(lzd, &str, s, &len);
        lazy_doc_close(lzd);
        *(s++) = '}';
        }
    *(s++) = ']';
    *(s++) = '\0';
        rstr = rb_str_new2(str);
        free(str);
        return rstr;
}
to_s(field = :id) → string click to toggle source

Returns a string representation of the top_doc in readable format.

static VALUE
frb_td_to_s(int argc, VALUE *argv, VALUE self)
{
    int i;
    VALUE rhits = rb_funcall(self, id_hits, 0);
    Searcher *sea = (Searcher *)DATA_PTR(rb_funcall(self, id_searcher, 0));
    const int len = RARRAY_LEN(rhits);
    int capa = len * 64 + 100;
    int p = 0;
    char *str = ALLOC_N(char, len * 64 + 100);
    Symbol field = fsym_id;
    VALUE rstr;

    if (argc) {
        field = frb_field(argv[0]);
    }

    sprintf(str, "TopDocs: total_hits = %ld, max_score = %lf [\n",
            FIX2LONG(rb_funcall(self, id_total_hits, 0)),
            NUM2DBL(rb_funcall(self, id_max_score, 0)));
    p = (int)strlen(str);

    for (i = 0; i < len; i++) {
        VALUE rhit = RARRAY_PTR(rhits)[i];
        int doc_id = FIX2INT(rb_funcall(rhit, id_doc, 0));
        char *value = "";
        size_t value_len = 0;
        LazyDoc *lzd = sea->get_lazy_doc(sea, doc_id);
        LazyDocField *lzdf = lazy_doc_get(lzd, field);
        if (NULL != lzdf) {
            value = lazy_df_get_data(lzdf, 0);
            value_len = strlen(value);
        }
        if (p + value_len + 64 > capa) {
            capa += (value_len + 64) * (len - i);
            REALLOC_N(str, char, capa);
        }

        sprintf(str + p, "\t%d \"%s\": %0.5f\n", doc_id, value,
                NUM2DBL(rb_funcall(rhit, id_score, 0)));
        p += strlen(str + p);
        lazy_doc_close(lzd);
    }

    sprintf(str + p, "]\n");
    rstr = rb_str_new2(str);
    free(str);
    return rstr;
}