jk-ferret 0.11.8.2
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +24 -0
- data/MIT-LICENSE +20 -0
- data/README +90 -0
- data/RELEASE_CHANGES +137 -0
- data/RELEASE_NOTES +60 -0
- data/Rakefile +443 -0
- data/TODO +109 -0
- data/TUTORIAL +231 -0
- data/bin/ferret-browser +79 -0
- data/ext/BZLIB_blocksort.c +1094 -0
- data/ext/BZLIB_bzlib.c +1578 -0
- data/ext/BZLIB_compress.c +672 -0
- data/ext/BZLIB_crctable.c +104 -0
- data/ext/BZLIB_decompress.c +626 -0
- data/ext/BZLIB_huffman.c +205 -0
- data/ext/BZLIB_randtable.c +84 -0
- data/ext/STEMMER_api.c +66 -0
- data/ext/STEMMER_libstemmer.c +93 -0
- data/ext/STEMMER_stem_ISO_8859_1_danish.c +337 -0
- data/ext/STEMMER_stem_ISO_8859_1_dutch.c +624 -0
- data/ext/STEMMER_stem_ISO_8859_1_english.c +1117 -0
- data/ext/STEMMER_stem_ISO_8859_1_finnish.c +762 -0
- data/ext/STEMMER_stem_ISO_8859_1_french.c +1246 -0
- data/ext/STEMMER_stem_ISO_8859_1_german.c +503 -0
- data/ext/STEMMER_stem_ISO_8859_1_hungarian.c +1230 -0
- data/ext/STEMMER_stem_ISO_8859_1_italian.c +1065 -0
- data/ext/STEMMER_stem_ISO_8859_1_norwegian.c +297 -0
- data/ext/STEMMER_stem_ISO_8859_1_porter.c +749 -0
- data/ext/STEMMER_stem_ISO_8859_1_portuguese.c +1017 -0
- data/ext/STEMMER_stem_ISO_8859_1_spanish.c +1093 -0
- data/ext/STEMMER_stem_ISO_8859_1_swedish.c +307 -0
- data/ext/STEMMER_stem_ISO_8859_2_romanian.c +998 -0
- data/ext/STEMMER_stem_KOI8_R_russian.c +700 -0
- data/ext/STEMMER_stem_UTF_8_danish.c +339 -0
- data/ext/STEMMER_stem_UTF_8_dutch.c +634 -0
- data/ext/STEMMER_stem_UTF_8_english.c +1125 -0
- data/ext/STEMMER_stem_UTF_8_finnish.c +768 -0
- data/ext/STEMMER_stem_UTF_8_french.c +1256 -0
- data/ext/STEMMER_stem_UTF_8_german.c +509 -0
- data/ext/STEMMER_stem_UTF_8_hungarian.c +1234 -0
- data/ext/STEMMER_stem_UTF_8_italian.c +1073 -0
- data/ext/STEMMER_stem_UTF_8_norwegian.c +299 -0
- data/ext/STEMMER_stem_UTF_8_porter.c +755 -0
- data/ext/STEMMER_stem_UTF_8_portuguese.c +1023 -0
- data/ext/STEMMER_stem_UTF_8_romanian.c +1004 -0
- data/ext/STEMMER_stem_UTF_8_russian.c +694 -0
- data/ext/STEMMER_stem_UTF_8_spanish.c +1097 -0
- data/ext/STEMMER_stem_UTF_8_swedish.c +309 -0
- data/ext/STEMMER_stem_UTF_8_turkish.c +2205 -0
- data/ext/STEMMER_utilities.c +478 -0
- data/ext/analysis.c +1710 -0
- data/ext/analysis.h +266 -0
- data/ext/api.h +26 -0
- data/ext/array.c +125 -0
- data/ext/array.h +62 -0
- data/ext/bitvector.c +96 -0
- data/ext/bitvector.h +594 -0
- data/ext/bzlib.h +282 -0
- data/ext/bzlib_private.h +503 -0
- data/ext/compound_io.c +384 -0
- data/ext/config.h +52 -0
- data/ext/document.c +159 -0
- data/ext/document.h +63 -0
- data/ext/except.c +102 -0
- data/ext/except.h +176 -0
- data/ext/extconf.rb +15 -0
- data/ext/ferret.c +416 -0
- data/ext/ferret.h +94 -0
- data/ext/field_index.c +262 -0
- data/ext/field_index.h +52 -0
- data/ext/filter.c +157 -0
- data/ext/fs_store.c +493 -0
- data/ext/global.c +458 -0
- data/ext/global.h +302 -0
- data/ext/hash.c +524 -0
- data/ext/hash.h +515 -0
- data/ext/hashset.c +192 -0
- data/ext/hashset.h +215 -0
- data/ext/header.h +58 -0
- data/ext/helper.c +63 -0
- data/ext/helper.h +21 -0
- data/ext/index.c +6804 -0
- data/ext/index.h +935 -0
- data/ext/internal.h +1019 -0
- data/ext/lang.c +10 -0
- data/ext/lang.h +68 -0
- data/ext/libstemmer.h +79 -0
- data/ext/mempool.c +88 -0
- data/ext/mempool.h +43 -0
- data/ext/modules.h +190 -0
- data/ext/multimapper.c +351 -0
- data/ext/multimapper.h +60 -0
- data/ext/posh.c +1006 -0
- data/ext/posh.h +973 -0
- data/ext/priorityqueue.c +149 -0
- data/ext/priorityqueue.h +155 -0
- data/ext/q_boolean.c +1621 -0
- data/ext/q_const_score.c +162 -0
- data/ext/q_filtered_query.c +212 -0
- data/ext/q_fuzzy.c +280 -0
- data/ext/q_match_all.c +149 -0
- data/ext/q_multi_term.c +673 -0
- data/ext/q_parser.c +3103 -0
- data/ext/q_phrase.c +1206 -0
- data/ext/q_prefix.c +98 -0
- data/ext/q_range.c +682 -0
- data/ext/q_span.c +2390 -0
- data/ext/q_term.c +337 -0
- data/ext/q_wildcard.c +167 -0
- data/ext/r_analysis.c +2626 -0
- data/ext/r_index.c +3468 -0
- data/ext/r_qparser.c +635 -0
- data/ext/r_search.c +4490 -0
- data/ext/r_store.c +513 -0
- data/ext/r_utils.c +1131 -0
- data/ext/ram_store.c +476 -0
- data/ext/scanner.c +895 -0
- data/ext/scanner.h +36 -0
- data/ext/scanner_mb.c +6701 -0
- data/ext/scanner_utf8.c +4415 -0
- data/ext/search.c +1864 -0
- data/ext/search.h +953 -0
- data/ext/similarity.c +151 -0
- data/ext/similarity.h +89 -0
- data/ext/sort.c +786 -0
- data/ext/stem_ISO_8859_1_danish.h +16 -0
- data/ext/stem_ISO_8859_1_dutch.h +16 -0
- data/ext/stem_ISO_8859_1_english.h +16 -0
- data/ext/stem_ISO_8859_1_finnish.h +16 -0
- data/ext/stem_ISO_8859_1_french.h +16 -0
- data/ext/stem_ISO_8859_1_german.h +16 -0
- data/ext/stem_ISO_8859_1_hungarian.h +16 -0
- data/ext/stem_ISO_8859_1_italian.h +16 -0
- data/ext/stem_ISO_8859_1_norwegian.h +16 -0
- data/ext/stem_ISO_8859_1_porter.h +16 -0
- data/ext/stem_ISO_8859_1_portuguese.h +16 -0
- data/ext/stem_ISO_8859_1_spanish.h +16 -0
- data/ext/stem_ISO_8859_1_swedish.h +16 -0
- data/ext/stem_ISO_8859_2_romanian.h +16 -0
- data/ext/stem_KOI8_R_russian.h +16 -0
- data/ext/stem_UTF_8_danish.h +16 -0
- data/ext/stem_UTF_8_dutch.h +16 -0
- data/ext/stem_UTF_8_english.h +16 -0
- data/ext/stem_UTF_8_finnish.h +16 -0
- data/ext/stem_UTF_8_french.h +16 -0
- data/ext/stem_UTF_8_german.h +16 -0
- data/ext/stem_UTF_8_hungarian.h +16 -0
- data/ext/stem_UTF_8_italian.h +16 -0
- data/ext/stem_UTF_8_norwegian.h +16 -0
- data/ext/stem_UTF_8_porter.h +16 -0
- data/ext/stem_UTF_8_portuguese.h +16 -0
- data/ext/stem_UTF_8_romanian.h +16 -0
- data/ext/stem_UTF_8_russian.h +16 -0
- data/ext/stem_UTF_8_spanish.h +16 -0
- data/ext/stem_UTF_8_swedish.h +16 -0
- data/ext/stem_UTF_8_turkish.h +16 -0
- data/ext/stopwords.c +410 -0
- data/ext/store.c +698 -0
- data/ext/store.h +799 -0
- data/ext/symbol.c +10 -0
- data/ext/symbol.h +23 -0
- data/ext/term_vectors.c +73 -0
- data/ext/threading.h +31 -0
- data/ext/win32.h +62 -0
- data/lib/ferret.rb +30 -0
- data/lib/ferret/browser.rb +246 -0
- data/lib/ferret/browser/s/global.js +192 -0
- data/lib/ferret/browser/s/style.css +148 -0
- data/lib/ferret/browser/views/document/list.rhtml +49 -0
- data/lib/ferret/browser/views/document/show.rhtml +27 -0
- data/lib/ferret/browser/views/error/index.rhtml +7 -0
- data/lib/ferret/browser/views/help/index.rhtml +8 -0
- data/lib/ferret/browser/views/home/index.rhtml +29 -0
- data/lib/ferret/browser/views/layout.rhtml +22 -0
- data/lib/ferret/browser/views/term-vector/index.rhtml +4 -0
- data/lib/ferret/browser/views/term/index.rhtml +199 -0
- data/lib/ferret/browser/views/term/termdocs.rhtml +1 -0
- data/lib/ferret/browser/webrick.rb +14 -0
- data/lib/ferret/document.rb +130 -0
- data/lib/ferret/field_infos.rb +44 -0
- data/lib/ferret/field_symbol.rb +87 -0
- data/lib/ferret/index.rb +973 -0
- data/lib/ferret/number_tools.rb +157 -0
- data/lib/ferret/version.rb +3 -0
- data/setup.rb +1555 -0
- data/test/long_running/largefile/tc_largefile.rb +46 -0
- data/test/test_all.rb +5 -0
- data/test/test_helper.rb +29 -0
- data/test/test_installed.rb +1 -0
- data/test/threading/number_to_spoken.rb +132 -0
- data/test/threading/thread_safety_index_test.rb +88 -0
- data/test/threading/thread_safety_read_write_test.rb +73 -0
- data/test/threading/thread_safety_test.rb +133 -0
- data/test/unit/analysis/tc_analyzer.rb +550 -0
- data/test/unit/analysis/tc_token_stream.rb +653 -0
- data/test/unit/index/tc_index.rb +867 -0
- data/test/unit/index/tc_index_reader.rb +699 -0
- data/test/unit/index/tc_index_writer.rb +447 -0
- data/test/unit/index/th_doc.rb +332 -0
- data/test/unit/query_parser/tc_query_parser.rb +238 -0
- data/test/unit/search/tc_filter.rb +156 -0
- data/test/unit/search/tc_fuzzy_query.rb +147 -0
- data/test/unit/search/tc_index_searcher.rb +67 -0
- data/test/unit/search/tc_multi_searcher.rb +128 -0
- data/test/unit/search/tc_multiple_search_requests.rb +58 -0
- data/test/unit/search/tc_search_and_sort.rb +179 -0
- data/test/unit/search/tc_sort.rb +49 -0
- data/test/unit/search/tc_sort_field.rb +27 -0
- data/test/unit/search/tc_spans.rb +190 -0
- data/test/unit/search/tm_searcher.rb +436 -0
- data/test/unit/store/tc_fs_store.rb +115 -0
- data/test/unit/store/tc_ram_store.rb +35 -0
- data/test/unit/store/tm_store.rb +34 -0
- data/test/unit/store/tm_store_lock.rb +68 -0
- data/test/unit/tc_document.rb +81 -0
- data/test/unit/tc_field_symbol.rb +26 -0
- data/test/unit/ts_analysis.rb +2 -0
- data/test/unit/ts_index.rb +2 -0
- data/test/unit/ts_largefile.rb +4 -0
- data/test/unit/ts_query_parser.rb +2 -0
- data/test/unit/ts_search.rb +2 -0
- data/test/unit/ts_store.rb +2 -0
- data/test/unit/ts_utils.rb +2 -0
- data/test/unit/utils/tc_bit_vector.rb +295 -0
- data/test/unit/utils/tc_number_tools.rb +117 -0
- data/test/unit/utils/tc_priority_queue.rb +106 -0
- data/test/utils/content_generator.rb +226 -0
- metadata +319 -0
data/ext/r_qparser.c
ADDED
@@ -0,0 +1,635 @@
|
|
1
|
+
#include "symbol.h"
|
2
|
+
#include "ferret.h"
|
3
|
+
#include "search.h"
|
4
|
+
|
5
|
+
static VALUE cQueryParser;
|
6
|
+
VALUE cQueryParseException;
|
7
|
+
|
8
|
+
extern VALUE sym_analyzer;
|
9
|
+
static VALUE sym_wild_card_downcase;
|
10
|
+
static VALUE sym_fields;
|
11
|
+
static VALUE sym_all_fields;
|
12
|
+
static VALUE sym_tkz_fields;
|
13
|
+
static VALUE sym_default_field;
|
14
|
+
static VALUE sym_validate_fields;
|
15
|
+
static VALUE sym_or_default;
|
16
|
+
static VALUE sym_default_slop;
|
17
|
+
static VALUE sym_handle_parse_errors;
|
18
|
+
static VALUE sym_clean_string;
|
19
|
+
static VALUE sym_max_clauses;
|
20
|
+
static VALUE sym_use_keywords;
|
21
|
+
static VALUE sym_use_typed_range_query;
|
22
|
+
|
23
|
+
extern VALUE frb_get_analyzer(Analyzer *a);
|
24
|
+
extern VALUE frb_get_q(Query *q);
|
25
|
+
extern Analyzer *frb_get_cwrapped_analyzer(VALUE ranalyzer);
|
26
|
+
|
27
|
+
/****************************************************************************
|
28
|
+
*
|
29
|
+
* QueryParser Methods
|
30
|
+
*
|
31
|
+
****************************************************************************/
|
32
|
+
|
33
|
+
static void
|
34
|
+
frb_qp_free(void *p)
|
35
|
+
{
|
36
|
+
object_del(p);
|
37
|
+
qp_destroy((QParser *)p);
|
38
|
+
}
|
39
|
+
|
40
|
+
static void
|
41
|
+
frb_qp_mark(void *p)
|
42
|
+
{
|
43
|
+
frb_gc_mark(((QParser *)p)->analyzer);
|
44
|
+
}
|
45
|
+
|
46
|
+
static HashSet *
|
47
|
+
frb_get_fields(VALUE rfields)
|
48
|
+
{
|
49
|
+
VALUE rval;
|
50
|
+
HashSet *fields;
|
51
|
+
char *s, *p, *str;
|
52
|
+
|
53
|
+
if (rfields == Qnil) return NULL;
|
54
|
+
|
55
|
+
fields = hs_new_ptr(NULL);
|
56
|
+
if (TYPE(rfields) == T_ARRAY) {
|
57
|
+
int i;
|
58
|
+
for (i = 0; i < RARRAY_LEN(rfields); i++) {
|
59
|
+
rval = rb_obj_as_string(RARRAY_PTR(rfields)[i]);
|
60
|
+
hs_add(fields, (char *)rintern(rval));
|
61
|
+
}
|
62
|
+
} else {
|
63
|
+
rval = rb_obj_as_string(rfields);
|
64
|
+
if (strcmp("*", rs2s(rval)) == 0) {
|
65
|
+
hs_destroy(fields);
|
66
|
+
fields = NULL;
|
67
|
+
} else {
|
68
|
+
s = str = rstrdup(rval);
|
69
|
+
while ((p = strchr(s, '|')) != '\0') {
|
70
|
+
*p = '\0';
|
71
|
+
hs_add(fields, (char *)intern(s));
|
72
|
+
s = p + 1;
|
73
|
+
}
|
74
|
+
hs_add(fields, (char *)intern(s));
|
75
|
+
free(str);
|
76
|
+
}
|
77
|
+
}
|
78
|
+
return fields;
|
79
|
+
}
|
80
|
+
|
81
|
+
static void
|
82
|
+
hs_safe_merge(HashSet *merger, HashSet *mergee)
|
83
|
+
{
|
84
|
+
HashSetEntry *hse;
|
85
|
+
for (hse = mergee->first; hse; hse = hse->next) {
|
86
|
+
hs_add(merger, (char *)hse->elem);
|
87
|
+
}
|
88
|
+
}
|
89
|
+
|
90
|
+
/*
|
91
|
+
* call-seq:
|
92
|
+
* QueryParser.new(options = {}) -> QueryParser
|
93
|
+
*
|
94
|
+
* Create a new QueryParser. The QueryParser is used to convert string
|
95
|
+
* queries into Query objects. The options are;
|
96
|
+
*
|
97
|
+
* === Options
|
98
|
+
*
|
99
|
+
* :default_field:: Default: "*" (all fields). The default field to
|
100
|
+
* search when no field is specified in the search
|
101
|
+
* string. It can also be an array of fields.
|
102
|
+
* :analyzer:: Default: StandardAnalyzer. Analyzer used by the
|
103
|
+
* query parser to parse query terms
|
104
|
+
* :wild_card_downcase:: Default: true. Specifies whether wild-card queries
|
105
|
+
* and range queries should be downcased or not since
|
106
|
+
* they are not passed through the parser
|
107
|
+
* :fields:: Default: []. Lets the query parser know what
|
108
|
+
* fields are available for searching, particularly
|
109
|
+
* when the "*" is specified as the search field
|
110
|
+
* :tokenized_fields:: Default: :fields. Lets the query parser know which
|
111
|
+
* fields are tokenized so it knows which fields to
|
112
|
+
* run the analyzer over.
|
113
|
+
* :validate_fields:: Default: false. Set to true if you want an
|
114
|
+
* exception to be raised if there is an attempt to
|
115
|
+
* search a non-existent field
|
116
|
+
* :or_default:: Default: true. Use "OR" as the default boolean
|
117
|
+
* operator
|
118
|
+
* :default_slop:: Default: 0. Default slop to use in PhraseQuery
|
119
|
+
* :handle_parse_errors:: Default: true. QueryParser will quietly handle all
|
120
|
+
* parsing errors internally. If you'd like to handle
|
121
|
+
* them yourself, set this parameter to false.
|
122
|
+
* :clean_string:: Default: true. QueryParser will do a quick
|
123
|
+
* once-over the query string make sure that quotes
|
124
|
+
* and brackets match up and special characters are
|
125
|
+
* escaped
|
126
|
+
* :max_clauses:: Default: 512. the maximum number of clauses
|
127
|
+
* allowed in boolean queries and the maximum number
|
128
|
+
* of terms allowed in multi, prefix, wild-card or
|
129
|
+
* fuzzy queries when those queries are generated by
|
130
|
+
* rewriting other queries
|
131
|
+
* :use_keywords:: Default: true. By default AND, OR, NOT and REQ are
|
132
|
+
* keywords used by the query parser. Sometimes this
|
133
|
+
* is undesirable. For example, if your application
|
134
|
+
* allows searching for US states by their
|
135
|
+
* abbreviation, then OR will be a common query
|
136
|
+
* string. By setting :use_keywords to false, OR will
|
137
|
+
* no longer be a keyword allowing searches for the
|
138
|
+
* state of Oregon. You will still be able to use
|
139
|
+
* boolean queries by using the + and - characters.
|
140
|
+
* :use_typed_range_query:: Default: false. Use TypedRangeQuery instead of
|
141
|
+
* the standard RangeQuery when parsing
|
142
|
+
* range queries. This is useful if you have number
|
143
|
+
* fields which you want to perform range queries
|
144
|
+
* on. You won't need to pad or normalize the data
|
145
|
+
* in the field in anyway to get correct results.
|
146
|
+
* However, performance will be a lot slower for
|
147
|
+
* large indexes, hence the default.
|
148
|
+
* Note: the default is set to true in the Index
|
149
|
+
* class.
|
150
|
+
*/
|
151
|
+
static VALUE
|
152
|
+
frb_qp_init(int argc, VALUE *argv, VALUE self)
|
153
|
+
{
|
154
|
+
VALUE roptions = Qnil;
|
155
|
+
VALUE rval;
|
156
|
+
Analyzer *analyzer = NULL;
|
157
|
+
bool has_options = false;
|
158
|
+
|
159
|
+
HashSet *all_fields = NULL;
|
160
|
+
HashSet *tkz_fields = NULL;
|
161
|
+
HashSet *def_fields = NULL;
|
162
|
+
QParser *qp;
|
163
|
+
|
164
|
+
if (rb_scan_args(argc, argv, "01", &roptions) > 0) {
|
165
|
+
if (TYPE(roptions) == T_HASH) {
|
166
|
+
has_options = true;
|
167
|
+
if (Qnil != (rval = rb_hash_aref(roptions, sym_default_field))) {
|
168
|
+
def_fields = frb_get_fields(rval);
|
169
|
+
}
|
170
|
+
if (Qnil != (rval = rb_hash_aref(roptions, sym_analyzer))) {
|
171
|
+
analyzer = frb_get_cwrapped_analyzer(rval);
|
172
|
+
}
|
173
|
+
if (Qnil != (rval = rb_hash_aref(roptions, sym_all_fields))) {
|
174
|
+
all_fields = frb_get_fields(rval);
|
175
|
+
}
|
176
|
+
if (Qnil != (rval = rb_hash_aref(roptions, sym_fields))) {
|
177
|
+
all_fields = frb_get_fields(rval);
|
178
|
+
}
|
179
|
+
if (Qnil != (rval = rb_hash_aref(roptions, sym_tkz_fields))) {
|
180
|
+
tkz_fields = frb_get_fields(rval);
|
181
|
+
}
|
182
|
+
} else {
|
183
|
+
def_fields = frb_get_fields(roptions);
|
184
|
+
roptions = Qnil;
|
185
|
+
}
|
186
|
+
}
|
187
|
+
if (all_fields == NULL) {
|
188
|
+
all_fields = hs_new_ptr(NULL);
|
189
|
+
}
|
190
|
+
|
191
|
+
if (!analyzer) {
|
192
|
+
analyzer = mb_standard_analyzer_new(true);
|
193
|
+
}
|
194
|
+
|
195
|
+
qp = qp_new(analyzer);
|
196
|
+
hs_destroy(qp->all_fields);
|
197
|
+
hs_destroy(qp->def_fields);
|
198
|
+
//hs_destroy(qp->tokenized_fields);
|
199
|
+
|
200
|
+
if (def_fields) hs_safe_merge(all_fields, def_fields);
|
201
|
+
if (tkz_fields) hs_safe_merge(all_fields, tkz_fields);
|
202
|
+
qp->all_fields = all_fields;
|
203
|
+
qp->def_fields = def_fields ? def_fields : all_fields;
|
204
|
+
qp->tokenized_fields = tkz_fields ? tkz_fields : all_fields;
|
205
|
+
qp->fields_top->fields = def_fields;
|
206
|
+
|
207
|
+
qp->allow_any_fields = true;
|
208
|
+
qp->clean_str = true;
|
209
|
+
qp->handle_parse_errors = true;
|
210
|
+
/* handle options */
|
211
|
+
if (roptions != Qnil) {
|
212
|
+
if (Qnil != (rval = rb_hash_aref(roptions, sym_handle_parse_errors))) {
|
213
|
+
qp->handle_parse_errors = RTEST(rval);
|
214
|
+
}
|
215
|
+
if (Qnil != (rval = rb_hash_aref(roptions, sym_validate_fields))) {
|
216
|
+
qp->allow_any_fields = !RTEST(rval);
|
217
|
+
}
|
218
|
+
if (Qnil != (rval = rb_hash_aref(roptions, sym_wild_card_downcase))) {
|
219
|
+
qp->wild_lower = RTEST(rval);
|
220
|
+
}
|
221
|
+
if (Qnil != (rval = rb_hash_aref(roptions, sym_or_default))) {
|
222
|
+
qp->or_default = RTEST(rval);
|
223
|
+
}
|
224
|
+
if (Qnil != (rval = rb_hash_aref(roptions, sym_default_slop))) {
|
225
|
+
qp->def_slop = FIX2INT(rval);
|
226
|
+
}
|
227
|
+
if (Qnil != (rval = rb_hash_aref(roptions, sym_clean_string))) {
|
228
|
+
qp->clean_str = RTEST(rval);
|
229
|
+
}
|
230
|
+
if (Qnil != (rval = rb_hash_aref(roptions, sym_max_clauses))) {
|
231
|
+
qp->max_clauses = FIX2INT(rval);
|
232
|
+
}
|
233
|
+
if (Qnil != (rval = rb_hash_aref(roptions, sym_use_keywords))) {
|
234
|
+
qp->use_keywords = RTEST(rval);
|
235
|
+
}
|
236
|
+
if (Qnil != (rval = rb_hash_aref(roptions, sym_use_typed_range_query))){
|
237
|
+
qp->use_typed_range_query = RTEST(rval);
|
238
|
+
}
|
239
|
+
}
|
240
|
+
Frt_Wrap_Struct(self, frb_qp_mark, frb_qp_free, qp);
|
241
|
+
object_add(qp, self);
|
242
|
+
return self;
|
243
|
+
}
|
244
|
+
|
245
|
+
#define GET_QP QParser *qp = (QParser *)DATA_PTR(self)
|
246
|
+
/*
|
247
|
+
* call-seq:
|
248
|
+
* query_parser.parse(query_string) -> Query
|
249
|
+
*
|
250
|
+
* Parse a query string returning a Query object if parsing was successful.
|
251
|
+
* Will raise a QueryParseException if unsuccessful.
|
252
|
+
*/
|
253
|
+
static VALUE
|
254
|
+
frb_qp_parse(VALUE self, VALUE rstr)
|
255
|
+
{
|
256
|
+
const char *msg = NULL;
|
257
|
+
volatile VALUE rq;
|
258
|
+
GET_QP;
|
259
|
+
rstr = rb_obj_as_string(rstr);
|
260
|
+
TRY
|
261
|
+
rq = frb_get_q(qp_parse(qp, rs2s(rstr)));
|
262
|
+
break;
|
263
|
+
default:
|
264
|
+
msg = xcontext.msg;
|
265
|
+
HANDLED();
|
266
|
+
XENDTRY
|
267
|
+
|
268
|
+
if (msg) {
|
269
|
+
rb_raise(cQueryParseException, msg);
|
270
|
+
}
|
271
|
+
|
272
|
+
return rq;
|
273
|
+
}
|
274
|
+
|
275
|
+
/*
|
276
|
+
* call-seq:
|
277
|
+
* query_parser.fields -> Array of Symbols
|
278
|
+
*
|
279
|
+
* Returns the list of all fields that the QueryParser knows about.
|
280
|
+
*/
|
281
|
+
static VALUE
|
282
|
+
frb_qp_get_fields(VALUE self)
|
283
|
+
{
|
284
|
+
GET_QP;
|
285
|
+
HashSet *fields = qp->all_fields;
|
286
|
+
HashSetEntry *hse;
|
287
|
+
VALUE rfields = rb_ary_new();
|
288
|
+
|
289
|
+
for (hse = fields->first; hse; hse = hse->next) {
|
290
|
+
rb_ary_push(rfields, ID2SYM(rb_intern((char *)hse->elem)));
|
291
|
+
}
|
292
|
+
|
293
|
+
return rfields;
|
294
|
+
}
|
295
|
+
|
296
|
+
/*
|
297
|
+
* call-seq:
|
298
|
+
* query_parser.fields = fields -> self
|
299
|
+
*
|
300
|
+
* Set the list of fields. These fields are expanded for searches on "*".
|
301
|
+
*/
|
302
|
+
static VALUE
|
303
|
+
frb_qp_set_fields(VALUE self, VALUE rfields)
|
304
|
+
{
|
305
|
+
GET_QP;
|
306
|
+
HashSet *fields = frb_get_fields(rfields);
|
307
|
+
|
308
|
+
/* if def_fields == all_fields then we need to replace both */
|
309
|
+
if (qp->def_fields == qp->all_fields) qp->def_fields = NULL;
|
310
|
+
if (qp->tokenized_fields == qp->all_fields) qp->tokenized_fields = NULL;
|
311
|
+
|
312
|
+
if (fields == NULL) {
|
313
|
+
fields = hs_new_ptr(NULL);
|
314
|
+
}
|
315
|
+
|
316
|
+
/* make sure all the fields in tokenized fields are contained in
|
317
|
+
* all_fields */
|
318
|
+
if (qp->tokenized_fields) hs_safe_merge(fields, qp->tokenized_fields);
|
319
|
+
|
320
|
+
/* delete old fields set */
|
321
|
+
assert(qp->all_fields->free_elem_i == dummy_free);
|
322
|
+
hs_destroy(qp->all_fields);
|
323
|
+
|
324
|
+
/* add the new fields set and add to def_fields if necessary */
|
325
|
+
qp->all_fields = fields;
|
326
|
+
if (qp->def_fields == NULL) {
|
327
|
+
qp->def_fields = fields;
|
328
|
+
qp->fields_top->fields = fields;
|
329
|
+
}
|
330
|
+
if (qp->tokenized_fields == NULL) qp->tokenized_fields = fields;
|
331
|
+
|
332
|
+
return self;
|
333
|
+
}
|
334
|
+
|
335
|
+
/*
|
336
|
+
* call-seq:
|
337
|
+
* query_parser.tokenized_fields -> Array of Symbols
|
338
|
+
*
|
339
|
+
* Returns the list of all tokenized_fields that the QueryParser knows about.
|
340
|
+
*/
|
341
|
+
static VALUE
|
342
|
+
frb_qp_get_tkz_fields(VALUE self)
|
343
|
+
{
|
344
|
+
GET_QP;
|
345
|
+
HashSet *fields = qp->tokenized_fields;
|
346
|
+
if (fields) {
|
347
|
+
VALUE rfields = rb_ary_new();
|
348
|
+
HashSetEntry *hse;
|
349
|
+
|
350
|
+
for (hse = fields->first; hse; hse = hse->next) {
|
351
|
+
rb_ary_push(rfields, ID2SYM(rb_intern((char *)hse->elem)));
|
352
|
+
}
|
353
|
+
|
354
|
+
return rfields;
|
355
|
+
}
|
356
|
+
else {
|
357
|
+
return Qnil;
|
358
|
+
}
|
359
|
+
}
|
360
|
+
|
361
|
+
/*
|
362
|
+
* call-seq:
|
363
|
+
* query_parser.tokenized_fields = fields -> self
|
364
|
+
*
|
365
|
+
* Set the list of tokenized_fields. These tokenized_fields are tokenized in
|
366
|
+
* the queries. If this is set to Qnil then all fields will be tokenized.
|
367
|
+
*/
|
368
|
+
static VALUE
|
369
|
+
frb_qp_set_tkz_fields(VALUE self, VALUE rfields)
|
370
|
+
{
|
371
|
+
GET_QP;
|
372
|
+
if (qp->tokenized_fields != qp->all_fields) {
|
373
|
+
hs_destroy(qp->tokenized_fields);
|
374
|
+
}
|
375
|
+
qp->tokenized_fields = frb_get_fields(rfields);
|
376
|
+
return self;
|
377
|
+
}
|
378
|
+
|
379
|
+
/****************************************************************************
|
380
|
+
*
|
381
|
+
* Init function
|
382
|
+
*
|
383
|
+
****************************************************************************/
|
384
|
+
|
385
|
+
/* rdoc hack
|
386
|
+
extern VALUE mFerret = rb_define_module("Ferret");
|
387
|
+
extern VALUE cQueryParser = rb_define_module_under(mFerret, "QueryParser");
|
388
|
+
*/
|
389
|
+
|
390
|
+
/*
|
391
|
+
* Document-class: Ferret::QueryParser::QueryParseException
|
392
|
+
*
|
393
|
+
* == Summary
|
394
|
+
*
|
395
|
+
* Exception raised when there is an error parsing the query string passed to
|
396
|
+
* QueryParser.
|
397
|
+
*/
|
398
|
+
void
|
399
|
+
Init_QueryParseException(void)
|
400
|
+
{
|
401
|
+
cQueryParseException = rb_define_class_under(cQueryParser,
|
402
|
+
"QueryParseException",
|
403
|
+
rb_eStandardError);
|
404
|
+
}
|
405
|
+
|
406
|
+
/*
|
407
|
+
* Document-class: Ferret::QueryParser
|
408
|
+
*
|
409
|
+
* == Summary
|
410
|
+
*
|
411
|
+
* The QueryParser is used to transform user submitted query strings into
|
412
|
+
* QueryObjects. Ferret using its own Query Language known from now on as
|
413
|
+
* Ferret Query Language or FQL.
|
414
|
+
*
|
415
|
+
* == Ferret Query Language
|
416
|
+
*
|
417
|
+
* === Preamble
|
418
|
+
*
|
419
|
+
* The following characters are special characters in FQL;
|
420
|
+
*
|
421
|
+
* :, (, ), [, ], {, }, !, +, ", ~, ^, -, |, <, >, =, *, ?, \
|
422
|
+
*
|
423
|
+
* If you want to use one of these characters in one of your terms you need
|
424
|
+
* to escape it with a \ character. \ escapes itself. The exception to this
|
425
|
+
* rule is within Phrases which a strings surrounded by double quotes (and
|
426
|
+
* will be explained further bellow in the section on PhraseQueries). In
|
427
|
+
* Phrases, only ", | and <> have special meaning and need to be escaped if
|
428
|
+
* you want the literal value. <> is escaped \<\>.
|
429
|
+
*
|
430
|
+
* In the following examples I have only written the query string. This would
|
431
|
+
* be parse like;
|
432
|
+
*
|
433
|
+
* query = query_parser.parse("pet:(dog AND cat)")
|
434
|
+
* puts query # => "+pet:dog +pet:cat"
|
435
|
+
*
|
436
|
+
* === TermQuery
|
437
|
+
*
|
438
|
+
* A term query is the most basic query of all and is what most of the other
|
439
|
+
* queries are built upon. The term consists of a single word. eg;
|
440
|
+
*
|
441
|
+
* 'term'
|
442
|
+
*
|
443
|
+
* Note that the analyzer will be run on the term and if it splits the term
|
444
|
+
* in two then it will be turned into a phrase query. For example, with the
|
445
|
+
* plain Ferret::Analysis::Analyzer, the following;
|
446
|
+
*
|
447
|
+
* 'dave12balmain'
|
448
|
+
*
|
449
|
+
* is equivalent to;
|
450
|
+
*
|
451
|
+
* '"dave balmain"'
|
452
|
+
*
|
453
|
+
* Which we will explain now...
|
454
|
+
*
|
455
|
+
* === PhraseQuery
|
456
|
+
*
|
457
|
+
* A phrase query is a string of terms surrounded by double quotes. For
|
458
|
+
* example you could write;
|
459
|
+
*
|
460
|
+
* '"quick brown fox"'
|
461
|
+
*
|
462
|
+
* But if a "fast" fox is just as good as a quick one you could use the |
|
463
|
+
* character to specify alternate terms.
|
464
|
+
*
|
465
|
+
* '"quick|speedy|fast brown fox"'
|
466
|
+
*
|
467
|
+
* What if we don't care what colour the fox is. We can use the <> to specify
|
468
|
+
* a place setter. eg;
|
469
|
+
*
|
470
|
+
* '"quick|speedy|fast <> fox"'
|
471
|
+
*
|
472
|
+
* This will match any word in between quick and fox. Alternatively we could
|
473
|
+
* set the "slop" for the phrase which allows a certain variation in the
|
474
|
+
* match of the phrase. The slop for a phrase is an integer indicating how
|
475
|
+
* many positions you are allowed to move the terms to get a match. Read more
|
476
|
+
* about the slop factor in Ferret::Search::PhraseQuery. To set the slop
|
477
|
+
* factor for a phrase you can type;
|
478
|
+
*
|
479
|
+
* '"big house"~2'
|
480
|
+
*
|
481
|
+
* This would match "big house", "big red house", "big red brick house" and
|
482
|
+
* even "house big". That's right, you don't need to have th terms in order
|
483
|
+
* if you allow some slop in your phrases. (See Ferret::Search::Spans if you
|
484
|
+
* need a phrase type query with ordered terms.)
|
485
|
+
*
|
486
|
+
* These basic queries will be run on the default field which is set when you
|
487
|
+
* create the query_parser. But what if you want to search a different field.
|
488
|
+
* You'll be needing a ...
|
489
|
+
*
|
490
|
+
* === FieldQuery
|
491
|
+
*
|
492
|
+
* A field query is any field prefixed by <fieldname>:. For example, to
|
493
|
+
* search for all instances of the term "ski" in field "sport", you'd write;
|
494
|
+
*
|
495
|
+
* 'sport:ski'
|
496
|
+
* Or we can apply a field to phrase;
|
497
|
+
*
|
498
|
+
* 'sport:"skiing is fun"'
|
499
|
+
*
|
500
|
+
* Now we have a few types of queries, we'll be needing to glue them together
|
501
|
+
* with a ...
|
502
|
+
*
|
503
|
+
* === BooleanQuery
|
504
|
+
*
|
505
|
+
* There are a couple of ways of writing boolean queries. Firstly you can
|
506
|
+
* specify which terms are required, optional or required not to exist (not).
|
507
|
+
*
|
508
|
+
* * '+' or "REQ" can be used to indicate a required query. "REQ" must be
|
509
|
+
* surrounded by white space.
|
510
|
+
* * '-', '!' or "NOT" are used to indicate query that is required to be
|
511
|
+
* false. "NOT" must be surrounded by white space.
|
512
|
+
* * all other queries are optional if the above symbols are used.
|
513
|
+
*
|
514
|
+
* Some examples;
|
515
|
+
*
|
516
|
+
* '+sport:ski -sport:snowboard sport:toboggan'
|
517
|
+
* '+ingredient:chocolate +ingredient:strawberries -ingredient:wheat'
|
518
|
+
*
|
519
|
+
* You may also use the boolean operators "AND", "&&", "OR" and "||". eg;
|
520
|
+
*
|
521
|
+
* 'sport:ski AND NOT sport:snowboard OR sport:toboggan'
|
522
|
+
* 'ingredient:chocolate AND ingredient:strawberries AND NOT ingredient:wheat'
|
523
|
+
*
|
524
|
+
* You can set the default operator when you create the query parse.
|
525
|
+
*
|
526
|
+
* === RangeQuery
|
527
|
+
*
|
528
|
+
* A range query finds all documents with terms between the two query terms.
|
529
|
+
* This can be very useful in particular for dates. eg;
|
530
|
+
*
|
531
|
+
* 'date:[20050725 20050905]' # all dates >= 20050725 and <= 20050905
|
532
|
+
* 'date:[20050725 20050905}' # all dates >= 20050725 and < 20050905
|
533
|
+
* 'date:{20050725 20050905]' # all dates > 20050725 and <= 20050905
|
534
|
+
* 'date:{20050725 20050905}' # all dates > 20050725 and < 20050905
|
535
|
+
*
|
536
|
+
* You can also do open ended queries like this;
|
537
|
+
*
|
538
|
+
* 'date:[20050725>' # all dates >= 20050725
|
539
|
+
* 'date:{20050725>' # all dates > 20050725
|
540
|
+
* 'date:<20050905]' # all dates <= 20050905
|
541
|
+
* 'date:<20050905}' # all dates < 20050905
|
542
|
+
*
|
543
|
+
* Or like this;
|
544
|
+
*
|
545
|
+
* 'date: >= 20050725'
|
546
|
+
* 'date: > 20050725'
|
547
|
+
* 'date: <= 20050905'
|
548
|
+
* 'date: < 20050905'
|
549
|
+
*
|
550
|
+
* If you prefer the above style you could use a boolean query but like this;
|
551
|
+
*
|
552
|
+
* 'date:( >= 20050725 AND <= 20050905)'
|
553
|
+
*
|
554
|
+
* But rangequery only solution shown first will be faster.
|
555
|
+
*
|
556
|
+
* === WildQuery
|
557
|
+
*
|
558
|
+
* A wild query is a query using the pattern matching characters * and ?. *
|
559
|
+
* matches 0 or more characters while ? matches a single character. This type
|
560
|
+
* of query can be really useful for matching hierarchical categories for
|
561
|
+
* example. Let's say we had this structure;
|
562
|
+
*
|
563
|
+
* /sport/skiing
|
564
|
+
* /sport/cycling
|
565
|
+
* /coding1/ruby
|
566
|
+
* /coding1/c
|
567
|
+
* /coding2/python
|
568
|
+
* /coding2/perl
|
569
|
+
*
|
570
|
+
* If you wanted all categories with programming languages you could use the
|
571
|
+
* query;
|
572
|
+
*
|
573
|
+
* 'category:/coding?/?*'
|
574
|
+
*
|
575
|
+
* Note that this query can be quite expensive if not used carefully. In the
|
576
|
+
* example above there would be no problem but you should be careful not use
|
577
|
+
* the wild characters at the beginning of the query as it'll have to iterate
|
578
|
+
* through every term in that field. Having said that, some fields like the
|
579
|
+
* category field above will only have a small number of distinct fields so
|
580
|
+
* this could be okay.
|
581
|
+
*
|
582
|
+
* === FuzzyQuery
|
583
|
+
*
|
584
|
+
* This is like the sloppy phrase query above, except you are now adding slop
|
585
|
+
* to a term. Basically it measures the Levenshtein distance between two
|
586
|
+
* terms and if the value is below the slop threshold the term is a match.
|
587
|
+
* This time though the slop must be a float between 0 and 1.0, 1.0 being a
|
588
|
+
* perfect match and 0 being far from a match. The default is set to 0.5 so
|
589
|
+
* you don't need to give a slop value if you don't want to. You can set the
|
590
|
+
* default in the Ferret::Search::FuzzyQuery class. Here are a couple of
|
591
|
+
* examples;
|
592
|
+
*
|
593
|
+
* 'content:ferret~'
|
594
|
+
* 'content:Ostralya~0.4'
|
595
|
+
*
|
596
|
+
* Note that this query can be quite expensive. If you'd like to use this
|
597
|
+
* query, you may want to set a minimum prefix length in the FuzzyQuery
|
598
|
+
* class. This can substantially reduce the number of terms that the query
|
599
|
+
* will iterate over.
|
600
|
+
*
|
601
|
+
*/
|
602
|
+
void
|
603
|
+
Init_QueryParser(void)
|
604
|
+
{
|
605
|
+
/* hash keys */
|
606
|
+
sym_wild_card_downcase = ID2SYM(rb_intern("wild_card_downcase"));
|
607
|
+
sym_fields = ID2SYM(rb_intern("fields"));
|
608
|
+
sym_all_fields = ID2SYM(rb_intern("all_fields"));
|
609
|
+
sym_tkz_fields = ID2SYM(rb_intern("tokenized_fields"));
|
610
|
+
sym_default_field = ID2SYM(rb_intern("default_field"));
|
611
|
+
sym_validate_fields = ID2SYM(rb_intern("validate_fields"));
|
612
|
+
sym_or_default = ID2SYM(rb_intern("or_default"));
|
613
|
+
sym_default_slop = ID2SYM(rb_intern("default_slop"));
|
614
|
+
sym_handle_parse_errors = ID2SYM(rb_intern("handle_parse_errors"));
|
615
|
+
sym_clean_string = ID2SYM(rb_intern("clean_string"));
|
616
|
+
sym_max_clauses = ID2SYM(rb_intern("max_clauses"));
|
617
|
+
sym_use_keywords = ID2SYM(rb_intern("use_keywords"));
|
618
|
+
sym_use_typed_range_query = ID2SYM(rb_intern("use_typed_range_query"));
|
619
|
+
|
620
|
+
/* QueryParser */
|
621
|
+
cQueryParser = rb_define_class_under(mFerret, "QueryParser", rb_cObject);
|
622
|
+
rb_define_alloc_func(cQueryParser, frb_data_alloc);
|
623
|
+
|
624
|
+
rb_define_method(cQueryParser, "initialize", frb_qp_init, -1);
|
625
|
+
rb_define_method(cQueryParser, "parse", frb_qp_parse, 1);
|
626
|
+
rb_define_method(cQueryParser, "fields", frb_qp_get_fields, 0);
|
627
|
+
rb_define_method(cQueryParser, "fields=", frb_qp_set_fields, 1);
|
628
|
+
rb_define_method(cQueryParser, "tokenized_fields",
|
629
|
+
frb_qp_get_tkz_fields, 0);
|
630
|
+
rb_define_method(cQueryParser, "tokenized_fields=",
|
631
|
+
frb_qp_set_tkz_fields, 1);
|
632
|
+
|
633
|
+
Init_QueryParseException();
|
634
|
+
}
|
635
|
+
|