MultiTermQuery matches documents that contain one of a list of terms in a specific field. This is the basic building block for queries such as;
MultiTermQuery is very similar to a boolean “Or” query. It is highly optimized though as it focuses on a single field.
multi_term_query = MultiTermQuery.new(:content, :max_term => 10) multi_term_query << "Ruby" << "Ferret" << "Rails" << "Search"
Get the default value for :max_terms
in a MultiTermQuery. This value is also used by
PrefixQuery, FuzzyQuery and WildcardQuery.
static VALUE frb_mtq_get_dmt(VALUE self) { return rb_cvar_get(cMultiTermQuery, id_default_max_terms); }
Set the default value for :max_terms
in a MultiTermQuery. This value is also used by
PrefixQuery, FuzzyQuery and WildcardQuery.
static VALUE frb_mtq_set_dmt(VALUE self, VALUE rnum_terms) { int max_terms = FIX2INT(rnum_terms); if (max_terms <= 0) { rb_raise(rb_eArgError, "%d <= 0. @@max_terms must be > 0", max_terms); } #ifdef FRT_RUBY_VERSION_1_9 rb_cvar_set(cMultiTermQuery, id_default_max_terms, rnum_terms); #else rb_cvar_set(cMultiTermQuery, id_default_max_terms, rnum_terms, Qfalse); #endif return rnum_terms; }
Create a new MultiTermQuery on field
field
. You will also need to add terms to the query using the
#add_term method.
There are several options available to you when creating a MultiTermQueries;
You can specify the maximum number of terms that can be added to the query. This is to prevent memory usage overflow, particularly when don't directly control the addition of terms to the Query object like when you create Wildcard queries. For example, searching for “content:*” would cause problems without this limit.
The minimum score a term must have to be added to the query. For example you could implement your own wild-card queries that gives matches a score. To limit the number of terms added to the query you could set a lower limit to this score. FuzzyQuery in particular makes use of this parameter.
static VALUE frb_mtq_init(int argc, VALUE *argv, VALUE self) { VALUE rfield, roptions; float min_score = 0.0; int max_terms = FIX2INT(frb_mtq_get_dmt(self)); Query *q; if (rb_scan_args(argc, argv, "11", &rfield, &roptions) == 2) { VALUE v; if (Qnil != (v = rb_hash_aref(roptions, sym_max_terms))) { max_terms = FIX2INT(v); } if (Qnil != (v = rb_hash_aref(roptions, sym_min_score))) { min_score = (float)NUM2DBL(v); } } q = multi_tq_new_conf(frb_field(rfield), max_terms, min_score); Frt_Wrap_Struct(self, NULL, &frb_q_free, q); object_add(q, self); return self; }
Add a term to the MultiTermQuery with the score 1.0 unless specified otherwise.
static VALUE frb_mtq_add_term(int argc, VALUE *argv, VALUE self) { GET_Q(); VALUE rterm, rboost; float boost = 1.0; char *term = NULL; if (rb_scan_args(argc, argv, "11", &rterm, &rboost) == 2) { boost = (float)NUM2DBL(rboost); } term = StringValuePtr(rterm); multi_tq_add_term_boost(q, term, boost); return self; }
Add a term to the MultiTermQuery with the score 1.0 unless specified otherwise.
static VALUE frb_mtq_add_term(int argc, VALUE *argv, VALUE self) { GET_Q(); VALUE rterm, rboost; float boost = 1.0; char *term = NULL; if (rb_scan_args(argc, argv, "11", &rterm, &rboost) == 2) { boost = (float)NUM2DBL(rboost); } term = StringValuePtr(rterm); multi_tq_add_term_boost(q, term, boost); return self; }