ft_nlq_search.c 8.84 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */

/* Written by Sergei A. Golubchik, who has a shared copyright to this code */

unknown's avatar
unknown committed
19
#define FT_CORE
20 21 22 23
#include "ftdefs.h"

/* search with natural language queries */

24 25
typedef struct ft_doc_rec
{
unknown's avatar
unknown committed
26 27 28 29
  my_off_t  dpos;
  double    weight;
} FT_DOC;

30 31
struct st_ft_info
{
unknown's avatar
unknown committed
32 33 34 35 36 37 38
  struct _ft_vft *please;
  MI_INFO  *info;
  int       ndocs;
  int       curdoc;
  FT_DOC    doc[1];
};

39 40
typedef struct st_all_in_one
{
41 42
  MI_INFO    *info;
  uint	      keynr;
43
  CHARSET_INFO *charset;
44 45 46 47
  uchar      *keybuff;
  TREE	      dtree;
} ALL_IN_ONE;

48 49
typedef struct st_ft_superdoc
{
50 51 52 53 54
    FT_DOC   doc;
    FT_WORD *word_ptr;
    double   tmp_weight;
} FT_SUPERDOC;

55 56
static int FT_SUPERDOC_cmp(void* cmp_arg __attribute__((unused)),
			   FT_SUPERDOC *p1, FT_SUPERDOC *p2)
57 58 59 60 61 62 63 64 65 66
{
  if (p1->doc.dpos < p2->doc.dpos)
    return -1;
  if (p1->doc.dpos == p2->doc.dpos)
    return 0;
  return 1;
}

static int walk_and_match(FT_WORD *word, uint32 count, ALL_IN_ONE *aio)
{
67 68
  int	       subkeys, r;
  uint	       keylen, doc_cnt;
69 70
  FT_SUPERDOC  sdoc, *sptr;
  TREE_ELEMENT *selem;
unknown's avatar
unknown committed
71
  double       gweight=1;
72 73 74 75
  MI_INFO      *info=aio->info;
  uchar        *keybuff=aio->keybuff;
  MI_KEYDEF    *keyinfo=info->s->keyinfo+aio->keynr;
  my_off_t     key_root=info->s->state.key_root[aio->keynr];
76
  uint         extra=HA_FT_WLEN+info->s->base.rec_reflength;
77 78 79 80 81 82
#if HA_FT_WTYPE == HA_KEYTYPE_FLOAT
  float tmp_weight;
#else
#error
#endif

unknown's avatar
unknown committed
83 84
  DBUG_ENTER("walk_and_match");

85 86
  word->weight=LWS_FOR_QUERY;

87
  keylen=_ft_make_key(info,aio->keynr,(char*) keybuff,word,0);
88 89 90
  keylen-=HA_FT_WLEN;
  doc_cnt=0;

unknown's avatar
unknown committed
91
  /* Skip rows inserted by current inserted */
unknown's avatar
unknown committed
92
  for (r=_mi_search(info, keyinfo, keybuff, keylen, SEARCH_FIND, key_root) ;
93
       (subkeys=ft_sintXkorr(info->lastkey+info->lastkey_length-extra)) > 0 &&
unknown's avatar
unknown committed
94
       !r && info->lastpos >= info->state->data_file_length ;
unknown's avatar
unknown committed
95 96 97 98
       r= _mi_search_next(info, keyinfo, info->lastkey,
                          info->lastkey_length, SEARCH_BIGGER, key_root))
    ;

99
  info->update|= HA_STATE_AKTIV;              /* for _mi_test_if_changed() */
100

101
  while (!r && gweight)
102
  {
103 104

    if (keylen &&
105
        mi_compare_text(aio->charset,info->lastkey+1,
106
                        info->lastkey_length-extra-1, keybuff+1,keylen-1,0,0))
unknown's avatar
unknown committed
107
     break;
108

109 110 111 112 113 114 115 116 117 118 119 120 121
    if (subkeys<0)
    {
      if (doc_cnt)
        DBUG_RETURN(1); /* index is corrupted */
      /*
        TODO here: unsafe optimization, should this word
        be skipped (based on subkeys) ?
      */
      keybuff+=keylen;
      keyinfo=& info->s->ft2_keyinfo;
      key_root=info->lastpos;
      keylen=0;
      r=_mi_search_first(info, keyinfo, key_root);
122
      goto do_skip;
123
    }
124
#if HA_FT_WTYPE == HA_KEYTYPE_FLOAT
125
    tmp_weight=*(float*)&subkeys;
126 127 128
#else
#error
#endif
129 130
    if (tmp_weight==0)
      DBUG_RETURN(doc_cnt); /* stopword, doc_cnt should be 0 */
131

132
    sdoc.doc.dpos=info->lastpos;
133 134

    /* saving document matched into dtree */
unknown's avatar
unknown committed
135
    if (!(selem=tree_insert(&aio->dtree, &sdoc, 0, aio->dtree.custom_arg)))
unknown's avatar
unknown committed
136
      DBUG_RETURN(1);
137 138 139

    sptr=(FT_SUPERDOC *)ELEMENT_KEY((&aio->dtree), selem);

140
    if (selem->count==1) /* document's first match */
141 142 143 144 145 146 147 148
      sptr->doc.weight=0;
    else
      sptr->doc.weight+=sptr->tmp_weight*sptr->word_ptr->weight;

    sptr->word_ptr=word;
    sptr->tmp_weight=tmp_weight;

    doc_cnt++;
149

150 151 152
    gweight=word->weight*GWS_IN_USE;
    if (gweight < 0 || doc_cnt > 2000000)
      gweight=0;
153

154 155 156
    if (_mi_test_if_changed(info) == 0)
	r=_mi_search_next(info, keyinfo, info->lastkey, info->lastkey_length,
                          SEARCH_BIGGER, key_root);
157
    else
158 159
	r=_mi_search(info, keyinfo, info->lastkey, info->lastkey_length,
                     SEARCH_BIGGER, key_root);
160 161 162
do_skip:
    while ((subkeys=ft_sintXkorr(info->lastkey+info->lastkey_length-extra)) > 0 &&
           !r && info->lastpos >= info->state->data_file_length)
unknown's avatar
unknown committed
163 164
      r= _mi_search_next(info, keyinfo, info->lastkey, info->lastkey_length,
                         SEARCH_BIGGER, key_root);
unknown's avatar
unknown committed
165

166
  }
167
  word->weight=gweight;
unknown's avatar
unknown committed
168 169

  DBUG_RETURN(0);
170 171
}

