A BooleanQuery is used for combining many queries into one. This is best illustrated with an 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)
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; }
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 to add to the BooleanQuery
occurrence requirement for the query being added. Must be one of
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; }
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 to add to the BooleanQuery
occurrence requirement for the query being added. Must be one of
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; }