forked from elastic/elasticsearch-rails
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmongoid_basic_test.rb
177 lines (128 loc) · 5.71 KB
/
mongoid_basic_test.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
require 'test_helper'
Mongo.setup!
if Mongo.available?
Mongo.connect_to 'mongoid_articles'
module Elasticsearch
module Model
class MongoidBasicIntegrationTest < Elasticsearch::Test::IntegrationTestCase
class ::MongoidArticle
include Mongoid::Document
include Elasticsearch::Model
include Elasticsearch::Model::Callbacks
field :id, type: String
field :title, type: String
attr_accessible :title if respond_to? :attr_accessible
settings index: { number_of_shards: 1, number_of_replicas: 0 } do
mapping do
indexes :title, type: 'string', analyzer: 'snowball'
indexes :created_at, type: 'date'
end
end
def as_indexed_json(options={})
as_json(except: [:id, :_id])
end
end
context "Mongoid integration" do
setup do
Elasticsearch::Model::Adapter.register \
Elasticsearch::Model::Adapter::Mongoid,
lambda { |klass| !!defined?(::Mongoid::Document) && klass.respond_to?(:ancestors) && klass.ancestors.include?(::Mongoid::Document) }
MongoidArticle.__elasticsearch__.create_index! force: true
MongoidArticle.delete_all
MongoidArticle.create! title: 'Test'
MongoidArticle.create! title: 'Testing Coding'
MongoidArticle.create! title: 'Coding'
MongoidArticle.__elasticsearch__.refresh_index!
MongoidArticle.__elasticsearch__.client.cluster.health wait_for_status: 'yellow'
end
should "index and find a document" do
response = MongoidArticle.search('title:test')
assert response.any?
assert_equal 2, response.results.size
assert_equal 2, response.records.size
assert_instance_of Elasticsearch::Model::Response::Result, response.results.first
assert_instance_of MongoidArticle, response.records.first
assert_equal 'Test', response.results.first.title
assert_equal 'Test', response.records.first.title
end
should "iterate over results" do
response = MongoidArticle.search('title:test')
assert_equal ['Test', 'Testing Coding'], response.results.map(&:title)
assert_equal ['Test', 'Testing Coding'], response.records.map(&:title)
end
should "access results from records" do
response = MongoidArticle.search('title:test')
response.records.each_with_hit do |r, h|
assert_not_nil h._score
assert_not_nil h._source.title
end
end
should "preserve the search results order for records" do
response = MongoidArticle.search('title:code')
response.records.each_with_hit do |r, h|
assert_equal h._id, r.id.to_s
end
response.records.map_with_hit do |r, h|
assert_equal h._id, r.id.to_s
end
end
should "remove document from index on destroy" do
article = MongoidArticle.first
article.destroy
assert_equal 2, MongoidArticle.count
MongoidArticle.__elasticsearch__.refresh_index!
response = MongoidArticle.search 'title:test'
assert_equal 1, response.results.size
assert_equal 1, response.records.size
end
should "index updates to the document" do
article = MongoidArticle.first
article.title = 'Writing'
article.save
MongoidArticle.__elasticsearch__.refresh_index!
response = MongoidArticle.search 'title:write'
assert_equal 1, response.results.size
assert_equal 1, response.records.size
end
should "return results for a DSL search" do
response = MongoidArticle.search query: { match: { title: { query: 'test' } } }
assert_equal 2, response.results.size
assert_equal 2, response.records.size
end
should "return a paged collection" do
response = MongoidArticle.search query: { match: { title: { query: 'test' } } },
size: 2,
from: 1
assert_equal 1, response.results.size
assert_equal 1, response.records.size
assert_equal 'Testing Coding', response.results.first.title
assert_equal 'Testing Coding', response.records.first.title
end
context "importing" do
setup do
MongoidArticle.delete_all
97.times { |i| MongoidArticle.create! title: "Test #{i}" }
MongoidArticle.__elasticsearch__.create_index! force: true
MongoidArticle.__elasticsearch__.client.cluster.health wait_for_status: 'yellow'
end
should "import all the documents" do
assert_equal 97, MongoidArticle.count
MongoidArticle.__elasticsearch__.refresh_index!
assert_equal 0, MongoidArticle.search('*').results.total
batches = 0
errors = MongoidArticle.import(batch_size: 10) do |response|
batches += 1
end
assert_equal 0, errors
assert_equal 10, batches
MongoidArticle.__elasticsearch__.refresh_index!
assert_equal 97, MongoidArticle.search('*').results.total
response = MongoidArticle.search('test')
assert response.results.any?, "Search has not returned results: #{response.to_a}"
end
end
end
end
end
end
end