172

173 174 175
static int walk_and_copy(FT_SUPERDOC *from,
			 uint32 count __attribute__((unused)), FT_DOC **to)
{
unknown's avatar
unknown committed
176
  DBUG_ENTER("walk_and_copy");
177 178 179 180
  from->doc.weight+=from->tmp_weight*from->word_ptr->weight;
  (*to)->dpos=from->doc.dpos;
  (*to)->weight=from->doc.weight;
  (*to)++;
unknown's avatar
unknown committed
181
  DBUG_RETURN(0);
182 183
}

184 185 186 187 188
static int walk_and_push(FT_SUPERDOC *from,
			 uint32 count __attribute__((unused)), QUEUE *best)
{
  DBUG_ENTER("walk_and_copy");
  from->doc.weight+=from->tmp_weight*from->word_ptr->weight;
unknown's avatar
unknown committed
189
  set_if_smaller(best->elements, ft_query_expansion_limit-1);
190 191 192
  queue_insert(best, (byte *)& from->doc);
  DBUG_RETURN(0);
}
193

194 195 196

static int FT_DOC_cmp(void *unused __attribute__((unused)),
                      FT_DOC *a, FT_DOC *b)
197
{
198
  return sgn(b->weight - a->weight);
199 200
}

201

unknown's avatar
unknown committed
202
FT_INFO *ft_init_nlq_search(MI_INFO *info, uint keynr, byte *query,
203
			    uint query_len, uint flags, byte *record)
