isomorfeus-ferret 0.12.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (222) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +612 -0
  3. data/README.md +44 -0
  4. data/ext/isomorfeus_ferret_ext/benchmark.c +223 -0
  5. data/ext/isomorfeus_ferret_ext/benchmark.h +45 -0
  6. data/ext/isomorfeus_ferret_ext/benchmarks_all.h +25 -0
  7. data/ext/isomorfeus_ferret_ext/bm_bitvector.c +123 -0
  8. data/ext/isomorfeus_ferret_ext/bm_hash.c +118 -0
  9. data/ext/isomorfeus_ferret_ext/bm_micro_string.c +40 -0
  10. data/ext/isomorfeus_ferret_ext/bm_store.c +93 -0
  11. data/ext/isomorfeus_ferret_ext/email.rl +21 -0
  12. data/ext/isomorfeus_ferret_ext/extconf.rb +5 -0
  13. data/ext/isomorfeus_ferret_ext/fio_tmpfile.h +53 -0
  14. data/ext/isomorfeus_ferret_ext/frb_analysis.c +2577 -0
  15. data/ext/isomorfeus_ferret_ext/frb_index.c +3457 -0
  16. data/ext/isomorfeus_ferret_ext/frb_lang.c +9 -0
  17. data/ext/isomorfeus_ferret_ext/frb_lang.h +17 -0
  18. data/ext/isomorfeus_ferret_ext/frb_qparser.c +629 -0
  19. data/ext/isomorfeus_ferret_ext/frb_search.c +4460 -0
  20. data/ext/isomorfeus_ferret_ext/frb_store.c +515 -0
  21. data/ext/isomorfeus_ferret_ext/frb_threading.h +30 -0
  22. data/ext/isomorfeus_ferret_ext/frb_utils.c +1127 -0
  23. data/ext/isomorfeus_ferret_ext/frt_analysis.c +1644 -0
  24. data/ext/isomorfeus_ferret_ext/frt_analysis.h +247 -0
  25. data/ext/isomorfeus_ferret_ext/frt_array.c +124 -0
  26. data/ext/isomorfeus_ferret_ext/frt_array.h +54 -0
  27. data/ext/isomorfeus_ferret_ext/frt_bitvector.c +95 -0
  28. data/ext/isomorfeus_ferret_ext/frt_bitvector.h +586 -0
  29. data/ext/isomorfeus_ferret_ext/frt_compound_io.c +374 -0
  30. data/ext/isomorfeus_ferret_ext/frt_config.h +44 -0
  31. data/ext/isomorfeus_ferret_ext/frt_document.c +134 -0
  32. data/ext/isomorfeus_ferret_ext/frt_document.h +52 -0
  33. data/ext/isomorfeus_ferret_ext/frt_except.c +95 -0
  34. data/ext/isomorfeus_ferret_ext/frt_except.h +188 -0
  35. data/ext/isomorfeus_ferret_ext/frt_field_index.c +233 -0
  36. data/ext/isomorfeus_ferret_ext/frt_field_index.h +42 -0
  37. data/ext/isomorfeus_ferret_ext/frt_filter.c +157 -0
  38. data/ext/isomorfeus_ferret_ext/frt_fs_store.c +502 -0
  39. data/ext/isomorfeus_ferret_ext/frt_global.c +427 -0
  40. data/ext/isomorfeus_ferret_ext/frt_global.h +290 -0
  41. data/ext/isomorfeus_ferret_ext/frt_hash.c +518 -0
  42. data/ext/isomorfeus_ferret_ext/frt_hash.h +466 -0
  43. data/ext/isomorfeus_ferret_ext/frt_hashset.c +191 -0
  44. data/ext/isomorfeus_ferret_ext/frt_hashset.h +206 -0
  45. data/ext/isomorfeus_ferret_ext/frt_helper.c +62 -0
  46. data/ext/isomorfeus_ferret_ext/frt_helper.h +13 -0
  47. data/ext/isomorfeus_ferret_ext/frt_ind.c +353 -0
  48. data/ext/isomorfeus_ferret_ext/frt_ind.h +54 -0
  49. data/ext/isomorfeus_ferret_ext/frt_index.c +6377 -0
  50. data/ext/isomorfeus_ferret_ext/frt_index.h +880 -0
  51. data/ext/isomorfeus_ferret_ext/frt_lang.c +104 -0
  52. data/ext/isomorfeus_ferret_ext/frt_lang.h +44 -0
  53. data/ext/isomorfeus_ferret_ext/frt_mempool.c +87 -0
  54. data/ext/isomorfeus_ferret_ext/frt_mempool.h +33 -0
  55. data/ext/isomorfeus_ferret_ext/frt_multimapper.c +349 -0
  56. data/ext/isomorfeus_ferret_ext/frt_multimapper.h +52 -0
  57. data/ext/isomorfeus_ferret_ext/frt_posh.c +1006 -0
  58. data/ext/isomorfeus_ferret_ext/frt_posh.h +973 -0
  59. data/ext/isomorfeus_ferret_ext/frt_priorityqueue.c +147 -0
  60. data/ext/isomorfeus_ferret_ext/frt_priorityqueue.h +147 -0
  61. data/ext/isomorfeus_ferret_ext/frt_q_boolean.c +1612 -0
  62. data/ext/isomorfeus_ferret_ext/frt_q_const_score.c +157 -0
  63. data/ext/isomorfeus_ferret_ext/frt_q_filtered_query.c +209 -0
  64. data/ext/isomorfeus_ferret_ext/frt_q_fuzzy.c +281 -0
  65. data/ext/isomorfeus_ferret_ext/frt_q_match_all.c +147 -0
  66. data/ext/isomorfeus_ferret_ext/frt_q_multi_term.c +672 -0
  67. data/ext/isomorfeus_ferret_ext/frt_q_parser.c +3084 -0
  68. data/ext/isomorfeus_ferret_ext/frt_q_phrase.c +1182 -0
  69. data/ext/isomorfeus_ferret_ext/frt_q_prefix.c +98 -0
  70. data/ext/isomorfeus_ferret_ext/frt_q_range.c +665 -0
  71. data/ext/isomorfeus_ferret_ext/frt_q_span.c +2386 -0
  72. data/ext/isomorfeus_ferret_ext/frt_q_term.c +311 -0
  73. data/ext/isomorfeus_ferret_ext/frt_q_wildcard.c +166 -0
  74. data/ext/isomorfeus_ferret_ext/frt_ram_store.c +460 -0
  75. data/ext/isomorfeus_ferret_ext/frt_scanner.c +899 -0
  76. data/ext/isomorfeus_ferret_ext/frt_scanner.h +28 -0
  77. data/ext/isomorfeus_ferret_ext/frt_scanner_mb.c +6705 -0
  78. data/ext/isomorfeus_ferret_ext/frt_scanner_utf8.c +4419 -0
  79. data/ext/isomorfeus_ferret_ext/frt_search.c +1824 -0
  80. data/ext/isomorfeus_ferret_ext/frt_search.h +924 -0
  81. data/ext/isomorfeus_ferret_ext/frt_similarity.c +150 -0
  82. data/ext/isomorfeus_ferret_ext/frt_similarity.h +79 -0
  83. data/ext/isomorfeus_ferret_ext/frt_sort.c +796 -0
  84. data/ext/isomorfeus_ferret_ext/frt_stopwords.c +395 -0
  85. data/ext/isomorfeus_ferret_ext/frt_store.c +680 -0
  86. data/ext/isomorfeus_ferret_ext/frt_store.h +789 -0
  87. data/ext/isomorfeus_ferret_ext/frt_term_vectors.c +72 -0
  88. data/ext/isomorfeus_ferret_ext/frt_threading.h +23 -0
  89. data/ext/isomorfeus_ferret_ext/frt_win32.h +54 -0
  90. data/ext/isomorfeus_ferret_ext/isomorfeus_ferret.c +409 -0
  91. data/ext/isomorfeus_ferret_ext/isomorfeus_ferret.h +95 -0
  92. data/ext/isomorfeus_ferret_ext/libstemmer.c +93 -0
  93. data/ext/isomorfeus_ferret_ext/libstemmer.h +73 -0
  94. data/ext/isomorfeus_ferret_ext/q_parser.y +1366 -0
  95. data/ext/isomorfeus_ferret_ext/scanner.h +28 -0
  96. data/ext/isomorfeus_ferret_ext/scanner.in +43 -0
  97. data/ext/isomorfeus_ferret_ext/scanner.rl +84 -0
  98. data/ext/isomorfeus_ferret_ext/scanner_mb.rl +200 -0
  99. data/ext/isomorfeus_ferret_ext/scanner_utf8.rl +85 -0
  100. data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_danish.c +324 -0
  101. data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_danish.h +7 -0
  102. data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_dutch.c +610 -0
  103. data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_dutch.h +6 -0
  104. data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_english.c +1104 -0
  105. data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_english.h +6 -0
  106. data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_finnish.c +749 -0
  107. data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_finnish.h +7 -0
  108. data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_french.c +1233 -0
  109. data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_french.h +6 -0
  110. data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_german.c +490 -0
  111. data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_german.h +6 -0
  112. data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_hungarian.c +1217 -0
  113. data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_hungarian.h +7 -0
  114. data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_italian.c +1052 -0
  115. data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_italian.h +6 -0
  116. data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_norwegian.c +283 -0
  117. data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_norwegian.h +6 -0
  118. data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_porter.c +735 -0
  119. data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_porter.h +6 -0
  120. data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_portuguese.c +1003 -0
  121. data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_portuguese.h +7 -0
  122. data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_spanish.c +1079 -0
  123. data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_spanish.h +6 -0
  124. data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_swedish.c +293 -0
  125. data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_swedish.h +6 -0
  126. data/ext/isomorfeus_ferret_ext/stem_ISO_8859_2_romanian.c +984 -0
  127. data/ext/isomorfeus_ferret_ext/stem_ISO_8859_2_romanian.h +6 -0
  128. data/ext/isomorfeus_ferret_ext/stem_KOI8_R_russian.c +686 -0
  129. data/ext/isomorfeus_ferret_ext/stem_KOI8_R_russian.h +6 -0
  130. data/ext/isomorfeus_ferret_ext/stem_UTF_8_danish.c +325 -0
  131. data/ext/isomorfeus_ferret_ext/stem_UTF_8_danish.h +6 -0
  132. data/ext/isomorfeus_ferret_ext/stem_UTF_8_dutch.c +620 -0
  133. data/ext/isomorfeus_ferret_ext/stem_UTF_8_dutch.h +6 -0
  134. data/ext/isomorfeus_ferret_ext/stem_UTF_8_english.c +1111 -0
  135. data/ext/isomorfeus_ferret_ext/stem_UTF_8_english.h +6 -0
  136. data/ext/isomorfeus_ferret_ext/stem_UTF_8_finnish.c +754 -0
  137. data/ext/isomorfeus_ferret_ext/stem_UTF_8_finnish.h +6 -0
  138. data/ext/isomorfeus_ferret_ext/stem_UTF_8_french.c +1242 -0
  139. data/ext/isomorfeus_ferret_ext/stem_UTF_8_french.h +6 -0
  140. data/ext/isomorfeus_ferret_ext/stem_UTF_8_german.c +495 -0
  141. data/ext/isomorfeus_ferret_ext/stem_UTF_8_german.h +6 -0
  142. data/ext/isomorfeus_ferret_ext/stem_UTF_8_hungarian.c +1220 -0
  143. data/ext/isomorfeus_ferret_ext/stem_UTF_8_hungarian.h +6 -0
  144. data/ext/isomorfeus_ferret_ext/stem_UTF_8_italian.c +1059 -0
  145. data/ext/isomorfeus_ferret_ext/stem_UTF_8_italian.h +6 -0
  146. data/ext/isomorfeus_ferret_ext/stem_UTF_8_norwegian.c +285 -0
  147. data/ext/isomorfeus_ferret_ext/stem_UTF_8_norwegian.h +6 -0
  148. data/ext/isomorfeus_ferret_ext/stem_UTF_8_porter.c +741 -0
  149. data/ext/isomorfeus_ferret_ext/stem_UTF_8_porter.h +6 -0
  150. data/ext/isomorfeus_ferret_ext/stem_UTF_8_portuguese.c +1009 -0
  151. data/ext/isomorfeus_ferret_ext/stem_UTF_8_portuguese.h +6 -0
  152. data/ext/isomorfeus_ferret_ext/stem_UTF_8_romanian.c +990 -0
  153. data/ext/isomorfeus_ferret_ext/stem_UTF_8_romanian.h +6 -0
  154. data/ext/isomorfeus_ferret_ext/stem_UTF_8_russian.c +680 -0
  155. data/ext/isomorfeus_ferret_ext/stem_UTF_8_russian.h +6 -0
  156. data/ext/isomorfeus_ferret_ext/stem_UTF_8_spanish.c +1083 -0
  157. data/ext/isomorfeus_ferret_ext/stem_UTF_8_spanish.h +6 -0
  158. data/ext/isomorfeus_ferret_ext/stem_UTF_8_swedish.c +294 -0
  159. data/ext/isomorfeus_ferret_ext/stem_UTF_8_swedish.h +6 -0
  160. data/ext/isomorfeus_ferret_ext/stem_UTF_8_turkish.c +2191 -0
  161. data/ext/isomorfeus_ferret_ext/stem_UTF_8_turkish.h +6 -0
  162. data/ext/isomorfeus_ferret_ext/stem_api.c +66 -0
  163. data/ext/isomorfeus_ferret_ext/stem_api.h +26 -0
  164. data/ext/isomorfeus_ferret_ext/stem_header.h +57 -0
  165. data/ext/isomorfeus_ferret_ext/stem_modules.h +190 -0
  166. data/ext/isomorfeus_ferret_ext/stem_modules.txt +50 -0
  167. data/ext/isomorfeus_ferret_ext/stem_utilities.c +478 -0
  168. data/ext/isomorfeus_ferret_ext/test.c +850 -0
  169. data/ext/isomorfeus_ferret_ext/test.h +416 -0
  170. data/ext/isomorfeus_ferret_ext/test_1710.c +63 -0
  171. data/ext/isomorfeus_ferret_ext/test_analysis.c +1221 -0
  172. data/ext/isomorfeus_ferret_ext/test_array.c +272 -0
  173. data/ext/isomorfeus_ferret_ext/test_bitvector.c +600 -0
  174. data/ext/isomorfeus_ferret_ext/test_compound_io.c +170 -0
  175. data/ext/isomorfeus_ferret_ext/test_document.c +156 -0
  176. data/ext/isomorfeus_ferret_ext/test_except.c +244 -0
  177. data/ext/isomorfeus_ferret_ext/test_fields.c +522 -0
  178. data/ext/isomorfeus_ferret_ext/test_file_deleter.c +185 -0
  179. data/ext/isomorfeus_ferret_ext/test_filter.c +331 -0
  180. data/ext/isomorfeus_ferret_ext/test_fs_store.c +25 -0
  181. data/ext/isomorfeus_ferret_ext/test_global.c +299 -0
  182. data/ext/isomorfeus_ferret_ext/test_hash.c +485 -0
  183. data/ext/isomorfeus_ferret_ext/test_hashset.c +288 -0
  184. data/ext/isomorfeus_ferret_ext/test_helper.c +47 -0
  185. data/ext/isomorfeus_ferret_ext/test_highlighter.c +548 -0
  186. data/ext/isomorfeus_ferret_ext/test_index.c +2323 -0
  187. data/ext/isomorfeus_ferret_ext/test_lang.c +74 -0
  188. data/ext/isomorfeus_ferret_ext/test_mempool.c +102 -0
  189. data/ext/isomorfeus_ferret_ext/test_multimapper.c +64 -0
  190. data/ext/isomorfeus_ferret_ext/test_priorityqueue.c +213 -0
  191. data/ext/isomorfeus_ferret_ext/test_q_const_score.c +84 -0
  192. data/ext/isomorfeus_ferret_ext/test_q_filtered.c +61 -0
  193. data/ext/isomorfeus_ferret_ext/test_q_fuzzy.c +241 -0
  194. data/ext/isomorfeus_ferret_ext/test_q_parser.c +464 -0
  195. data/ext/isomorfeus_ferret_ext/test_q_span.c +575 -0
  196. data/ext/isomorfeus_ferret_ext/test_ram_store.c +77 -0
  197. data/ext/isomorfeus_ferret_ext/test_search.c +1874 -0
  198. data/ext/isomorfeus_ferret_ext/test_segments.c +167 -0
  199. data/ext/isomorfeus_ferret_ext/test_similarity.c +25 -0
  200. data/ext/isomorfeus_ferret_ext/test_sort.c +333 -0
  201. data/ext/isomorfeus_ferret_ext/test_store.c +591 -0
  202. data/ext/isomorfeus_ferret_ext/test_store.h +3 -0
  203. data/ext/isomorfeus_ferret_ext/test_term.c +351 -0
  204. data/ext/isomorfeus_ferret_ext/test_term_vectors.c +373 -0
  205. data/ext/isomorfeus_ferret_ext/test_test.c +83 -0
  206. data/ext/isomorfeus_ferret_ext/test_threading.c +188 -0
  207. data/ext/isomorfeus_ferret_ext/testhelper.c +561 -0
  208. data/ext/isomorfeus_ferret_ext/testhelper.h +25 -0
  209. data/ext/isomorfeus_ferret_ext/tests_all.h +87 -0
  210. data/ext/isomorfeus_ferret_ext/uchar-ucs4.rl +1854 -0
  211. data/ext/isomorfeus_ferret_ext/uchar-utf8.rl +1999 -0
  212. data/ext/isomorfeus_ferret_ext/url.rl +27 -0
  213. data/ext/isomorfeus_ferret_ext/word_list.h +15156 -0
  214. data/lib/isomorfeus/ferret/document.rb +132 -0
  215. data/lib/isomorfeus/ferret/field_symbol.rb +85 -0
  216. data/lib/isomorfeus/ferret/index/field_infos.rb +48 -0
  217. data/lib/isomorfeus/ferret/index/index.rb +970 -0
  218. data/lib/isomorfeus/ferret/monitor.rb +323 -0
  219. data/lib/isomorfeus/ferret/stdlib_patches.rb +151 -0
  220. data/lib/isomorfeus/ferret/version.rb +5 -0
  221. data/lib/isomorfeus-ferret.rb +8 -0
  222. metadata +307 -0
