4
/****************************************************************************
8
****************************************************************************/
10
#define PfxQ(query) ((PrefixQuery *)(query))
12
static char *prq_to_s(Query *self, const char *current_field)
15
const char *prefix = PfxQ(self)->prefix;
16
const char *field = PfxQ(self)->field;
17
size_t plen = strlen(prefix);
18
size_t flen = strlen(field);
20
bptr = buffer = ALLOC_N(char, plen + flen + 35);
22
if (strcmp(field, current_field) != 0) {
23
sprintf(bptr, "%s:", field);
27
sprintf(bptr, "%s*", prefix);
29
if (self->boost != 1.0) {
31
dbl_to_s(++bptr, self->boost);
37
static Query *prq_rewrite(Query *self, IndexReader *ir)
39
const char *field = PfxQ(self)->field;
40
const int field_num = fis_get_field_num(ir->fis, field);
41
Query *volatile q = multi_tq_new_conf(field, MTQMaxTerms(self), 0.0);
42
q->boost = self->boost; /* set the boost */
45
const char *prefix = PfxQ(self)->prefix;
46
TermEnum *te = ir->terms_from(ir, field_num, prefix);
47
const char *term = te->curr_term;
48
size_t prefix_len = strlen(prefix);
52
if (strncmp(term, prefix, prefix_len) != 0) {
55
multi_tq_add_term(q, term); /* found a match */
56
} while (te->next(te));
65
static void prq_destroy(Query *self)
67
free(PfxQ(self)->field);
68
free(PfxQ(self)->prefix);
72
static unsigned long prq_hash(Query *self)
74
return str_hash(PfxQ(self)->field) ^ str_hash(PfxQ(self)->prefix);
77
static int prq_eq(Query *self, Query *o)
79
return (strcmp(PfxQ(self)->prefix, PfxQ(o)->prefix) == 0)
80
&& (strcmp(PfxQ(self)->field, PfxQ(o)->field) == 0);
83
Query *prefixq_new(const char *field, const char *prefix)
85
Query *self = q_new(PrefixQuery);
87
PfxQ(self)->field = estrdup(field);
88
PfxQ(self)->prefix = estrdup(prefix);
89
MTQMaxTerms(self) = PREFIX_QUERY_MAX_TERMS;
91
self->type = PREFIX_QUERY;
92
self->rewrite = &prq_rewrite;
93
self->to_s = &prq_to_s;
94
self->hash = &prq_hash;
96
self->destroy_i = &prq_destroy;
97
self->create_weight_i = &q_create_weight_unsup;