204
{
205
  TREE	      wtree;
206
  ALL_IN_ONE  aio;
207
  FT_DOC     *dptr;
unknown's avatar
unknown committed
208 209
  FT_INFO    *dlist=NULL;
  my_off_t    saved_lastpos=info->lastpos;
unknown's avatar
unknown committed
210
  DBUG_ENTER("ft_init_nlq_search");
211

212
/* black magic ON */
unknown's avatar
unknown committed
213
  if ((int) (keynr = _mi_check_index(info,keynr)) < 0)
unknown's avatar
unknown committed
214
    DBUG_RETURN(NULL);
unknown's avatar
unknown committed
215
  if (_mi_readinfo(info,F_RDLCK,1))
unknown's avatar
unknown committed
216
    DBUG_RETURN(NULL);
217 218
/* black magic OFF */

unknown's avatar
unknown committed
219
  aio.info=info;
220
  aio.keynr=keynr;
221
  aio.charset=info->s->keyinfo[keynr].seg->charset;
222
  aio.keybuff=info->lastkey+info->s->base.max_key_length;
223

224
  bzero(&wtree,sizeof(wtree));
225

unknown's avatar
unknown committed
226 227
  init_tree(&aio.dtree,0,0,sizeof(FT_SUPERDOC),(qsort_cmp2)&FT_SUPERDOC_cmp,0,
            NULL, NULL);
228

229 230
  ft_parse_init(&wtree, aio.charset);
  if (ft_parse(&wtree,query,query_len,0))
231
    goto err;
232

233
  if (tree_walk(&wtree, (tree_walk_action)&walk_and_match, &aio,
234
		left_root_right))
235 236 237 238 239
    goto err;

  if (flags & FT_EXPAND && ft_query_expansion_limit)
  {
    QUEUE best;
240 241
    init_queue(&best,ft_query_expansion_limit,0,0, (queue_compare) &FT_DOC_cmp,
	       0);
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
    tree_walk(&aio.dtree, (tree_walk_action) &walk_and_push,
              &best, left_root_right);
    while (best.elements)
    {
      my_off_t docid=((FT_DOC *)queue_remove(& best, 0))->dpos;
      if (!(*info->read_record)(info,docid,record))
      {
        info->update|= HA_STATE_AKTIV;
        _mi_ft_parse(&wtree, info, keynr, record,1);
      }
    }
    delete_queue(&best);
    reset_tree(&aio.dtree);
    if (tree_walk(&wtree, (tree_walk_action)&walk_and_match, &aio,
                  left_root_right))
      goto err;

  }
260

unknown's avatar
unknown committed
261
  dlist=(FT_INFO *)my_malloc(sizeof(FT_INFO)+
262 263
			     sizeof(FT_DOC)*(aio.dtree.elements_in_tree-1),
			     MYF(0));
264 265
  if (!dlist)
    goto err;
266

267
  dlist->please= (struct _ft_vft *) & _ft_vft_nlq;
268 269 270 271 272
  dlist->ndocs=aio.dtree.elements_in_tree;
  dlist->curdoc=-1;
  dlist->info=aio.info;
  dptr=dlist->doc;

273 274
  tree_walk(&aio.dtree, (tree_walk_action) &walk_and_copy,
	    &dptr, left_root_right);
275

276
  if (flags & FT_SORTED)
277
    qsort2(dlist->doc, dlist->ndocs, sizeof(FT_DOC), (qsort2_cmp)&FT_DOC_cmp, 0);
278 279

err:
280 281
  delete_tree(&aio.dtree);
  delete_tree(&wtree);
unknown's avatar
unknown committed
282
  info->lastpos=saved_lastpos;
unknown's avatar
unknown committed
283
  DBUG_RETURN(dlist);
284 285
}

286

unknown's avatar
unknown committed
287
int ft_nlq_read_next(FT_INFO *handler, char *record)
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
{
  MI_INFO *info= (MI_INFO *) handler->info;

  if (++handler->curdoc >= handler->ndocs)
  {
    --handler->curdoc;
    return HA_ERR_END_OF_FILE;
  }

  info->update&= (HA_STATE_CHANGED | HA_STATE_ROW_CHANGED);

  info->lastpos=handler->doc[handler->curdoc].dpos;
  if (!(*info->read_record)(info,info->lastpos,record))
  {
    info->update|= HA_STATE_AKTIV;		/* Record is read */
    return 0;
  }
  return my_errno;
}

308

309
float ft_nlq_find_relevance(FT_INFO *handler,
unknown's avatar
unknown committed
310 311
			    byte *record __attribute__((unused)),
			    uint length __attribute__((unused)))
unknown's avatar
unknown committed
312 313 314
{
  int a,b,c;
  FT_DOC  *docs=handler->doc;
315 316 317 318
  my_off_t docid=handler->info->lastpos;

  if (docid == HA_POS_ERROR)
    return -5.0;
unknown's avatar
unknown committed
319

unknown's avatar
unknown committed
320
  /* Assuming docs[] is sorted by dpos... */
unknown's avatar
unknown committed
321 322 323 324 325 326 327 328 329

  for (a=0, b=handler->ndocs, c=(a+b)/2; b-a>1; c=(a+b)/2)
  {
    if (docs[c].dpos > docid)
      b=c;
    else
      a=c;
  }
  if (docs[a].dpos == docid)
unknown's avatar
unknown committed
330
    return (float) docs[a].weight;
unknown's avatar
unknown committed
331 332 333 334
  else
    return 0.0;
}

335

unknown's avatar
unknown committed
336 337 338 339 340
void ft_nlq_close_search(FT_INFO *handler)
{
  my_free((gptr)handler,MYF(0));
}

341

unknown's avatar
unknown committed
342 343
float ft_nlq_get_relevance(FT_INFO *handler)
{
unknown's avatar
unknown committed
344
  return (float) handler->doc[handler->curdoc].weight;
unknown's avatar
unknown committed
345 346
}

347

unknown's avatar
unknown committed
348 349 350 351 352
void ft_nlq_reinit_search(FT_INFO *handler)
{
  handler->curdoc=-1;
}