sdsykes-ferret 0.11.6.19
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 +102 -0
- data/Rakefile +338 -0
- data/TODO +17 -0
- data/TUTORIAL +231 -0
- data/bin/ferret-browser +79 -0
- data/ext/analysis.c +1555 -0
- data/ext/analysis.h +219 -0
- data/ext/api.c +69 -0
- data/ext/api.h +27 -0
- data/ext/array.c +123 -0
- data/ext/array.h +53 -0
- data/ext/bitvector.c +540 -0
- data/ext/bitvector.h +272 -0
- data/ext/compound_io.c +383 -0
- data/ext/config.h +42 -0
- data/ext/document.c +156 -0
- data/ext/document.h +53 -0
- data/ext/except.c +120 -0
- data/ext/except.h +168 -0
- data/ext/extconf.rb +14 -0
- data/ext/ferret.c +402 -0
- data/ext/ferret.h +91 -0
- data/ext/filter.c +156 -0
- data/ext/fs_store.c +483 -0
- data/ext/global.c +418 -0
- data/ext/global.h +117 -0
- data/ext/hash.c +567 -0
- data/ext/hash.h +473 -0
- data/ext/hashset.c +170 -0
- data/ext/hashset.h +187 -0
- data/ext/header.h +58 -0
- data/ext/helper.c +62 -0
- data/ext/helper.h +13 -0
- data/ext/inc/lang.h +48 -0
- data/ext/inc/threading.h +31 -0
- data/ext/index.c +6425 -0
- data/ext/index.h +961 -0
- data/ext/lang.h +66 -0
- data/ext/libstemmer.c +92 -0
- data/ext/libstemmer.h +79 -0
- data/ext/mempool.c +87 -0
- data/ext/mempool.h +35 -0
- data/ext/modules.h +162 -0
- data/ext/multimapper.c +310 -0
- data/ext/multimapper.h +51 -0
- data/ext/posh.c +1006 -0
- data/ext/posh.h +1007 -0
- data/ext/priorityqueue.c +151 -0
- data/ext/priorityqueue.h +143 -0
- data/ext/q_boolean.c +1608 -0
- data/ext/q_const_score.c +161 -0
- data/ext/q_filtered_query.c +209 -0
- data/ext/q_fuzzy.c +268 -0
- data/ext/q_match_all.c +148 -0
- data/ext/q_multi_term.c +677 -0
- data/ext/q_parser.c +2825 -0
- data/ext/q_phrase.c +1126 -0
- data/ext/q_prefix.c +100 -0
- data/ext/q_range.c +350 -0
- data/ext/q_span.c +2402 -0
- data/ext/q_term.c +337 -0
- data/ext/q_wildcard.c +171 -0
- data/ext/r_analysis.c +2575 -0
- data/ext/r_index.c +3472 -0
- data/ext/r_qparser.c +585 -0
- data/ext/r_search.c +4105 -0
- data/ext/r_store.c +513 -0
- data/ext/r_utils.c +963 -0
- data/ext/ram_store.c +471 -0
- data/ext/search.c +1741 -0
- data/ext/search.h +885 -0
- data/ext/similarity.c +150 -0
- data/ext/similarity.h +82 -0
- data/ext/sort.c +983 -0
- data/ext/stem_ISO_8859_1_danish.c +338 -0
- data/ext/stem_ISO_8859_1_danish.h +16 -0
- data/ext/stem_ISO_8859_1_dutch.c +635 -0
- data/ext/stem_ISO_8859_1_dutch.h +16 -0
- data/ext/stem_ISO_8859_1_english.c +1156 -0
- data/ext/stem_ISO_8859_1_english.h +16 -0
- data/ext/stem_ISO_8859_1_finnish.c +792 -0
- data/ext/stem_ISO_8859_1_finnish.h +16 -0
- data/ext/stem_ISO_8859_1_french.c +1276 -0
- data/ext/stem_ISO_8859_1_french.h +16 -0
- data/ext/stem_ISO_8859_1_german.c +512 -0
- data/ext/stem_ISO_8859_1_german.h +16 -0
- data/ext/stem_ISO_8859_1_italian.c +1091 -0
- data/ext/stem_ISO_8859_1_italian.h +16 -0
- data/ext/stem_ISO_8859_1_norwegian.c +296 -0
- data/ext/stem_ISO_8859_1_norwegian.h +16 -0
- data/ext/stem_ISO_8859_1_porter.c +776 -0
- data/ext/stem_ISO_8859_1_porter.h +16 -0
- data/ext/stem_ISO_8859_1_portuguese.c +1035 -0
- data/ext/stem_ISO_8859_1_portuguese.h +16 -0
- data/ext/stem_ISO_8859_1_spanish.c +1119 -0
- data/ext/stem_ISO_8859_1_spanish.h +16 -0
- data/ext/stem_ISO_8859_1_swedish.c +307 -0
- data/ext/stem_ISO_8859_1_swedish.h +16 -0
- data/ext/stem_KOI8_R_russian.c +701 -0
- data/ext/stem_KOI8_R_russian.h +16 -0
- data/ext/stem_UTF_8_danish.c +344 -0
- data/ext/stem_UTF_8_danish.h +16 -0
- data/ext/stem_UTF_8_dutch.c +653 -0
- data/ext/stem_UTF_8_dutch.h +16 -0
- data/ext/stem_UTF_8_english.c +1176 -0
- data/ext/stem_UTF_8_english.h +16 -0
- data/ext/stem_UTF_8_finnish.c +808 -0
- data/ext/stem_UTF_8_finnish.h +16 -0
- data/ext/stem_UTF_8_french.c +1296 -0
- data/ext/stem_UTF_8_french.h +16 -0
- data/ext/stem_UTF_8_german.c +526 -0
- data/ext/stem_UTF_8_german.h +16 -0
- data/ext/stem_UTF_8_italian.c +1113 -0
- data/ext/stem_UTF_8_italian.h +16 -0
- data/ext/stem_UTF_8_norwegian.c +302 -0
- data/ext/stem_UTF_8_norwegian.h +16 -0
- data/ext/stem_UTF_8_porter.c +794 -0
- data/ext/stem_UTF_8_porter.h +16 -0
- data/ext/stem_UTF_8_portuguese.c +1055 -0
- data/ext/stem_UTF_8_portuguese.h +16 -0
- data/ext/stem_UTF_8_russian.c +709 -0
- data/ext/stem_UTF_8_russian.h +16 -0
- data/ext/stem_UTF_8_spanish.c +1137 -0
- data/ext/stem_UTF_8_spanish.h +16 -0
- data/ext/stem_UTF_8_swedish.c +313 -0
- data/ext/stem_UTF_8_swedish.h +16 -0
- data/ext/stopwords.c +401 -0
- data/ext/store.c +692 -0
- data/ext/store.h +777 -0
- data/ext/term_vectors.c +352 -0
- data/ext/threading.h +31 -0
- data/ext/utilities.c +446 -0
- data/ext/win32.h +54 -0
- data/lib/ferret.rb +29 -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/index.rb +786 -0
- data/lib/ferret/number_tools.rb +157 -0
- data/lib/ferret_version.rb +3 -0
- data/setup.rb +1555 -0
- data/test/test_all.rb +5 -0
- data/test/test_helper.rb +24 -0
- data/test/threading/number_to_spoken.rb +132 -0
- data/test/threading/thread_safety_index_test.rb +79 -0
- data/test/threading/thread_safety_read_write_test.rb +76 -0
- data/test/threading/thread_safety_test.rb +133 -0
- data/test/unit/analysis/tc_analyzer.rb +548 -0
- data/test/unit/analysis/tc_token_stream.rb +646 -0
- data/test/unit/index/tc_index.rb +762 -0
- data/test/unit/index/tc_index_reader.rb +699 -0
- data/test/unit/index/tc_index_writer.rb +437 -0
- data/test/unit/index/th_doc.rb +315 -0
- data/test/unit/largefile/tc_largefile.rb +46 -0
- data/test/unit/query_parser/tc_query_parser.rb +238 -0
- data/test/unit/search/tc_filter.rb +135 -0
- data/test/unit/search/tc_fuzzy_query.rb +147 -0
- data/test/unit/search/tc_index_searcher.rb +61 -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 +384 -0
- data/test/unit/store/tc_fs_store.rb +77 -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/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
- metadata +285 -0
data/ext/q_term.c
ADDED
@@ -0,0 +1,337 @@
|
|
1
|
+
#include <string.h>
|
2
|
+
#include "search.h"
|
3
|
+
|
4
|
+
#define TQ(query) ((TermQuery *)(query))
|
5
|
+
#define TSc(scorer) ((TermScorer *)(scorer))
|
6
|
+
|
7
|
+
/***************************************************************************
|
8
|
+
*
|
9
|
+
* TermScorer
|
10
|
+
*
|
11
|
+
***************************************************************************/
|
12
|
+
|
13
|
+
#define SCORE_CACHE_SIZE 32
|
14
|
+
#define TDE_READ_SIZE 32
|
15
|
+
|
16
|
+
typedef struct TermScorer
|
17
|
+
{
|
18
|
+
Scorer super;
|
19
|
+
int docs[TDE_READ_SIZE];
|
20
|
+
int freqs[TDE_READ_SIZE];
|
21
|
+
int pointer;
|
22
|
+
int pointer_max;
|
23
|
+
float score_cache[SCORE_CACHE_SIZE];
|
24
|
+
Weight *weight;
|
25
|
+
TermDocEnum *tde;
|
26
|
+
uchar *norms;
|
27
|
+
float weight_value;
|
28
|
+
} TermScorer;
|
29
|
+
|
30
|
+
static float tsc_score(Scorer *self)
|
31
|
+
{
|
32
|
+
TermScorer *ts = TSc(self);
|
33
|
+
int freq = ts->freqs[ts->pointer];
|
34
|
+
float score;
|
35
|
+
/* compute tf(f)*weight */
|
36
|
+
if (freq < SCORE_CACHE_SIZE) { /* check cache */
|
37
|
+
score = ts->score_cache[freq]; /* cache hit */
|
38
|
+
}
|
39
|
+
else {
|
40
|
+
/* cache miss */
|
41
|
+
score = sim_tf(self->similarity, (float)freq) * ts->weight_value;
|
42
|
+
}
|
43
|
+
/* normalize for field */
|
44
|
+
score *= sim_decode_norm(self->similarity, ts->norms[self->doc]);
|
45
|
+
return score;
|
46
|
+
}
|
47
|
+
|
48
|
+
static bool tsc_next(Scorer *self)
|
49
|
+
{
|
50
|
+
TermScorer *ts = TSc(self);
|
51
|
+
|
52
|
+
ts->pointer++;
|
53
|
+
if (ts->pointer >= ts->pointer_max) {
|
54
|
+
/* refill buffer */
|
55
|
+
ts->pointer_max = ts->tde->read(ts->tde, ts->docs, ts->freqs,
|
56
|
+
TDE_READ_SIZE);
|
57
|
+
if (ts->pointer_max != 0) {
|
58
|
+
ts->pointer = 0;
|
59
|
+
}
|
60
|
+
else {
|
61
|
+
return false;
|
62
|
+
}
|
63
|
+
}
|
64
|
+
self->doc = ts->docs[ts->pointer];
|
65
|
+
return true;
|
66
|
+
}
|
67
|
+
|
68
|
+
static bool tsc_skip_to(Scorer *self, int doc_num)
|
69
|
+
{
|
70
|
+
TermScorer *ts = TSc(self);
|
71
|
+
TermDocEnum *tde = ts->tde;
|
72
|
+
|
73
|
+
/* first scan in cache */
|
74
|
+
while (++(ts->pointer) < ts->pointer_max) {
|
75
|
+
if (ts->docs[ts->pointer] >= doc_num) {
|
76
|
+
self->doc = ts->docs[ts->pointer];
|
77
|
+
return true;
|
78
|
+
}
|
79
|
+
}
|
80
|
+
|
81
|
+
/* not found in cache, seek underlying stream */
|
82
|
+
if (tde->skip_to(tde, doc_num)) {
|
83
|
+
ts->pointer_max = 1;
|
84
|
+
ts->pointer = 0;
|
85
|
+
ts->docs[0] = self->doc = tde->doc_num(tde);
|
86
|
+
ts->freqs[0] = tde->freq(tde);
|
87
|
+
return true;
|
88
|
+
}
|
89
|
+
else {
|
90
|
+
return false;
|
91
|
+
}
|
92
|
+
}
|
93
|
+
|
94
|
+
static Explanation *tsc_explain(Scorer *self, int doc_num)
|
95
|
+
{
|
96
|
+
TermScorer *ts = TSc(self);
|
97
|
+
Query *query = ts->weight->get_query(ts->weight);
|
98
|
+
int tf = 0;
|
99
|
+
|
100
|
+
tsc_skip_to(self, doc_num);
|
101
|
+
if (self->doc == doc_num) {
|
102
|
+
tf = ts->freqs[ts->pointer];
|
103
|
+
}
|
104
|
+
return expl_new(sim_tf(self->similarity, (float)tf),
|
105
|
+
"tf(term_freq(%s:%s)=%d)",
|
106
|
+
TQ(query)->field, TQ(query)->term, tf);
|
107
|
+
}
|
108
|
+
|
109
|
+
static void tsc_destroy(Scorer *self)
|
110
|
+
{
|
111
|
+
TSc(self)->tde->close(TSc(self)->tde);
|
112
|
+
scorer_destroy_i(self);
|
113
|
+
}
|
114
|
+
|
115
|
+
static Scorer *tsc_new(Weight *weight, TermDocEnum *tde, uchar *norms)
|
116
|
+
{
|
117
|
+
int i;
|
118
|
+
Scorer *self = scorer_new(TermScorer, weight->similarity);
|
119
|
+
TSc(self)->weight = weight;
|
120
|
+
TSc(self)->tde = tde;
|
121
|
+
TSc(self)->norms = norms;
|
122
|
+
TSc(self)->weight_value = weight->value;
|
123
|
+
|
124
|
+
for (i = 0; i < SCORE_CACHE_SIZE; i++) {
|
125
|
+
TSc(self)->score_cache[i]
|
126
|
+
= sim_tf(self->similarity, (float)i) * TSc(self)->weight_value;
|
127
|
+
}
|
128
|
+
|
129
|
+
self->score = &tsc_score;
|
130
|
+
self->next = &tsc_next;
|
131
|
+
self->skip_to = &tsc_skip_to;
|
132
|
+
self->explain = &tsc_explain;
|
133
|
+
self->destroy = &tsc_destroy;
|
134
|
+
return self;
|
135
|
+
}
|
136
|
+
|
137
|
+
/***************************************************************************
|
138
|
+
*
|
139
|
+
* TermWeight
|
140
|
+
*
|
141
|
+
***************************************************************************/
|
142
|
+
|
143
|
+
static Scorer *tw_scorer(Weight *self, IndexReader *ir)
|
144
|
+
{
|
145
|
+
TermQuery *tq = TQ(self->query);
|
146
|
+
TermDocEnum *tde = ir_term_docs_for(ir, tq->field, tq->term);
|
147
|
+
if (!tde) {
|
148
|
+
return NULL;
|
149
|
+
}
|
150
|
+
|
151
|
+
return tsc_new(self, tde, ir_get_norms(ir, tq->field));
|
152
|
+
}
|
153
|
+
|
154
|
+
static Explanation *tw_explain(Weight *self, IndexReader *ir, int doc_num)
|
155
|
+
{
|
156
|
+
Explanation *qnorm_expl;
|
157
|
+
Explanation *field_expl;
|
158
|
+
Scorer *scorer;
|
159
|
+
Explanation *tf_expl;
|
160
|
+
uchar *field_norms;
|
161
|
+
float field_norm;
|
162
|
+
Explanation *field_norm_expl;
|
163
|
+
|
164
|
+
char *query_str = self->query->to_s(self->query, "");
|
165
|
+
TermQuery *tq = TQ(self->query);
|
166
|
+
char *term = tq->term;
|
167
|
+
char *field = tq->field;
|
168
|
+
|
169
|
+
Explanation *expl = expl_new(0.0, "weight(%s in %d), product of:",
|
170
|
+
query_str, doc_num);
|
171
|
+
|
172
|
+
/* We need two of these as it's included in both the query explanation
|
173
|
+
* and the field explanation */
|
174
|
+
Explanation *idf_expl1 = expl_new(self->idf, "idf(doc_freq=%d)",
|
175
|
+
ir_doc_freq(ir, field, term));
|
176
|
+
Explanation *idf_expl2 = expl_new(self->idf, "idf(doc_freq=%d)",
|
177
|
+
ir_doc_freq(ir, field, term));
|
178
|
+
|
179
|
+
/* explain query weight */
|
180
|
+
Explanation *query_expl = expl_new(0.0, "query_weight(%s), product of:",
|
181
|
+
query_str);
|
182
|
+
free(query_str);
|
183
|
+
|
184
|
+
if (self->query->boost != 1.0) {
|
185
|
+
expl_add_detail(query_expl, expl_new(self->query->boost, "boost"));
|
186
|
+
}
|
187
|
+
|
188
|
+
expl_add_detail(query_expl, idf_expl1);
|
189
|
+
|
190
|
+
qnorm_expl = expl_new(self->qnorm, "query_norm");
|
191
|
+
expl_add_detail(query_expl, qnorm_expl);
|
192
|
+
|
193
|
+
query_expl->value = self->query->boost
|
194
|
+
* idf_expl1->value * qnorm_expl->value;
|
195
|
+
|
196
|
+
expl_add_detail(expl, query_expl);
|
197
|
+
|
198
|
+
/* explain field weight */
|
199
|
+
field_expl = expl_new(0.0, "field_weight(%s:%s in %d), product of:",
|
200
|
+
field, term, doc_num);
|
201
|
+
|
202
|
+
scorer = self->scorer(self, ir);
|
203
|
+
tf_expl = scorer->explain(scorer, doc_num);
|
204
|
+
scorer->destroy(scorer);
|
205
|
+
expl_add_detail(field_expl, tf_expl);
|
206
|
+
expl_add_detail(field_expl, idf_expl2);
|
207
|
+
|
208
|
+
field_norms = ir_get_norms(ir, field);
|
209
|
+
field_norm = (field_norms
|
210
|
+
? sim_decode_norm(self->similarity, field_norms[doc_num])
|
211
|
+
: (float)0.0);
|
212
|
+
field_norm_expl = expl_new(field_norm, "field_norm(field=%s, doc=%d)",
|
213
|
+
field, doc_num);
|
214
|
+
|
215
|
+
expl_add_detail(field_expl, field_norm_expl);
|
216
|
+
|
217
|
+
field_expl->value = tf_expl->value * idf_expl2->value
|
218
|
+
* field_norm_expl->value;
|
219
|
+
|
220
|
+
/* combine them */
|
221
|
+
if (query_expl->value == 1.0) {
|
222
|
+
expl_destroy(expl);
|
223
|
+
return field_expl;
|
224
|
+
} else {
|
225
|
+
expl->value = (query_expl->value * field_expl->value);
|
226
|
+
expl_add_detail(expl, field_expl);
|
227
|
+
return expl;
|
228
|
+
}
|
229
|
+
}
|
230
|
+
|
231
|
+
static char *tw_to_s(Weight *self)
|
232
|
+
{
|
233
|
+
return strfmt("TermWeight(%f)", self->value);
|
234
|
+
}
|
235
|
+
|
236
|
+
static Weight *tw_new(Query *query, Searcher *searcher)
|
237
|
+
{
|
238
|
+
Weight *self = w_new(Weight, query);
|
239
|
+
self->scorer = &tw_scorer;
|
240
|
+
self->explain = &tw_explain;
|
241
|
+
self->to_s = &tw_to_s;
|
242
|
+
|
243
|
+
self->similarity = query->get_similarity(query, searcher);
|
244
|
+
self->idf = sim_idf(self->similarity,
|
245
|
+
searcher->doc_freq(searcher,
|
246
|
+
TQ(query)->field,
|
247
|
+
TQ(query)->term),
|
248
|
+
searcher->max_doc(searcher)); /* compute idf */
|
249
|
+
|
250
|
+
return self;
|
251
|
+
}
|
252
|
+
|
253
|
+
/***************************************************************************
|
254
|
+
*
|
255
|
+
* TermQuery
|
256
|
+
*
|
257
|
+
***************************************************************************/
|
258
|
+
|
259
|
+
static void tq_destroy(Query *self)
|
260
|
+
{
|
261
|
+
free(TQ(self)->term);
|
262
|
+
free(TQ(self)->field);
|
263
|
+
q_destroy_i(self);
|
264
|
+
}
|
265
|
+
|
266
|
+
static char *tq_to_s(Query *self, const char *field)
|
267
|
+
{
|
268
|
+
size_t flen = strlen(TQ(self)->field);
|
269
|
+
size_t tlen = strlen(TQ(self)->term);
|
270
|
+
char *buffer = ALLOC_N(char, 34 + flen + tlen);
|
271
|
+
char *b = buffer;
|
272
|
+
if (strcmp(field, TQ(self)->field) != 0) {
|
273
|
+
memcpy(b, TQ(self)->field, sizeof(char) * flen);
|
274
|
+
b[flen] = ':';
|
275
|
+
b += flen + 1;
|
276
|
+
}
|
277
|
+
memcpy(b, TQ(self)->term, tlen);
|
278
|
+
b += tlen;
|
279
|
+
*b = 0;
|
280
|
+
if (self->boost != 1.0) {
|
281
|
+
*b = '^';
|
282
|
+
dbl_to_s(b+1, self->boost);
|
283
|
+
}
|
284
|
+
return buffer;
|
285
|
+
}
|
286
|
+
|
287
|
+
static void tq_extract_terms(Query *self, HashSet *terms)
|
288
|
+
{
|
289
|
+
hs_add(terms, term_new(TQ(self)->field, TQ(self)->term));
|
290
|
+
}
|
291
|
+
|
292
|
+
static unsigned long tq_hash(Query *self)
|
293
|
+
{
|
294
|
+
return str_hash(TQ(self)->term) ^ str_hash(TQ(self)->field);
|
295
|
+
}
|
296
|
+
|
297
|
+
static int tq_eq(Query *self, Query *o)
|
298
|
+
{
|
299
|
+
return (strcmp(TQ(self)->term, TQ(o)->term) == 0)
|
300
|
+
&& (strcmp(TQ(self)->field, TQ(o)->field) == 0);
|
301
|
+
}
|
302
|
+
|
303
|
+
static MatchVector *tq_get_matchv_i(Query *self, MatchVector *mv,
|
304
|
+
TermVector *tv)
|
305
|
+
{
|
306
|
+
if (strcmp(tv->field, TQ(self)->field) == 0) {
|
307
|
+
int i;
|
308
|
+
TVTerm *tv_term = tv_get_tv_term(tv, TQ(self)->term);
|
309
|
+
if (tv_term) {
|
310
|
+
for (i = 0; i < tv_term->freq; i++) {
|
311
|
+
int pos = tv_term->positions[i];
|
312
|
+
matchv_add(mv, pos, pos);
|
313
|
+
}
|
314
|
+
}
|
315
|
+
}
|
316
|
+
return mv;
|
317
|
+
}
|
318
|
+
|
319
|
+
Query *tq_new(const char *field, const char *term)
|
320
|
+
{
|
321
|
+
Query *self = q_new(TermQuery);
|
322
|
+
|
323
|
+
TQ(self)->field = estrdup(field);
|
324
|
+
TQ(self)->term = estrdup(term);
|
325
|
+
|
326
|
+
self->type = TERM_QUERY;
|
327
|
+
self->extract_terms = &tq_extract_terms;
|
328
|
+
self->to_s = &tq_to_s;
|
329
|
+
self->hash = &tq_hash;
|
330
|
+
self->eq = &tq_eq;
|
331
|
+
|
332
|
+
self->destroy_i = &tq_destroy;
|
333
|
+
self->create_weight_i = &tw_new;
|
334
|
+
self->get_matchv_i = &tq_get_matchv_i;
|
335
|
+
|
336
|
+
return self;
|
337
|
+
}
|
data/ext/q_wildcard.c
ADDED
@@ -0,0 +1,171 @@
|
|
1
|
+
#include <string.h>
|
2
|
+
#include "search.h"
|
3
|
+
|
4
|
+
/****************************************************************************
|
5
|
+
*
|
6
|
+
* WildCardQuery
|
7
|
+
*
|
8
|
+
****************************************************************************/
|
9
|
+
|
10
|
+
#define WCQ(query) ((WildCardQuery *)(query))
|
11
|
+
|
12
|
+
static char *wcq_to_s(Query *self, const char *current_field)
|
13
|
+
{
|
14
|
+
char *buffer, *bptr;
|
15
|
+
const char *field = WCQ(self)->field;
|
16
|
+
const char *pattern = WCQ(self)->pattern;
|
17
|
+
size_t flen = strlen(field);
|
18
|
+
size_t plen = strlen(pattern);
|
19
|
+
bptr = buffer = ALLOC_N(char, plen + flen + 35);
|
20
|
+
|
21
|
+
if (strcmp(field, current_field) != 0) {
|
22
|
+
sprintf(bptr, "%s:", field);
|
23
|
+
bptr += flen + 1;
|
24
|
+
}
|
25
|
+
sprintf(bptr, "%s", pattern);
|
26
|
+
bptr += plen;
|
27
|
+
|
28
|
+
if (self->boost != 1.0) {
|
29
|
+
*bptr = '^';
|
30
|
+
dbl_to_s(++bptr, self->boost);
|
31
|
+
}
|
32
|
+
|
33
|
+
return buffer;
|
34
|
+
}
|
35
|
+
|
36
|
+
bool wc_match(const char *pattern, const char *text)
|
37
|
+
{
|
38
|
+
const char *p = pattern, *t = text, *xt;
|
39
|
+
|
40
|
+
/* include '\0' as we need to match empty string */
|
41
|
+
const char *text_last = t + strlen(t);
|
42
|
+
|
43
|
+
for (;; p++, t++) {
|
44
|
+
|
45
|
+
/* end of text so make sure end of pattern doesn't matter */
|
46
|
+
if (*t == '\0') {
|
47
|
+
while (*p) {
|
48
|
+
if (*p != WILD_STRING) {
|
49
|
+
return false;
|
50
|
+
}
|
51
|
+
p++;
|
52
|
+
}
|
53
|
+
return true;
|
54
|
+
}
|
55
|
+
|
56
|
+
/* If we've gone past the end of the pattern, return false. */
|
57
|
+
if (*p == '\0') {
|
58
|
+
return false;
|
59
|
+
}
|
60
|
+
|
61
|
+
/* Match a single character, so continue. */
|
62
|
+
if (*p == WILD_CHAR) {
|
63
|
+
continue;
|
64
|
+
}
|
65
|
+
|
66
|
+
if (*p == WILD_STRING) {
|
67
|
+
/* Look at the character beyond the '*'. */
|
68
|
+
p++;
|
69
|
+
/* Examine the string, starting at the last character. */
|
70
|
+
for (xt = text_last; xt >= t; xt--) {
|
71
|
+
if (wc_match(p, xt)) return true;
|
72
|
+
}
|
73
|
+
return false;
|
74
|
+
}
|
75
|
+
if (*p != *t) {
|
76
|
+
return false;
|
77
|
+
}
|
78
|
+
}
|
79
|
+
|
80
|
+
return false;
|
81
|
+
}
|
82
|
+
|
83
|
+
static Query *wcq_rewrite(Query *self, IndexReader *ir)
|
84
|
+
{
|
85
|
+
Query *q;
|
86
|
+
const char *field = WCQ(self)->field;
|
87
|
+
const char *pattern = WCQ(self)->pattern;
|
88
|
+
const char *first_star = strchr(pattern, WILD_STRING);
|
89
|
+
const char *first_ques = strchr(pattern, WILD_CHAR);
|
90
|
+
|
91
|
+
if (NULL == first_star && NULL == first_ques) {
|
92
|
+
q = tq_new(field, pattern);
|
93
|
+
q->boost = self->boost;
|
94
|
+
}
|
95
|
+
else {
|
96
|
+
const int field_num = fis_get_field_num(ir->fis, field);
|
97
|
+
q = multi_tq_new_conf(field, MTQMaxTerms(self), 0.0);
|
98
|
+
|
99
|
+
if (field_num >= 0) {
|
100
|
+
TermEnum *te;
|
101
|
+
char prefix[MAX_WORD_SIZE] = "";
|
102
|
+
int prefix_len;
|
103
|
+
|
104
|
+
pattern = (first_ques && (!first_star || first_star > first_ques))
|
105
|
+
? first_ques : first_star;
|
106
|
+
|
107
|
+
prefix_len = (int)(pattern - WCQ(self)->pattern);
|
108
|
+
|
109
|
+
if (prefix_len > 0) {
|
110
|
+
memcpy(prefix, WCQ(self)->pattern, prefix_len);
|
111
|
+
prefix[prefix_len] = '\0';
|
112
|
+
}
|
113
|
+
|
114
|
+
te = ir->terms_from(ir, field_num, prefix);
|
115
|
+
|
116
|
+
if (te != NULL) {
|
117
|
+
const char *term = te->curr_term;
|
118
|
+
const char *pat_term = term + prefix_len;
|
119
|
+
do {
|
120
|
+
if (prefix && strncmp(term, prefix, prefix_len) != 0) {
|
121
|
+
break;
|
122
|
+
}
|
123
|
+
|
124
|
+
if (wc_match(pattern, pat_term)) {
|
125
|
+
multi_tq_add_term(q, term);
|
126
|
+
}
|
127
|
+
} while (te->next(te) != NULL);
|
128
|
+
te->close(te);
|
129
|
+
}
|
130
|
+
}
|
131
|
+
}
|
132
|
+
|
133
|
+
return q;
|
134
|
+
}
|
135
|
+
|
136
|
+
static void wcq_destroy(Query *self)
|
137
|
+
{
|
138
|
+
free(WCQ(self)->field);
|
139
|
+
free(WCQ(self)->pattern);
|
140
|
+
q_destroy_i(self);
|
141
|
+
}
|
142
|
+
|
143
|
+
static unsigned long wcq_hash(Query *self)
|
144
|
+
{
|
145
|
+
return str_hash(WCQ(self)->field) ^ str_hash(WCQ(self)->pattern);
|
146
|
+
}
|
147
|
+
|
148
|
+
static int wcq_eq(Query *self, Query *o)
|
149
|
+
{
|
150
|
+
return (strcmp(WCQ(self)->pattern, WCQ(o)->pattern) == 0)
|
151
|
+
&& (strcmp(WCQ(self)->field, WCQ(o)->field) == 0);
|
152
|
+
}
|
153
|
+
|
154
|
+
Query *wcq_new(const char *field, const char *pattern)
|
155
|
+
{
|
156
|
+
Query *self = q_new(WildCardQuery);
|
157
|
+
|
158
|
+
WCQ(self)->field = estrdup(field);
|
159
|
+
WCQ(self)->pattern = estrdup(pattern);
|
160
|
+
MTQMaxTerms(self) = WILD_CARD_QUERY_MAX_TERMS;
|
161
|
+
|
162
|
+
self->type = WILD_CARD_QUERY;
|
163
|
+
self->rewrite = &wcq_rewrite;
|
164
|
+
self->to_s = &wcq_to_s;
|
165
|
+
self->hash = &wcq_hash;
|
166
|
+
self->eq = &wcq_eq;
|
167
|
+
self->destroy_i = &wcq_destroy;
|
168
|
+
self->create_weight_i = &q_create_weight_unsup;
|
169
|
+
|
170
|
+
return self;
|
171
|
+
}
|