1
require File.dirname(__FILE__) + "/../../test_helper"
3
class SearchAndSortTest < Test::Unit::TestCase
6
include Ferret::Analysis
10
@dir = RAMDirectory.new()
11
iw = IndexWriter.new(:dir => @dir,
12
:analyzer => WhiteSpaceAnalyzer.new(),
13
:create => true, :min_merge_docs => 3)
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
26
doc.extend(Ferret::BoostMixin)
27
doc.boost = doc[:float].to_f
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)
43
# test sorting works for smaller ranged query
46
top_docs = is.search(query, {:sort => sort,
50
assert_equal(expected[offset + i], top_docs.hits[i].doc)
54
def test_sort_field_to_s()
55
assert_equal("<SCORE>", SortField::SCORE.to_s);
56
sf = SortField.new("MyScore",
59
assert_equal("MyScore:<SCORE>!", sf.to_s)
60
assert_equal("<DOC>", SortField::DOC_ID.to_s);
61
sf = SortField.new("MyDoc",
64
assert_equal("MyDoc:<DOC>!", sf.to_s)
65
sf = SortField.new(:date,
67
assert_equal("date:<integer>", sf.to_s)
68
sf = SortField.new(:date,
71
assert_equal("date:<integer>!", sf.to_s)
72
sf = SortField.new(:price,
74
assert_equal("price:<float>", sf.to_s)
75
sf = SortField.new(:price,
78
assert_equal("price:<float>!", sf.to_s)
79
sf = SortField.new(:content,
81
assert_equal("content:<string>", sf.to_s)
82
sf = SortField.new(:content,
85
assert_equal("content:<string>!", sf.to_s)
86
sf = SortField.new(:auto_field,
88
assert_equal("auto_field:<auto>", sf.to_s)
89
sf = SortField.new(:auto_field,
92
assert_equal("auto_field:<auto>!", sf.to_s)
97
assert_equal("Sort[<SCORE>, <DOC>]", sort.to_s)
98
sf = SortField.new(:auto_field,
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)
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])
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")
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)])
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")
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")
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")
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,
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])