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/q_term.c
ADDED
@@ -0,0 +1,337 @@
|
|
1
|
+
#include "symbol.h"
|
2
|
+
#include <string.h>
|
3
|
+
#include "search.h"
|
4
|
+
#include "internal.h"
|
5
|
+
|
6
|
+
#define TQ(query) ((TermQuery *)(query))
|
7
|
+
#define TSc(scorer) ((TermScorer *)(scorer))
|
8
|
+
|
9
|
+
/***************************************************************************
|
10
|
+
*
|
11
|
+
* TermScorer
|
12
|
+
*
|
13
|
+
***************************************************************************/
|
14
|
+
|
15
|
+
#define SCORE_CACHE_SIZE 32
|
16
|
+
#define TDE_READ_SIZE 32
|
17
|
+
|
18
|
+
typedef struct TermScorer
|
19
|
+
{
|
20
|
+
Scorer super;
|
21
|
+
int docs[TDE_READ_SIZE];
|
22
|
+
int freqs[TDE_READ_SIZE];
|
23
|
+
int pointer;
|
24
|
+
int pointer_max;
|
25
|
+
float score_cache[SCORE_CACHE_SIZE];
|
26
|
+
Weight *weight;
|
27
|
+
TermDocEnum *tde;
|
28
|
+
uchar *norms;
|
29
|
+
float weight_value;
|
30
|
+
} TermScorer;
|
31
|
+
|
32
|
+
static float tsc_score(Scorer *self)
|
33
|
+
{
|
34
|
+
TermScorer *ts = TSc(self);
|
35
|
+
int freq = ts->freqs[ts->pointer];
|
36
|
+
float score;
|
37
|
+
/* compute tf(f)*weight */
|
38
|
+
if (freq < SCORE_CACHE_SIZE) { /* check cache */
|
39
|
+
score = ts->score_cache[freq]; /* cache hit */
|
40
|
+
}
|
41
|
+
else {
|
42
|
+
/* cache miss */
|
43
|
+
score = sim_tf(self->similarity, (float)freq) * ts->weight_value;
|
44
|
+
}
|
45
|
+
/* normalize for field */
|
46
|
+
score *= sim_decode_norm(self->similarity, ts->norms[self->doc]);
|
47
|
+
return score;
|
48
|
+
}
|
49
|
+
|
50
|
+
static bool tsc_next(Scorer *self)
|
51
|
+
{
|
52
|
+
TermScorer *ts = TSc(self);
|
53
|
+
|
54
|
+
ts->pointer++;
|
55
|
+
if (ts->pointer >= ts->pointer_max) {
|
56
|
+
/* refill buffer */
|
57
|
+
ts->pointer_max = ts->tde->read(ts->tde, ts->docs, ts->freqs,
|
58
|
+
TDE_READ_SIZE);
|
59
|
+
if (ts->pointer_max != 0) {
|
60
|
+
ts->pointer = 0;
|
61
|
+
}
|
62
|
+
else {
|
63
|
+
return false;
|
64
|
+
}
|
65
|
+
}
|
66
|
+
self->doc = ts->docs[ts->pointer];
|
67
|
+
return true;
|
68
|
+
}
|
69
|
+
|
70
|
+
static bool tsc_skip_to(Scorer *self, int doc_num)
|
71
|
+
{
|
72
|
+
TermScorer *ts = TSc(self);
|
73
|
+
TermDocEnum *tde = ts->tde;
|
74
|
+
|
75
|
+
/* first scan in cache */
|
76
|
+
while (++(ts->pointer) < ts->pointer_max) {
|
77
|
+
if (ts->docs[ts->pointer] >= doc_num) {
|
78
|
+
self->doc = ts->docs[ts->pointer];
|
79
|
+
return true;
|
80
|
+
}
|
81
|
+
}
|
82
|
+
|
83
|
+
/* not found in cache, seek underlying stream */
|
84
|
+
if (tde->skip_to(tde, doc_num)) {
|
85
|
+
ts->pointer_max = 1;
|
86
|
+
ts->pointer = 0;
|
87
|
+
ts->docs[0] = self->doc = tde->doc_num(tde);
|
88
|
+
ts->freqs[0] = tde->freq(tde);
|
89
|
+
return true;
|
90
|
+
}
|
91
|
+
else {
|
92
|
+
return false;
|
93
|
+
}
|
94
|
+
}
|
95
|
+
|
96
|
+
static Explanation *tsc_explain(Scorer *self, int doc_num)
|
97
|
+
{
|
98
|
+
TermScorer *ts = TSc(self);
|
99
|
+
Query *query = ts->weight->get_query(ts->weight);
|
100
|
+
int tf = 0;
|
101
|
+
|
102
|
+
tsc_skip_to(self, doc_num);
|
103
|
+
if (self->doc == doc_num) {
|
104
|
+
tf = ts->freqs[ts->pointer];
|
105
|
+
}
|
106
|
+
return expl_new(sim_tf(self->similarity, (float)tf),
|
107
|
+
"tf(term_freq(%s:%s)=%d)",
|
108
|
+
S(TQ(query)->field), TQ(query)->term, tf);
|
109
|
+
}
|
110
|
+
|
111
|
+
static void tsc_destroy(Scorer *self)
|
112
|
+
{
|
113
|
+
TSc(self)->tde->close(TSc(self)->tde);
|
114
|
+
scorer_destroy_i(self);
|
115
|
+
}
|
116
|
+
|
117
|
+
static Scorer *tsc_new(Weight *weight, TermDocEnum *tde, uchar *norms)
|
118
|
+
{
|
119
|
+
int i;
|
120
|
+
Scorer *self = scorer_new(TermScorer, weight->similarity);
|
121
|
+
TSc(self)->weight = weight;
|
122
|
+
TSc(self)->tde = tde;
|
123
|
+
TSc(self)->norms = norms;
|
124
|
+
TSc(self)->weight_value = weight->value;
|
125
|
+
|
126
|
+
for (i = 0; i < SCORE_CACHE_SIZE; i++) {
|
127
|
+
TSc(self)->score_cache[i]
|
128
|
+
= sim_tf(self->similarity, (float)i) * TSc(self)->weight_value;
|
129
|
+
}
|
130
|
+
|
131
|
+
self->score = &tsc_score;
|
132
|
+
self->next = &tsc_next;
|
133
|
+
self->skip_to = &tsc_skip_to;
|
134
|
+
self->explain = &tsc_explain;
|
135
|
+
self->destroy = &tsc_destroy;
|
136
|
+
return self;
|
137
|
+
}
|
138
|
+
|
139
|
+
/***************************************************************************
|
140
|
+
*
|
141
|
+
* TermWeight
|
142
|
+
*
|
143
|
+
***************************************************************************/
|
144
|
+
|
145
|
+
static Scorer *tw_scorer(Weight *self, IndexReader *ir)
|
146
|
+
{
|
147
|
+
TermQuery *tq = TQ(self->query);
|
148
|
+
TermDocEnum *tde = ir_term_docs_for(ir, tq->field, tq->term);
|
149
|
+
/* ir_term_docs_for should always return a TermDocEnum */
|
150
|
+
assert(NULL != tde);
|
151
|
+
|
152
|
+
return tsc_new(self, tde, ir_get_norms(ir, tq->field));
|
153
|
+
}
|
154
|
+
|
155
|
+
static Explanation *tw_explain(Weight *self, IndexReader *ir, int doc_num)
|
156
|
+
{
|
157
|
+
Explanation *qnorm_expl;
|
158
|
+
Explanation *field_expl;
|
159
|
+
Scorer *scorer;
|
160
|
+
Explanation *tf_expl;
|
161
|
+
uchar *field_norms;
|
162
|
+
float field_norm;
|
163
|
+
Explanation *field_norm_expl;
|
164
|
+
|
165
|
+
char *query_str = self->query->to_s(self->query, NULL);
|
166
|
+
TermQuery *tq = TQ(self->query);
|
167
|
+
char *term = tq->term;
|
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, tq->field, term));
|
176
|
+
Explanation *idf_expl2 = expl_new(self->idf, "idf(doc_freq=%d)",
|
177
|
+
ir_doc_freq(ir, tq->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
|
+
S(tq->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, tq->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
|
+
S(tq->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
|
+
q_destroy_i(self);
|
263
|
+
}
|
264
|
+
|
265
|
+
static char *tq_to_s(Query *self, Symbol default_field)
|
266
|
+
{
|
267
|
+
const char *field = S(TQ(self)->field);
|
268
|
+
size_t flen = strlen(field);
|
269
|
+
size_t tlen = strlen(TQ(self)->term);
|
270
|
+
char *buffer = ALLOC_N(char, 34 + flen + tlen);
|
271
|
+
char *b = buffer;
|
272
|
+
if (default_field != TQ(self)->field) {
|
273
|
+
memcpy(b, 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) ^ sym_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
|
+
&& (TQ(self)->field == TQ(o)->field);
|
301
|
+
}
|
302
|
+
|
303
|
+
static MatchVector *tq_get_matchv_i(Query *self, MatchVector *mv,
|
304
|
+
TermVector *tv)
|
305
|
+
{
|
306
|
+
if (tv->field == TQ(self)->field) {
|
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(Symbol field, const char *term)
|
320
|
+
{
|
321
|
+
Query *self = q_new(TermQuery);
|
322
|
+
|
323
|
+
TQ(self)->field = 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,167 @@
|
|
1
|
+
#include <string.h>
|
2
|
+
#include "search.h"
|
3
|
+
#include "symbol.h"
|
4
|
+
#include "internal.h"
|
5
|
+
|
6
|
+
/****************************************************************************
|
7
|
+
*
|
8
|
+
* WildCardQuery
|
9
|
+
*
|
10
|
+
****************************************************************************/
|
11
|
+
|
12
|
+
#define WCQ(query) ((WildCardQuery *)(query))
|
13
|
+
|
14
|
+
static char *wcq_to_s(Query *self, Symbol default_field)
|
15
|
+
{
|
16
|
+
char *buffer, *bptr;
|
17
|
+
const char *field_str = S(WCQ(self)->field);
|
18
|
+
const char *pattern = WCQ(self)->pattern;
|
19
|
+
bptr = buffer = ALLOC_N(char, strlen(pattern) + strlen(field_str) + 35);
|
20
|
+
|
21
|
+
if (WCQ(self)->field != default_field) {
|
22
|
+
bptr += sprintf(bptr, "%s:", field_str);
|
23
|
+
}
|
24
|
+
bptr += sprintf(bptr, "%s", pattern);
|
25
|
+
|
26
|
+
if (self->boost != 1.0) {
|
27
|
+
*bptr = '^';
|
28
|
+
dbl_to_s(++bptr, self->boost);
|
29
|
+
}
|
30
|
+
|
31
|
+
return buffer;
|
32
|
+
}
|
33
|
+
|
34
|
+
bool wc_match(const char *pattern, const char *text)
|
35
|
+
{
|
36
|
+
const char *p = pattern, *t = text, *xt;
|
37
|
+
|
38
|
+
/* include '\0' as we need to match empty string */
|
39
|
+
const char *text_last = t + strlen(t);
|
40
|
+
|
41
|
+
for (;; p++, t++) {
|
42
|
+
|
43
|
+
/* end of text so make sure end of pattern doesn't matter */
|
44
|
+
if (*t == '\0') {
|
45
|
+
while (*p) {
|
46
|
+
if (*p != WILD_STRING) {
|
47
|
+
return false;
|
48
|
+
}
|
49
|
+
p++;
|
50
|
+
}
|
51
|
+
return true;
|
52
|
+
}
|
53
|
+
|
54
|
+
/* If we've gone past the end of the pattern, return false. */
|
55
|
+
if (*p == '\0') {
|
56
|
+
return false;
|
57
|
+
}
|
58
|
+
|
59
|
+
/* Match a single character, so continue. */
|
60
|
+
if (*p == WILD_CHAR) {
|
61
|
+
continue;
|
62
|
+
}
|
63
|
+
|
64
|
+
if (*p == WILD_STRING) {
|
65
|
+
/* Look at the character beyond the '*'. */
|
66
|
+
p++;
|
67
|
+
/* Examine the string, starting at the last character. */
|
68
|
+
for (xt = text_last; xt >= t; xt--) {
|
69
|
+
if (wc_match(p, xt)) return true;
|
70
|
+
}
|
71
|
+
return false;
|
72
|
+
}
|
73
|
+
if (*p != *t) {
|
74
|
+
return false;
|
75
|
+
}
|
76
|
+
}
|
77
|
+
|
78
|
+
return false;
|
79
|
+
}
|
80
|
+
|
81
|
+
static Query *wcq_rewrite(Query *self, IndexReader *ir)
|
82
|
+
{
|
83
|
+
Query *q;
|
84
|
+
const char *pattern = WCQ(self)->pattern;
|
85
|
+
const char *first_star = strchr(pattern, WILD_STRING);
|
86
|
+
const char *first_ques = strchr(pattern, WILD_CHAR);
|
87
|
+
|
88
|
+
if (NULL == first_star && NULL == first_ques) {
|
89
|
+
q = tq_new(WCQ(self)->field, pattern);
|
90
|
+
q->boost = self->boost;
|
91
|
+
}
|
92
|
+
else {
|
93
|
+
const int field_num = fis_get_field_num(ir->fis, WCQ(self)->field);
|
94
|
+
q = multi_tq_new_conf(WCQ(self)->field, MTQMaxTerms(self), 0.0);
|
95
|
+
|
96
|
+
if (field_num >= 0) {
|
97
|
+
TermEnum *te;
|
98
|
+
char prefix[MAX_WORD_SIZE] = "";
|
99
|
+
int prefix_len;
|
100
|
+
|
101
|
+
pattern = (first_ques && (!first_star || first_star > first_ques))
|
102
|
+
? first_ques : first_star;
|
103
|
+
|
104
|
+
prefix_len = (int)(pattern - WCQ(self)->pattern);
|
105
|
+
|
106
|
+
if (prefix_len > 0) {
|
107
|
+
memcpy(prefix, WCQ(self)->pattern, prefix_len);
|
108
|
+
prefix[prefix_len] = '\0';
|
109
|
+
}
|
110
|
+
|
111
|
+
te = ir->terms_from(ir, field_num, prefix);
|
112
|
+
|
113
|
+
if (te != NULL) {
|
114
|
+
const char *term = te->curr_term;
|
115
|
+
const char *pat_term = term + prefix_len;
|
116
|
+
do {
|
117
|
+
if (prefix[0] && strncmp(term, prefix, prefix_len) != 0) {
|
118
|
+
break;
|
119
|
+
}
|
120
|
+
|
121
|
+
if (wc_match(pattern, pat_term)) {
|
122
|
+
multi_tq_add_term(q, term);
|
123
|
+
}
|
124
|
+
} while (te->next(te) != NULL);
|
125
|
+
te->close(te);
|
126
|
+
}
|
127
|
+
}
|
128
|
+
}
|
129
|
+
|
130
|
+
return q;
|
131
|
+
}
|
132
|
+
|
133
|
+
static void wcq_destroy(Query *self)
|
134
|
+
{
|
135
|
+
free(WCQ(self)->pattern);
|
136
|
+
q_destroy_i(self);
|
137
|
+
}
|
138
|
+
|
139
|
+
static unsigned long wcq_hash(Query *self)
|
140
|
+
{
|
141
|
+
return sym_hash(WCQ(self)->field) ^ str_hash(WCQ(self)->pattern);
|
142
|
+
}
|
143
|
+
|
144
|
+
static int wcq_eq(Query *self, Query *o)
|
145
|
+
{
|
146
|
+
return (strcmp(WCQ(self)->pattern, WCQ(o)->pattern) == 0)
|
147
|
+
&& (WCQ(self)->field == WCQ(o)->field);
|
148
|
+
}
|
149
|
+
|
150
|
+
Query *wcq_new(Symbol field, const char *pattern)
|
151
|
+
{
|
152
|
+
Query *self = q_new(WildCardQuery);
|
153
|
+
|
154
|
+
WCQ(self)->field = field;
|
155
|
+
WCQ(self)->pattern = estrdup(pattern);
|
156
|
+
MTQMaxTerms(self) = WILD_CARD_QUERY_MAX_TERMS;
|
157
|
+
|
158
|
+
self->type = WILD_CARD_QUERY;
|
159
|
+
self->rewrite = &wcq_rewrite;
|
160
|
+
self->to_s = &wcq_to_s;
|
161
|
+
self->hash = &wcq_hash;
|
162
|
+
self->eq = &wcq_eq;
|
163
|
+
self->destroy_i = &wcq_destroy;
|
164
|
+
self->create_weight_i = &q_create_weight_unsup;
|
165
|
+
|
166
|
+
return self;
|
167
|
+
}
|