class Ferret::Search::Spans::SpanNearQuery
Summary¶ ↑
A SpanNearQuery is like a combination
between a PhraseQuery and a BooleanQuery. It matches sub-SpanQueries
which are added as clauses but those clauses must occur within a
slop
edit distance of each other. You can also specify that
clauses must occur in_order
.
Example¶ ↑
query = SpanNearQuery.new(:slop => 2) query << SpanTermQuery.new(:field, "quick") query << SpanTermQuery.new(:field, "brown") query << SpanTermQuery.new(:field, "fox") # matches => "quick brown speckled sleepy fox" |______2______^ # matches => "quick brown speckled fox" |__1__^ # matches => "brown quick _____ fox" ^_____2_____| query = SpanNearQuery.new(:slop => 2, :in_order => true) query << SpanTermQuery.new(:field, "quick") query << SpanTermQuery.new(:field, "brown") query << SpanTermQuery.new(:field, "fox") # matches => "quick brown speckled sleepy fox" |______2______^ # matches => "quick brown speckled fox" |__1__^ # doesn't match => "brown quick _____ fox" # not in order ^_____2_____|
NOTE¶ ↑
SpanNearQuery only works with other SpanQueries.
Public Class Methods
Create a new SpanNearQuery. You can add an
array of clauses with the :clause
parameter or you can add
clauses individually using the #add method.
query = SpanNearQuery.new(:clauses => [spanq1, spanq2, spanq3]) # is equivalent to query = SpanNearQuery.new() query << spanq1 << spanq2 << spanq3
You have two other options which you can set.
- :slop
-
Default: 0. Works exactly like a PhraseQuery slop. It is the amount of slop allowed in the match (the term edit distance allowed in the match).
- :in_order
-
Default: false. Specifies whether or not the matches have to occur in the order they were added to the query. When slop is set to 0, this parameter will make no difference.
static VALUE frb_spannq_init(int argc, VALUE *argv, VALUE self) { Query *q; VALUE roptions; int slop = 0; bool in_order = false; if (rb_scan_args(argc, argv, "01", &roptions) > 0) { VALUE v; if (Qnil != (v = rb_hash_aref(roptions, sym_slop))) { slop = FIX2INT(v); } if (Qnil != (v = rb_hash_aref(roptions, sym_in_order))) { in_order = RTEST(v); } } q = spannq_new(slop, in_order); if (argc > 0) { VALUE v; if (Qnil != (v = rb_hash_aref(roptions, sym_clauses))) { int i; Query *clause; Check_Type(v, T_ARRAY); for (i = 0; i < RARRAY_LEN(v); i++) { Data_Get_Struct(RARRAY_PTR(v)[i], Query, clause); spannq_add_clause(q, clause); } } } Frt_Wrap_Struct(self, &frb_spannq_mark, &frb_q_free, q); object_add(q, self); return self; }
Public Instance Methods
Add a clause to the SpanNearQuery. Clauses are stored in the order they are added to the query which is important for matching. Note that clauses must be SpanQueries, not other types of query.
static VALUE frb_spannq_add(VALUE self, VALUE rclause) { GET_Q(); Query *clause; Data_Get_Struct(rclause, Query, clause); spannq_add_clause(q, clause); return self; }
Add a clause to the SpanNearQuery. Clauses are stored in the order they are added to the query which is important for matching. Note that clauses must be SpanQueries, not other types of query.
static VALUE frb_spannq_add(VALUE self, VALUE rclause) { GET_Q(); Query *clause; Data_Get_Struct(rclause, Query, clause); spannq_add_clause(q, clause); return self; }