Class: Ferret::Search::BooleanQuery::BooleanClause

Summary

A BooleanClause holes a single query within a BooleanQuery specifying wither the query +:must+ match, +:should+ match or +:must_not+ match. BooleanClauses can be used to pass a clause from one BooleanQuery to another although it is generally easier just to add a query directly to a BooleanQuery using the BooleanQuery#add_query method.

Example

  clause1 = BooleanClause.new(query1, :should)
  clause2 = BooleanClause.new(query2, :should)

  query = BooleanQuery.new
  query << clause1 << clause2

Public Class Methods


BooleanClause.new(query, occur = :should) → BooleanClause

Create a new BooleanClause object, wrapping the query query. occur must be one of +:must+, +:should+ or +:must_not+.

/*
 *  call-seq:
 *     BooleanClause.new(query, occur = :should) -> BooleanClause
 *
 *  Create a new BooleanClause object, wrapping the query +query+. +occur+
 *  must be one of +:must+, +:should+ or +:must_not+.
 */
static VALUE
frb_bc_init(int argc, VALUE *argv, VALUE self)
{
    BooleanClause *bc;
    VALUE rquery, roccur;
    unsigned int occur = BC_SHOULD;
    Query *sub_q;
    if (rb_scan_args(argc, argv, "11", &rquery, &roccur) == 2) {
        occur = frb_get_occur(roccur);
    }
    Data_Get_Struct(rquery, Query, sub_q);
    REF(sub_q);
    bc = bc_new(sub_q, occur);
    Frt_Wrap_Struct(self, &frb_bc_mark, &frb_bc_free, bc);
    object_add(bc, self);
    return self;
}

Public Instance Methods


clause.occur = occur → occur

Set the occur value for this BooleanClause. occur must be one of +:must+, +:should+ or +:must_not+.

/*
 *  call-seq:
 *     clause.occur = occur -> occur
 *
 *  Set the +occur+ value for this BooleanClause. +occur+ must be one of
 *  +:must+, +:should+ or +:must_not+.
 */
static VALUE
frb_bc_set_occur(VALUE self, VALUE roccur)
{
    GET_BC();
    BCType occur = frb_get_occur(roccur);
    bc_set_occur(bc, occur);

    return roccur;
}

clause.prohibited? → bool

Return true if this clause is prohibited. ie, this will be true if occur was equal to +:must_not+.

/*
 *  call-seq:
 *     clause.prohibited? -> bool
 *
 *  Return true if this clause is prohibited. ie, this will be true if occur was
 *  equal to +:must_not+.
 */
static VALUE
frb_bc_is_prohibited(VALUE self)
{
    GET_BC();
    return bc->is_prohibited ? Qtrue : Qfalse;
}

clause.query → query

Return the query object wrapped by this BooleanClause.

/*
 *  call-seq:
 *     clause.query -> query
 *
 *  Return the query object wrapped by this BooleanClause.
 */
static VALUE
frb_bc_get_query(VALUE self)
{
    GET_BC();
    return object_get(bc->query);
}

clause.query = query → query

Set the query wrapped by this BooleanClause.

/*
 *  call-seq:
 *     clause.query = query -> query
 *
 *  Set the query wrapped by this BooleanClause.
 */
static VALUE
frb_bc_set_query(VALUE self, VALUE rquery)
{
    GET_BC();
    Data_Get_Struct(rquery, Query, bc->query);
    return rquery;
}

clause.required? → bool

Return true if this clause is required. ie, this will be true if occur was equal to +:must+.

/*
 *  call-seq:
 *     clause.required? -> bool
 *
 *  Return true if this clause is required. ie, this will be true if occur was
 *  equal to +:must+.
 */
static VALUE
frb_bc_is_required(VALUE self)
{
    GET_BC();
    return bc->is_required ? Qtrue : Qfalse;
}

clause.to_s → string

Return a string representation of this clause. This will not be used by BooleanQuery#to_s. It is only used by BooleanClause#to_s and will specify whether the clause is +:must+, +:should+ or +:must_not+.

/*
 *  call-seq:
 *     clause.to_s -> string
 *
 *  Return a string representation of this clause. This will not be used by
 *  BooleanQuery#to_s. It is only used by BooleanClause#to_s and will specify
 *  whether the clause is +:must+, +:should+ or +:must_not+.
 */
static VALUE
frb_bc_to_s(VALUE self)
{
    VALUE rstr;
    char *qstr, *ostr = "", *str;
    int len;
    GET_BC();
    qstr = bc->query->to_s(bc->query, NULL);
    switch (bc->occur) {
        case BC_SHOULD:
            ostr = "Should";
            break;
        case BC_MUST:
            ostr = "Must";
            break;
        case BC_MUST_NOT:
            ostr = "Must Not";
            break;
    }
    len = strlen(ostr) + strlen(qstr) + 2;
    str = ALLOC_N(char, len);
    sprintf(str, "%s:%s", ostr, qstr);
    rstr = rb_str_new(str, len);
    free(qstr);
    free(str);
    return rstr;
}