### Home Classes Methods

### In Files

- ext/r_search.c

### Parent

Ferret::Search::Query

### Namespace

- CLASS Ferret::Search::BooleanQuery::BooleanClause

### Methods

- ::new
- #<<
- #add_query

### Files

- MIT-LICENSE
- README
- TODO
- TUTORIAL

### Class/Module Index

Quicksearch

- Ferret
- 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
- WEBrick
- WEBrick::FerretBrowserHandler
- Date
- DateTime
- FileUtils
- Float
- Integer
- String
- Symbol
- Time

No matching classes.

# Ferret::Search::BooleanQuery

## Summary

A BooleanQuery is used for combining many queries into one. This is best illustrated with an example.

## Example

Lets say we wanted to find all documents with the term "Ruby" in the `:title` and the term "Ferret" in the `:content` field or the `:title` field written before January 2006. You could build the query like this.

    tq1 = TermQuery.new(:title, "ruby")
    tq21 = TermQuery.new(:title, "ferret")
    tq22 = TermQuery.new(:content, "ferret")
    bq2 = BooleanQuery.new
    bq2 << tq21 << tq22

    rq3 = RangeQuery.new(:written, :< => "200601")

    query = BooleanQuery.new
    query.add_query(tq1, :must).add_query(bq2, :must).add_query(rq3, :must)

### Public Class Methods

new(coord_disable = false) click to toggle source

Create a new BooleanQuery. If you don't care about the scores of the sub-queries added to the query (as would be the case for many automatically generated queries) you can disable the coord_factor of the score. This will slightly improve performance for the query. Usually you should leave this parameter as is.

    static VALUE
    frb_bq_init(int argc, VALUE *argv, VALUE self)
    {
        VALUE rcoord_disabled;
        bool coord_disabled = false;
        Query *q;
        if (rb_scan_args(argc, argv, "01", &rcoord_disabled)) {
            coord_disabled = RTEST(rcoord_disabled);
        }
        q = bq_new(coord_disabled);
        Frt_Wrap_Struct(self, &frb_bq_mark, &frb_q_free, q);
        object_add(q, self);
        return self;
    }

### Public Instance Methods

add_query(query, occur = :should) → boolean_clause click to toggle source
<<(query, occur = :should) → boolean_clause
boolean_query << boolean_clause → boolean_clause

Us this method to add sub-queries to a BooleanQuery. You can either add a straight Query or a BooleanClause. When adding a Query, the default occurrence requirement is :should. That is the Query's match will be scored but it isn't essential for a match. If the query should be essential, use :must. For exclusive queries use :must_not.

When adding a Boolean clause to a BooleanQuery there is no need to set the occurrence property because it is already set in the BooleanClause. Therefor the `occur` parameter will be ignored in this case.

query | Query to add to the BooleanQuery

occur

occurrence requirement for the query being added. Must be one of

:must, :should, :must_not

returns | BooleanClause which was added

    static VALUE
    frb_bq_add_query(int argc, VALUE *argv, VALUE self)
    {
        GET_Q();
        VALUE rquery, roccur;
        BCType occur = BC_SHOULD;
        Query *sub_q;
        VALUE klass;

        if (rb_scan_args(argc, argv, "11", &rquery, &roccur) == 2) {
            occur = frb_get_occur(roccur);
        }
        klass = CLASS_OF(rquery);
        if (klass == cBooleanClause) {
            BooleanClause *bc = (BooleanClause *)DATA_PTR(rquery);
            if (argc > 1) {
                rb_warning("Second argument to BooleanQuery#add is ignored "
                           "when adding BooleanClause");
            }
            bq_add_clause(q, bc);
            return rquery;
        } else if (TYPE(rquery) == T_DATA) {
            Data_Get_Struct(rquery, Query, sub_q);
            return frb_bc_wrap(bq_add_query(q, sub_q, occur));
        } else {
            rb_raise(rb_eArgError, "Cannot add %s to a BooleanQuery",
                     rb_class2name(klass));
        }
        return self;
    }

add_query(query, occur = :should) → boolean_clause click to toggle source
<<(query, occur = :should) → boolean_clause
boolean_query << boolean_clause → boolean_clause

Us this method to add sub-queries to a BooleanQuery. You can either add a straight Query or a BooleanClause. When adding a Query, the default occurrence requirement is :should. That is the Query's match will be scored but it isn't essential for a match. If the query should be essential, use :must. For exclusive queries use :must_not.

When adding a Boolean clause to a BooleanQuery there is no need to set the occurrence property because it is already set in the BooleanClause. Therefor the `occur` parameter will be ignored in this case.

query | Query to add to the BooleanQuery

occur

occurrence requirement for the query being added. Must be one of

:must, :should, :must_not

returns | BooleanClause which was added

    static VALUE
    frb_bq_add_query(int argc, VALUE *argv, VALUE self)
    {
        GET_Q();
        VALUE rquery, roccur;
        BCType occur = BC_SHOULD;
        Query *sub_q;
        VALUE klass;

        if (rb_scan_args(argc, argv, "11", &rquery, &roccur) == 2) {
            occur = frb_get_occur(roccur);
        }
        klass = CLASS_OF(rquery);
        if (klass == cBooleanClause) {
            BooleanClause *bc = (BooleanClause *)DATA_PTR(rquery);
            if (argc > 1) {
                rb_warning("Second argument to BooleanQuery#add is ignored "
                           "when adding BooleanClause");
            }
            bq_add_clause(q, bc);
            return rquery;
        } else if (TYPE(rquery) == T_DATA) {
            Data_Get_Struct(rquery, Query, sub_q);
            return frb_bc_wrap(bq_add_query(q, sub_q, occur));
        } else {
            rb_raise(rb_eArgError, "Cannot add %s to a BooleanQuery",
                     rb_class2name(klass));
        }
        return self;
    }

[Validate]

Generated with the Darkfish Rdoc Generator 2.