class Ferret::Search::Query

Summary

Abstract class representing a query to the index. There are a number of concrete Query implementations;

Explore these classes for the query right for you. The queries are passed to the Ferret::Search::Searcher#search* methods.

Query Boosts

Queries have a boost value so that you can make the results of one query more important than the results of another query when combining them in a BooleanQuery. For example, documents on Rails. To avoid getting results for train rails you might also add the tern Ruby but Rails is the more important term so you'd give it a boost.

Public Instance Methods

==(p1) click to toggle source

call-seq;

query.eql?(other_query) -> bool
query == other_query -> bool

Return true if query equals other_query. Theoretically, two queries are equal if the always return the same results, no matter what the contents of the index. Practically, however, this is difficult to implement efficiently for queries like BooleanQuery since the ordering of clauses unspecified. “Ruby AND Rails” will not match “Rails AND Ruby” for example, although their result sets will be identical. Most queries should match as expected however.

static VALUE
frb_q_eql(VALUE self, VALUE other)
{
    GET_Q();
    Query *oq;
    Data_Get_Struct(other, Query, oq);
    return q->eq(q, oq) ? Qtrue : Qfalse;
}
boost click to toggle source

Returns the queries boost value. See the Query description for more information on Query boosts.

static VALUE
frb_q_get_boost(VALUE self)
{
    GET_Q();
    return rb_float_new((double)q->boost);
}
boost = boost → boost click to toggle source

Set the boost for a query. See the Query description for more information on Query boosts.

static VALUE
frb_q_set_boost(VALUE self, VALUE rboost)
{
    GET_Q();
    q->boost = (float)NUM2DBL(rboost);
    return rboost;
}
eql?(p1) click to toggle source

call-seq;

query.eql?(other_query) -> bool
query == other_query -> bool

Return true if query equals other_query. Theoretically, two queries are equal if the always return the same results, no matter what the contents of the index. Practically, however, this is difficult to implement efficiently for queries like BooleanQuery since the ordering of clauses unspecified. “Ruby AND Rails” will not match “Rails AND Ruby” for example, although their result sets will be identical. Most queries should match as expected however.

static VALUE
frb_q_eql(VALUE self, VALUE other)
{
    GET_Q();
    Query *oq;
    Data_Get_Struct(other, Query, oq);
    return q->eq(q, oq) ? Qtrue : Qfalse;
}
hash → number click to toggle source

Return a hash value for the query. This is used for caching query results in a hash object.

static VALUE
frb_q_hash(VALUE self)
{
    GET_Q();
    return INT2FIX(q->hash(q));
}
terms(searcher) → term_array click to toggle source

Returns an array of terms searched for by this query. This can be used for implementing an external query highlighter for example. You must supply a searcher so that the query can be rewritten and optimized like it would be in a real search.

static VALUE
frb_q_get_terms(VALUE self, VALUE searcher)
{
    VALUE rterms = rb_ary_new();
    HashSet *terms = hs_new((hash_ft)&term_hash,
                            (eq_ft)&term_eq,
                            (free_ft)term_destroy);
    HashSetEntry *hse;
    GET_Q();
    Searcher *sea = (Searcher *)DATA_PTR(searcher);
    Query *rq = sea->rewrite(sea, q);
    rq->extract_terms(rq, terms);
    q_deref(rq);

    for (hse = terms->first; hse; hse = hse->next) {
        Term *term = (Term *)hse->elem;
        rb_ary_push(rterms, frb_get_term(term->field, term->text));
    }
    hs_destroy(terms);
    return rterms;
}
to_s → string click to toggle source

Return a string representation of the query. Most of the time, passing this string through the Query parser will give you the exact Query you began with. This can be a good way to explore how the QueryParser works.

static VALUE
frb_q_to_s(int argc, VALUE *argv, VALUE self)
{
    GET_Q();
    VALUE rstr, rfield;
    char *str;
    Symbol field = NULL;
    if (rb_scan_args(argc, argv, "01", &rfield)) {
        field = frb_field(rfield);
    }
    str = q->to_s(q, field);
    rstr = rb_str_new2(str);
    free(str);
    return rstr;
}