### Home Classes Methods

### In Files

- ext/r_search.c

### Parent

Object

### Methods

- #to_json
- #to_s

### Files

- MIT-LICENSE
- README
- TODO
- TUTORIAL

### Class/Module Index

Quicksearch

- Ferret
- Ferret::Analysis
- Ferret::Analysis::Analyzer
- Ferret::Analysis::AsciiLetterAnalyzer
- Ferret::Analysis::AsciiLetterTokenizer
- Ferret::Analysis::AsciiLowerCaseFilter
- Ferret::Analysis::AsciiStandardAnalyzer
- Ferret::Analysis::AsciiStandardTokenizer
- Ferret::Analysis::AsciiWhiteSpaceAnalyzer
- Ferret::Analysis::AsciiWhiteSpaceTokenizer
- Ferret::Analysis::HyphenFilter
- Ferret::Analysis::LetterAnalyzer
- Ferret::Analysis::LetterTokenizer
- Ferret::Analysis::LowerCaseFilter
- Ferret::Analysis::MappingFilter
- Ferret::Analysis::PerFieldAnalyzer
- Ferret::Analysis::RegExpAnalyzer
- Ferret::Analysis::RegExpTokenizer
- Ferret::Analysis::StandardAnalyzer
- Ferret::Analysis::StandardTokenizer
- Ferret::Analysis::StemFilter
- Ferret::Analysis::StopFilter
- Ferret::Analysis::Token
- Ferret::Analysis::TokenStream
- Ferret::Analysis::WhiteSpaceAnalyzer
- Ferret::Analysis::WhiteSpaceTokenizer
- Ferret::BlankSlate
- Ferret::BoostMixin
- Ferret::Browser
- Ferret::Browser::Controller
- Ferret::Browser::Delegator
- Ferret::Browser::DocumentController
- Ferret::Browser::ErrorController
- Ferret::Browser::HelpController
- Ferret::Browser::HomeController
- Ferret::Browser::TermController
- Ferret::Browser::TermVectorController
- Ferret::Browser::ViewHelper
- Ferret::Document
- Ferret::Field
- Ferret::FieldSymbol
- Ferret::FieldSymbolMethods
- Ferret::FileNotFoundError
- Ferret::Index
- Ferret::Index::FieldInfo
- Ferret::Index::FieldInfos
- Ferret::Index::Index
- Ferret::Index::IndexReader
- Ferret::Index::IndexWriter
- Ferret::Index::LazyDoc
- Ferret::Index::LazyDoc::LazyDocData
- Ferret::Index::TermDocEnum
- Ferret::Index::TermEnum
- Ferret::Index::TermVector
- Ferret::Index::TermVector::TVOffsets
- Ferret::Index::TermVector::TVTerm
- Ferret::ParseError
- Ferret::QueryParser
- Ferret::QueryParser::QueryParseException
- Ferret::Search
- Ferret::Search::BooleanQuery
- Ferret::Search::BooleanQuery::BooleanClause
- Ferret::Search::ConstantScoreQuery
- Ferret::Search::Explanation
- Ferret::Search::Filter
- Ferret::Search::FilteredQuery
- Ferret::Search::FuzzyQuery
- Ferret::Search::Hit
- Ferret::Search::MatchAllQuery
- Ferret::Search::MultiSearcher
- Ferret::Search::MultiTermQuery
- Ferret::Search::PhraseQuery
- Ferret::Search::PrefixQuery
- Ferret::Search::Query
- Ferret::Search::QueryFilter
- Ferret::Search::RangeFilter
- Ferret::Search::RangeQuery
- Ferret::Search::Searcher
- Ferret::Search::Sort
- Ferret::Search::SortField
- Ferret::Search::Spans
- Ferret::Search::Spans::SpanFirstQuery
- Ferret::Search::Spans::SpanMultiTermQuery
- Ferret::Search::Spans::SpanNearQuery
- Ferret::Search::Spans::SpanNotQuery
- Ferret::Search::Spans::SpanOrQuery
- Ferret::Search::Spans::SpanPrefixQuery
- Ferret::Search::Spans::SpanTermQuery
- Ferret::Search::TermQuery
- Ferret::Search::TopDocs
- Ferret::Search::TypedRangeFilter
- Ferret::Search::TypedRangeQuery
- Ferret::Search::WildcardQuery
- Ferret::StateError
- Ferret::Store
- Ferret::Store::Directory
- Ferret::Store::FSDirectory
- Ferret::Store::Lock
- Ferret::Store::Lock::LockError
- Ferret::Store::RAMDirectory
- Ferret::Utils
- Ferret::Utils::BitVector
- Ferret::Utils::MultiMapper
- Ferret::Utils::PriorityQueue
- WEBrick
- WEBrick::FerretBrowserHandler
- Date
- DateTime
- FileUtils
- Float
- Integer
- String
- Symbol
- Time

No matching classes.

# 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

## 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;
    }

[Validate]

Generated with the Darkfish Rdoc Generator 2.