sdsykes-ferret 0.11.6.19

Sign up to get free protection for your applications and to get access to all the features.
Files changed (195) hide show
  1. data/CHANGELOG +24 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README +102 -0
  4. data/Rakefile +338 -0
  5. data/TODO +17 -0
  6. data/TUTORIAL +231 -0
  7. data/bin/ferret-browser +79 -0
  8. data/ext/analysis.c +1555 -0
  9. data/ext/analysis.h +219 -0
  10. data/ext/api.c +69 -0
  11. data/ext/api.h +27 -0
  12. data/ext/array.c +123 -0
  13. data/ext/array.h +53 -0
  14. data/ext/bitvector.c +540 -0
  15. data/ext/bitvector.h +272 -0
  16. data/ext/compound_io.c +383 -0
  17. data/ext/config.h +42 -0
  18. data/ext/document.c +156 -0
  19. data/ext/document.h +53 -0
  20. data/ext/except.c +120 -0
  21. data/ext/except.h +168 -0
  22. data/ext/extconf.rb +14 -0
  23. data/ext/ferret.c +402 -0
  24. data/ext/ferret.h +91 -0
  25. data/ext/filter.c +156 -0
  26. data/ext/fs_store.c +483 -0
  27. data/ext/global.c +418 -0
  28. data/ext/global.h +117 -0
  29. data/ext/hash.c +567 -0
  30. data/ext/hash.h +473 -0
  31. data/ext/hashset.c +170 -0
  32. data/ext/hashset.h +187 -0
  33. data/ext/header.h +58 -0
  34. data/ext/helper.c +62 -0
  35. data/ext/helper.h +13 -0
  36. data/ext/inc/lang.h +48 -0
  37. data/ext/inc/threading.h +31 -0
  38. data/ext/index.c +6425 -0
  39. data/ext/index.h +961 -0
  40. data/ext/lang.h +66 -0
  41. data/ext/libstemmer.c +92 -0
  42. data/ext/libstemmer.h +79 -0
  43. data/ext/mempool.c +87 -0
  44. data/ext/mempool.h +35 -0
  45. data/ext/modules.h +162 -0
  46. data/ext/multimapper.c +310 -0
  47. data/ext/multimapper.h +51 -0
  48. data/ext/posh.c +1006 -0
  49. data/ext/posh.h +1007 -0
  50. data/ext/priorityqueue.c +151 -0
  51. data/ext/priorityqueue.h +143 -0
  52. data/ext/q_boolean.c +1608 -0
  53. data/ext/q_const_score.c +161 -0
  54. data/ext/q_filtered_query.c +209 -0
  55. data/ext/q_fuzzy.c +268 -0
  56. data/ext/q_match_all.c +148 -0
  57. data/ext/q_multi_term.c +677 -0
  58. data/ext/q_parser.c +2825 -0
  59. data/ext/q_phrase.c +1126 -0
  60. data/ext/q_prefix.c +100 -0
  61. data/ext/q_range.c +350 -0
  62. data/ext/q_span.c +2402 -0
  63. data/ext/q_term.c +337 -0
  64. data/ext/q_wildcard.c +171 -0
  65. data/ext/r_analysis.c +2575 -0
  66. data/ext/r_index.c +3472 -0
  67. data/ext/r_qparser.c +585 -0
  68. data/ext/r_search.c +4105 -0
  69. data/ext/r_store.c +513 -0
  70. data/ext/r_utils.c +963 -0
  71. data/ext/ram_store.c +471 -0
  72. data/ext/search.c +1741 -0
  73. data/ext/search.h +885 -0
  74. data/ext/similarity.c +150 -0
  75. data/ext/similarity.h +82 -0
  76. data/ext/sort.c +983 -0
  77. data/ext/stem_ISO_8859_1_danish.c +338 -0
  78. data/ext/stem_ISO_8859_1_danish.h +16 -0
  79. data/ext/stem_ISO_8859_1_dutch.c +635 -0
  80. data/ext/stem_ISO_8859_1_dutch.h +16 -0
  81. data/ext/stem_ISO_8859_1_english.c +1156 -0
  82. data/ext/stem_ISO_8859_1_english.h +16 -0
  83. data/ext/stem_ISO_8859_1_finnish.c +792 -0
  84. data/ext/stem_ISO_8859_1_finnish.h +16 -0
  85. data/ext/stem_ISO_8859_1_french.c +1276 -0
  86. data/ext/stem_ISO_8859_1_french.h +16 -0
  87. data/ext/stem_ISO_8859_1_german.c +512 -0
  88. data/ext/stem_ISO_8859_1_german.h +16 -0
  89. data/ext/stem_ISO_8859_1_italian.c +1091 -0
  90. data/ext/stem_ISO_8859_1_italian.h +16 -0
  91. data/ext/stem_ISO_8859_1_norwegian.c +296 -0
  92. data/ext/stem_ISO_8859_1_norwegian.h +16 -0
  93. data/ext/stem_ISO_8859_1_porter.c +776 -0
  94. data/ext/stem_ISO_8859_1_porter.h +16 -0
  95. data/ext/stem_ISO_8859_1_portuguese.c +1035 -0
  96. data/ext/stem_ISO_8859_1_portuguese.h +16 -0
  97. data/ext/stem_ISO_8859_1_spanish.c +1119 -0
  98. data/ext/stem_ISO_8859_1_spanish.h +16 -0
  99. data/ext/stem_ISO_8859_1_swedish.c +307 -0
  100. data/ext/stem_ISO_8859_1_swedish.h +16 -0
  101. data/ext/stem_KOI8_R_russian.c +701 -0
  102. data/ext/stem_KOI8_R_russian.h +16 -0
  103. data/ext/stem_UTF_8_danish.c +344 -0
  104. data/ext/stem_UTF_8_danish.h +16 -0
  105. data/ext/stem_UTF_8_dutch.c +653 -0
  106. data/ext/stem_UTF_8_dutch.h +16 -0
  107. data/ext/stem_UTF_8_english.c +1176 -0
  108. data/ext/stem_UTF_8_english.h +16 -0
  109. data/ext/stem_UTF_8_finnish.c +808 -0
  110. data/ext/stem_UTF_8_finnish.h +16 -0
  111. data/ext/stem_UTF_8_french.c +1296 -0
  112. data/ext/stem_UTF_8_french.h +16 -0
  113. data/ext/stem_UTF_8_german.c +526 -0
  114. data/ext/stem_UTF_8_german.h +16 -0
  115. data/ext/stem_UTF_8_italian.c +1113 -0
  116. data/ext/stem_UTF_8_italian.h +16 -0
  117. data/ext/stem_UTF_8_norwegian.c +302 -0
  118. data/ext/stem_UTF_8_norwegian.h +16 -0
  119. data/ext/stem_UTF_8_porter.c +794 -0
  120. data/ext/stem_UTF_8_porter.h +16 -0
  121. data/ext/stem_UTF_8_portuguese.c +1055 -0
  122. data/ext/stem_UTF_8_portuguese.h +16 -0
  123. data/ext/stem_UTF_8_russian.c +709 -0
  124. data/ext/stem_UTF_8_russian.h +16 -0
  125. data/ext/stem_UTF_8_spanish.c +1137 -0
  126. data/ext/stem_UTF_8_spanish.h +16 -0
  127. data/ext/stem_UTF_8_swedish.c +313 -0
  128. data/ext/stem_UTF_8_swedish.h +16 -0
  129. data/ext/stopwords.c +401 -0
  130. data/ext/store.c +692 -0
  131. data/ext/store.h +777 -0
  132. data/ext/term_vectors.c +352 -0
  133. data/ext/threading.h +31 -0
  134. data/ext/utilities.c +446 -0
  135. data/ext/win32.h +54 -0
  136. data/lib/ferret.rb +29 -0
  137. data/lib/ferret/browser.rb +246 -0
  138. data/lib/ferret/browser/s/global.js +192 -0
  139. data/lib/ferret/browser/s/style.css +148 -0
  140. data/lib/ferret/browser/views/document/list.rhtml +49 -0
  141. data/lib/ferret/browser/views/document/show.rhtml +27 -0
  142. data/lib/ferret/browser/views/error/index.rhtml +7 -0
  143. data/lib/ferret/browser/views/help/index.rhtml +8 -0
  144. data/lib/ferret/browser/views/home/index.rhtml +29 -0
  145. data/lib/ferret/browser/views/layout.rhtml +22 -0
  146. data/lib/ferret/browser/views/term-vector/index.rhtml +4 -0
  147. data/lib/ferret/browser/views/term/index.rhtml +199 -0
  148. data/lib/ferret/browser/views/term/termdocs.rhtml +1 -0
  149. data/lib/ferret/browser/webrick.rb +14 -0
  150. data/lib/ferret/document.rb +130 -0
  151. data/lib/ferret/field_infos.rb +44 -0
  152. data/lib/ferret/index.rb +786 -0
  153. data/lib/ferret/number_tools.rb +157 -0
  154. data/lib/ferret_version.rb +3 -0
  155. data/setup.rb +1555 -0
  156. data/test/test_all.rb +5 -0
  157. data/test/test_helper.rb +24 -0
  158. data/test/threading/number_to_spoken.rb +132 -0
  159. data/test/threading/thread_safety_index_test.rb +79 -0
  160. data/test/threading/thread_safety_read_write_test.rb +76 -0
  161. data/test/threading/thread_safety_test.rb +133 -0
  162. data/test/unit/analysis/tc_analyzer.rb +548 -0
  163. data/test/unit/analysis/tc_token_stream.rb +646 -0
  164. data/test/unit/index/tc_index.rb +762 -0
  165. data/test/unit/index/tc_index_reader.rb +699 -0
  166. data/test/unit/index/tc_index_writer.rb +437 -0
  167. data/test/unit/index/th_doc.rb +315 -0
  168. data/test/unit/largefile/tc_largefile.rb +46 -0
  169. data/test/unit/query_parser/tc_query_parser.rb +238 -0
  170. data/test/unit/search/tc_filter.rb +135 -0
  171. data/test/unit/search/tc_fuzzy_query.rb +147 -0
  172. data/test/unit/search/tc_index_searcher.rb +61 -0
  173. data/test/unit/search/tc_multi_searcher.rb +128 -0
  174. data/test/unit/search/tc_multiple_search_requests.rb +58 -0
  175. data/test/unit/search/tc_search_and_sort.rb +179 -0
  176. data/test/unit/search/tc_sort.rb +49 -0
  177. data/test/unit/search/tc_sort_field.rb +27 -0
  178. data/test/unit/search/tc_spans.rb +190 -0
  179. data/test/unit/search/tm_searcher.rb +384 -0
  180. data/test/unit/store/tc_fs_store.rb +77 -0
  181. data/test/unit/store/tc_ram_store.rb +35 -0
  182. data/test/unit/store/tm_store.rb +34 -0
  183. data/test/unit/store/tm_store_lock.rb +68 -0
  184. data/test/unit/tc_document.rb +81 -0
  185. data/test/unit/ts_analysis.rb +2 -0
  186. data/test/unit/ts_index.rb +2 -0
  187. data/test/unit/ts_largefile.rb +4 -0
  188. data/test/unit/ts_query_parser.rb +2 -0
  189. data/test/unit/ts_search.rb +2 -0
  190. data/test/unit/ts_store.rb +2 -0
  191. data/test/unit/ts_utils.rb +2 -0
  192. data/test/unit/utils/tc_bit_vector.rb +295 -0
  193. data/test/unit/utils/tc_number_tools.rb +117 -0
  194. data/test/unit/utils/tc_priority_queue.rb +106 -0
  195. metadata +285 -0
