1
require File.dirname(__FILE__) + "/../../test_helper"
4
class FilterTest < Test::Unit::TestCase
6
include Ferret::Analysis
10
@dir = Ferret::Store::RAMDirectory.new()
11
iw = IndexWriter.new(:dir => @dir,
12
:analyzer => WhiteSpaceAnalyzer.new(),
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}
33
def do_test_top_docs(searcher, query, expected, filter)
34
top_docs = searcher.search(query, {:filter => filter})
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)
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)
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)
72
bits = rf.bits(searcher.reader)
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)}
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)
96
bits = qf.bits(searcher.reader)
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)
123
bv = Ferret::Utils::BitVector.new
124
bv[0] = bv[2] = bv[4] = true
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)