/
fts0que.cc
4592 lines (3709 loc) · 119 KB
/
fts0que.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*****************************************************************************
Copyright (c) 2007, 2018, Oracle and/or its affiliates. All Rights Reserved.
Copyright (c) 2017, 2018, MariaDB Corporation.
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; version 2 of the License.
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.,
51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA
*****************************************************************************/
/**************************************************//**
@file fts/fts0que.cc
Full Text Search functionality.
Created 2007/03/27 Sunny Bains
Completed 2011/7/10 Sunny and Jimmy Yang
*******************************************************/
#include "ha_prototypes.h"
#include "dict0dict.h"
#include "ut0rbt.h"
#include "row0sel.h"
#include "fts0fts.h"
#include "fts0priv.h"
#include "fts0ast.h"
#include "fts0pars.h"
#include "fts0types.h"
#include "fts0plugin.h"
#include "ut0new.h"
#include <iomanip>
#include <vector>
#define FTS_ELEM(t, n, i, j) (t[(i) * n + (j)])
#define RANK_DOWNGRADE (-1.0F)
#define RANK_UPGRADE (1.0F)
/* Maximum number of words supported in a phrase or proximity search. */
#define MAX_PROXIMITY_ITEM 128
/* Memory used by rbt itself for create and node add */
#define SIZEOF_RBT_CREATE sizeof(ib_rbt_t) + sizeof(ib_rbt_node_t) * 2
#define SIZEOF_RBT_NODE_ADD sizeof(ib_rbt_node_t)
/*Initial byte length for 'words' in fts_ranking_t */
#define RANKING_WORDS_INIT_LEN 4
// FIXME: Need to have a generic iterator that traverses the ilist.
typedef std::vector<fts_string_t, ut_allocator<fts_string_t> > word_vector_t;
struct fts_word_freq_t;
/** State of an FTS query. */
struct fts_query_t {
mem_heap_t* heap; /*!< Heap to use for allocations */
trx_t* trx; /*!< The query transaction */
dict_index_t* index; /*!< The FTS index to search */
/*!< FTS auxiliary common table def */
fts_table_t fts_common_table;
fts_table_t fts_index_table;/*!< FTS auxiliary index table def */
ulint total_size; /*!< total memory size used by query */
fts_doc_ids_t* deleted; /*!< Deleted doc ids that need to be
filtered from the output */
fts_ast_node_t* root; /*!< Abstract syntax tree */
fts_ast_node_t* cur_node; /*!< Current tree node */
ib_rbt_t* word_map; /*!< Matched word map for
searching by word*/
word_vector_t* word_vector; /*!< Matched word vector for
searching by index */
ib_rbt_t* doc_ids; /*!< The current set of matching
doc ids, elements are of
type fts_ranking_t */
ib_rbt_t* intersection; /*!< The doc ids that were found in
doc_ids, this tree will become
the new doc_ids, elements are of type
fts_ranking_t */
/*!< Prepared statement to read the
nodes from the FTS INDEX */
que_t* read_nodes_graph;
fts_ast_oper_t oper; /*!< Current boolean mode operator */
/*!< TRUE if we want to collect the
word positions within the document */
ibool collect_positions;
ulint flags; /*!< Specify the full text search type,
such as boolean search, phrase
search, proximity search etc. */
ulint distance; /*!< The proximity distance of a
phrase search. */
/*!< These doc ids are used as a
boundary condition when searching the
FTS index rows */
doc_id_t lower_doc_id; /*!< Lowest doc id in doc_ids */
doc_id_t upper_doc_id; /*!< Highest doc id in doc_ids */
bool boolean_mode; /*!< TRUE if boolean mode query */
ib_vector_t* matched; /*!< Array of matching documents
(fts_match_t) to search for a phrase */
ib_vector_t** match_array; /*!< Used for proximity search, contains
position info for each matched word
in the word list */
ib_uint64_t total_docs; /*!< The total number of documents */
ulint total_words; /*!< The total number of words */
dberr_t error; /*!< Error code if any, that is
encountered during query processing */
ib_rbt_t* word_freqs; /*!< RB tree of word frequencies per
document, its elements are of type
fts_word_freq_t */
ib_rbt_t* wildcard_words; /*!< words with wildcard */
bool multi_exist; /*!< multiple FTS_EXIST oper */
st_mysql_ftparser* parser; /*!< fts plugin parser */
};
/** For phrase matching, first we collect the documents and the positions
then we match. */
struct fts_match_t {
doc_id_t doc_id; /*!< Document id */
ulint start; /*!< Start the phrase match from
this offset within the positions
vector. */
ib_vector_t* positions; /*!< Offsets of a word in a
document */
};
/** For matching tokens in a phrase search. We use this data structure in
the callback that determines whether a document should be accepted or
rejected for a phrase search. */
struct fts_select_t {
doc_id_t doc_id; /*!< The document id to match */
ulint min_pos; /*!< For found to be TRUE at least
one position must be greater than
min_pos. */
ibool found; /*!< TRUE if found */
fts_word_freq_t*
word_freq; /*!< Word frequency instance of the
current word being looked up in
the FTS index */
};
typedef std::vector<ulint, ut_allocator<ulint> > pos_vector_t;
/** structure defines a set of ranges for original documents, each of which
has a minimum position and maximum position. Text in such range should
contain all words in the proximity search. We will need to count the
words in such range to make sure it is less than the specified distance
of the proximity search */
struct fts_proximity_t {
ulint n_pos; /*!< number of position set, defines
a range (min to max) containing all
matching words */
pos_vector_t min_pos; /*!< the minimum position (in bytes)
of the range */
pos_vector_t max_pos; /*!< the maximum position (in bytes)
of the range */
};
/** The match positions and tokesn to match */
struct fts_phrase_t {
fts_phrase_t(const dict_table_t* table)
:
found(false),
match(NULL),
tokens(NULL),
distance(0),
charset(NULL),
heap(NULL),
page_size(dict_table_page_size(table)),
proximity_pos(NULL),
parser(NULL)
{
}
/** Match result */
ibool found;
/** Positions within text */
const fts_match_t* match;
/** Tokens to match */
const ib_vector_t* tokens;
/** For matching on proximity distance. Can be 0 for exact match */
ulint distance;
/** Phrase match charset */
CHARSET_INFO* charset;
/** Heap for word processing */
mem_heap_t* heap;
/** Row page size */
const page_size_t page_size;
/** Position info for proximity search verification. Records the
min and max position of words matched */
fts_proximity_t* proximity_pos;
/** FTS plugin parser */
st_mysql_ftparser* parser;
};
/** Paramter passed to fts phrase match by parser */
struct fts_phrase_param_t {
fts_phrase_t* phrase; /*!< Match phrase instance */
ulint token_index; /*!< Index of token to match next */
mem_heap_t* heap; /*!< Heap for word processing */
};
/** For storing the frequncy of a word/term in a document */
struct fts_doc_freq_t {
doc_id_t doc_id; /*!< Document id */
ulint freq; /*!< Frequency of a word in a document */
};
/** To determine the word frequency per document. */
struct fts_word_freq_t {
fts_string_t word; /*!< Word for which we need the freq,
it's allocated on the query heap */
ib_rbt_t* doc_freqs; /*!< RB Tree for storing per document
word frequencies. The elements are
of type fts_doc_freq_t */
ib_uint64_t doc_count; /*!< Total number of documents that
contain this word */
double idf; /*!< Inverse document frequency */
};
/********************************************************************
Callback function to fetch the rows in an FTS INDEX record.
@return always TRUE */
static
ibool
fts_query_index_fetch_nodes(
/*========================*/
void* row, /*!< in: sel_node_t* */
void* user_arg); /*!< in: pointer to ib_vector_t */
/********************************************************************
Read and filter nodes.
@return fts_node_t instance */
static
dberr_t
fts_query_filter_doc_ids(
/*=====================*/
fts_query_t* query, /*!< in: query instance */
const fts_string_t* word, /*!< in: the current word */
fts_word_freq_t* word_freq, /*!< in/out: word frequency */
const fts_node_t* node, /*!< in: current FTS node */
void* data, /*!< in: doc id ilist */
ulint len, /*!< in: doc id ilist size */
ibool calc_doc_count);/*!< in: whether to remember doc
count */
/** Process (nested) sub-expression, create a new result set to store the
sub-expression result by processing nodes under current sub-expression
list. Merge the sub-expression result with that of parent expression list.
@param[in,out] node current root node
@param[in,out] visitor callback function
@param[in,out] arg argument for callback
@return DB_SUCCESS if all go well */
static
dberr_t
fts_ast_visit_sub_exp(
fts_ast_node_t* node,
fts_ast_callback visitor,
void* arg);
#if 0
/*****************************************************************//***
Find a doc_id in a word's ilist.
@return TRUE if found. */
static
ibool
fts_query_find_doc_id(
/*==================*/
fts_select_t* select, /*!< in/out: search the doc id selected,
update the frequency if found. */
void* data, /*!< in: doc id ilist */
ulint len); /*!< in: doc id ilist size */
#endif
/*************************************************************//**
This function implements a simple "blind" query expansion search:
words in documents found in the first search pass will be used as
search arguments to search the document again, thus "expand"
the search result set.
@return DB_SUCCESS if success, otherwise the error code */
static
dberr_t
fts_expand_query(
/*=============*/
dict_index_t* index, /*!< in: FTS index to search */
fts_query_t* query) /*!< in: query result, to be freed
by the client */
MY_ATTRIBUTE((nonnull, warn_unused_result));
/*************************************************************//**
This function finds documents that contain all words in a
phrase or proximity search. And if proximity search, verify
the words are close enough to each other, as in specified distance.
This function is called for phrase and proximity search.
@return TRUE if documents are found, FALSE if otherwise */
static
ibool
fts_phrase_or_proximity_search(
/*===========================*/
fts_query_t* query, /*!< in/out: query instance
query->doc_ids might be instantiated
with qualified doc IDs */
ib_vector_t* tokens); /*!< in: Tokens contain words */
/*************************************************************//**
This function checks whether words in result documents are close to
each other (within proximity range as specified by "distance").
If "distance" is MAX_ULINT, then it will find all combinations of
positions of matching words and store min and max positions
in the "qualified_pos" for later verification.
@return true if words are close to each other, false if otherwise */
static
bool
fts_proximity_get_positions(
/*========================*/
fts_match_t** match, /*!< in: query instance */
ulint num_match, /*!< in: number of matching
items */
ulint distance, /*!< in: distance value
for proximity search */
fts_proximity_t* qualified_pos); /*!< out: the position info
records ranges containing
all matching words. */
#if 0
/********************************************************************
Get the total number of words in a documents. */
static
ulint
fts_query_terms_in_document(
/*========================*/
/*!< out: DB_SUCCESS if all go well
else error code */
fts_query_t* query, /*!< in: FTS query state */
doc_id_t doc_id, /*!< in: the word to check */
ulint* total); /*!< out: total words in document */
#endif
/********************************************************************
Compare two fts_doc_freq_t doc_ids.
@return < 0 if n1 < n2, 0 if n1 == n2, > 0 if n1 > n2 */
UNIV_INLINE
int
fts_freq_doc_id_cmp(
/*================*/
const void* p1, /*!< in: id1 */
const void* p2) /*!< in: id2 */
{
const fts_doc_freq_t* fq1 = (const fts_doc_freq_t*) p1;
const fts_doc_freq_t* fq2 = (const fts_doc_freq_t*) p2;
return((int) (fq1->doc_id - fq2->doc_id));
}
#if 0
/*******************************************************************//**
Print the table used for calculating LCS. */
static
void
fts_print_lcs_table(
/*================*/
const ulint* table, /*!< in: array to print */
ulint n_rows, /*!< in: total no. of rows */
ulint n_cols) /*!< in: total no. of cols */
{
ulint i;
for (i = 0; i < n_rows; ++i) {
ulint j;
printf("\n");
for (j = 0; j < n_cols; ++j) {
printf("%2lu ", FTS_ELEM(table, n_cols, i, j));
}
}
}
/********************************************************************
Find the longest common subsequence between the query string and
the document. */
static
ulint
fts_query_lcs(
/*==========*/
/*!< out: LCS (length) between
two ilists */
const ulint* p1, /*!< in: word positions of query */
ulint len_p1, /*!< in: no. of elements in p1 */
const ulint* p2, /*!< in: word positions within document */
ulint len_p2) /*!< in: no. of elements in p2 */
{
int i;
ulint len = 0;
ulint r = len_p1;
ulint c = len_p2;
ulint size = (r + 1) * (c + 1) * sizeof(ulint);
ulint* table = (ulint*) ut_malloc_nokey(size);
/* Traverse the table backwards, from the last row to the first and
also from the last column to the first. We compute the smaller
common subsequeces first, then use the caluclated values to determine
the longest common subsequence. The result will be in TABLE[0][0]. */
for (i = r; i >= 0; --i) {
int j;
for (j = c; j >= 0; --j) {
if (p1[i] == (ulint) -1 || p2[j] == (ulint) -1) {
FTS_ELEM(table, c, i, j) = 0;
} else if (p1[i] == p2[j]) {
FTS_ELEM(table, c, i, j) = FTS_ELEM(
table, c, i + 1, j + 1) + 1;
} else {
ulint value;
value = ut_max(
FTS_ELEM(table, c, i + 1, j),
FTS_ELEM(table, c, i, j + 1));
FTS_ELEM(table, c, i, j) = value;
}
}
}
len = FTS_ELEM(table, c, 0, 0);
fts_print_lcs_table(table, r, c);
printf("\nLen=" ULINTPF "\n", len);
ut_free(table);
return(len);
}
#endif
/*******************************************************************//**
Compare two fts_ranking_t instance on their rank value and doc ids in
descending order on the rank and ascending order on doc id.
@return 0 if p1 == p2, < 0 if p1 < p2, > 0 if p1 > p2 */
static
int
fts_query_compare_rank(
/*===================*/
const void* p1, /*!< in: pointer to elem */
const void* p2) /*!< in: pointer to elem */
{
const fts_ranking_t* r1 = (const fts_ranking_t*) p1;
const fts_ranking_t* r2 = (const fts_ranking_t*) p2;
if (r2->rank < r1->rank) {
return(-1);
} else if (r2->rank == r1->rank) {
if (r1->doc_id < r2->doc_id) {
return(1);
} else if (r1->doc_id > r2->doc_id) {
return(1);
}
return(0);
}
return(1);
}
/*******************************************************************//**
Create words in ranking */
static
void
fts_ranking_words_create(
/*=====================*/
fts_query_t* query, /*!< in: query instance */
fts_ranking_t* ranking) /*!< in: ranking instance */
{
ranking->words = static_cast<byte*>(
mem_heap_zalloc(query->heap, RANKING_WORDS_INIT_LEN));
ranking->words_len = RANKING_WORDS_INIT_LEN;
}
/*
The optimization here is using a char array(bitmap) to replace words rb tree
in fts_ranking_t.
It can save lots of memory except in some cases of QUERY EXPANSION.
'word_map' is used as a word dictionary, in which the key is a word, the value
is a number. In 'fts_ranking_words_add', we first check if the word is in 'word_map'.
if not, we add it into 'word_map', and give it a position(actually a number).
then we set the corresponding bit to '1' at the position in the char array 'words'.
'word_vector' is a useful backup of 'word_map', and we can get a word by its position,
more quickly than searching by value in 'word_map'. we use 'word_vector'
in 'fts_query_calculate_ranking' and 'fts_expand_query'. In the two functions, we need
to scan the bitmap 'words', and get a word when a bit is '1', then we get word_freq
by the word.
*/
/*******************************************************************//**
Add a word into ranking */
static
void
fts_ranking_words_add(
/*==================*/
fts_query_t* query, /*!< in: query instance */
fts_ranking_t* ranking, /*!< in: ranking instance */
const fts_string_t* word) /*!< in: term/word to add */
{
ulint pos;
ulint byte_offset;
ulint bit_offset;
ib_rbt_bound_t parent;
/* Note: we suppose the word map and vector are append-only. */
ut_ad(query->word_vector->size() == rbt_size(query->word_map));
/* We use ib_rbt to simulate a map, f_n_char means position. */
if (rbt_search(query->word_map, &parent, word) == 0) {
fts_string_t* result_word;
result_word = rbt_value(fts_string_t, parent.last);
pos = result_word->f_n_char;
ut_ad(pos < rbt_size(query->word_map));
} else {
/* Add the word to map. */
fts_string_t new_word;
pos = rbt_size(query->word_map);
fts_string_dup(&new_word, word, query->heap);
new_word.f_n_char = pos;
rbt_add_node(query->word_map, &parent, &new_word);
ut_ad(rbt_validate(query->word_map));
query->word_vector->push_back(new_word);
}
/* Check words len */
byte_offset = pos / CHAR_BIT;
if (byte_offset >= ranking->words_len) {
byte* words = ranking->words;
ulint words_len = ranking->words_len;
while (byte_offset >= words_len) {
words_len *= 2;
}
ranking->words = static_cast<byte*>(
mem_heap_zalloc(query->heap, words_len));
ut_memcpy(ranking->words, words, ranking->words_len);
ranking->words_len = words_len;
}
/* Set ranking words */
ut_ad(byte_offset < ranking->words_len);
bit_offset = pos % CHAR_BIT;
ranking->words[byte_offset] |= 1 << bit_offset;
}
/*******************************************************************//**
Get a word from a ranking
@return true if it's successful */
static
bool
fts_ranking_words_get_next(
/*=======================*/
const fts_query_t* query, /*!< in: query instance */
fts_ranking_t* ranking,/*!< in: ranking instance */
ulint* pos, /*!< in/out: word start pos */
fts_string_t* word) /*!< in/out: term/word to add */
{
bool ret = false;
ulint max_pos = ranking->words_len * CHAR_BIT;
/* Search for next word */
while (*pos < max_pos) {
ulint byte_offset = *pos / CHAR_BIT;
ulint bit_offset = *pos % CHAR_BIT;
if (ranking->words[byte_offset] & (1 << bit_offset)) {
ret = true;
break;
}
*pos += 1;
};
/* Get next word from word vector */
if (ret) {
ut_ad(*pos < query->word_vector->size());
*word = query->word_vector->at((size_t)*pos);
*pos += 1;
}
return ret;
}
/*******************************************************************//**
Add a word if it doesn't exist, to the term freq RB tree. We store
a pointer to the word that is passed in as the argument.
@return pointer to word */
static
fts_word_freq_t*
fts_query_add_word_freq(
/*====================*/
fts_query_t* query, /*!< in: query instance */
const fts_string_t* word) /*!< in: term/word to add */
{
ib_rbt_bound_t parent;
/* Lookup the word in our rb tree and add if it doesn't exist. */
if (rbt_search(query->word_freqs, &parent, word) != 0) {
fts_word_freq_t word_freq;
memset(&word_freq, 0, sizeof(word_freq));
fts_string_dup(&word_freq.word, word, query->heap);
word_freq.doc_count = 0;
word_freq.doc_freqs = rbt_create(
sizeof(fts_doc_freq_t), fts_freq_doc_id_cmp);
parent.last = rbt_add_node(
query->word_freqs, &parent, &word_freq);
query->total_size += word->f_len
+ SIZEOF_RBT_CREATE
+ SIZEOF_RBT_NODE_ADD
+ sizeof(fts_word_freq_t);
}
return(rbt_value(fts_word_freq_t, parent.last));
}
/*******************************************************************//**
Add a doc id if it doesn't exist, to the doc freq RB tree.
@return pointer to word */
static
fts_doc_freq_t*
fts_query_add_doc_freq(
/*===================*/
fts_query_t* query, /*!< in: query instance */
ib_rbt_t* doc_freqs, /*!< in: rb tree of fts_doc_freq_t */
doc_id_t doc_id) /*!< in: doc id to add */
{
ib_rbt_bound_t parent;
/* Lookup the doc id in our rb tree and add if it doesn't exist. */
if (rbt_search(doc_freqs, &parent, &doc_id) != 0) {
fts_doc_freq_t doc_freq;
memset(&doc_freq, 0, sizeof(doc_freq));
doc_freq.freq = 0;
doc_freq.doc_id = doc_id;
parent.last = rbt_add_node(doc_freqs, &parent, &doc_freq);
query->total_size += SIZEOF_RBT_NODE_ADD
+ sizeof(fts_doc_freq_t);
}
return(rbt_value(fts_doc_freq_t, parent.last));
}
/*******************************************************************//**
Add the doc id to the query set only if it's not in the
deleted array. */
static
void
fts_query_union_doc_id(
/*===================*/
fts_query_t* query, /*!< in: query instance */
doc_id_t doc_id, /*!< in: the doc id to add */
fts_rank_t rank) /*!< in: if non-zero, it is the
rank associated with the doc_id */
{
ib_rbt_bound_t parent;
ulint size = ib_vector_size(query->deleted->doc_ids);
fts_update_t* array = (fts_update_t*) query->deleted->doc_ids->data;
/* Check if the doc id is deleted and it's not already in our set. */
if (fts_bsearch(array, 0, static_cast<int>(size), doc_id) < 0
&& rbt_search(query->doc_ids, &parent, &doc_id) != 0) {
fts_ranking_t ranking;
ranking.rank = rank;
ranking.doc_id = doc_id;
fts_ranking_words_create(query, &ranking);
rbt_add_node(query->doc_ids, &parent, &ranking);
query->total_size += SIZEOF_RBT_NODE_ADD
+ sizeof(fts_ranking_t) + RANKING_WORDS_INIT_LEN;
}
}
/*******************************************************************//**
Remove the doc id from the query set only if it's not in the
deleted set. */
static
void
fts_query_remove_doc_id(
/*====================*/
fts_query_t* query, /*!< in: query instance */
doc_id_t doc_id) /*!< in: the doc id to add */
{
ib_rbt_bound_t parent;
ulint size = ib_vector_size(query->deleted->doc_ids);
fts_update_t* array = (fts_update_t*) query->deleted->doc_ids->data;
/* Check if the doc id is deleted and it's in our set. */
if (fts_bsearch(array, 0, static_cast<int>(size), doc_id) < 0
&& rbt_search(query->doc_ids, &parent, &doc_id) == 0) {
ut_free(rbt_remove_node(query->doc_ids, parent.last));
ut_ad(query->total_size >=
SIZEOF_RBT_NODE_ADD + sizeof(fts_ranking_t));
query->total_size -= SIZEOF_RBT_NODE_ADD
+ sizeof(fts_ranking_t);
}
}
/*******************************************************************//**
Find the doc id in the query set but not in the deleted set, artificialy
downgrade or upgrade its ranking by a value and make/initialize its ranking
under or above its normal range 0 to 1. This is used for Boolean Search
operator such as Negation operator, which makes word's contribution to the
row's relevance to be negative */
static
void
fts_query_change_ranking(
/*====================*/
fts_query_t* query, /*!< in: query instance */
doc_id_t doc_id, /*!< in: the doc id to add */
ibool downgrade) /*!< in: Whether to downgrade ranking */
{
ib_rbt_bound_t parent;
ulint size = ib_vector_size(query->deleted->doc_ids);
fts_update_t* array = (fts_update_t*) query->deleted->doc_ids->data;
/* Check if the doc id is deleted and it's in our set. */
if (fts_bsearch(array, 0, static_cast<int>(size), doc_id) < 0
&& rbt_search(query->doc_ids, &parent, &doc_id) == 0) {
fts_ranking_t* ranking;
ranking = rbt_value(fts_ranking_t, parent.last);
ranking->rank += downgrade ? RANK_DOWNGRADE : RANK_UPGRADE;
/* Allow at most 2 adjustment by RANK_DOWNGRADE (-0.5)
and RANK_UPGRADE (0.5) */
if (ranking->rank >= 1.0F) {
ranking->rank = 1.0F;
} else if (ranking->rank <= -1.0F) {
ranking->rank = -1.0F;
}
}
}
/*******************************************************************//**
Check the doc id in the query set only if it's not in the
deleted array. The doc ids that were found are stored in
another rb tree (fts_query_t::intersect). */
static
void
fts_query_intersect_doc_id(
/*=======================*/
fts_query_t* query, /*!< in: query instance */
doc_id_t doc_id, /*!< in: the doc id to add */
fts_rank_t rank) /*!< in: if non-zero, it is the
rank associated with the doc_id */
{
ib_rbt_bound_t parent;
ulint size = ib_vector_size(query->deleted->doc_ids);
fts_update_t* array = (fts_update_t*) query->deleted->doc_ids->data;
fts_ranking_t* ranking= NULL;
/* There are three types of intersect:
1. '+a': doc_ids is empty, add doc into intersect if it matches 'a'.
2. 'a +b': docs match 'a' is in doc_ids, add doc into intersect
if it matches 'b'. if the doc is also in doc_ids, then change the
doc's rank, and add 'a' in doc's words.
3. '+a +b': docs matching '+a' is in doc_ids, add doc into intsersect
if it matches 'b' and it's in doc_ids.(multi_exist = true). */
/* Check if the doc id is deleted and it's in our set */
if (fts_bsearch(array, 0, static_cast<int>(size), doc_id) < 0) {
fts_ranking_t new_ranking;
if (rbt_search(query->doc_ids, &parent, &doc_id) != 0) {
if (query->multi_exist) {
return;
} else {
new_ranking.words = NULL;
}
} else {
ranking = rbt_value(fts_ranking_t, parent.last);
/* We've just checked the doc id before */
if (ranking->words == NULL) {
ut_ad(rbt_search(query->intersection, &parent,
ranking) == 0);
return;
}
/* Merge rank */
rank += ranking->rank;
if (rank >= 1.0F) {
rank = 1.0F;
} else if (rank <= -1.0F) {
rank = -1.0F;
}
/* Take words */
new_ranking.words = ranking->words;
new_ranking.words_len = ranking->words_len;
}
new_ranking.rank = rank;
new_ranking.doc_id = doc_id;
if (rbt_search(query->intersection, &parent,
&new_ranking) != 0) {
if (new_ranking.words == NULL) {
fts_ranking_words_create(query, &new_ranking);
query->total_size += RANKING_WORDS_INIT_LEN;
} else {
/* Note that the intersection has taken
ownership of the ranking data. */
ranking->words = NULL;
}
rbt_add_node(query->intersection,
&parent, &new_ranking);
query->total_size += SIZEOF_RBT_NODE_ADD
+ sizeof(fts_ranking_t);
}
}
}
/*******************************************************************//**
Free the document ranking rb tree. */
static
void
fts_query_free_doc_ids(
/*===================*/
fts_query_t* query, /*!< in: query instance */
ib_rbt_t* doc_ids) /*!< in: rb tree to free */
{
const ib_rbt_node_t* node;
for (node = rbt_first(doc_ids); node; node = rbt_first(doc_ids)) {
fts_ranking_t* ranking;
ranking = rbt_value(fts_ranking_t, node);
if (ranking->words) {
ranking->words = NULL;
}
ut_free(rbt_remove_node(doc_ids, node));
ut_ad(query->total_size >=
SIZEOF_RBT_NODE_ADD + sizeof(fts_ranking_t));
query->total_size -= SIZEOF_RBT_NODE_ADD
+ sizeof(fts_ranking_t);
}
rbt_free(doc_ids);
ut_ad(query->total_size >= SIZEOF_RBT_CREATE);
query->total_size -= SIZEOF_RBT_CREATE;
}
/*******************************************************************//**
Add the word to the documents "list" of matching words from
the query. We make a copy of the word from the query heap. */
static
void
fts_query_add_word_to_document(
/*===========================*/
fts_query_t* query, /*!< in: query to update */
doc_id_t doc_id, /*!< in: the document to update */
const fts_string_t* word) /*!< in: the token to add */
{
ib_rbt_bound_t parent;
fts_ranking_t* ranking = NULL;
if (query->flags == FTS_OPT_RANKING) {
return;
}
/* First we search the intersection RB tree as it could have
taken ownership of the words rb tree instance. */
if (query->intersection
&& rbt_search(query->intersection, &parent, &doc_id) == 0) {
ranking = rbt_value(fts_ranking_t, parent.last);
}
if (ranking == NULL
&& rbt_search(query->doc_ids, &parent, &doc_id) == 0) {
ranking = rbt_value(fts_ranking_t, parent.last);
}
if (ranking != NULL) {
fts_ranking_words_add(query, ranking, word);
}
}
/*******************************************************************//**
Check the node ilist. */
static
void
fts_query_check_node(
/*=================*/
fts_query_t* query, /*!< in: query to update */
const fts_string_t* token, /*!< in: the token to search */
const fts_node_t* node) /*!< in: node to check */
{
/* Skip nodes whose doc ids are out range. */
if (query->oper == FTS_EXIST
&& ((query->upper_doc_id > 0
&& node->first_doc_id > query->upper_doc_id)
|| (query->lower_doc_id > 0
&& node->last_doc_id < query->lower_doc_id))) {
/* Ignore */
} else {
int ret;
ib_rbt_bound_t parent;
ulint ilist_size = node->ilist_size;
fts_word_freq_t*word_freqs;
/* The word must exist. */
ret = rbt_search(query->word_freqs, &parent, token);