rmla 1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,99 @@
1
+ class MongoidAlpha
2
+ include Mongoid::Document
3
+ include Mongoid::Mebla
4
+ field :name
5
+ field :value, :type => Integer
6
+ field :cost, :type => Float
7
+ field :hidden
8
+
9
+ self.whiny_indexing = true
10
+
11
+ referenced_in :mongoid_epsilon
12
+
13
+ search_in :name, :cost, :value
14
+ end
15
+
16
+ class MongoidBeta
17
+ include Mongoid::Document
18
+ include Mongoid::Mebla
19
+ field :name
20
+
21
+ self.whiny_indexing = true
22
+
23
+ embeds_many :mongoid_gammas
24
+
25
+ search_in :name => {:boost => 2.0, :analyzer => 'snowball'}
26
+ end
27
+
28
+ class MongoidTheta < MongoidAlpha
29
+ field :extra
30
+
31
+ search_in :extra
32
+ end
33
+
34
+ class MongoidTau < MongoidAlpha
35
+ field :extra2
36
+ end
37
+
38
+ class MongoidDelta
39
+ include Mongoid::Document
40
+ include Mongoid::Mebla
41
+ field :name
42
+
43
+ self.whiny_indexing = true
44
+ end
45
+
46
+ class MongoidOmega < MongoidDelta
47
+ search_in :name
48
+ end
49
+
50
+ class MongoidZeta
51
+ include Mongoid::Document
52
+ include Mongoid::Mebla
53
+ field :name
54
+ field :an_array, :type => Array
55
+
56
+ search_in :name, :an_array
57
+ end
58
+
59
+ class MongoidGamma
60
+ include Mongoid::Document
61
+ include Mongoid::Mebla
62
+ field :name
63
+ field :value, :type => Integer
64
+
65
+ self.whiny_indexing = true
66
+
67
+ embedded_in :mongoid_beta
68
+
69
+ search_in :name, :value, :embedded_in => :mongoid_beta
70
+ end
71
+
72
+ class MongoidPi
73
+ include Mongoid::Document
74
+ include Mongoid::Mebla
75
+ field :name
76
+
77
+ self.whiny_indexing = true
78
+
79
+ references_one :mongoid_epsilon
80
+
81
+ search_in :name, :does_smth
82
+
83
+ def does_smth
84
+ "returns smth"
85
+ end
86
+ end
87
+
88
+ class MongoidEpsilon
89
+ include Mongoid::Document
90
+ include Mongoid::Mebla
91
+ field :name
92
+
93
+ self.whiny_indexing = true
94
+
95
+ referenced_in :mongoid_pi
96
+ references_many :mongoid_alphas
97
+
98
+ search_in :name, :search_relations => {:mongoid_pi => :name, :mongoid_alphas => [:name, :value]}
99
+ end
@@ -0,0 +1,3 @@
1
+ username: ''
2
+ password: ''
3
+ host: localhost
@@ -0,0 +1,165 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+ require 'ruby-debug'
3
+
4
+ describe "Mebla" do
5
+ describe "indexing" do
6
+ it "should index existing records" do
7
+ Mebla.context.drop_index
8
+
9
+ fdocument = nil
10
+ ldocument = nil
11
+
12
+ MongoidAlpha.without_indexing do
13
+ fdocument = MongoidAlpha.create! :name => "Testing indexing bulkly", :value => 1, :cost => 1.0
14
+ ldocument = MongoidAlpha.create! :name => "Testing indexing bulkly other one", :value => 2, :cost => 2.0
15
+ end
16
+
17
+ Mebla.context.index_data
18
+
19
+ lambda {Mebla.context.slingshot_index.retrieve(:mongoid_alpha, fdocument.id.to_s)}.should_not raise_error
20
+ lambda {Mebla.context.slingshot_index.retrieve(:mongoid_alpha, ldocument.id.to_s)}.should_not raise_error
21
+ end
22
+
23
+ it "should delete the existing index and create a new one" do
24
+ Mebla.context.rebuild_index.should_not == false
25
+ end
26
+
27
+ it "should create the index with proper mapping" do
28
+ maps = Mebla.context.slingshot_index.mapping["mongoid_alpha"]["properties"]
29
+ maps["name"]["type"].should == "string"
30
+ maps["value"]["type"].should == "integer"
31
+ maps["cost"]["type"].should == "float"
32
+ end
33
+
34
+ describe "array fields" do
35
+ it "should index arrays" do
36
+ Mebla.context.drop_index
37
+
38
+ zeta = nil
39
+
40
+ MongoidZeta.without_indexing do
41
+ zeta = MongoidZeta.create! :name => "Document with an array", :an_array => [:item, :item2]
42
+ end
43
+
44
+ Mebla.context.index_data
45
+
46
+ lambda {Mebla.context.slingshot_index.retrieve(:mongoid_zeta, zeta.id.to_s)}.should_not raise_error
47
+ end
48
+ end
49
+
50
+ describe "method fields" do
51
+ it "should index method results" do
52
+ Mebla.context.drop_index
53
+
54
+ pi = nil
55
+
56
+ MongoidPi.without_indexing do
57
+ pi = MongoidPi.create! :name => "Document with an indexed method"
58
+ end
59
+
60
+ Mebla.context.index_data
61
+
62
+ lambda {Mebla.context.slingshot_index.retrieve(:mongoid_pi, pi.id.to_s)}.should_not raise_error
63
+ end
64
+ end
65
+
66
+ describe "relational fields" do
67
+ it "should index relational fields" do
68
+ Mebla.context.drop_index
69
+
70
+ pi = nil
71
+
72
+ MongoidPi.without_indexing do
73
+ pi = MongoidPi.create! :name => "A pi"
74
+ end
75
+
76
+ alpha = nil
77
+
78
+ MongoidAlpha.without_indexing do
79
+ alpha = MongoidAlpha.create! :name => "Testing index", :value => 1, :cost => 2.0
80
+ end
81
+
82
+ epsilon = nil
83
+
84
+ MongoidEpsilon.without_indexing do
85
+ epsilon = pi.create_mongoid_epsilon :name => "episilon", :mongoid_alphas => [alpha]
86
+ end
87
+
88
+ Mebla.context.index_data
89
+
90
+ lambda {Mebla.context.slingshot_index.retrieve(:mongoid_epsilon, epsilon.id.to_s)}.should_not raise_error
91
+ end
92
+ end
93
+
94
+ describe "for sub-classed documents" do
95
+ it "should index existing records" do
96
+ Mebla.context.drop_index
97
+
98
+ theta = nil
99
+
100
+ MongoidTheta.without_indexing do
101
+ theta = MongoidTheta.create! :extra => "Subclassed parent"
102
+ end
103
+
104
+ Mebla.context.index_data
105
+
106
+ lambda {Mebla.context.slingshot_index.retrieve(:mongoid_theta, theta.id.to_s)}.should_not raise_error
107
+ end
108
+
109
+ it "should not index non searchable subclassed models" do
110
+ Mebla.context.drop_index
111
+
112
+ tau = nil
113
+ MongoidAlpha.without_indexing do
114
+ MongoidAlpha.create! :name => "Testing indexing bulkly", :value => 1, :cost => 1.0
115
+ tau = MongoidTau.create! :extra2 => "Should not be indexed"
116
+ end
117
+
118
+ lambda {Mebla.context.index_data}.should_not raise_error
119
+ lambda {Mebla.context.slingshot_index.retrieve(:mongoid_tau, tau.id.to_s)}.should raise_error
120
+ end
121
+
122
+ it "should index only models with defined indecies" do
123
+ Mebla.context.drop_index
124
+
125
+ theta = nil
126
+
127
+ MongoidOmega.without_indexing do
128
+ theta = MongoidOmega.create! :name => "Subclassed parent"
129
+ end
130
+
131
+ lambda {Mebla.context.index_data}.should_not raise_error
132
+ end
133
+ end
134
+
135
+ describe "for embedded documents" do
136
+ it "should index existing records" do
137
+ Mebla.context.drop_index
138
+
139
+ beta = nil
140
+ gamma = nil
141
+
142
+ MongoidBeta.without_indexing do
143
+ beta = MongoidBeta.create! :name => "Embedor parent"
144
+ MongoidGamma.without_indexing do
145
+ gamma = beta.mongoid_gammas.create :name => "Embedded", :value => 1
146
+ end
147
+ end
148
+
149
+ Mebla.context.index_data
150
+
151
+ lambda {
152
+ Slingshot::Configuration.client.get "#{Mebla::Configuration.instance.url}/#{Mebla.context.slingshot_index_name}/mongoid_gamma/#{gamma.id.to_s}?routing=#{beta.id.to_s}"
153
+ }.should_not raise_error
154
+ end
155
+
156
+ it "should create the index with proper parent mapping" do
157
+ mappings = Mebla.context.slingshot_index.mapping["mongoid_gamma"]
158
+ routing = mappings["_routing"]
159
+ parent = mappings["_parent"]
160
+ routing["path"].should == "mongoid_beta_id"
161
+ parent["type"].should == "mongoid_beta"
162
+ end
163
+ end
164
+ end
165
+ end
@@ -0,0 +1,142 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe "Mebla" do
4
+ describe "searching" do
5
+ before(:each) do
6
+ Mebla.context.rebuild_index
7
+ MongoidAlpha.create! :name => "Testing index", :value => 1, :cost => 2.0
8
+ end
9
+
10
+ it "should search and return the only relevant result" do
11
+ results=MongoidAlpha.search "name: Testing index"
12
+
13
+ results.total.should == 1
14
+ end
15
+
16
+ it "should search and return the only relevant result, and cast it into the correct class type" do
17
+ results=MongoidAlpha.search "name: Testing index"
18
+
19
+ results.first.class.should == MongoidAlpha
20
+ end
21
+
22
+ describe "documents with arrays" do
23
+ before(:each) do
24
+ Mebla.context.rebuild_index
25
+ MongoidZeta.create! :name => "Document with array", :an_array => [:item, :item2]
26
+ end
27
+
28
+ it "should return arrays correctly" do
29
+ results = MongoidZeta.search "Document with array"
30
+
31
+ results.first.an_array.class.should == Array
32
+ end
33
+
34
+ it "should search within arrays" do
35
+ results = MongoidZeta.search "item2"
36
+
37
+ results.total.should == 1
38
+ end
39
+ end
40
+
41
+ describe "documents with indexed methods" do
42
+ before(:each) do
43
+ Mebla.context.rebuild_index
44
+ MongoidPi.create! :name => "Document with an indexed method"
45
+ end
46
+
47
+ it "should search within indexed methods" do
48
+ results = MongoidPi.search "returns smth"
49
+
50
+ results.total.should == 1
51
+ end
52
+ end
53
+
54
+ describe "documents with indexed relation fields" do
55
+ before(:each) do
56
+ Mebla.context.rebuild_index
57
+ pi = MongoidPi.create! :name => "A pi"
58
+ alpha = MongoidAlpha.create! :name => "Testing index", :value => 1, :cost => 2.0
59
+ epsilon = pi.create_mongoid_epsilon :name => "epsilon"#, :mongoid_alphas => [alpha] # currently there is a bug in setting the relation like this while creating the document
60
+ epsilon.mongoid_alphas << alpha
61
+ epsilon.save # another bug; mongoid doesn't raise the save callbacks for `<<` method
62
+ end
63
+
64
+ it "should search within indexed fields from the relations" do
65
+ results = MongoidEpsilon.search "Testing index"
66
+
67
+ results.total.should == 1
68
+ end
69
+ end
70
+
71
+ describe "multiple types" do
72
+ before(:each) do
73
+ MongoidBeta.create! :name => "Testing index"
74
+ end
75
+
76
+ it "should search and return all results of all class types" do
77
+ results=Mebla.search "name: Testing index"
78
+
79
+ results.total.should == 2
80
+ (results.each.collect{|e| e.class} & [MongoidAlpha, MongoidBeta]).should =~ [MongoidAlpha, MongoidBeta]
81
+ end
82
+
83
+ it "should search and return only results from the searched class type" do
84
+ results=MongoidAlpha.search "name: Testing index"
85
+
86
+ results.total.should == 1
87
+ results.first.class.should == MongoidAlpha
88
+ end
89
+ end
90
+
91
+ describe "embedded documents" do
92
+ before(:each) do
93
+ beta = MongoidBeta.create! :name => "Embedor parent"
94
+ beta.mongoid_gammas.create :name => "Embedded", :value => 1
95
+ end
96
+
97
+ it "should search and return the only relevant result" do
98
+ results=MongoidGamma.search "name: Embedded"
99
+
100
+ results.total.should == 1
101
+ end
102
+
103
+ it "should search and return the only relevant result, and cast it into the correct class type" do
104
+ results=MongoidGamma.search "name: Embedded"
105
+
106
+ results.first.class.should == MongoidGamma
107
+ end
108
+ end
109
+
110
+ describe "with options" do
111
+ before(:each) do
112
+ beta = MongoidBeta.create! :name => "Different map"
113
+ beta.mongoid_gammas.create :name => "Testing index 2", :value => 2
114
+ end
115
+
116
+ it "should sort descending according to the criteria defined" do
117
+ Mebla.search("Testing index").desc(:value).first.class.should == MongoidGamma
118
+ end
119
+
120
+ it "should sort ascending according to the criteria defined" do
121
+ Mebla.search("Testing index").asc(:value).first.class.should == MongoidAlpha
122
+ end
123
+
124
+ it "should search and only return results matching the term defined" do
125
+ Mebla.search.term(:name, "index").total.should == 2
126
+ end
127
+
128
+ it "should search and only return results matching the terms defined" do
129
+ Mebla.search.terms(:name, ["index", "map"]).total.should == 3
130
+ end
131
+
132
+ it "should search and filter results according to the filters defined" do
133
+ Mebla.search.terms(:name, ["index", "map"]).only(:value => [1]).total.should == 1
134
+ end
135
+
136
+ it "should search and return results along with facets" do
137
+ results = Mebla.search.terms(:name, ["index", "map"]).facet("values", :value)
138
+ results.facets["values"]["terms"].count.should == 2
139
+ end
140
+ end
141
+ end
142
+ end
@@ -0,0 +1,126 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe "Mebla" do
4
+ describe "synchronizing" do
5
+ before(:each) do
6
+ Mebla.context.rebuild_index
7
+ MongoidAlpha.create! :name => "Testing index", :value => 1, :cost => 2.0
8
+ end
9
+
10
+ it "should index new documents automatically" do
11
+ mdocument = MongoidAlpha.first
12
+ lambda {Mebla.context.slingshot_index.retrieve(:mongoid_alpha, mdocument.id.to_s)}.should_not raise_error
13
+ end
14
+
15
+ it "should remove deleted documents from index automatically" do
16
+ mdocument = MongoidAlpha.first
17
+ doc_id = mdocument.id.to_s
18
+ mdocument.destroy
19
+
20
+ lambda {MongoidAlpha.slingshot_index.retrieve(:mongoid_alpha, doc_id)}.should raise_error
21
+ end
22
+
23
+ it "should update the index automatically when a document is updated" do
24
+ udocument = MongoidAlpha.first
25
+ udocument.update_attributes(:cost => 3.1).should == true
26
+
27
+ result = Mebla.context.slingshot_index.retrieve(:mongoid_alpha, udocument.id.to_s)
28
+ result[:cost].should == 3.1
29
+ end
30
+ end
31
+
32
+ describe "sub classes" do
33
+ before(:each) do
34
+ Mebla.context.rebuild_index
35
+ MongoidTheta.create! :extra => "Is this indexed?"
36
+ end
37
+
38
+ it "should index sub-classed documents automatically under the sub-class type" do
39
+ mdocument = MongoidTheta.first
40
+ lambda {Mebla.context.slingshot_index.retrieve(:mongoid_theta, mdocument.id.to_s)}.should_not raise_error
41
+ end
42
+
43
+ it "should index sub-classed documents automatically and correctly" do
44
+ pdocument = MongoidTheta.first
45
+ result = Mebla.context.slingshot_index.retrieve(:mongoid_theta, pdocument.id.to_s)
46
+ result[:extra].should == "Is this indexed?"
47
+ end
48
+ end
49
+
50
+ describe "documents with indexed method fields" do
51
+ before(:each) do
52
+ Mebla.context.rebuild_index
53
+ MongoidPi.create! :name => "Testing indexing methods"
54
+ end
55
+
56
+ it "should index method" do
57
+ pdocument = MongoidPi.first
58
+
59
+ lambda {Mebla.context.slingshot_index.retrieve(:mongoid_pi, pdocument.id.to_s)}.should_not raise_error
60
+ end
61
+
62
+ it "should index method fields documents automatically and correctly" do
63
+ pdocument = MongoidPi.first
64
+ result = Mebla.context.slingshot_index.retrieve(:mongoid_pi, pdocument.id.to_s)
65
+ result[:does_smth].should == "returns smth"
66
+ end
67
+ end
68
+
69
+ describe "documents with indexed relation fields" do
70
+ before(:each) do
71
+ Mebla.context.rebuild_index
72
+ pi = MongoidPi.create! :name => "A pi"
73
+ alpha = MongoidAlpha.create! :name => "Testing index", :value => 1, :cost => 2.0
74
+ epsilon = pi.create_mongoid_epsilon :name => "epsilon"#, :mongoid_alphas => [alpha] # currently there is a bug in setting the relation like this while creating the document
75
+ epsilon.mongoid_alphas << alpha
76
+ epsilon.save # another bug; mongoid doesn't raise the save callbacks for `<<` method
77
+ end
78
+
79
+ it "should index documents with indexed fields from the relations" do
80
+ edocument = MongoidEpsilon.first
81
+
82
+ lambda {Mebla.context.slingshot_index.retrieve(:mongoid_epsilon, edocument.id.to_s)}.should_not raise_error
83
+ end
84
+
85
+ it "should index fields from the relations with the document" do
86
+ edocument = MongoidEpsilon.first
87
+
88
+ result = Mebla.context.slingshot_index.retrieve(:mongoid_epsilon, edocument.id.to_s)
89
+ result[:mongoid_pi][:name].should == "A pi"
90
+ result[:mongoid_alphas].is_a?(Array).should == true
91
+ result[:mongoid_alphas].first["name"].should == "Testing index"
92
+ result[:mongoid_alphas].first["value"].should == 1
93
+ end
94
+ end
95
+
96
+ describe "array fields documents" do
97
+ it "should index array fields and retrieve them correctly" do
98
+ Mebla.context.rebuild_index
99
+ zdocument = MongoidZeta.create :name => "Document with array", :an_array => [:index, :index2, :index2]
100
+
101
+ lambda {Mebla.context.slingshot_index.retrieve(:mongoid_zeta, zdocument.id.to_s)}.should_not raise_error
102
+ end
103
+
104
+ it "should index array fields and retrieve them correctly" do
105
+ Mebla.context.rebuild_index
106
+ zdocument = MongoidZeta.create :name => "Document with array", :an_array => [:index, :index2, :index2]
107
+
108
+ lambda {Mebla.context.slingshot_index.retrieve(:mongoid_zeta, zdocument.id.to_s)}.should_not raise_error
109
+ end
110
+ end
111
+
112
+ describe "embedded documents" do
113
+ before(:each) do
114
+ Mebla.context.rebuild_index
115
+ beta = MongoidBeta.create! :name => "Embedor parent"
116
+ beta.mongoid_gammas.create :name => "Embedded", :value => 1
117
+ end
118
+
119
+ it "should index embedded documents automatically and link to the parent" do
120
+ mdocument = MongoidBeta.first.mongoid_gammas.first
121
+ lambda {
122
+ Slingshot::Configuration.client.get "#{Mebla::Configuration.instance.url}/#{Mebla.context.slingshot_index_name}/mongoid_gamma/#{mdocument.id.to_s}?routing=#{mdocument.mongoid_beta.id.to_s}"
123
+ }.should_not raise_error
124
+ end
125
+ end
126
+ end