1
/* Copyright (C) 2001-2005 MySQL AB
3
This program is free software; you can redistribute it and/or modify
4
it under the terms of the GNU General Public License as published by
5
the Free Software Foundation; version 2 of the License.
7
This program is distributed in the hope that it will be useful,
8
but WITHOUT ANY WARRANTY; without even the implied warranty of
9
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
GNU General Public License for more details.
12
You should have received a copy of the GNU General Public License
13
along with this program; if not, write to the Free Software
14
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
16
/* Written by Sergei A. Golubchik, who has a shared copyright to this code */
21
/* search with natural language queries */
23
typedef struct ft_doc_rec
31
struct _ft_vft *please;
38
typedef struct st_all_in_one
42
CHARSET_INFO *charset;
47
typedef struct st_ft_superdoc
54
static int FT_SUPERDOC_cmp(void* cmp_arg __attribute__((unused)),
55
FT_SUPERDOC *p1, FT_SUPERDOC *p2)
57
if (p1->doc.dpos < p2->doc.dpos)
59
if (p1->doc.dpos == p2->doc.dpos)
64
static int walk_and_match(FT_WORD *word, uint32 count, ALL_IN_ONE *aio)
68
FT_SUPERDOC sdoc, *sptr;
71
MI_INFO *info=aio->info;
72
uchar *keybuff=aio->keybuff;
73
MI_KEYDEF *keyinfo=info->s->keyinfo+aio->keynr;
74
my_off_t key_root=info->s->state.key_root[aio->keynr];
75
uint extra=HA_FT_WLEN+info->s->base.rec_reflength;
76
#if HA_FT_WTYPE == HA_KEYTYPE_FLOAT
82
DBUG_ENTER("walk_and_match");
84
word->weight=LWS_FOR_QUERY;
86
keylen=_ft_make_key(info,aio->keynr,keybuff,word,0);
90
/* Skip rows inserted by current inserted */
91
for (r=_mi_search(info, keyinfo, keybuff, keylen, SEARCH_FIND, key_root) ;
93
(subkeys=ft_sintXkorr(info->lastkey+info->lastkey_length-extra)) > 0 &&
94
info->lastpos >= info->state->data_file_length ;
95
r= _mi_search_next(info, keyinfo, info->lastkey,
96
info->lastkey_length, SEARCH_BIGGER, key_root))
99
info->update|= HA_STATE_AKTIV; /* for _mi_test_if_changed() */
101
/* The following should be safe, even if we compare doubles */
102
while (!r && gweight)
106
ha_compare_text(aio->charset,info->lastkey+1,
107
info->lastkey_length-extra-1, keybuff+1,keylen-1,0,0))
113
DBUG_RETURN(1); /* index is corrupted */
115
TODO here: unsafe optimization, should this word
116
be skipped (based on subkeys) ?
119
keyinfo=& info->s->ft2_keyinfo;
120
key_root=info->lastpos;
122
r=_mi_search_first(info, keyinfo, key_root);
125
#if HA_FT_WTYPE == HA_KEYTYPE_FLOAT
126
tmp_weight=*(float*)&subkeys;
130
/* The following should be safe, even if we compare doubles */
132
DBUG_RETURN(doc_cnt); /* stopword, doc_cnt should be 0 */
134
sdoc.doc.dpos=info->lastpos;
136
/* saving document matched into dtree */
137
if (!(selem=tree_insert(&aio->dtree, &sdoc, 0, aio->dtree.custom_arg)))
140
sptr=(FT_SUPERDOC *)ELEMENT_KEY((&aio->dtree), selem);
142
if (selem->count==1) /* document's first match */
145
sptr->doc.weight+=sptr->tmp_weight*sptr->word_ptr->weight;
148
sptr->tmp_weight=tmp_weight;
152
gweight=word->weight*GWS_IN_USE;
153
if (gweight < 0 || doc_cnt > 2000000)
156
if (_mi_test_if_changed(info) == 0)
157
r=_mi_search_next(info, keyinfo, info->lastkey, info->lastkey_length,
158
SEARCH_BIGGER, key_root);
160
r=_mi_search(info, keyinfo, info->lastkey, info->lastkey_length,
161
SEARCH_BIGGER, key_root);
163
while ((subkeys=ft_sintXkorr(info->lastkey+info->lastkey_length-extra)) > 0 &&
164
!r && info->lastpos >= info->state->data_file_length)
165
r= _mi_search_next(info, keyinfo, info->lastkey, info->lastkey_length,
166
SEARCH_BIGGER, key_root);
169
word->weight=gweight;
175
static int walk_and_copy(FT_SUPERDOC *from,
176
uint32 count __attribute__((unused)), FT_DOC **to)
178
DBUG_ENTER("walk_and_copy");
179
from->doc.weight+=from->tmp_weight*from->word_ptr->weight;
180
(*to)->dpos=from->doc.dpos;
181
(*to)->weight=from->doc.weight;
186
static int walk_and_push(FT_SUPERDOC *from,
187
uint32 count __attribute__((unused)), QUEUE *best)
189
DBUG_ENTER("walk_and_copy");
190
from->doc.weight+=from->tmp_weight*from->word_ptr->weight;
191
set_if_smaller(best->elements, ft_query_expansion_limit-1);
192
queue_insert(best, (uchar *)& from->doc);
197
static int FT_DOC_cmp(void *unused __attribute__((unused)),
198
FT_DOC *a, FT_DOC *b)
200
return sgn(b->weight - a->weight);
204
FT_INFO *ft_init_nlq_search(MI_INFO *info, uint keynr, uchar *query,
205
uint query_len, uint flags, uchar *record)
211
my_off_t saved_lastpos=info->lastpos;
212
struct st_mysql_ftparser *parser;
213
MYSQL_FTPARSER_PARAM *ftparser_param;
214
DBUG_ENTER("ft_init_nlq_search");
217
if ((int) (keynr = _mi_check_index(info,keynr)) < 0)
219
if (_mi_readinfo(info,F_RDLCK,1))
221
/* black magic OFF */
225
aio.charset=info->s->keyinfo[keynr].seg->charset;
226
aio.keybuff=info->lastkey+info->s->base.max_key_length;
227
parser= info->s->keyinfo[keynr].parser;
228
if (! (ftparser_param= ftparser_call_initializer(info, keynr, 0)))
231
bzero(&wtree,sizeof(wtree));
233
init_tree(&aio.dtree,0,0,sizeof(FT_SUPERDOC),(qsort_cmp2)&FT_SUPERDOC_cmp,0,
236
ft_parse_init(&wtree, aio.charset);
237
ftparser_param->flags= 0;
238
if (ft_parse(&wtree, query, query_len, parser, ftparser_param,
242
if (tree_walk(&wtree, (tree_walk_action)&walk_and_match, &aio,
246
if (flags & FT_EXPAND && ft_query_expansion_limit)
249
init_queue(&best,ft_query_expansion_limit,0,0, (queue_compare) &FT_DOC_cmp,
251
tree_walk(&aio.dtree, (tree_walk_action) &walk_and_push,
252
&best, left_root_right);
253
while (best.elements)
255
my_off_t docid=((FT_DOC *)queue_remove(& best, 0))->dpos;
256
if (!(*info->read_record)(info,docid,record))
258
info->update|= HA_STATE_AKTIV;
259
ftparser_param->flags= MYSQL_FTFLAGS_NEED_COPY;
260
if (unlikely(_mi_ft_parse(&wtree, info, keynr, record, ftparser_param,
269
reset_tree(&aio.dtree);
270
if (tree_walk(&wtree, (tree_walk_action)&walk_and_match, &aio,
277
If ndocs == 0, this will not allocate RAM for FT_INFO.doc[],
278
so if ndocs == 0, FT_INFO.doc[] must not be accessed.
280
dlist=(FT_INFO *)my_malloc(sizeof(FT_INFO)+
282
(int)(aio.dtree.elements_in_tree-1),
287
dlist->please= (struct _ft_vft *) & _ft_vft_nlq;
288
dlist->ndocs=aio.dtree.elements_in_tree;
290
dlist->info=aio.info;
293
tree_walk(&aio.dtree, (tree_walk_action) &walk_and_copy,
294
&dptr, left_root_right);
296
if (flags & FT_SORTED)
297
my_qsort2(dlist->doc, dlist->ndocs, sizeof(FT_DOC), (qsort2_cmp)&FT_DOC_cmp,
301
delete_tree(&aio.dtree);
303
info->lastpos=saved_lastpos;
308
int ft_nlq_read_next(FT_INFO *handler, char *record)
310
MI_INFO *info= (MI_INFO *) handler->info;
312
if (++handler->curdoc >= handler->ndocs)
315
return HA_ERR_END_OF_FILE;
318
info->update&= (HA_STATE_CHANGED | HA_STATE_ROW_CHANGED);
320
info->lastpos=handler->doc[handler->curdoc].dpos;
321
if (!(*info->read_record)(info,info->lastpos,(uchar*) record))
323
info->update|= HA_STATE_AKTIV; /* Record is read */
330
float ft_nlq_find_relevance(FT_INFO *handler,
331
uchar *record __attribute__((unused)),
332
uint length __attribute__((unused)))
335
FT_DOC *docs=handler->doc;
336
my_off_t docid=handler->info->lastpos;
338
if (docid == HA_POS_ERROR)
341
/* Assuming docs[] is sorted by dpos... */
343
for (a=0, b=handler->ndocs, c=(a+b)/2; b-a>1; c=(a+b)/2)
345
if (docs[c].dpos > docid)
350
/* bounds check to avoid accessing unallocated handler->doc */
351
if (a < handler->ndocs && docs[a].dpos == docid)
352
return (float) docs[a].weight;
358
void ft_nlq_close_search(FT_INFO *handler)
360
my_free((uchar*)handler,MYF(0));
364
float ft_nlq_get_relevance(FT_INFO *handler)
366
return (float) handler->doc[handler->curdoc].weight;
370
void ft_nlq_reinit_search(FT_INFO *handler)