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

« back to all changes in this revision

Viewing changes to test/unit/search/tc_filter.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
 
 
4
class FilterTest < Test::Unit::TestCase
 
5
  include Ferret::Search
 
6
  include Ferret::Analysis
 
7
  include Ferret::Index
 
8
 
 
9
  def setup()
 
10
    @dir = Ferret::Store::RAMDirectory.new()
 
11
    iw = IndexWriter.new(:dir => @dir,
 
12
                         :analyzer => WhiteSpaceAnalyzer.new(),
 
13
                         :create => true)
 
14
    [ 
 
15
      {:int => "0", :date => "20040601", :switch => "on"},
 
16
      {:int => "1", :date => "20041001", :switch => "off"},
 
17
      {:int => "2", :date => "20051101", :switch => "on"},
 
18
      {:int => "3", :date => "20041201", :switch => "off"},
 
19
      {:int => "4", :date => "20051101", :switch => "on"},
 
20
      {:int => "5", :date => "20041201", :switch => "off"},
 
21
      {:int => "6", :date => "20050101", :switch => "on"},
 
22
      {:int => "7", :date => "20040701", :switch => "off"},
 
23
      {:int => "8", :date => "20050301", :switch => "on"},
 
24
      {:int => "9", :date => "20050401", :switch => "off"}
 
25
    ].each {|doc| iw << doc}
 
26
    iw.close
 
27
  end
 
28
 
 
29
  def teardown()
 
30
    @dir.close()
 
31
  end
 
32
 
 
33
  def do_test_top_docs(searcher, query, expected, filter)
 
34
    top_docs = searcher.search(query, {:filter => filter})
 
35
    #puts top_docs
 
36
    assert_equal(expected.size, top_docs.hits.size)
 
37
    top_docs.total_hits.times do |i|
 
38
      assert_equal(expected[i], top_docs.hits[i].doc)
 
39
    end
 
40
  end
 
41
 
 
42
  def test_filter_proc
 
43
    searcher = Searcher.new(@dir)
 
44
    q = MatchAllQuery.new()
 
45
    filter_proc = lambda {|doc, score, s| (s[doc][:int] % 2) == 0}
 
46
    top_docs = searcher.search(q, :filter_proc => filter_proc)
 
47
    top_docs.hits.each do |hit|
 
48
      assert_equal(0, searcher[hit.doc][:int] % 2)
 
49
    end
 
50
  end
 
51
 
 
52
  def test_range_filter
 
53
    searcher = Searcher.new(@dir)
 
54
    q = MatchAllQuery.new()
 
55
    rf = RangeFilter.new(:int, :>= => "2", :<= => "6")
 
56
    do_test_top_docs(searcher, q, [2,3,4,5,6], rf)
 
57
    rf = RangeFilter.new(:int, :>= => "2", :< => "6")
 
58
    do_test_top_docs(searcher, q, [2,3,4,5], rf)
 
59
    rf = RangeFilter.new(:int, :> => "2", :<= => "6")
 
60
    do_test_top_docs(searcher, q, [3,4,5,6], rf)
 
61
    rf = RangeFilter.new(:int, :> => "2", :< => "6")
 
62
    do_test_top_docs(searcher, q, [3,4,5], rf)
 
63
    rf = RangeFilter.new(:int, :>= => "6")
 
64
    do_test_top_docs(searcher, q, [6,7,8,9], rf)
 
65
    rf = RangeFilter.new(:int, :> => "6")
 
66
    do_test_top_docs(searcher, q, [7,8,9], rf)
 
67
    rf = RangeFilter.new(:int, :<= => "2")
 
68
    do_test_top_docs(searcher, q, [0,1,2], rf)
 
69
    rf = RangeFilter.new(:int, :< => "2")
 
70
    do_test_top_docs(searcher, q, [0,1], rf)
 
71
 
 
72
    bits = rf.bits(searcher.reader)
 
73
    assert(bits[0])
 
74
    assert(bits[1])
 
75
    assert(!bits[2])
 
76
    assert(!bits[3])
 
77
    assert(!bits[4])
 
78
  end
 
79
 
 
80
  def test_range_filter_errors
 
81
    assert_raise(ArgumentError) {f = RangeFilter.new(:f, :> => "b", :< => "a")}
 
82
    assert_raise(ArgumentError) {f = RangeFilter.new(:f, :include_lower => true)}
 
83
    assert_raise(ArgumentError) {f = RangeFilter.new(:f, :include_upper => true)}
 
84
  end
 
85
 
 
86
  def test_query_filter()
 
87
    searcher = Searcher.new(@dir)
 
88
    q = MatchAllQuery.new()
 
89
    qf = QueryFilter.new(TermQuery.new(:switch, "on"))
 
90
    do_test_top_docs(searcher, q, [0,2,4,6,8], qf)
 
91
    # test again to test caching doesn't break it
 
92
    do_test_top_docs(searcher, q, [0,2,4,6,8], qf)
 
93
    qf = QueryFilter.new(TermQuery.new(:switch, "off"))
 
94
    do_test_top_docs(searcher, q, [1,3,5,7,9], qf)
 
95
 
 
96
    bits = qf.bits(searcher.reader)
 
97
    assert(bits[1])
 
98
    assert(bits[3])
 
99
    assert(bits[5])
 
100
    assert(bits[7])
 
101
    assert(bits[9])
 
102
    assert(!bits[0])
 
103
    assert(!bits[2])
 
104
    assert(!bits[4])
 
105
    assert(!bits[6])
 
106
    assert(!bits[8])
 
107
  end
 
108
 
 
109
  def test_filtered_query
 
110
    searcher = Searcher.new(@dir)
 
111
    q = MatchAllQuery.new()
 
112
    rf = RangeFilter.new(:int, :>= => "2", :<= => "6")
 
113
    rq = FilteredQuery.new(q, rf)
 
114
    qf = QueryFilter.new(TermQuery.new(:switch, "on"))
 
115
    do_test_top_docs(searcher, rq, [2,4,6], qf)
 
116
    query = FilteredQuery.new(rq, qf)
 
117
    rf2 = RangeFilter.new(:int, :>= => "3")
 
118
    do_test_top_docs(searcher, query, [4,6], rf2)
 
119
  end
 
120
 
 
121
  class CustomFilter
 
122
    def bits(ir)
 
123
      bv = Ferret::Utils::BitVector.new
 
124
      bv[0] = bv[2] = bv[4] = true
 
125
      bv
 
126
    end
 
127
  end
 
128
 
 
129
  def test_custom_filter
 
130
    searcher = Searcher.new(@dir)
 
131
    q = MatchAllQuery.new
 
132
    filt = CustomFilter.new
 
133
    do_test_top_docs(searcher, q, [0, 2, 4], filt)
 
134
  end
 
135
end