/
redis_text_search_core_spec.rb
122 lines (108 loc) · 5.27 KB
/
redis_text_search_core_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
class Post
include Redis::TextSearch
text_index :title
text_index :tags, :exact => true
def self.finder_method(ids, options)
ids
end
def initialize(attrib)
@attrib = attrib
@id = attrib[:id] || 1
end
def id; @id; end
def method_missing(name, *args)
@attrib[name] || super
end
end
TITLES = [
'Some plain text',
'More plain textstring',
'Come get somebody personal',
'*Welcome to Nate\'s new BLOG!!',
]
TAGS = [
['personal', 'nontechnical'],
['mysql', 'technical'],
['gaming','technical']
]
describe Redis::TextSearch do
before :all do
Post.redis.del('post:text_index:title:so')
Post.redis.del('post:text_index:title:som')
Post.redis.del('post:text_index:title:some')
Post.redis.del('post:text_index:title:pl')
Post.redis.del('post:text_index:title:pla')
Post.redis.del('post:text_index:title:plai')
Post.redis.del('post:text_index:title:plain')
Post.redis.del('post:text_index:title:te')
Post.redis.del('post:text_index:title:tex')
Post.redis.del('post:text_index:title:text')
Post.redis.del('post:text_index:title:texts')
Post.redis.del('post:text_index:title:textst')
Post.redis.del('post:text_index:title:textstr')
Post.redis.del('post:text_index:title:textstri')
Post.redis.del('post:text_index:title:textstrin')
Post.redis.del('post:text_index:title:textstring')
Post.redis.del('post:text_index:tags:personal')
Post.redis.del('post:text_index:tags:nontechnical')
end
it "should define text indexes in the class" do
Post.text_indexes[:title][:key].should == 'post:text_index:title'
Post.text_indexes[:tags][:key].should == 'post:text_index:tags'
end
it "should update text indexes correctly" do
post = Post.new(:title => TITLES[0], :tags => TAGS[0], :id => 1)
post2 = Post.new(:title => TITLES[1], :tags => TAGS[1], :id => 2)
post.update_text_indexes
post2.update_text_indexes
Post.redis.set_members('post:text_index:title:so').should == ['1']
Post.redis.set_members('post:text_index:title:som').should == ['1']
Post.redis.set_members('post:text_index:title:some').should == ['1']
Post.redis.set_members('post:text_index:title:pl').sort.should == ['1','2']
Post.redis.set_members('post:text_index:title:pla').sort.should == ['1','2']
Post.redis.set_members('post:text_index:title:plai').sort.should == ['1','2']
Post.redis.set_members('post:text_index:title:plain').sort.should == ['1','2']
Post.redis.set_members('post:text_index:title:te').sort.should == ['1','2']
Post.redis.set_members('post:text_index:title:tex').sort.should == ['1','2']
Post.redis.set_members('post:text_index:title:text').sort.should == ['1','2']
Post.redis.set_members('post:text_index:title:texts').should == ['2']
Post.redis.set_members('post:text_index:title:textst').should == ['2']
Post.redis.set_members('post:text_index:title:textstr').should == ['2']
Post.redis.set_members('post:text_index:title:textstri').should == ['2']
Post.redis.set_members('post:text_index:title:textstrin').should == ['2']
Post.redis.set_members('post:text_index:title:textstring').should == ['2']
Post.redis.set_members('post:text_index:tags:pe').should == []
Post.redis.set_members('post:text_index:tags:per').should == []
Post.redis.set_members('post:text_index:tags:pers').should == []
Post.redis.set_members('post:text_index:tags:perso').should == []
Post.redis.set_members('post:text_index:tags:person').should == []
Post.redis.set_members('post:text_index:tags:persona').should == []
Post.redis.set_members('post:text_index:tags:personal').should == ['1']
Post.redis.set_members('post:text_index:tags:no').should == []
Post.redis.set_members('post:text_index:tags:non').should == []
Post.redis.set_members('post:text_index:tags:nont').should == []
Post.redis.set_members('post:text_index:tags:nonte').should == []
Post.redis.set_members('post:text_index:tags:nontec').should == []
Post.redis.set_members('post:text_index:tags:nontech').should == []
Post.redis.set_members('post:text_index:tags:nontechn').should == []
Post.redis.set_members('post:text_index:tags:nontechni').should == []
Post.redis.set_members('post:text_index:tags:nontechnic').should == []
Post.redis.set_members('post:text_index:tags:nontechnica').should == []
Post.redis.set_members('post:text_index:tags:nontechnical').should == ['1']
end
it "should search text indexes and return records" do
Post.text_search('some').should == ['1']
Post.new(:title => TITLES[2], :tags => TAGS[2], :id => 3).update_text_indexes
Post.text_search('some').sort.should == ['1','3']
Post.text_search('plain').sort.should == ['1','2']
Post.text_search('plain','text').sort.should == ['1','2']
Post.text_search('plain','textstr').sort.should == ['2']
Post.text_search('some','text').sort.should == ['1']
Post.text_search('nontechnical').sort.should == ['1']
Post.text_search('personal').sort.should == ['1','3']
Post.text_search('personal', :fields => :tags).sort.should == ['1']
Post.text_search('personal', :fields => [:tags]).sort.should == ['1']
Post.text_search('nontechnical', :fields => [:title]).sort.should == []
end
end