@@ -0,0 +1,9 @@
1
+ #include "frt_except.h"
2
+ #include "frb_lang.h"
3
+
4
+ struct timeval rb_time_interval _((VALUE));
5
+
6
+ extern void frt_micro_sleep(const int micro_seconds)
7
+ {
8
+ rb_thread_wait_for(rb_time_interval(rb_float_new((double)micro_seconds/1000000.0)));
9
+ }
@@ -0,0 +1,17 @@
1
+ #ifndef FRT_LANG_H
2
+ #define FRT_LANG_H
3
+
4
+ #define RUBY_BINDINGS 1
5
+
6
+ #include <stdarg.h>
7
+ #include <ruby.h>
8
+
9
+ #undef close
10
+ #undef rename
11
+ #undef read
12
+
13
+ #define frt_emalloc xmalloc
14
+ #define frt_ecalloc(n) xcalloc(n, 1)
15
+ #define frt_erealloc xrealloc
16
+
17
+ #endif
@@ -0,0 +1,629 @@
1
+ #include "frt_search.h"
2
+ #include "isomorfeus_ferret.h"
3
+
4
+ static VALUE cQueryParser;
5
+ VALUE cQueryParseException;
6
+
7
+ extern VALUE sym_analyzer;
8
+ static VALUE sym_wild_card_downcase;
9
+ static VALUE sym_fields;
10
+ static VALUE sym_all_fields;
11
+ static VALUE sym_tkz_fields;
12
+ static VALUE sym_default_field;
13
+ static VALUE sym_validate_fields;
14
+ static VALUE sym_or_default;
15
+ static VALUE sym_default_slop;
16
+ static VALUE sym_handle_parse_errors;
17
+ static VALUE sym_clean_string;
18
+ static VALUE sym_max_clauses;
19
+ static VALUE sym_use_keywords;
20
+ static VALUE sym_use_typed_range_query;
21
+
22
+ extern VALUE frb_get_analyzer(FrtAnalyzer *a);
23
+ extern VALUE frb_get_q(FrtQuery *q);
24
+ extern FrtAnalyzer *frb_get_cwrapped_analyzer(VALUE ranalyzer);
25
+ extern FrtQuery *qp_parse(FrtQParser *self, char *qstr);
26
+
27
+ /****************************************************************************
28
+ *
29
+ * QueryParser Methods
30
+ *
31
+ ****************************************************************************/
32
+
33
+ static void
34
+ frb_qp_free(void *p)
35
+ {
36
+ object_del(p);
37
+ frt_qp_destroy((FrtQParser *)p);
38
+ }
39
+
40
+ static void
41
+ frb_qp_mark(void *p)
42
+ {
43
+ frb_gc_mark(((FrtQParser *)p)->analyzer);
44
+ }
45
+
46
+ static FrtHashSet *
47
+ frb_get_fields(VALUE rfields, FrtHashSet *other_fields)
48
+ {
49
+ VALUE rval;
50
+ FrtHashSet *fields;
51
+ char *s, *p, *str;
52
+
53
+ if (rfields == Qnil) return NULL;
54
+
55
+ fields = frt_hs_new_ptr(NULL);
56
+ if (TYPE(rfields) == T_ARRAY) {
57
+ int i;
58
+ for (i = 0; i < RARRAY_LEN(rfields); i++) {
59
+ rval = rb_obj_as_string(RARRAY_PTR(rfields)[i]);
60
+ frt_hs_add(fields, (void *)rb_intern(rs2s(rval)));
61
+ }
62
+ } else {
63
+ rval = rb_obj_as_string(rfields);
64
+ if (strcmp("*", rs2s(rval)) == 0) {
65
+ frt_hs_destroy(fields);
66
+ fields = NULL;
67
+ } else {
68
+ s = str = rstrdup(rval);
69
+ while ((p = strchr(s, '|')) && *p != '\0') {
70
+ *p = '\0';
71
+ frt_hs_add(fields, (void *)rb_intern(s));
72
+ s = p + 1;
73
+ }
74
+ frt_hs_add(fields, (void *)rb_intern(s));
75
+ free(str);
76
+ }
77
+ }
78
+ return fields;
79
+ }
80
+
81
+ static void
82
+ hs_safe_merge(FrtHashSet *merger, FrtHashSet *mergee)
83
+ {
84
+ FrtHashSetEntry *entry = mergee->first;
85
+ for (; entry != NULL; entry = entry->next) {
86
+ frt_hs_add_safe(merger, entry->elem);
87
+ }
88
+ }
89
+
90
+ /*
91
+ * call-seq:
92
+ * QueryParser.new(options = {}) -> QueryParser
93
+ *
94
+ * Create a new QueryParser. The QueryParser is used to convert string
95
+ * queries into Query objects. The options are;
96
+ *
97
+ * === Options
98
+ *
99
+ * :default_field:: Default: "*" (all fields). The default field to
100
+ * search when no field is specified in the search
101
+ * string. It can also be an array of fields.
102
+ * :analyzer:: Default: StandardAnalyzer. FrtAnalyzer used by the
103
+ * query parser to parse query terms
104
+ * :wild_card_downcase:: Default: true. Specifies whether wild-card queries
105
+ * and range queries should be downcased or not since
106
+ * they are not passed through the parser
107
+ * :fields:: Default: []. Lets the query parser know what
108
+ * fields are available for searching, particularly
109
+ * when the "*" is specified as the search field
110
+ * :tokenized_fields:: Default: :fields. Lets the query parser know which
111
+ * fields are tokenized so it knows which fields to
112
+ * run the analyzer over.
113
+ * :validate_fields:: Default: false. Set to true if you want an
114
+ * exception to be raised if there is an attempt to
115
+ * search a non-existent field
116
+ * :or_default:: Default: true. Use "OR" as the default boolean
117
+ * operator
118
+ * :default_slop:: Default: 0. Default slop to use in PhraseQuery
119
+ * :handle_parse_errors:: Default: true. QueryParser will quietly handle all
120
+ * parsing errors internally. If you'd like to handle
121
+ * them yourself, set this parameter to false.
122
+ * :clean_string:: Default: true. QueryParser will do a quick
123
+ * once-over the query string make sure that quotes
124
+ * and brackets match up and special characters are
125
+ * escaped
126
+ * :max_clauses:: Default: 512. the maximum number of clauses
127
+ * allowed in boolean queries and the maximum number
128
+ * of terms allowed in multi, prefix, wild-card or
129
+ * fuzzy queries when those queries are generated by
130
+ * rewriting other queries
131
+ * :use_keywords:: Default: true. By default AND, OR, NOT and REQ are
132
+ * keywords used by the query parser. Sometimes this
133
+ * is undesirable. For example, if your application
134
+ * allows searching for US states by their
135
+ * abbreviation, then OR will be a common query
136
+ * string. By setting :use_keywords to false, OR will
137
+ * no longer be a keyword allowing searches for the
138
+ * state of Oregon. You will still be able to use
139
+ * boolean queries by using the + and - characters.
140
+ * :use_typed_range_query:: Default: false. Use TypedRangeQuery instead of
141
+ * the standard RangeQuery when parsing
142
+ * range queries. This is useful if you have number
143
+ * fields which you want to perform range queries
144
+ * on. You won't need to pad or normalize the data
145
+ * in the field in anyway to get correct results.
146
+ * However, performance will be a lot slower for
147
+ * large indexes, hence the default.
148
+ * Note: the default is set to true in the Index
149
+ * class.
150
+ */
151
+ static VALUE
152
+ frb_qp_init(int argc, VALUE *argv, VALUE self)
153
+ {
154
+ VALUE roptions = Qnil;
155
+ VALUE rval;
156
+ FrtAnalyzer *analyzer = NULL;
157
+ FrtHashSet *def_fields = NULL;
158
+ FrtHashSet *all_fields = NULL;
159
+ FrtHashSet *tkz_fields = NULL;
160
+ FrtQParser *qp;
161
+
162
+ if (rb_scan_args(argc, argv, "01", &roptions) > 0) {
163
+ if (TYPE(roptions) == T_HASH) {
164
+ if (Qnil != (rval = rb_hash_aref(roptions, sym_default_field))) {
165
+ def_fields = frb_get_fields(rval, NULL);
166
+ }
167
+ if (Qnil != (rval = rb_hash_aref(roptions, sym_analyzer))) {
168
+ analyzer = frb_get_cwrapped_analyzer(rval);
169
+ }
170
+ if (Qnil != (rval = rb_hash_aref(roptions, sym_all_fields))) {
171
+ all_fields = frb_get_fields(rval, def_fields);
172
+ }
173
+ if (Qnil != (rval = rb_hash_aref(roptions, sym_fields))) {
174
+ all_fields = frb_get_fields(rval, def_fields);
175
+ }
176
+ if (Qnil != (rval = rb_hash_aref(roptions, sym_tkz_fields))) {
177
+ tkz_fields = frb_get_fields(rval, def_fields);
178
+ }
179
+ } else {
180
+ def_fields = frb_get_fields(roptions, def_fields);
181
+ roptions = Qnil;
182
+ }
183
+ }
184
+ if (all_fields == NULL) {
185
+ all_fields = frt_hs_new_ptr(NULL);
186
+ }
187
+ if (!analyzer) {
188
+ analyzer = frt_mb_standard_analyzer_new(true);
189
+ }
190
+ qp = frt_qp_new(analyzer);
191
+ //frt_hs_destroy(qp->all_fields);
192
+ //frt_hs_destroy(qp->def_fields);
193
+ //frt_hs_destroy(qp->tokenized_fields);
194
+ if (def_fields) hs_safe_merge(all_fields, def_fields);
195
+ if (tkz_fields) hs_safe_merge(all_fields, tkz_fields);
196
+ qp->all_fields = all_fields;
197
+ qp->def_fields = def_fields ? def_fields : all_fields;
198
+ qp->tokenized_fields = tkz_fields ? tkz_fields : all_fields;
199
+ qp->fields_top->fields = def_fields;
200
+
201
+ qp->allow_any_fields = true;
202
+ qp->clean_str = true;
203
+ qp->handle_parse_errors = true;
204
+ /* handle options */
205
+ if (roptions != Qnil) {
206
+ if (Qnil != (rval = rb_hash_aref(roptions, sym_handle_parse_errors))) {
207
+ qp->handle_parse_errors = RTEST(rval);
208
+ }
209
+ if (Qnil != (rval = rb_hash_aref(roptions, sym_validate_fields))) {
210
+ qp->allow_any_fields = !RTEST(rval);
211
+ }
212
+ if (Qnil != (rval = rb_hash_aref(roptions, sym_wild_card_downcase))) {
213
+ qp->wild_lower = RTEST(rval);
214
+ }
215
+ if (Qnil != (rval = rb_hash_aref(roptions, sym_or_default))) {
216
+ qp->or_default = RTEST(rval);
217
+ }
218
+ if (Qnil != (rval = rb_hash_aref(roptions, sym_default_slop))) {
219
+ qp->def_slop = FIX2INT(rval);
220
+ }
221
+ if (Qnil != (rval = rb_hash_aref(roptions, sym_clean_string))) {
222
+ qp->clean_str = RTEST(rval);
223
+ }
224
+ if (Qnil != (rval = rb_hash_aref(roptions, sym_max_clauses))) {
225
+ qp->max_clauses = FIX2INT(rval);
226
+ }
227
+ if (Qnil != (rval = rb_hash_aref(roptions, sym_use_keywords))) {
228
+ qp->use_keywords = RTEST(rval);
229
+ }
230
+ if (Qnil != (rval = rb_hash_aref(roptions, sym_use_typed_range_query))){
231
+ qp->use_typed_range_query = RTEST(rval);
232
+ }
233
+ }
234
+ Frt_Wrap_Struct(self, frb_qp_mark, frb_qp_free, qp);
235
+ object_add(qp, self);
236
+ return self;
237
+ }
238
+
239
+ #define GET_QP FrtQParser *qp = (FrtQParser *)DATA_PTR(self)
240
+ /*
241
+ * call-seq:
242
+ * query_parser.parse(query_string) -> Query
243
+ *
244
+ * Parse a query string returning a Query object if parsing was successful.
245
+ * Will raise a QueryParseException if unsuccessful.
246
+ */
247
+ static VALUE
248
+ frb_qp_parse(VALUE self, VALUE rstr)
249
+ {
250
+ const char *volatile msg = NULL;
251
+ volatile VALUE rq;
252
+ GET_QP;
253
+ FrtQuery *q;
254
+ rstr = rb_obj_as_string(rstr);
255
+ FRT_TRY
256
+ q = qp_parse(qp, rs2s(rstr));
257
+ rq = frb_get_q(q);
258
+ break;
259
+ default:
260
+ msg = xcontext.msg;
261
+ FRT_HANDLED();
262
+ FRT_XENDTRY
263
+ if (msg) {
264
+ rb_raise(cQueryParseException, "%s", msg);
265
+ }
266
+ return rq;
267
+ }
268
+
269
+ /*
270
+ * call-seq:
271
+ * query_parser.fields -> Array of Symbols
272
+ *
273
+ * Returns the list of all fields that the QueryParser knows about.
274
+ */
275
+ static VALUE
276
+ frb_qp_get_fields(VALUE self)
277
+ {
278
+ GET_QP;
279
+ FrtHashSet *fields = qp->all_fields;
280
+ FrtHashSetEntry *hse;
281
+ VALUE rfields = rb_ary_new();
282
+
283
+ for (hse = fields->first; hse; hse = hse->next) {
284
+ rb_ary_push(rfields, ID2SYM(rb_intern((char *)hse->elem)));
285
+ }
286
+
287
+ return rfields;
288
+ }
289
+
290
+ /*
291
+ * call-seq:
292
+ * query_parser.fields = fields -> self
293
+ *
294
+ * Set the list of fields. These fields are expanded for searches on "*".
295
+ */
296
+ static VALUE
297
+ frb_qp_set_fields(VALUE self, VALUE rfields)
298
+ {
299
+ GET_QP;
300
+ FrtHashSet *fields = frb_get_fields(rfields, NULL);
301
+
302
+ /* if def_fields == all_fields then we need to replace both */
303
+ if (qp->def_fields == qp->all_fields) qp->def_fields = NULL;
304
+ if (qp->tokenized_fields == qp->all_fields) qp->tokenized_fields = NULL;
305
+
306
+ if (fields == NULL) {
307
+ fields = frt_hs_new_ptr(NULL);
308
+ }
309
+
310
+ /* make sure all the fields in tokenized fields are contained in
311
+ * all_fields */
312
+ if (qp->tokenized_fields) hs_safe_merge(fields, qp->tokenized_fields);
313
+
314
+ /* delete old fields set */
315
+ assert(qp->all_fields->free_elem_i == frt_dummy_free);
316
+ frt_hs_destroy(qp->all_fields);
317
+
318
+ /* add the new fields set and add to def_fields if necessary */
319
+ qp->all_fields = fields;
320
+ if (qp->def_fields == NULL) {
321
+ qp->def_fields = fields;
322
+ qp->fields_top->fields = fields;
323
+ }
324
+ if (qp->tokenized_fields == NULL) qp->tokenized_fields = fields;
325
+
326
+ return self;
327
+ }
328
+
329
+ /*
330
+ * call-seq:
331
+ * query_parser.tokenized_fields -> Array of Symbols
332
+ *
333
+ * Returns the list of all tokenized_fields that the QueryParser knows about.
334
+ */
335
+ static VALUE
336
+ frb_qp_get_tkz_fields(VALUE self)
337
+ {
338
+ GET_QP;
339
+ FrtHashSet *fields = qp->tokenized_fields;
340
+ if (fields) {
341
+ VALUE rfields = rb_ary_new();
342
+ FrtHashSetEntry *hse;
343
+
344
+ for (hse = fields->first; hse; hse = hse->next) {
345
+ rb_ary_push(rfields, ID2SYM(rb_intern((char *)hse->elem)));
346
+ }
347
+
348
+ return rfields;
349
+ }
350
+ else {
351
+ return Qnil;
352
+ }
353
+ }
354
+
355
+ /*
356
+ * call-seq:
357
+ * query_parser.tokenized_fields = fields -> self
358
+ *
359
+ * Set the list of tokenized_fields. These tokenized_fields are tokenized in
360
+ * the queries. If this is set to Qnil then all fields will be tokenized.
361
+ */
362
+ static VALUE
363
+ frb_qp_set_tkz_fields(VALUE self, VALUE rfields)
364
+ {
365
+ GET_QP;
366
+ if (qp->tokenized_fields != qp->all_fields) {
367
+ frt_hs_destroy(qp->tokenized_fields);
368
+ }
369
+ qp->tokenized_fields = frb_get_fields(rfields, NULL);
370
+ return self;
371
+ }
372
+
373
+ /****************************************************************************
374
+ *
375
+ * Init function
376
+ *
377
+ ****************************************************************************/
378
+
379
+ /* rdoc hack
380
+ extern VALUE mFerret = rb_define_module("Ferret");
381
+ extern VALUE cQueryParser = rb_define_module_under(mFerret, "QueryParser");
382
+ */
383
+
384
+ /*
385
+ * Document-class: Ferret::QueryParser::QueryParseException
386
+ *
387
+ * == Summary
388
+ *
389
+ * Exception raised when there is an error parsing the query string passed to
390
+ * QueryParser.
391
+ */
392
+ void
393
+ Init_QueryParseException(void)
394
+ {
395
+ cQueryParseException = rb_define_class_under(cQueryParser,
396
+ "QueryParseException",
397
+ rb_eStandardError);
398
+ }
399
+
400
+ /*
401
+ * Document-class: Ferret::QueryParser
402
+ *
403
+ * == Summary
404
+ *
405
+ * The QueryParser is used to transform user submitted query strings into
406
+ * QueryObjects. Ferret using its own Query Language known from now on as
407
+ * Ferret Query Language or FQL.
408
+ *
409
+ * == Ferret Query Language
410
+ *
411
+ * === Preamble
412
+ *
413
+ * The following characters are special characters in FQL;
414
+ *
415
+ * :, (, ), [, ], {, }, !, +, ", ~, ^, -, |, <, >, =, *, ?, \
416
+ *
417
+ * If you want to use one of these characters in one of your terms you need
418
+ * to escape it with a \ character. \ escapes itself. The exception to this
419
+ * rule is within Phrases which a strings surrounded by double quotes (and
420
+ * will be explained further bellow in the section on PhraseQueries). In
421
+ * Phrases, only ", | and <> have special meaning and need to be escaped if
422
+ * you want the literal value. <> is escaped \<\>.
423
+ *
424
+ * In the following examples I have only written the query string. This would
425
+ * be parse like;
426
+ *
427
+ * query = query_parser.parse("pet:(dog AND cat)")
428
+ * puts query # => "+pet:dog +pet:cat"
429
+ *
430
+ * === TermQuery
431
+ *
432
+ * A term query is the most basic query of all and is what most of the other
433
+ * queries are built upon. The term consists of a single word. eg;
434
+ *
435
+ * 'term'
436
+ *
437
+ * Note that the analyzer will be run on the term and if it splits the term
438
+ * in two then it will be turned into a phrase query. For example, with the
439
+ * plain Ferret::Analysis::Analyzer, the following;
440
+ *
441
+ * 'dave12balmain'
442
+ *
443
+ * is equivalent to;
444
+ *
445
+ * '"dave balmain"'
446
+ *
447
+ * Which we will explain now...
448
+ *
449
+ * === PhraseQuery
450
+ *
451
+ * A phrase query is a string of terms surrounded by double quotes. For
452
+ * example you could write;
453
+ *
454
+ * '"quick brown fox"'
455
+ *
456
+ * But if a "fast" fox is just as good as a quick one you could use the |
457
+ * character to specify alternate terms.
458
+ *
459
+ * '"quick|speedy|fast brown fox"'
460
+ *
461
+ * What if we don't care what colour the fox is. We can use the <> to specify
462
+ * a place setter. eg;
463
+ *
464
+ * '"quick|speedy|fast <> fox"'
465
+ *
466
+ * This will match any word in between quick and fox. Alternatively we could
467
+ * set the "slop" for the phrase which allows a certain variation in the
468
+ * match of the phrase. The slop for a phrase is an integer indicating how
469
+ * many positions you are allowed to move the terms to get a match. Read more
470
+ * about the slop factor in Ferret::Search::PhraseQuery. To set the slop
471
+ * factor for a phrase you can type;
472
+ *
473
+ * '"big house"~2'
474
+ *
475
+ * This would match "big house", "big red house", "big red brick house" and
476
+ * even "house big". That's right, you don't need to have th terms in order
477
+ * if you allow some slop in your phrases. (See Ferret::Search::Spans if you
478
+ * need a phrase type query with ordered terms.)
479
+ *
480
+ * These basic queries will be run on the default field which is set when you
481
+ * create the query_parser. But what if you want to search a different field.
482
+ * You'll be needing a ...
483
+ *
484
+ * === FieldQuery
485
+ *
486
+ * A field query is any field prefixed by <fieldname>:. For example, to
487
+ * search for all instances of the term "ski" in field "sport", you'd write;
488
+ *
489
+ * 'sport:ski'
490
+ * Or we can apply a field to phrase;
491
+ *
492
+ * 'sport:"skiing is fun"'
493
+ *
494
+ * Now we have a few types of queries, we'll be needing to glue them together
495
+ * with a ...
496
+ *
497
+ * === BooleanQuery
498
+ *
499
+ * There are a couple of ways of writing boolean queries. Firstly you can
500
+ * specify which terms are required, optional or required not to exist (not).
501
+ *
502
+ * * '+' or "REQ" can be used to indicate a required query. "REQ" must be
503
+ * surrounded by white space.
504
+ * * '-', '!' or "NOT" are used to indicate query that is required to be
505
+ * false. "NOT" must be surrounded by white space.
506
+ * * all other queries are optional if the above symbols are used.
507
+ *
508
+ * Some examples;
509
+ *
510
+ * '+sport:ski -sport:snowboard sport:toboggan'
511
+ * '+ingredient:chocolate +ingredient:strawberries -ingredient:wheat'
512
+ *
513
+ * You may also use the boolean operators "AND", "&&", "OR" and "||". eg;
514
+ *
515
+ * 'sport:ski AND NOT sport:snowboard OR sport:toboggan'
516
+ * 'ingredient:chocolate AND ingredient:strawberries AND NOT ingredient:wheat'
517
+ *
518
+ * You can set the default operator when you create the query parse.
519
+ *
520
+ * === RangeQuery
521
+ *
522
+ * A range query finds all documents with terms between the two query terms.
523
+ * This can be very useful in particular for dates. eg;
524
+ *
525
+ * 'date:[20050725 20050905]' # all dates >= 20050725 and <= 20050905
526
+ * 'date:[20050725 20050905}' # all dates >= 20050725 and < 20050905
527
+ * 'date:{20050725 20050905]' # all dates > 20050725 and <= 20050905
528
+ * 'date:{20050725 20050905}' # all dates > 20050725 and < 20050905
529
+ *
530
+ * You can also do open ended queries like this;
531
+ *
532
+ * 'date:[20050725>' # all dates >= 20050725
533
+ * 'date:{20050725>' # all dates > 20050725
534
+ * 'date:<20050905]' # all dates <= 20050905
535
+ * 'date:<20050905}' # all dates < 20050905
536
+ *
537
+ * Or like this;
538
+ *
539
+ * 'date: >= 20050725'
540
+ * 'date: > 20050725'
541
+ * 'date: <= 20050905'
542
+ * 'date: < 20050905'
543
+ *
544
+ * If you prefer the above style you could use a boolean query but like this;
545
+ *
546
+ * 'date:( >= 20050725 AND <= 20050905)'
547
+ *
548
+ * But rangequery only solution shown first will be faster.
549
+ *
550
+ * === WildQuery
551
+ *
552
+ * A wild query is a query using the pattern matching characters * and ?. *
553
+ * matches 0 or more characters while ? matches a single character. This type
554
+ * of query can be really useful for matching hierarchical categories for
555
+ * example. Let's say we had this structure;
556
+ *
557
+ * /sport/skiing
558
+ * /sport/cycling
559
+ * /coding1/ruby
560
+ * /coding1/c
561
+ * /coding2/python
562
+ * /coding2/perl
563
+ *
564
+ * If you wanted all categories with programming languages you could use the
565
+ * query;
566
+ *
567
+ * 'category:/coding?/?*'
568
+ *
569
+ * Note that this query can be quite expensive if not used carefully. In the
570
+ * example above there would be no problem but you should be careful not use
571
+ * the wild characters at the beginning of the query as it'll have to iterate
572
+ * through every term in that field. Having said that, some fields like the
573
+ * category field above will only have a small number of distinct fields so
574
+ * this could be okay.
575
+ *
576
+ * === FuzzyQuery
577
+ *
578
+ * This is like the sloppy phrase query above, except you are now adding slop
579
+ * to a term. Basically it measures the Levenshtein distance between two
580
+ * terms and if the value is below the slop threshold the term is a match.
581
+ * This time though the slop must be a float between 0 and 1.0, 1.0 being a
582
+ * perfect match and 0 being far from a match. The default is set to 0.5 so
583
+ * you don't need to give a slop value if you don't want to. You can set the
584
+ * default in the Ferret::Search::FuzzyQuery class. Here are a couple of
585
+ * examples;
586
+ *
587
+ * 'content:ferret~'
588
+ * 'content:Ostralya~0.4'
589
+ *
590
+ * Note that this query can be quite expensive. If you'd like to use this
591
+ * query, you may want to set a minimum prefix length in the FuzzyQuery
592
+ * class. This can substantially reduce the number of terms that the query
593
+ * will iterate over.
594
+ *
595
+ */
596
+ void
597
+ Init_QueryParser(void)
598
+ {
599
+ /* hash keys */
600
+ sym_wild_card_downcase = ID2SYM(rb_intern("wild_card_downcase"));
601
+ sym_fields = ID2SYM(rb_intern("fields"));
602
+ sym_all_fields = ID2SYM(rb_intern("all_fields"));
603
+ sym_tkz_fields = ID2SYM(rb_intern("tokenized_fields"));
604
+ sym_default_field = ID2SYM(rb_intern("default_field"));
605
+ sym_validate_fields = ID2SYM(rb_intern("validate_fields"));
606
+ sym_or_default = ID2SYM(rb_intern("or_default"));
607
+ sym_default_slop = ID2SYM(rb_intern("default_slop"));
608
+ sym_handle_parse_errors = ID2SYM(rb_intern("handle_parse_errors"));
609
+ sym_clean_string = ID2SYM(rb_intern("clean_string"));
610
+ sym_max_clauses = ID2SYM(rb_intern("max_clauses"));
611
+ sym_use_keywords = ID2SYM(rb_intern("use_keywords"));
612
+ sym_use_typed_range_query = ID2SYM(rb_intern("use_typed_range_query"));
613
+
614
+ /* QueryParser */
615
+ cQueryParser = rb_define_class_under(mFerret, "QueryParser", rb_cObject);
616
+ rb_define_alloc_func(cQueryParser, frb_data_alloc);
617
+
618
+ rb_define_method(cQueryParser, "initialize", frb_qp_init, -1);
619
+ rb_define_method(cQueryParser, "parse", frb_qp_parse, 1);
620
+ rb_define_method(cQueryParser, "fields", frb_qp_get_fields, 0);
621
+ rb_define_method(cQueryParser, "fields=", frb_qp_set_fields, 1);
622
+ rb_define_method(cQueryParser, "tokenized_fields",
623
+ frb_qp_get_tkz_fields, 0);
624
+ rb_define_method(cQueryParser, "tokenized_fields=",
625
+ frb_qp_set_tkz_fields, 1);
626
+
627
+ Init_QueryParseException();
628
+ }
629
+