~ubuntu-branches/ubuntu/trusty/ruby-ferret/trusty

« back to all changes in this revision

Viewing changes to test/unit/search/tc_search_and_sort.rb

  • Committer: Bazaar Package Importer
  • Author(s): Antonio Terceiro
  • Date: 2011-07-28 00:02:49 UTC
  • Revision ID: james.westby@ubuntu.com-20110728000249-v0443y69ftcpxwi6
Tags: upstream-0.11.6
ImportĀ upstreamĀ versionĀ 0.11.6

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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