/*
* call-seq:
* phrase_query.add_term(term, position_increment = 1) -> phrase_query
* phrase_query << term -> phrase_query
*
* Add a term to the phrase query. By default the position_increment is set
* to 1 so each term you add is expected to come directly after the previous
* term. By setting position_increment to 2 you are specifying that the term
* you just added should occur two terms after the previous term. For
* example;
*
* phrase_query.add_term("big").add_term("house", 2)
* # matches => "big brick house"
* # matches => "big red house"
* # doesn't match => "big house"
*/
static VALUE
frt_phq_add(int argc, VALUE *argv, VALUE self)
{
VALUE rterm, rpos_inc;
int pos_inc = 1;
GET_Q();
if (rb_scan_args(argc, argv, "11", &rterm, &rpos_inc) == 2) {
pos_inc = FIX2INT(rpos_inc);
}
switch (TYPE(rterm)) {
case T_STRING:
{
phq_add_term(q, StringValuePtr(rterm), pos_inc);
break;
}
case T_ARRAY:
{
int i;
char *t;
if (RARRAY(rterm)->len < 1) {
rb_raise(rb_eArgError, "Cannot add empty array to a "
"PhraseQuery. You must add either a string or "
"an array of strings");
}
t = StringValuePtr(RARRAY(rterm)->ptr[0]);
phq_add_term(q, t, pos_inc);
for (i = 1; i < RARRAY(rterm)->len; i++) {
t = StringValuePtr(RARRAY(rterm)->ptr[i]);
phq_append_multi_term(q, t);
}
break;
}
default:
rb_raise(rb_eArgError, "You can only add a string or an array of "
"strings to a PhraseQuery, not a %s\n",
rs2s(rb_obj_as_string(rterm)));
}
return self;
}