@@ -0,0 +1,27 @@
1
+ require File.dirname(__FILE__) + "/../../test_helper"
2
+
3
+ class SortFieldTest < Test::Unit::TestCase
4
+ include Ferret::Search
5
+
6
+ def test_field_score()
7
+ fs = SortField::SCORE
8
+ assert_equal(:score, fs.type)
9
+ assert_nil(fs.name)
10
+ assert(!fs.reverse?, "SCORE_ID should not be reverse")
11
+ assert_nil(fs.comparator)
12
+ end
13
+
14
+ def test_field_doc()
15
+ fs = SortField::DOC_ID
16
+ assert_equal(:doc_id, fs.type)
17
+ assert_nil(fs.name)
18
+ assert(!fs.reverse?, "DOC_ID should be reverse")
19
+ assert_nil(fs.comparator)
20
+ end
21
+
22
+ def test_error_raised()
23
+ assert_raise(ArgumentError) {
24
+ fs = SortField.new(nil, :type => :integer)
25
+ }
26
+ end
27
+ end
@@ -0,0 +1,190 @@
1
+ require File.dirname(__FILE__) + "/../../test_helper"
2
+
3
+
4
+ class SpansBasicTest < Test::Unit::TestCase
5
+ include Ferret::Search
6
+ include Ferret::Store
7
+ include Ferret::Index
8
+ include Ferret::Search::Spans
9
+ include Ferret::Analysis
10
+
11
+ def setup()
12
+ @dir = RAMDirectory.new
13
+ iw = IndexWriter.new(:dir => @dir,
14
+ :analyzer => WhiteSpaceAnalyzer.new(),
15
+ :create => true)
16
+ [
17
+ "start finish one two three four five six seven",
18
+ "start one finish two three four five six seven",
19
+ "start one two finish three four five six seven flip",
20
+ "start one two three finish four five six seven",
21
+ "start one two three four finish five six seven",
22
+ "start one two three four five finish six seven",
23
+ "start one two three four five six finish seven eight",
24
+ "start one two three four five six seven finish eight nine",
25
+ "start one two three four five six finish seven eight",
26
+ "start one two three four five finish six seven",
27
+ "start one two three four finish five six seven",
28
+ "start one two three finish four five six seven",
29
+ "start one two finish three four five six seven flop",
30
+ "start one finish two three four five six seven",
31
+ "start finish one two three four five six seven",
32
+ "start start one two three four five six seven",
33
+ "finish start one two three four five six seven",
34
+ "finish one start two three four five six seven toot",
35
+ "finish one two start three four five six seven",
36
+ "finish one two three start four five six seven",
37
+ "finish one two three four start five six seven",
38
+ "finish one two three four five start six seven",
39
+ "finish one two three four five six start seven eight",
40
+ "finish one two three four five six seven start eight nine",
41
+ "finish one two three four five six start seven eight",
42
+ "finish one two three four five start six seven",
43
+ "finish one two three four start five six seven",
44
+ "finish one two three start four five six seven",
45
+ "finish one two start three four five six seven",
46
+ "finish one start two three four five six seven",
47
+ "finish start one two three four five six seven"
48
+ ].each { |line| iw << {:field => line} }
49
+
50
+ iw.close()
51
+
52
+ @searcher = Searcher.new(@dir)
53
+ end
54
+
55
+ def teardown()
56
+ @searcher.close
57
+ @dir.close
58
+ end
59
+
60
+ def number_split(i)
61
+ if (i < 10)
62
+ return "<#{i}>"
63
+ elsif (i < 100)
64
+ return "<#{((i/10)*10)}> <#{i%10}>"
65
+ else
66
+ return "<#{((i/100)*100)}> <#{(((i%100)/10)*10)}> <#{i%10}>"
67
+ end
68
+ end
69
+
70
+ def check_hits(query, expected, test_explain = false, top=nil)
71
+ top_docs = @searcher.search(query, {:limit => expected.length + 1})
72
+ assert_equal(expected.length, top_docs.hits.size)
73
+ assert_equal(top, top_docs.hits[0].doc) if top
74
+ assert_equal(expected.length, top_docs.total_hits)
75
+ top_docs.hits.each do |hit|
76
+ assert(expected.include?(hit.doc),
77
+ "#{hit.doc} was found unexpectedly")
78
+ if test_explain
79
+ assert(hit.score.approx_eql?(@searcher.explain(query, hit.doc).score),
80
+ "Scores(#{hit.score} != " +
81
+ "#{@searcher.explain(query, hit.doc).score})")
82
+ end
83
+ end
84
+ end
85
+
86
+ def test_span_term_query()
87
+ tq = SpanTermQuery.new(:field, "nine")
88
+ check_hits(tq, [7,23], true)
89
+ tq = SpanTermQuery.new(:field, "eight")
90
+ check_hits(tq, [6,7,8,22,23,24])
91
+ end
92
+
93
+ def test_span_multi_term_query()
94
+ tq = SpanMultiTermQuery.new(:field, ["eight", "nine"])
95
+ check_hits(tq, [6,7,8,22,23,24], true)
96
+ tq = SpanMultiTermQuery.new(:field, ["flip", "flop", "toot", "nine"])
97
+ check_hits(tq, [2,7,12,17,23])
98
+ end
99
+
100
+ def test_span_prefix_query()
101
+ tq = SpanPrefixQuery.new(:field, "fl")
102
+ check_hits(tq, [2, 12], true)
103
+ end
104
+
105
+ def test_span_near_query()
106
+ tq1 = SpanTermQuery.new(:field, "start")
107
+ tq2 = SpanTermQuery.new(:field, "finish")
108
+ q = SpanNearQuery.new(:clauses => [tq1, tq2], :in_order => true)
109
+ check_hits(q, [0,14], true)
110
+ q = SpanNearQuery.new()
111
+ q << tq1 << tq2
112
+ check_hits(q, [0,14,16,30], true)
113
+ q = SpanNearQuery.new(:clauses => [tq1, tq2],
114
+ :slop => 1, :in_order => true)
115
+ check_hits(q, [0,1,13,14])
116
+ q = SpanNearQuery.new(:clauses => [tq1, tq2], :slop => 1)
117
+ check_hits(q, [0,1,13,14,16,17,29,30])
118
+ q = SpanNearQuery.new(:clauses => [tq1, tq2],
119
+ :slop => 4, :in_order => true)
120
+ check_hits(q, [0,1,2,3,4,10,11,12,13,14])
121
+ q = SpanNearQuery.new(:clauses => [tq1, tq2], :slop => 4)
122
+ check_hits(q, [0,1,2,3,4,10,11,12,13,14,16,17,18,19,20,26,27,28,29,30])
123
+ q = SpanNearQuery.new(:clauses => [
124
+ SpanPrefixQuery.new(:field, 'se'),
125
+ SpanPrefixQuery.new(:field, 'fl')], :slop => 0)
126
+ check_hits(q, [2, 12], true)
127
+ end
128
+
129
+ def test_span_not_query()
130
+ tq1 = SpanTermQuery.new(:field, "start")
131
+ tq2 = SpanTermQuery.new(:field, "finish")
132
+ tq3 = SpanTermQuery.new(:field, "two")
133
+ tq4 = SpanTermQuery.new(:field, "five")
134
+ nearq1 = SpanNearQuery.new(:clauses => [tq1, tq2],
135
+ :slop => 4, :in_order => true)
136
+ nearq2 = SpanNearQuery.new(:clauses => [tq3, tq4],
137
+ :slop => 4, :in_order => true)
138
+ q = SpanNotQuery.new(nearq1, nearq2)
139
+ check_hits(q, [0,1,13,14], true)
140
+ nearq1 = SpanNearQuery.new(:clauses => [tq1, tq2], :slop => 4)
141
+ q = SpanNotQuery.new(nearq1, nearq2)
142
+ check_hits(q, [0,1,13,14,16,17,29,30])
143
+ nearq1 = SpanNearQuery.new(:clauses => [tq1, tq3],
144
+ :slop => 4, :in_order => true)
145
+ nearq2 = SpanNearQuery.new(:clauses => [tq2, tq4], :slop => 8)
146
+ q = SpanNotQuery.new(nearq1, nearq2)
147
+ check_hits(q, [2,3,4,5,6,7,8,9,10,11,12,15])
148
+ end
149
+
150
+ def test_span_first_query()
151
+ finish_first = [16,17,18,19,20,21,22,23,24,25,26,27,28,29,30]
152
+ tq = SpanTermQuery.new(:field, "finish")
153
+ q = SpanFirstQuery.new(tq, 1)
154
+ check_hits(q, finish_first, true)
155
+ q = SpanFirstQuery.new(tq, 5)
156
+ check_hits(q, [0,1,2,3,11,12,13,14]+finish_first, false)
157
+ end
158
+
159
+ def test_span_or_query_query()
160
+ tq1 = SpanTermQuery.new(:field, "start")
161
+ tq2 = SpanTermQuery.new(:field, "finish")
162
+ tq3 = SpanTermQuery.new(:field, "five")
163
+ nearq1 = SpanNearQuery.new(:clauses => [tq1, tq2], :slop => 1,
164
+ :in_order => true)
165
+ nearq2 = SpanNearQuery.new(:clauses => [tq2, tq3], :slop => 0)
166
+ q = SpanOrQuery.new([nearq1, nearq2])
167
+ check_hits(q, [0,1,4,5,9,10,13,14], false)
168
+ nearq1 = SpanNearQuery.new(:clauses => [tq1, tq2], :slop => 0)
169
+ nearq2 = SpanNearQuery.new(:clauses => [tq2, tq3], :slop => 1)
170
+ q = SpanOrQuery.new([nearq1, nearq2])
171
+ check_hits(q, [0,3,4,5,6,8,9,10,11,14,16,30], false)
172
+ end
173
+
174
+ def test_span_prefix_query_max_terms
175
+ @dir = RAMDirectory.new
176
+ iw = IndexWriter.new(:dir => @dir,
177
+ :analyzer => WhiteSpaceAnalyzer.new())
178
+ 2000.times { |i| iw << {:field => "prefix#{i} term#{i}"} }
179
+ iw.close()
180
+ @searcher = Searcher.new(@dir)
181
+
182
+ pq = SpanPrefixQuery.new(:field, "prefix")
183
+ tq = SpanTermQuery.new(:field, "term1500")
184
+ q = SpanNearQuery.new(:clauses => [pq, tq], :in_order => true)
185
+ check_hits(q, [], false)
186
+ pq = SpanPrefixQuery.new(:field, "prefix", 2000)
187
+ q = SpanNearQuery.new(:clauses => [pq, tq], :in_order => true)
188
+ check_hits(q, [1500], false)
189
+ end
190
+ end
@@ -0,0 +1,384 @@
1
+ require File.dirname(__FILE__) + "/../../test_helper"
2
+
3
+ module SearcherTests
4
+ include Ferret::Search
5
+
6
+ def test_term_query
7
+ tq = TermQuery.new(:field, "word2")
8
+ tq.boost = 100
9
+ check_hits(tq, [1,4,8])
10
+ #puts @searcher.explain(tq, 1)
11
+ #puts @searcher.explain(tq, 4)
12
+ #puts @searcher.explain(tq, 8)
13
+
14
+ tq = TermQuery.new(:field, "2342")
15
+ check_hits(tq, [])
16
+
17
+ tq = TermQuery.new(:field, "")
18
+ check_hits(tq, [])
19
+
20
+ tq = TermQuery.new(:field, "word1")
21
+ top_docs = @searcher.search(tq)
22
+ assert_equal(@searcher.max_doc, top_docs.total_hits)
23
+ assert_equal(10, top_docs.hits.size)
24
+ top_docs = @searcher.search(tq, {:limit => 20})
25
+ assert_equal(@searcher.max_doc, top_docs.hits.size)
26
+
27
+ assert_equal([Ferret::Term.new(:field, "word1")], tq.terms(@searcher))
28
+ end
29
+
30
+ def check_docs(query, options, expected=[])
31
+ top_docs = @searcher.search(query, options)
32
+ docs = top_docs.hits
33
+ assert_equal(expected.length, docs.length)
34
+ docs.length.times do |i|
35
+ assert_equal(expected[i], docs[i].doc)
36
+ end
37
+ end
38
+
39
+ def test_offset
40
+ tq = TermQuery.new(:field, "word1")
41
+ tq.boost = 100
42
+ top_docs = @searcher.search(tq, {:limit => 100})
43
+ expected = []
44
+ top_docs.hits.each do |sd|
45
+ expected << sd.doc
46
+ end
47
+
48
+ assert_raise(ArgumentError) { @searcher.search(tq, {:offset => -1}) }
49
+ assert_raise(ArgumentError) { @searcher.search(tq, {:limit => 0}) }
50
+ assert_raise(ArgumentError) { @searcher.search(tq, {:limit => -1}) }
51
+
52
+ check_docs(tq, {:limit => 8, :offset => 0}, expected[0,8])
53
+ check_docs(tq, {:limit => 3, :offset => 1}, expected[1,3])
54
+ check_docs(tq, {:limit => 6, :offset => 2}, expected[2,6])
55
+ check_docs(tq, {:limit => 2, :offset => expected.length}, [])
56
+ check_docs(tq, {:limit => 2, :offset => expected.length + 100}, [])
57
+ check_docs(tq, {:limit => :all}, expected)
58
+ check_docs(tq, {:limit => :all, :offset => 2}, expected[2..-1])
59
+ end
60
+
61
+ def test_multi_term_query
62
+ mtq = MultiTermQuery.new(:field, :max_terms => 4, :min_score => 0.5)
63
+ check_hits(mtq, [])
64
+ assert_equal('""', mtq.to_s(:field))
65
+ assert_equal('field:""', mtq.to_s)
66
+
67
+ [
68
+ ["brown", 1.0, '"brown"'],
69
+ ["fox", 0.1, '"brown"'],
70
+ ["fox", 0.6, '"fox^0.6|brown"'],
71
+ ["fast", 50.0, '"fox^0.6|brown|fast^50.0"']
72
+ ].each do |term, boost, str|
73
+ mtq.add_term(term, boost)
74
+ assert_equal(str, mtq.to_s(:field))
75
+ assert_equal("field:#{str}", mtq.to_s())
76
+ end
77
+
78
+ mtq.boost = 80.1
79
+ assert_equal('field:"fox^0.6|brown|fast^50.0"^80.1', mtq.to_s())
80
+ mtq << "word1"
81
+ assert_equal('field:"fox^0.6|brown|word1|fast^50.0"^80.1', mtq.to_s())
82
+ mtq << "word2"
83
+ assert_equal('field:"brown|word1|word2|fast^50.0"^80.1', mtq.to_s())
84
+ mtq << "word3"
85
+ assert_equal('field:"brown|word1|word2|fast^50.0"^80.1', mtq.to_s())
86
+
87
+ terms = mtq.terms(@searcher)
88
+ assert(terms.index(Ferret::Term.new(:field, "brown")))
89
+ assert(terms.index(Ferret::Term.new(:field, "word1")))
90
+ assert(terms.index(Ferret::Term.new(:field, "word2")))
91
+ assert(terms.index(Ferret::Term.new(:field, "fast")))
92
+ end
93
+
94
+ def test_boolean_query
95
+ bq = BooleanQuery.new()
96
+ tq1 = TermQuery.new(:field, "word1")
97
+ tq2 = TermQuery.new(:field, "word3")
98
+ bq.add_query(tq1, :must)
99
+ bq.add_query(tq2, :must)
100
+ check_hits(bq, [2,3,6,8,11,14], 14)
101
+
102
+ tq3 = TermQuery.new(:field, "word2")
103
+ bq.add_query(tq3, :should)
104
+ check_hits(bq, [2,3,6,8,11,14], 8)
105
+
106
+ bq = BooleanQuery.new()
107
+ bq.add_query(tq2, :must)
108
+ bq.add_query(tq3, :must_not)
109
+ check_hits(bq, [2,3,6,11,14])
110
+
111
+ bq = BooleanQuery.new()
112
+ bq.add_query(tq2, :must_not)
113
+ check_hits(bq, [0,1,4,5,7,9,10,12,13,15,16,17])
114
+
115
+ bq = BooleanQuery.new()
116
+ bq.add_query(tq2, :should)
117
+ bq.add_query(tq3, :should)
118
+ check_hits(bq, [1,2,3,4,6,8,11,14])
119
+
120
+ bq = BooleanQuery.new()
121
+ bc1 = BooleanQuery::BooleanClause.new(tq2, :should)
122
+ bc2 = BooleanQuery::BooleanClause.new(tq3, :should)
123
+ bq << bc1
124
+ bq << bc2
125
+ check_hits(bq, [1,2,3,4,6,8,11,14])
126
+ end
127
+
128
+ def test_phrase_query()
129
+ pq = PhraseQuery.new(:field)
130
+ assert_equal("\"\"", pq.to_s(:field))
131
+ assert_equal("field:\"\"", pq.to_s)
132
+
133
+ pq << "quick" << "brown" << "fox"
134
+ check_hits(pq, [1])
135
+
136
+ pq = PhraseQuery.new(:field, 1)
137
+ pq << "quick"
138
+ pq.add_term("fox", 2)
139
+ check_hits(pq, [1,11,14,16])
140
+
141
+ pq.slop = 0
142
+ check_hits(pq, [1,11,14])
143
+
144
+ pq.slop = 1
145
+ check_hits(pq, [1,11,14,16])
146
+
147
+ pq.slop = 4
148
+ check_hits(pq, [1,11,14,16,17])
149
+ end
150
+
151
+ def test_range_query()
152
+ rq = RangeQuery.new(:date, :lower => "20051006", :upper => "20051010")
153
+ check_hits(rq, [6,7,8,9,10])
154
+
155
+ rq = RangeQuery.new(:date, :>= => "20051006", :<= => "20051010")
156
+ check_hits(rq, [6,7,8,9,10])
157
+
158
+ rq = RangeQuery.new(:date, :lower => "20051006", :upper => "20051010",
159
+ :include_lower => false)
160
+ check_hits(rq, [7,8,9,10])
161
+
162
+ rq = RangeQuery.new(:date, :> => "20051006", :<= => "20051010")
163
+ check_hits(rq, [7,8,9,10])
164
+
165
+ rq = RangeQuery.new(:date, :lower => "20051006", :upper => "20051010",
166
+ :include_upper => false)
167
+ check_hits(rq, [6,7,8,9])
168
+
169
+ rq = RangeQuery.new(:date, :>= => "20051006", :< => "20051010")
170
+ check_hits(rq, [6,7,8,9])
171
+
172
+ rq = RangeQuery.new(:date, :lower => "20051006", :upper => "20051010",
173
+ :include_lower => false, :include_upper => false)
174
+ check_hits(rq, [7,8,9])
175
+
176
+ rq = RangeQuery.new(:date, :> => "20051006", :< => "20051010")
177
+ check_hits(rq, [7,8,9])
178
+
179
+ rq = RangeQuery.new(:date, :upper => "20051003")
180
+ check_hits(rq, [0,1,2,3])
181
+
182
+ rq = RangeQuery.new(:date, :<= => "20051003")
183
+ check_hits(rq, [0,1,2,3])
184
+
185
+ rq = RangeQuery.new(:date, :upper => "20051003", :include_upper => false)
186
+ check_hits(rq, [0,1,2])
187
+
188
+ rq = RangeQuery.new(:date, :< => "20051003")
189
+ check_hits(rq, [0,1,2])
190
+
191
+ rq = RangeQuery.new(:date, :lower => "20051014")
192
+ check_hits(rq, [14,15,16,17])
193
+
194
+ rq = RangeQuery.new(:date, :>= => "20051014")
195
+ check_hits(rq, [14,15,16,17])
196
+
197
+ rq = RangeQuery.new(:date, :lower => "20051014", :include_lower => false)
198
+ check_hits(rq, [15,16,17])
199
+
200
+ rq = RangeQuery.new(:date, :> => "20051014")
201
+ check_hits(rq, [15,16,17])
202
+ end
203
+
204
+ def test_prefix_query()
205
+ pq = PrefixQuery.new(:category, "cat1")
206
+ check_hits(pq, [0, 1, 2, 3, 4, 13, 14, 15, 16, 17])
207
+
208
+ pq = PrefixQuery.new(:category, "cat1/sub2")
209
+ check_hits(pq, [3, 4, 13, 15])
210
+ end
211
+
212
+ def test_wildcard_query()
213
+ wq = WildcardQuery.new(:category, "cat1*")
214
+ check_hits(wq, [0, 1, 2, 3, 4, 13, 14, 15, 16, 17])
215
+
216
+ wq = WildcardQuery.new(:category, "cat1*/su??ub2")
217
+ check_hits(wq, [4, 16])
218
+
219
+ wq = WildcardQuery.new(:category, "*/sub2*")
220
+ check_hits(wq, [3, 4, 13, 15])
221
+ end
222
+
223
+ def test_multi_phrase_query()
224
+ mpq = PhraseQuery.new(:field)
225
+ mpq << ["quick", "fast"]
226
+ mpq << ["brown", "red", "hairy"]
227
+ mpq << "fox"
228
+ check_hits(mpq, [1, 8, 11, 14])
229
+
230
+ mpq.slop = 4
231
+ check_hits(mpq, [1, 8, 11, 14, 16, 17])
232
+ end
233
+
234
+ def test_highlighter()
235
+ dir = Ferret::Store::RAMDirectory.new
236
+ iw = Ferret::Index::IndexWriter.new(:dir => dir,
237
+ :analyzer => Ferret::Analysis::WhiteSpaceAnalyzer.new())
238
+ long_text = "big " + "between " * 2000 + 'house'
239
+ [
240
+ {:field => "the words we are searching for are one and two also " +
241
+ "sometimes looking for them as a phrase like this; one " +
242
+ "two lets see how it goes"},
243
+ {:long => 'before ' * 1000 + long_text + ' after' * 1000},
244
+ {:dates => '20070505 20071230 20060920 20081111'},
245
+ ].each {|doc| iw << doc }
246
+ iw.close
247
+
248
+ searcher = Searcher.new(dir)
249
+
250
+ q = TermQuery.new(:field, "one");
251
+ highlights = searcher.highlight(q, 0, :field,
252
+ :excerpt_length => 10,
253
+ :num_excerpts => 1)
254
+ assert_equal(1, highlights.size)
255
+ assert_equal("...are <b>one</b>...", highlights[0])
256
+
257
+ highlights = searcher.highlight(q, 0, :field,
258
+ :excerpt_length => 10,
259
+ :num_excerpts => 2)
260
+ assert_equal(2, highlights.size)
261
+ assert_equal("...are <b>one</b>...", highlights[0])
262
+ assert_equal("...this; <b>one</b>...", highlights[1])
263
+
264
+ highlights = searcher.highlight(q, 0, :field,
265
+ :excerpt_length => 10,
266
+ :num_excerpts => 3)
267
+ assert_equal(3, highlights.size)
268
+ assert_equal("the words...", highlights[0])
269
+ assert_equal("...are <b>one</b>...", highlights[1])
270
+ assert_equal("...this; <b>one</b>...", highlights[2])
271
+
272
+ highlights = searcher.highlight(q, 0, :field,
273
+ :excerpt_length => 10,
274
+ :num_excerpts => 4)
275
+ assert_equal(3, highlights.size)
276
+ assert_equal("the words we are...", highlights[0])
277
+ assert_equal("...are <b>one</b>...", highlights[1])
278
+ assert_equal("...this; <b>one</b>...", highlights[2])
279
+
280
+ highlights = searcher.highlight(q, 0, :field,
281
+ :excerpt_length => 10,
282
+ :num_excerpts => 5)
283
+ assert_equal(2, highlights.size)
284
+ assert_equal("the words we are searching for are <b>one</b>...", highlights[0])
285
+ assert_equal("...this; <b>one</b>...", highlights[1])
286
+
287
+ highlights = searcher.highlight(q, 0, :field,
288
+ :excerpt_length => 10,
289
+ :num_excerpts => 20)
290
+ assert_equal(1, highlights.size)
291
+ assert_equal("the words we are searching for are <b>one</b> and two also " +
292
+ "sometimes looking for them as a phrase like this; <b>one</b> " +
293
+ "two lets see how it goes", highlights[0])
294
+
295
+ highlights = searcher.highlight(q, 0, :field,
296
+ :excerpt_length => 1000,
297
+ :num_excerpts => 1)
298
+ assert_equal(1, highlights.size)
299
+ assert_equal("the words we are searching for are <b>one</b> and two also " +
300
+ "sometimes looking for them as a phrase like this; <b>one</b> " +
301
+ "two lets see how it goes", highlights[0])
302
+
303
+ q = BooleanQuery.new(false)
304
+ q << TermQuery.new(:field, "one")
305
+ q << TermQuery.new(:field, "two")
306
+
307
+ highlights = searcher.highlight(q, 0, :field,
308
+ :excerpt_length => 15,
309
+ :num_excerpts => 2)
310
+ assert_equal(2, highlights.size)
311
+ assert_equal("...<b>one</b> and <b>two</b>...", highlights[0])
312
+ assert_equal("...this; <b>one</b> <b>two</b>...", highlights[1])
313
+
314
+ q << (PhraseQuery.new(:field) << "one" << "two")
315
+
316
+ highlights = searcher.highlight(q, 0, :field,
317
+ :excerpt_length => 15,
318
+ :num_excerpts => 2)
319
+ assert_equal(2, highlights.size)
320
+ assert_equal("...<b>one</b> and <b>two</b>...", highlights[0])
321
+ assert_equal("...this; <b>one two</b>...", highlights[1])
322
+
323
+ highlights = searcher.highlight(q, 0, :field,
324
+ :excerpt_length => 15,
325
+ :num_excerpts => 1)
326
+ assert_equal(1, highlights.size)
327
+ # should have a higher priority since it the merger of three matches
328
+ assert_equal("...this; <b>one two</b>...", highlights[0])
329
+
330
+ highlights = searcher.highlight(q, 0, :not_a_field,
331
+ :excerpt_length => 15,
332
+ :num_excerpts => 1)
333
+ assert_nil(highlights)
334
+
335
+ q = TermQuery.new(:wrong_field, "one")
336
+ highlights = searcher.highlight(q, 0, :wrong_field,
337
+ :excerpt_length => 15,
338
+ :num_excerpts => 1)
339
+ assert_nil(highlights)
340
+
341
+ q = BooleanQuery.new(false)
342
+ q << (PhraseQuery.new(:field) << "the" << "words")
343
+ q << (PhraseQuery.new(:field) << "for" << "are" << "one" << "and" << "two")
344
+ q << TermQuery.new(:field, "words")
345
+ q << TermQuery.new(:field, "one")
346
+ q << TermQuery.new(:field, "two")
347
+
348
+ highlights = searcher.highlight(q, 0, :field,
349
+ :excerpt_length => 10,
350
+ :num_excerpts => 1)
351
+ assert_equal(1, highlights.size)
352
+ assert_equal("<b>the words</b>...", highlights[0])
353
+
354
+ highlights = searcher.highlight(q, 0, :field,
355
+ :excerpt_length => 10,
356
+ :num_excerpts => 2)
357
+ assert_equal(2, highlights.size)
358
+ assert_equal("<b>the words</b>...", highlights[0])
359
+ assert_equal("...<b>one</b> <b>two</b>...", highlights[1])
360
+
361
+ # {:dates => '20070505, 20071230, 20060920, 20081111'},
362
+ [
363
+ [RangeQuery.new(:dates, :>= => '20081111'),
364
+ '20070505 20071230 20060920 <b>20081111</b>'],
365
+ [RangeQuery.new(:dates, :>= => '20070101'),
366
+ '<b>20070505</b> <b>20071230</b> 20060920 <b>20081111</b>'],
367
+ [PrefixQuery.new(:dates, '2007'),
368
+ '<b>20070505</b> <b>20071230</b> 20060920 20081111'],
369
+ ].each do |query, expected|
370
+ assert_equal([expected],
371
+ searcher.highlight(query, 2, :dates))
372
+ end
373
+
374
+ #q = PhraseQuery.new(:long) << 'big' << 'house'
375
+ #q.slop = 4000
376
+ #highlights = searcher.highlight(q, 1, :long,
377
+ # :excerpt_length => 400,
378
+ # :num_excerpts => 2)
379
+ #assert_equal(1, highlights.size)
380
+ #puts highlights[0]
381
+ #assert_equal("<b>the words</b>...", highlights[0])
382
+ #assert_equal("...<b>one</b> <b>two</b>...", highlights[1])
383
+ end
384
+ end