sdsykes-ferret 0.11.6.19
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +24 -0
- data/MIT-LICENSE +20 -0
- data/README +102 -0
- data/Rakefile +338 -0
- data/TODO +17 -0
- data/TUTORIAL +231 -0
- data/bin/ferret-browser +79 -0
- data/ext/analysis.c +1555 -0
- data/ext/analysis.h +219 -0
- data/ext/api.c +69 -0
- data/ext/api.h +27 -0
- data/ext/array.c +123 -0
- data/ext/array.h +53 -0
- data/ext/bitvector.c +540 -0
- data/ext/bitvector.h +272 -0
- data/ext/compound_io.c +383 -0
- data/ext/config.h +42 -0
- data/ext/document.c +156 -0
- data/ext/document.h +53 -0
- data/ext/except.c +120 -0
- data/ext/except.h +168 -0
- data/ext/extconf.rb +14 -0
- data/ext/ferret.c +402 -0
- data/ext/ferret.h +91 -0
- data/ext/filter.c +156 -0
- data/ext/fs_store.c +483 -0
- data/ext/global.c +418 -0
- data/ext/global.h +117 -0
- data/ext/hash.c +567 -0
- data/ext/hash.h +473 -0
- data/ext/hashset.c +170 -0
- data/ext/hashset.h +187 -0
- data/ext/header.h +58 -0
- data/ext/helper.c +62 -0
- data/ext/helper.h +13 -0
- data/ext/inc/lang.h +48 -0
- data/ext/inc/threading.h +31 -0
- data/ext/index.c +6425 -0
- data/ext/index.h +961 -0
- data/ext/lang.h +66 -0
- data/ext/libstemmer.c +92 -0
- data/ext/libstemmer.h +79 -0
- data/ext/mempool.c +87 -0
- data/ext/mempool.h +35 -0
- data/ext/modules.h +162 -0
- data/ext/multimapper.c +310 -0
- data/ext/multimapper.h +51 -0
- data/ext/posh.c +1006 -0
- data/ext/posh.h +1007 -0
- data/ext/priorityqueue.c +151 -0
- data/ext/priorityqueue.h +143 -0
- data/ext/q_boolean.c +1608 -0
- data/ext/q_const_score.c +161 -0
- data/ext/q_filtered_query.c +209 -0
- data/ext/q_fuzzy.c +268 -0
- data/ext/q_match_all.c +148 -0
- data/ext/q_multi_term.c +677 -0
- data/ext/q_parser.c +2825 -0
- data/ext/q_phrase.c +1126 -0
- data/ext/q_prefix.c +100 -0
- data/ext/q_range.c +350 -0
- data/ext/q_span.c +2402 -0
- data/ext/q_term.c +337 -0
- data/ext/q_wildcard.c +171 -0
- data/ext/r_analysis.c +2575 -0
- data/ext/r_index.c +3472 -0
- data/ext/r_qparser.c +585 -0
- data/ext/r_search.c +4105 -0
- data/ext/r_store.c +513 -0
- data/ext/r_utils.c +963 -0
- data/ext/ram_store.c +471 -0
- data/ext/search.c +1741 -0
- data/ext/search.h +885 -0
- data/ext/similarity.c +150 -0
- data/ext/similarity.h +82 -0
- data/ext/sort.c +983 -0
- data/ext/stem_ISO_8859_1_danish.c +338 -0
- data/ext/stem_ISO_8859_1_danish.h +16 -0
- data/ext/stem_ISO_8859_1_dutch.c +635 -0
- data/ext/stem_ISO_8859_1_dutch.h +16 -0
- data/ext/stem_ISO_8859_1_english.c +1156 -0
- data/ext/stem_ISO_8859_1_english.h +16 -0
- data/ext/stem_ISO_8859_1_finnish.c +792 -0
- data/ext/stem_ISO_8859_1_finnish.h +16 -0
- data/ext/stem_ISO_8859_1_french.c +1276 -0
- data/ext/stem_ISO_8859_1_french.h +16 -0
- data/ext/stem_ISO_8859_1_german.c +512 -0
- data/ext/stem_ISO_8859_1_german.h +16 -0
- data/ext/stem_ISO_8859_1_italian.c +1091 -0
- data/ext/stem_ISO_8859_1_italian.h +16 -0
- data/ext/stem_ISO_8859_1_norwegian.c +296 -0
- data/ext/stem_ISO_8859_1_norwegian.h +16 -0
- data/ext/stem_ISO_8859_1_porter.c +776 -0
- data/ext/stem_ISO_8859_1_porter.h +16 -0
- data/ext/stem_ISO_8859_1_portuguese.c +1035 -0
- data/ext/stem_ISO_8859_1_portuguese.h +16 -0
- data/ext/stem_ISO_8859_1_spanish.c +1119 -0
- data/ext/stem_ISO_8859_1_spanish.h +16 -0
- data/ext/stem_ISO_8859_1_swedish.c +307 -0
- data/ext/stem_ISO_8859_1_swedish.h +16 -0
- data/ext/stem_KOI8_R_russian.c +701 -0
- data/ext/stem_KOI8_R_russian.h +16 -0
- data/ext/stem_UTF_8_danish.c +344 -0
- data/ext/stem_UTF_8_danish.h +16 -0
- data/ext/stem_UTF_8_dutch.c +653 -0
- data/ext/stem_UTF_8_dutch.h +16 -0
- data/ext/stem_UTF_8_english.c +1176 -0
- data/ext/stem_UTF_8_english.h +16 -0
- data/ext/stem_UTF_8_finnish.c +808 -0
- data/ext/stem_UTF_8_finnish.h +16 -0
- data/ext/stem_UTF_8_french.c +1296 -0
- data/ext/stem_UTF_8_french.h +16 -0
- data/ext/stem_UTF_8_german.c +526 -0
- data/ext/stem_UTF_8_german.h +16 -0
- data/ext/stem_UTF_8_italian.c +1113 -0
- data/ext/stem_UTF_8_italian.h +16 -0
- data/ext/stem_UTF_8_norwegian.c +302 -0
- data/ext/stem_UTF_8_norwegian.h +16 -0
- data/ext/stem_UTF_8_porter.c +794 -0
- data/ext/stem_UTF_8_porter.h +16 -0
- data/ext/stem_UTF_8_portuguese.c +1055 -0
- data/ext/stem_UTF_8_portuguese.h +16 -0
- data/ext/stem_UTF_8_russian.c +709 -0
- data/ext/stem_UTF_8_russian.h +16 -0
- data/ext/stem_UTF_8_spanish.c +1137 -0
- data/ext/stem_UTF_8_spanish.h +16 -0
- data/ext/stem_UTF_8_swedish.c +313 -0
- data/ext/stem_UTF_8_swedish.h +16 -0
- data/ext/stopwords.c +401 -0
- data/ext/store.c +692 -0
- data/ext/store.h +777 -0
- data/ext/term_vectors.c +352 -0
- data/ext/threading.h +31 -0
- data/ext/utilities.c +446 -0
- data/ext/win32.h +54 -0
- data/lib/ferret.rb +29 -0
- data/lib/ferret/browser.rb +246 -0
- data/lib/ferret/browser/s/global.js +192 -0
- data/lib/ferret/browser/s/style.css +148 -0
- data/lib/ferret/browser/views/document/list.rhtml +49 -0
- data/lib/ferret/browser/views/document/show.rhtml +27 -0
- data/lib/ferret/browser/views/error/index.rhtml +7 -0
- data/lib/ferret/browser/views/help/index.rhtml +8 -0
- data/lib/ferret/browser/views/home/index.rhtml +29 -0
- data/lib/ferret/browser/views/layout.rhtml +22 -0
- data/lib/ferret/browser/views/term-vector/index.rhtml +4 -0
- data/lib/ferret/browser/views/term/index.rhtml +199 -0
- data/lib/ferret/browser/views/term/termdocs.rhtml +1 -0
- data/lib/ferret/browser/webrick.rb +14 -0
- data/lib/ferret/document.rb +130 -0
- data/lib/ferret/field_infos.rb +44 -0
- data/lib/ferret/index.rb +786 -0
- data/lib/ferret/number_tools.rb +157 -0
- data/lib/ferret_version.rb +3 -0
- data/setup.rb +1555 -0
- data/test/test_all.rb +5 -0
- data/test/test_helper.rb +24 -0
- data/test/threading/number_to_spoken.rb +132 -0
- data/test/threading/thread_safety_index_test.rb +79 -0
- data/test/threading/thread_safety_read_write_test.rb +76 -0
- data/test/threading/thread_safety_test.rb +133 -0
- data/test/unit/analysis/tc_analyzer.rb +548 -0
- data/test/unit/analysis/tc_token_stream.rb +646 -0
- data/test/unit/index/tc_index.rb +762 -0
- data/test/unit/index/tc_index_reader.rb +699 -0
- data/test/unit/index/tc_index_writer.rb +437 -0
- data/test/unit/index/th_doc.rb +315 -0
- data/test/unit/largefile/tc_largefile.rb +46 -0
- data/test/unit/query_parser/tc_query_parser.rb +238 -0
- data/test/unit/search/tc_filter.rb +135 -0
- data/test/unit/search/tc_fuzzy_query.rb +147 -0
- data/test/unit/search/tc_index_searcher.rb +61 -0
- data/test/unit/search/tc_multi_searcher.rb +128 -0
- data/test/unit/search/tc_multiple_search_requests.rb +58 -0
- data/test/unit/search/tc_search_and_sort.rb +179 -0
- data/test/unit/search/tc_sort.rb +49 -0
- data/test/unit/search/tc_sort_field.rb +27 -0
- data/test/unit/search/tc_spans.rb +190 -0
- data/test/unit/search/tm_searcher.rb +384 -0
- data/test/unit/store/tc_fs_store.rb +77 -0
- data/test/unit/store/tc_ram_store.rb +35 -0
- data/test/unit/store/tm_store.rb +34 -0
- data/test/unit/store/tm_store_lock.rb +68 -0
- data/test/unit/tc_document.rb +81 -0
- data/test/unit/ts_analysis.rb +2 -0
- data/test/unit/ts_index.rb +2 -0
- data/test/unit/ts_largefile.rb +4 -0
- data/test/unit/ts_query_parser.rb +2 -0
- data/test/unit/ts_search.rb +2 -0
- data/test/unit/ts_store.rb +2 -0
- data/test/unit/ts_utils.rb +2 -0
- data/test/unit/utils/tc_bit_vector.rb +295 -0
- data/test/unit/utils/tc_number_tools.rb +117 -0
- data/test/unit/utils/tc_priority_queue.rb +106 -0
- metadata +285 -0
@@ -0,0 +1,61 @@
|
|
1
|
+
require File.dirname(__FILE__) + "/../../test_helper"
|
2
|
+
require File.dirname(__FILE__) + "/tm_searcher"
|
3
|
+
|
4
|
+
class SearcherTest < Test::Unit::TestCase
|
5
|
+
include Ferret::Search
|
6
|
+
include Ferret::Store
|
7
|
+
include Ferret::Analysis
|
8
|
+
include Ferret::Index
|
9
|
+
|
10
|
+
include SearcherTests
|
11
|
+
|
12
|
+
def setup()
|
13
|
+
@dir = RAMDirectory.new()
|
14
|
+
iw = IndexWriter.new(:dir => @dir,
|
15
|
+
:analyzer => WhiteSpaceAnalyzer.new(),
|
16
|
+
:create => true)
|
17
|
+
@documents = IndexTestHelper::SEARCH_TEST_DOCS
|
18
|
+
@documents.each { |doc| iw << doc }
|
19
|
+
iw.close()
|
20
|
+
@searcher = Searcher.new(@dir)
|
21
|
+
end
|
22
|
+
|
23
|
+
def teardown()
|
24
|
+
@searcher.close
|
25
|
+
@dir.close()
|
26
|
+
end
|
27
|
+
|
28
|
+
def get_docs(hits)
|
29
|
+
docs = []
|
30
|
+
hits.each do |hit|
|
31
|
+
docs << hit.doc
|
32
|
+
end
|
33
|
+
docs
|
34
|
+
end
|
35
|
+
|
36
|
+
def check_hits(query, expected, top=nil, total_hits=nil)
|
37
|
+
options = {}
|
38
|
+
options[:limit] = expected.size + 1 if (expected.size > 10)
|
39
|
+
top_docs = @searcher.search(query, options)
|
40
|
+
assert_equal(expected.length, top_docs.hits.size)
|
41
|
+
assert_equal(top, top_docs.hits[0].doc) if top
|
42
|
+
if total_hits
|
43
|
+
assert_equal(total_hits, top_docs.total_hits)
|
44
|
+
else
|
45
|
+
assert_equal(expected.length, top_docs.total_hits)
|
46
|
+
end
|
47
|
+
top_docs.hits.each do |score_doc|
|
48
|
+
assert(expected.include?(score_doc.doc),
|
49
|
+
"#{score_doc.doc} was found unexpectedly")
|
50
|
+
assert(score_doc.score.approx_eql?(@searcher.explain(query, score_doc.doc).score),
|
51
|
+
"Scores(#{score_doc.score} != #{@searcher.explain(query, score_doc.doc).score})")
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
def test_get_doc()
|
56
|
+
assert_equal(18, @searcher.max_doc)
|
57
|
+
assert_equal("20050930", @searcher.get_document(0)[:date])
|
58
|
+
assert_equal("cat1/sub2/subsub2", @searcher.get_document(4)[:category])
|
59
|
+
assert_equal("20051012", @searcher.get_document(12)[:date])
|
60
|
+
end
|
61
|
+
end
|
@@ -0,0 +1,128 @@
|
|
1
|
+
require File.dirname(__FILE__) + "/../../test_helper"
|
2
|
+
require File.join(File.dirname(__FILE__), "tc_index_searcher.rb")
|
3
|
+
|
4
|
+
# make sure a MultiSearcher searching only one index
|
5
|
+
# passes all the Searcher tests
|
6
|
+
class SimpleMultiSearcherTest < SearcherTest
|
7
|
+
alias :old_setup :setup
|
8
|
+
def setup()
|
9
|
+
old_setup
|
10
|
+
@searcher = MultiSearcher.new([Searcher.new(@dir)])
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
|
15
|
+
# checks query results of a multisearcher searching two indexes
|
16
|
+
# against those of a single indexsearcher searching the same
|
17
|
+
# set of documents
|
18
|
+
class MultiSearcherTest < Test::Unit::TestCase
|
19
|
+
include Ferret::Search
|
20
|
+
include Ferret::Store
|
21
|
+
include Ferret::Analysis
|
22
|
+
include Ferret::Index
|
23
|
+
|
24
|
+
include SearcherTests
|
25
|
+
|
26
|
+
DOCUMENTS1 = [
|
27
|
+
{"date" => "20050930", :field => "word1",
|
28
|
+
"cat" => "cat1/"},
|
29
|
+
{"date" => "20051001", :field => "word1 word2 the quick brown fox",
|
30
|
+
"cat" => "cat1/sub1"},
|
31
|
+
{"date" => "20051002", :field => "word1 word3",
|
32
|
+
"cat" => "cat1/sub1/subsub1"},
|
33
|
+
{"date" => "20051003", :field => "word1 word3",
|
34
|
+
"cat" => "cat1/sub2"},
|
35
|
+
{"date" => "20051004", :field => "word1 word2",
|
36
|
+
"cat" => "cat1/sub2/subsub2"},
|
37
|
+
{"date" => "20051005", :field => "word1",
|
38
|
+
"cat" => "cat2/sub1"},
|
39
|
+
{"date" => "20051006", :field => "word1 word3",
|
40
|
+
"cat" => "cat2/sub1"},
|
41
|
+
{"date" => "20051007", :field => "word1",
|
42
|
+
"cat" => "cat2/sub1"},
|
43
|
+
{"date" => "20051008", :field => "word1 word2 word3 the fast brown fox",
|
44
|
+
"cat" => "cat2/sub1"}
|
45
|
+
]
|
46
|
+
|
47
|
+
DOCUMENTS2 = [
|
48
|
+
{"date" => "20051009", :field => "word1",
|
49
|
+
"cat" => "cat3/sub1"},
|
50
|
+
{"date" => "20051010", :field => "word1",
|
51
|
+
"cat" => "cat3/sub1"},
|
52
|
+
{"date" => "20051011", :field => "word1 word3 the quick red fox",
|
53
|
+
"cat" => "cat3/sub1"},
|
54
|
+
{"date" => "20051012", :field => "word1",
|
55
|
+
"cat" => "cat3/sub1"},
|
56
|
+
{"date" => "20051013", :field => "word1",
|
57
|
+
"cat" => "cat1/sub2"},
|
58
|
+
{"date" => "20051014", :field => "word1 word3 the quick hairy fox",
|
59
|
+
"cat" => "cat1/sub1"},
|
60
|
+
{"date" => "20051015", :field => "word1",
|
61
|
+
"cat" => "cat1/sub2/subsub1"},
|
62
|
+
{"date" => "20051016",
|
63
|
+
:field => "word1 the quick fox is brown and hairy and a little red",
|
64
|
+
"cat" => "cat1/sub1/subsub2"},
|
65
|
+
{"date" => "20051017", :field => "word1 the brown fox is quick and red",
|
66
|
+
"cat" => "cat1/"}
|
67
|
+
]
|
68
|
+
|
69
|
+
def setup()
|
70
|
+
# create MultiSearcher from two seperate searchers
|
71
|
+
dir1 = RAMDirectory.new()
|
72
|
+
iw1 = IndexWriter.new(:dir => dir1,
|
73
|
+
:analyzer => WhiteSpaceAnalyzer.new(),
|
74
|
+
:create => true)
|
75
|
+
DOCUMENTS1.each { |doc| iw1 << doc }
|
76
|
+
iw1.close()
|
77
|
+
|
78
|
+
dir2 = RAMDirectory.new()
|
79
|
+
iw2 = IndexWriter.new(:dir => dir2,
|
80
|
+
:analyzer => WhiteSpaceAnalyzer.new(),
|
81
|
+
:create => true)
|
82
|
+
DOCUMENTS2.each { |doc| iw2 << doc }
|
83
|
+
iw2.close()
|
84
|
+
@searcher = Ferret::Search::MultiSearcher.new([Searcher.new(dir1),
|
85
|
+
Searcher.new(dir2)])
|
86
|
+
|
87
|
+
# create single searcher
|
88
|
+
dir = RAMDirectory.new
|
89
|
+
iw = IndexWriter.new(:dir => dir,
|
90
|
+
:analyzer => WhiteSpaceAnalyzer.new(),
|
91
|
+
:create => true)
|
92
|
+
DOCUMENTS1.each { |doc| iw << doc }
|
93
|
+
DOCUMENTS2.each { |doc| iw << doc }
|
94
|
+
iw.close
|
95
|
+
@single = Searcher.new(dir)
|
96
|
+
|
97
|
+
#@query_parser = Ferret::QueryParser.new([:date, :field, :cat], :analyzer => WhiteSpaceAnalyzer.new())
|
98
|
+
end
|
99
|
+
|
100
|
+
def teardown()
|
101
|
+
@searcher.close
|
102
|
+
@single.close
|
103
|
+
end
|
104
|
+
|
105
|
+
def check_hits(query, ignore1, ignore2 = nil, ignore3 = nil)
|
106
|
+
multi_docs = @searcher.search(query)
|
107
|
+
single_docs = @single.search(query)
|
108
|
+
assert_equal(single_docs.hits.size, multi_docs.hits.size, 'hit count')
|
109
|
+
assert_equal(single_docs.total_hits, multi_docs.total_hits, 'hit count')
|
110
|
+
|
111
|
+
multi_docs.hits.each_with_index { |sd, id|
|
112
|
+
assert_equal(single_docs.hits[id].doc, sd.doc)
|
113
|
+
assert(single_docs.hits[id].score.approx_eql?(sd.score),
|
114
|
+
"#{single_docs.hits[id]} != #{sd.score}")
|
115
|
+
}
|
116
|
+
end
|
117
|
+
|
118
|
+
def test_get_doc()
|
119
|
+
assert_equal(18, @searcher.max_doc)
|
120
|
+
assert_equal("20050930", @searcher.get_document(0)[:date])
|
121
|
+
assert_equal("cat1/sub2/subsub2", @searcher[4][:cat])
|
122
|
+
assert_equal("20051012", @searcher.get_document(12)[:date])
|
123
|
+
assert_equal(18, @single.max_doc)
|
124
|
+
assert_equal("20050930", @single.get_document(0)[:date])
|
125
|
+
assert_equal("cat1/sub2/subsub2", @single[4][:cat])
|
126
|
+
assert_equal("20051012", @single.get_document(12)[:date])
|
127
|
+
end
|
128
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
require File.dirname(__FILE__) + "/../../test_helper"
|
2
|
+
|
3
|
+
class MultipleSearchRequestsTest < Test::Unit::TestCase
|
4
|
+
include Ferret::Search
|
5
|
+
include Ferret::Store
|
6
|
+
include Ferret::Analysis
|
7
|
+
include Ferret::Index
|
8
|
+
|
9
|
+
def setup()
|
10
|
+
dpath = File.expand_path(File.join(File.dirname(__FILE__),
|
11
|
+
'../../temp/fsdir'))
|
12
|
+
fs_dir = Ferret::Store::FSDirectory.new(dpath, true)
|
13
|
+
|
14
|
+
iw = IndexWriter.new(:dir => fs_dir, :create => true, :key => [:id])
|
15
|
+
1000.times do |x|
|
16
|
+
doc = {:id => x}
|
17
|
+
iw << doc
|
18
|
+
end
|
19
|
+
iw.close()
|
20
|
+
fs_dir.close()
|
21
|
+
|
22
|
+
@ix = Index.new(:path => dpath, :create => true, :key => [:id])
|
23
|
+
end
|
24
|
+
|
25
|
+
def tear_down()
|
26
|
+
@ix.close
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_repeated_queries_segmentation_fault
|
30
|
+
1000.times do |x|
|
31
|
+
bq = BooleanQuery.new()
|
32
|
+
tq1 = TermQuery.new(:id, 1)
|
33
|
+
tq2 = TermQuery.new(:another_id, 1)
|
34
|
+
bq.add_query(tq1, :must)
|
35
|
+
bq.add_query(tq2, :must)
|
36
|
+
top_docs = @ix.search(bq)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def test_repeated_queries_bus_error
|
41
|
+
1000.times do |x|
|
42
|
+
bq = BooleanQuery.new()
|
43
|
+
tq1 = TermQuery.new(:id, '1')
|
44
|
+
tq2 = TermQuery.new(:another_id, '1')
|
45
|
+
tq3 = TermQuery.new(:yet_another_id, '1')
|
46
|
+
tq4 = TermQuery.new(:still_another_id, '1')
|
47
|
+
tq5 = TermQuery.new(:one_more_id, '1')
|
48
|
+
tq6 = TermQuery.new(:and_another_id, '1')
|
49
|
+
bq.add_query(tq1, :must)
|
50
|
+
bq.add_query(tq2, :must)
|
51
|
+
bq.add_query(tq3, :must)
|
52
|
+
bq.add_query(tq4, :must)
|
53
|
+
bq.add_query(tq5, :must)
|
54
|
+
bq.add_query(tq6, :must)
|
55
|
+
top_docs = @ix.search(bq)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
@@ -0,0 +1,179 @@
|
|
1
|
+
require File.dirname(__FILE__) + "/../../test_helper"
|
2
|
+
|
3
|
+
class SearchAndSortTest < Test::Unit::TestCase
|
4
|
+
include Ferret::Search
|
5
|
+
include Ferret::Store
|
6
|
+
include Ferret::Analysis
|
7
|
+
include Ferret::Index
|
8
|
+
|
9
|
+
def setup()
|
10
|
+
@dir = RAMDirectory.new()
|
11
|
+
iw = IndexWriter.new(:dir => @dir,
|
12
|
+
:analyzer => WhiteSpaceAnalyzer.new(),
|
13
|
+
:create => true, :min_merge_docs => 3)
|
14
|
+
[ # len mod
|
15
|
+
{:x => "findall", :string => "a", :int => "6", :float => "0.01"}, # 4 0
|
16
|
+
{:x => "findall", :string => "c", :int => "5", :float => "0.1"}, # 3 3
|
17
|
+
{:x => "findall", :string => "e", :int => "2", :float => "0.001"}, # 5 1
|
18
|
+
{:x => "findall", :string => "g", :int => "1", :float => "1.0"}, # 3 3
|
19
|
+
{:x => "findall", :string => nil, :int => "3", :float => "0.0001"}, # 6 2
|
20
|
+
{:x => "findall", :string => "", :int => "4", :float => "10.0"}, # 4 0
|
21
|
+
{:x => "findall", :string => "h", :int => "5", :float => "0.00001"}, # 7 3
|
22
|
+
{:x => "findall", :string => "f", :int => "2", :float => "100.0"}, # 5 1
|
23
|
+
{:x => "findall", :string => "d", :int => "3", :float => "1000.0"}, # 6 2
|
24
|
+
{:x => "findall", :string => "b", :int => "4", :float => "0.000001"} # 8 0
|
25
|
+
].each do |doc|
|
26
|
+
doc.extend(Ferret::BoostMixin)
|
27
|
+
doc.boost = doc[:float].to_f
|
28
|
+
iw << doc
|
29
|
+
end
|
30
|
+
iw.close
|
31
|
+
end
|
32
|
+
|
33
|
+
def teardown()
|
34
|
+
@dir.close()
|
35
|
+
end
|
36
|
+
|
37
|
+
def do_test_top_docs(is, query, expected, sort = nil)
|
38
|
+
top_docs = is.search(query, {:sort => sort})
|
39
|
+
top_docs.total_hits.times do |i|
|
40
|
+
assert_equal(expected[i], top_docs.hits[i].doc)
|
41
|
+
end
|
42
|
+
|
43
|
+
# test sorting works for smaller ranged query
|
44
|
+
offset = 3
|
45
|
+
limit = 3
|
46
|
+
top_docs = is.search(query, {:sort => sort,
|
47
|
+
:offset => offset,
|
48
|
+
:limit => limit})
|
49
|
+
limit.times do |i|
|
50
|
+
assert_equal(expected[offset + i], top_docs.hits[i].doc)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
def test_sort_field_to_s()
|
55
|
+
assert_equal("<SCORE>", SortField::SCORE.to_s);
|
56
|
+
sf = SortField.new("MyScore",
|
57
|
+
{:type => :score,
|
58
|
+
:reverse => true})
|
59
|
+
assert_equal("MyScore:<SCORE>!", sf.to_s)
|
60
|
+
assert_equal("<DOC>", SortField::DOC_ID.to_s);
|
61
|
+
sf = SortField.new("MyDoc",
|
62
|
+
{:type => :doc_id,
|
63
|
+
:reverse => true})
|
64
|
+
assert_equal("MyDoc:<DOC>!", sf.to_s)
|
65
|
+
sf = SortField.new(:date,
|
66
|
+
{:type => :integer})
|
67
|
+
assert_equal("date:<integer>", sf.to_s)
|
68
|
+
sf = SortField.new(:date,
|
69
|
+
{:type => :integer,
|
70
|
+
:reverse => true})
|
71
|
+
assert_equal("date:<integer>!", sf.to_s)
|
72
|
+
sf = SortField.new(:price,
|
73
|
+
{:type => :float})
|
74
|
+
assert_equal("price:<float>", sf.to_s)
|
75
|
+
sf = SortField.new(:price,
|
76
|
+
{:type => :float,
|
77
|
+
:reverse => true})
|
78
|
+
assert_equal("price:<float>!", sf.to_s)
|
79
|
+
sf = SortField.new(:content,
|
80
|
+
{:type => :string})
|
81
|
+
assert_equal("content:<string>", sf.to_s)
|
82
|
+
sf = SortField.new(:content,
|
83
|
+
{:type => :string,
|
84
|
+
:reverse => true})
|
85
|
+
assert_equal("content:<string>!", sf.to_s)
|
86
|
+
sf = SortField.new(:auto_field,
|
87
|
+
{:type => :auto})
|
88
|
+
assert_equal("auto_field:<auto>", sf.to_s)
|
89
|
+
sf = SortField.new(:auto_field,
|
90
|
+
{:type => :auto,
|
91
|
+
:reverse => true})
|
92
|
+
assert_equal("auto_field:<auto>!", sf.to_s)
|
93
|
+
end
|
94
|
+
|
95
|
+
def test_sort_to_s()
|
96
|
+
sort = Sort.new
|
97
|
+
assert_equal("Sort[<SCORE>, <DOC>]", sort.to_s)
|
98
|
+
sf = SortField.new(:auto_field,
|
99
|
+
{:type => :auto,
|
100
|
+
:reverse => true})
|
101
|
+
sort = Sort.new([sf, SortField::SCORE, SortField::DOC_ID])
|
102
|
+
assert_equal("Sort[auto_field:<auto>!, <SCORE>, <DOC>]", sort.to_s)
|
103
|
+
sort = Sort.new([:one, :two, SortField::DOC_ID])
|
104
|
+
assert_equal("Sort[one:<auto>, two:<auto>, <DOC>]", sort.to_s)
|
105
|
+
sort = Sort.new([:one, :two])
|
106
|
+
assert_equal("Sort[one:<auto>, two:<auto>, <DOC>]", sort.to_s)
|
107
|
+
end
|
108
|
+
|
109
|
+
|
110
|
+
def test_sorts()
|
111
|
+
is = Searcher.new(@dir)
|
112
|
+
q = TermQuery.new(:x, "findall")
|
113
|
+
do_test_top_docs(is, q, [8,7,5,3,1,0,2,4,6,9])
|
114
|
+
do_test_top_docs(is, q, [8,7,5,3,1,0,2,4,6,9], Sort::RELEVANCE)
|
115
|
+
do_test_top_docs(is, q, [8,7,5,3,1,0,2,4,6,9], [SortField::SCORE])
|
116
|
+
do_test_top_docs(is, q, [0,1,2,3,4,5,6,7,8,9], Sort::INDEX_ORDER)
|
117
|
+
do_test_top_docs(is, q, [0,1,2,3,4,5,6,7,8,9], [SortField::DOC_ID])
|
118
|
+
|
119
|
+
## int
|
120
|
+
sf_int = SortField.new(:int, {:type => :integer, :reverse => true})
|
121
|
+
do_test_top_docs(is, q, [0,1,6,5,9,4,8,2,7,3], [sf_int])
|
122
|
+
do_test_top_docs(is, q, [0,1,6,5,9,4,8,2,7,3], "int DESC")
|
123
|
+
do_test_top_docs(is, q, [0,1,6,5,9,8,4,7,2,3], [sf_int, SortField::SCORE])
|
124
|
+
do_test_top_docs(is, q, [0,1,6,5,9,8,4,7,2,3], "int DESC, SCORE")
|
125
|
+
sf_int = SortField.new(:int, {:type => :integer})
|
126
|
+
do_test_top_docs(is, q, [3,2,7,4,8,5,9,1,6,0], [sf_int])
|
127
|
+
do_test_top_docs(is, q, [3,2,7,4,8,5,9,1,6,0], "int")
|
128
|
+
|
129
|
+
## byte
|
130
|
+
do_test_top_docs(is, q, [3,2,7,4,8,5,9,1,6,0],
|
131
|
+
SortField.new(:int, :type => :byte))
|
132
|
+
do_test_top_docs(is, q, [0,1,6,5,9,4,8,2,7,3],
|
133
|
+
[SortField.new(:int, :type => :byte, :reverse => true)])
|
134
|
+
|
135
|
+
|
136
|
+
## float
|
137
|
+
sf_float = SortField.new(:float, {:type => :float, :reverse => true})
|
138
|
+
do_test_top_docs(is, q, [8,7,5,3,1,0,2,4,6,9],
|
139
|
+
Sort.new([sf_float, SortField::SCORE]))
|
140
|
+
do_test_top_docs(is, q, [8,7,5,3,1,0,2,4,6,9], "float DESC, SCORE")
|
141
|
+
sf_float = SortField.new(:float, {:type => :float})
|
142
|
+
do_test_top_docs(is, q, [9,6,4,2,0,1,3,5,7,8],
|
143
|
+
Sort.new([sf_float, SortField::SCORE]))
|
144
|
+
do_test_top_docs(is, q, [9,6,4,2,0,1,3,5,7,8], "float, SCORE")
|
145
|
+
|
146
|
+
## str
|
147
|
+
sf_str = SortField.new(:string, {:type => :string})
|
148
|
+
do_test_top_docs(is, q, [0,9,1,8,2,7,3,6,5,4], [sf_str, SortField::SCORE])
|
149
|
+
do_test_top_docs(is, q, [0,9,1,8,2,7,3,6,4,5], "string")
|
150
|
+
|
151
|
+
## auto
|
152
|
+
do_test_top_docs(is, q, [0,9,1,8,2,7,3,6,4,5], Sort.new(:string))
|
153
|
+
do_test_top_docs(is, q, [3,2,7,4,8,5,9,1,6,0], Sort.new([:int]))
|
154
|
+
do_test_top_docs(is, q, [9,6,4,2,0,1,3,5,7,8], Sort.new(:float))
|
155
|
+
do_test_top_docs(is, q, [9,6,4,2,0,1,3,5,7,8], :float)
|
156
|
+
do_test_top_docs(is, q, [8,7,5,3,1,0,2,4,6,9], Sort.new(:float, true))
|
157
|
+
do_test_top_docs(is, q, [0,6,1,5,9,4,8,7,2,3], Sort.new([:int, :string], true))
|
158
|
+
do_test_top_docs(is, q, [0,6,1,5,9,4,8,7,2,3], "int DESC, string DESC")
|
159
|
+
do_test_top_docs(is, q, [3,2,7,8,4,9,5,1,6,0], Sort.new([:int, :string]))
|
160
|
+
do_test_top_docs(is, q, [3,2,7,8,4,9,5,1,6,0], [:int, :string])
|
161
|
+
do_test_top_docs(is, q, [3,2,7,8,4,9,5,1,6,0], "int, string")
|
162
|
+
end
|
163
|
+
|
164
|
+
#LENGTH = SortField::SortType.new("length", lambda{|str| str.length})
|
165
|
+
#LENGTH_MODULO = SortField::SortType.new("length_mod", lambda{|str| str.length},
|
166
|
+
# lambda{|i, j| (i%4) <=> (j%4)})
|
167
|
+
#def test_special_sorts
|
168
|
+
# is = IndexSearcher.new(@dir)
|
169
|
+
# q = TermQuery.new(Term.new(:x, "findall"))
|
170
|
+
# sf = SortField.new(:float, {:type => LENGTH, :reverse => true})
|
171
|
+
# do_test_top_docs(is, q, [9,6,4,8,2,7,0,5,1,3], [sf])
|
172
|
+
# sf = SortField.new(:float, {:type => LENGTH_MODULO, :reverse => true})
|
173
|
+
# do_test_top_docs(is, q, [1,3,6,4,8,2,7,0,5,9], [sf])
|
174
|
+
# sf = SortField.new(:float, {:type => LENGTH,
|
175
|
+
# :reverse => true,
|
176
|
+
# :comparator => lambda{|i,j| (j%4) <=> (i%4)}})
|
177
|
+
# do_test_top_docs(is, q, [0,5,9,2,7,4,8,1,3,6], [sf])
|
178
|
+
#end
|
179
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
require File.dirname(__FILE__) + "/../../test_helper"
|
2
|
+
|
3
|
+
class SortTest < Test::Unit::TestCase
|
4
|
+
include Ferret::Search
|
5
|
+
|
6
|
+
def test_basic()
|
7
|
+
s = Sort::RELEVANCE
|
8
|
+
assert_equal(2, s.fields.size)
|
9
|
+
assert_equal(SortField::SCORE, s.fields[0])
|
10
|
+
assert_equal(SortField::DOC_ID, s.fields[1])
|
11
|
+
|
12
|
+
s = Sort::INDEX_ORDER
|
13
|
+
assert_equal(1, s.fields.size)
|
14
|
+
assert_equal(SortField::DOC_ID, s.fields[0])
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_string_init()
|
18
|
+
s = Sort.new(:field)
|
19
|
+
assert_equal(2, s.fields.size)
|
20
|
+
assert_equal(:auto, s.fields[0].type)
|
21
|
+
assert_equal(:field, s.fields[0].name)
|
22
|
+
assert_equal(SortField::DOC_ID, s.fields[1])
|
23
|
+
|
24
|
+
s = Sort.new([:field1, :field2, :field3])
|
25
|
+
assert_equal(4, s.fields.size)
|
26
|
+
assert_equal(:auto, s.fields[0].type)
|
27
|
+
assert_equal(:field1, s.fields[0].name)
|
28
|
+
assert_equal(:auto, s.fields[1].type)
|
29
|
+
assert_equal(:field2, s.fields[1].name)
|
30
|
+
assert_equal(:auto, s.fields[2].type)
|
31
|
+
assert_equal(:field3, s.fields[2].name)
|
32
|
+
assert_equal(SortField::DOC_ID, s.fields[3])
|
33
|
+
end
|
34
|
+
|
35
|
+
def test_multi_fields()
|
36
|
+
sf1 = SortField.new(:field, {:type => :integer,
|
37
|
+
:reverse => true})
|
38
|
+
sf2 = SortField::SCORE
|
39
|
+
sf3 = SortField::DOC_ID
|
40
|
+
s = Sort.new([sf1, sf2, sf3])
|
41
|
+
|
42
|
+
assert_equal(3, s.fields.size)
|
43
|
+
assert_equal(:integer, s.fields[0].type)
|
44
|
+
assert_equal(:field, s.fields[0].name)
|
45
|
+
assert(s.fields[0].reverse?)
|
46
|
+
assert_equal(SortField::SCORE, s.fields[1])
|
47
|
+
assert_equal(SortField::DOC_ID, s.fields[2])
|
48
|
+
end
|
49
|
+
end
|